From 7d36faad0366adae278cb2fa6dafeb32eafb64a9 Mon Sep 17 00:00:00 2001 From: Jerry Fan Date: Thu, 12 Sep 2024 12:58:52 -0400 Subject: [PATCH] lint fix --- .../api/v4/affiliates-controller.test.ts | 28 +++++------ .../api/v4/affiliates-controller.ts | 46 +++++++++++-------- 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/indexer/services/comlink/__tests__/controllers/api/v4/affiliates-controller.test.ts b/indexer/services/comlink/__tests__/controllers/api/v4/affiliates-controller.test.ts index 7cf7847a79..465865020d 100644 --- a/indexer/services/comlink/__tests__/controllers/api/v4/affiliates-controller.test.ts +++ b/indexer/services/comlink/__tests__/controllers/api/v4/affiliates-controller.test.ts @@ -25,7 +25,7 @@ describe('affiliates-controller#V4', () => { await testMocks.seedData(); await SubaccountUsernamesTable.create(testConstants.defaultSubaccountUsername); }); - + afterEach(async () => { await dbHelpers.clearData(); }); @@ -46,20 +46,20 @@ describe('affiliates-controller#V4', () => { }); it('should fail if address does not exist', async () => { - const nonExistentAddress = 'adgsakhasgt' - const response: request.Response = await sendRequest({ + const nonExistentAddress = 'adgsakhasgt'; + await sendRequest({ type: RequestMethod.GET, path: `/v4/affiliates/metadata?address=${nonExistentAddress}`, expectedStatus: 404, // helper performs expect on status }); }); - it('should classify not volume eligible', async () => { + it('should classify not volume eligible', async () => { await WalletTable.update( - { + { address: testConstants.defaultWallet.address, - totalVolume: "0", - totalTradingRewards: "0", + totalVolume: '0', + totalTradingRewards: '0', }, ); const response: request.Response = await sendRequest({ @@ -74,12 +74,12 @@ describe('affiliates-controller#V4', () => { }); }); - it('should classify volume eligible', async () => { + it('should classify volume eligible', async () => { await WalletTable.update( - { + { address: testConstants.defaultWallet.address, - totalVolume: "100000", - totalTradingRewards: "0", + totalVolume: '100000', + totalTradingRewards: '0', }, ); const response: request.Response = await sendRequest({ @@ -92,7 +92,7 @@ describe('affiliates-controller#V4', () => { isVolumeEligible: true, isAffiliate: false, }); - }); + }); it('should classify is not affiliate', async () => { // AffiliateReferredUsersTable is empty @@ -128,8 +128,8 @@ describe('affiliates-controller#V4', () => { it('should fail if subaccount username not found', async () => { // create defaultWallet2 without subaccount username - WalletTable.create(testConstants.defaultWallet2); - const response: request.Response = await sendRequest({ + await WalletTable.create(testConstants.defaultWallet2); + await sendRequest({ type: RequestMethod.GET, path: `/v4/affiliates/metadata?address=${testConstants.defaultWallet2.address}`, expectedStatus: 500, // helper performs expect on status diff --git a/indexer/services/comlink/src/controllers/api/v4/affiliates-controller.ts b/indexer/services/comlink/src/controllers/api/v4/affiliates-controller.ts index 78d51a9a32..97b54c363a 100644 --- a/indexer/services/comlink/src/controllers/api/v4/affiliates-controller.ts +++ b/indexer/services/comlink/src/controllers/api/v4/affiliates-controller.ts @@ -1,18 +1,19 @@ import { stats } from '@dydxprotocol-indexer/base'; -import express from 'express'; -import { checkSchema, matchedData } from 'express-validator'; -import { - Controller, Get, Query, Route, -} from 'tsoa'; import { WalletTable, AffiliateReferredUsersTable, SubaccountTable, SubaccountUsernamesTable, } from '@dydxprotocol-indexer/postgres'; -import { NotFoundError, UnexpectedServerError } from '../../../lib/errors'; +import express from 'express'; +import { checkSchema, matchedData } from 'express-validator'; +import { + Controller, Get, Query, Route, +} from 'tsoa'; + import { getReqRateLimiter } from '../../../caches/rate-limiters'; import config from '../../../config'; +import { NotFoundError, UnexpectedServerError } from '../../../lib/errors'; import { handleControllerError } from '../../../lib/helpers'; import { rateLimiterMiddleware } from '../../../lib/rate-limit'; import { handleValidationErrors } from '../../../request-helpers/error-handler'; @@ -39,47 +40,54 @@ class AffiliatesController extends Controller { async getMetadata( @Query() address: string, ): Promise { + const [walletRow, referredUserRows] = await Promise.all([ + WalletTable.findById(address), + AffiliateReferredUsersTable.findByAffiliateAddress(address), + ]); + // Check that the address exists - const walletRow = await WalletTable.findById(address); if (!walletRow) { throw new NotFoundError(`Wallet with address ${address} not found`); } - const isVolumeEligible = Number(walletRow.totalVolume) >= config.VOLUME_ELIGIBILITY_THRESHOLD; // Check if the address is an affiliate (has referred users) - const referredUserRows = await AffiliateReferredUsersTable.findByAffiliateAddress(address); - const isAffiliate = referredUserRows != undefined ? referredUserRows.length > 0 : false; + const isVolumeEligible = Number(walletRow.totalVolume) >= config.VOLUME_ELIGIBILITY_THRESHOLD; + const isAffiliate = referredUserRows !== undefined ? referredUserRows.length > 0 : false; // Get referral code (subaccount 0 username) const subaccountRows = await SubaccountTable.findAll( { - address: address, + address, subaccountNumber: 0, }, [], - ) - // No need to check subaccountRows.length > 1 because subaccountNumber is unique for an address + ); + // No need to check subaccountRows.length > 1 as subaccountNumber is unique for an address if (subaccountRows.length === 0) { + // error logging will be performed by handleInternalServerError throw new UnexpectedServerError(`Subaccount 0 not found for address ${address}`); } const subaccountId = subaccountRows[0].id; + const usernameRows = await SubaccountUsernamesTable.findAll( { subaccountId: [subaccountId], }, [], - ) + ); + // No need to check usernameRows.length > 1 as subAccountId is unique (foreign key constraint) + // This error can happen if a user calls this endpoint before subaccount-username-generator + // has generated the username if (usernameRows.length === 0) { + stats.increment(`${config.SERVICE_NAME}.${controllerName}.get_metadata.subaccount_username_not_found`); throw new UnexpectedServerError(`Username not found for subaccount ${subaccountId}`); - } else if (usernameRows.length > 1) { - throw new UnexpectedServerError(`Found multiple usernames for subaccount ${subaccountId}`); } const referralCode = usernameRows[0].username; return { - referralCode: referralCode, - isVolumeEligible: isVolumeEligible, - isAffiliate: isAffiliate, + referralCode, + isVolumeEligible, + isAffiliate, }; }