From 6ca6830bac99dd4b27a5de535d64b14e4d352820 Mon Sep 17 00:00:00 2001 From: Jerry Fan Date: Tue, 17 Sep 2024 13:07:05 -0400 Subject: [PATCH] pr revision --- .../stores/affiliate-info-table.test.ts | 77 +++++++++++-------- .../src/stores/affiliate-info-table.ts | 1 - .../src/stores/subaccount-usernames-table.ts | 6 +- .../postgres/src/types/db-model-types.ts | 2 +- .../api/v4/affiliates-controller.test.ts | 10 ++- .../api/v4/affiliates-controller.ts | 27 ++++--- 6 files changed, 71 insertions(+), 52 deletions(-) diff --git a/indexer/packages/postgres/__tests__/stores/affiliate-info-table.test.ts b/indexer/packages/postgres/__tests__/stores/affiliate-info-table.test.ts index c3a6c79bd0..79223a837c 100644 --- a/indexer/packages/postgres/__tests__/stores/affiliate-info-table.test.ts +++ b/indexer/packages/postgres/__tests__/stores/affiliate-info-table.test.ts @@ -69,23 +69,24 @@ describe('Affiliate info store', () => { describe('paginatedFindWithAddressFilter', () => { beforeEach(async () => { await migrate(); - for (let i = 0; i < 10; i++) { - await AffiliateInfoTable.create({ + await Promise.all( + Array.from({ length: 10 }, (_, i) => AffiliateInfoTable.create({ ...defaultAffiliateInfo, address: `address_${i}`, affiliateEarnings: i.toString(), - }); - } + }), + ), + ); }); it('Successfully filters by address', async () => { - // eslint-disable-next-line max-len - const infos: AffiliateInfoFromDatabase[] | undefined = await AffiliateInfoTable.paginatedFindWithAddressFilter( - ['address_0'], - 0, - 10, - false, - ); + const infos: AffiliateInfoFromDatabase[] | undefined = await AffiliateInfoTable + .paginatedFindWithAddressFilter( + ['address_0'], + 0, + 10, + false, + ); expect(infos).toBeDefined(); expect(infos!.length).toEqual(1); expect(infos![0]).toEqual(expect.objectContaining({ @@ -96,13 +97,13 @@ describe('Affiliate info store', () => { }); it('Successfully sorts by affiliate earning', async () => { - // eslint-disable-next-line max-len - const infos: AffiliateInfoFromDatabase[] | undefined = await AffiliateInfoTable.paginatedFindWithAddressFilter( - [], - 0, - 10, - true, - ); + const infos: AffiliateInfoFromDatabase[] | undefined = await AffiliateInfoTable + .paginatedFindWithAddressFilter( + [], + 0, + 10, + true, + ); expect(infos).toBeDefined(); expect(infos!.length).toEqual(10); expect(infos![0]).toEqual(expect.objectContaining({ @@ -118,13 +119,13 @@ describe('Affiliate info store', () => { }); it('Successfully uses offset and limit', async () => { - // eslint-disable-next-line max-len - const infos: AffiliateInfoFromDatabase[] | undefined = await AffiliateInfoTable.paginatedFindWithAddressFilter( - [], - 5, - 2, - false, - ); + const infos: AffiliateInfoFromDatabase[] | undefined = await AffiliateInfoTable + .paginatedFindWithAddressFilter( + [], + 5, + 2, + false, + ); expect(infos).toBeDefined(); expect(infos!.length).toEqual(2); expect(infos![0]).toEqual(expect.objectContaining({ @@ -140,13 +141,13 @@ describe('Affiliate info store', () => { }); it('Successfully filters, sorts, offsets, and limits', async () => { - // eslint-disable-next-line max-len - const infos: AffiliateInfoFromDatabase[] | undefined = await AffiliateInfoTable.paginatedFindWithAddressFilter( - [], - 3, - 2, - true, - ); + const infos: AffiliateInfoFromDatabase[] | undefined = await AffiliateInfoTable + .paginatedFindWithAddressFilter( + [], + 3, + 2, + true, + ); expect(infos).toBeDefined(); expect(infos!.length).toEqual(2); expect(infos![0]).toEqual(expect.objectContaining({ @@ -160,5 +161,17 @@ describe('Affiliate info store', () => { affiliateEarnings: '5', })); }); + + it('Returns empty array if no results', async () => { + const infos: AffiliateInfoFromDatabase[] | undefined = await AffiliateInfoTable + .paginatedFindWithAddressFilter( + ['address_11'], + 0, + 10, + false, + ); + expect(infos).toBeDefined(); + expect(infos!.length).toEqual(0); + }); }); }); diff --git a/indexer/packages/postgres/src/stores/affiliate-info-table.ts b/indexer/packages/postgres/src/stores/affiliate-info-table.ts index 7972bfcef5..7ef02c7a91 100644 --- a/indexer/packages/postgres/src/stores/affiliate-info-table.ts +++ b/indexer/packages/postgres/src/stores/affiliate-info-table.ts @@ -119,6 +119,5 @@ export async function paginatedFindWithAddressFilter( // Apply pagination using offset and limit baseQuery = baseQuery.offset(offset).limit(limit); - // Returning all fields return baseQuery.returning('*'); } diff --git a/indexer/packages/postgres/src/stores/subaccount-usernames-table.ts b/indexer/packages/postgres/src/stores/subaccount-usernames-table.ts index 56abec087d..a60b1c6da8 100644 --- a/indexer/packages/postgres/src/stores/subaccount-usernames-table.ts +++ b/indexer/packages/postgres/src/stores/subaccount-usernames-table.ts @@ -19,7 +19,7 @@ import { Options, Ordering, QueryableField, - AddressUsernameFromDatabase, + AddressUsername, } from '../types'; export async function findAll( @@ -118,12 +118,12 @@ export async function getSubaccountsWithoutUsernames( export async function findByAddress( addresses: string[], -): Promise { +): Promise { if (addresses.length === 0) { return []; } - const result: { rows: AddressUsernameFromDatabase[] } = await knexReadReplica + const result: { rows: AddressUsername[] } = await knexReadReplica .getConnection() .raw( ` diff --git a/indexer/packages/postgres/src/types/db-model-types.ts b/indexer/packages/postgres/src/types/db-model-types.ts index 363676a45d..06b12f36c1 100644 --- a/indexer/packages/postgres/src/types/db-model-types.ts +++ b/indexer/packages/postgres/src/types/db-model-types.ts @@ -264,7 +264,7 @@ export interface SubaccountUsernamesFromDatabase { subaccountId: string, } -export interface AddressUsernameFromDatabase { +export interface AddressUsername { address: string, username: string, } 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 00ddd81972..ea89899e15 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 @@ -200,9 +200,11 @@ describe('affiliates-controller#V4', () => { await SubaccountUsernamesTable.create(testConstants.subaccountUsernameWithAlternativeAddress); // Create affiliate infos - await AffiliateInfoTable.create(defaultInfo); - await AffiliateInfoTable.create(defaultInfo2); - await AffiliateInfoTable.create(defaultInfo3); + await Promise.all([ + AffiliateInfoTable.create(defaultInfo), + AffiliateInfoTable.create(defaultInfo2), + AffiliateInfoTable.create(defaultInfo3), + ]); }); afterEach(async () => { @@ -246,7 +248,7 @@ describe('affiliates-controller#V4', () => { expect(response.body.total).toEqual(expectedResponse.total); }); - it('should handle no results when filter by address', async () => { + it('should handle no results', async () => { const req: AffiliateSnapshotRequest = { addressFilter: ['nonexistentaddress'], }; 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 fec1bed002..5a224a7228 100644 --- a/indexer/services/comlink/src/controllers/api/v4/affiliates-controller.ts +++ b/indexer/services/comlink/src/controllers/api/v4/affiliates-controller.ts @@ -1,6 +1,6 @@ import { logger, stats } from '@dydxprotocol-indexer/base'; import { - AddressUsernameFromDatabase, + AddressUsername, WalletTable, AffiliateInfoTable, AffiliateReferredUsersTable, @@ -129,13 +129,13 @@ class AffiliatesController extends Controller { const finalLimit: number = limit ?? 1000; const finalsortByAffiliateEarning: boolean = sortByAffiliateEarning ?? false; - // eslint-disable-next-line max-len - const infos: AffiliateInfoFromDatabase[] | undefined = await AffiliateInfoTable.paginatedFindWithAddressFilter( - finalAddressFilter, - finalOffset, - finalLimit, - finalsortByAffiliateEarning, - ); + const infos: AffiliateInfoFromDatabase[] | undefined = await AffiliateInfoTable + .paginatedFindWithAddressFilter( + finalAddressFilter, + finalOffset, + finalLimit, + finalsortByAffiliateEarning, + ); // No results found if (infos === undefined) { @@ -147,8 +147,8 @@ class AffiliatesController extends Controller { } // Get referral codes - // eslint-disable-next-line max-len - const addressUsernames: AddressUsernameFromDatabase[] = await SubaccountUsernamesTable.findByAddress( + const addressUsernames: + AddressUsername[] = await SubaccountUsernamesTable.findByAddress( infos.map((info) => info.address), ); const addressUsernameMap: Record = {}; @@ -156,9 +156,14 @@ class AffiliatesController extends Controller { addressUsernameMap[addressUsername.address] = addressUsername.username; }); if (addressUsernames.length !== infos.length) { + const addressesNotFound = infos + .map((info) => info.address) + .filter((address) => !(address in addressUsernameMap)) + .join(', '); + logger.warning({ at: 'affiliates-controller#snapshot', - message: `Could not find referral code for following addresses: ${infos.map((info) => info.address).filter((address) => !(address in addressUsernameMap)).join(', ')}`, + message: `Could not find referral code for the following addresses: ${addressesNotFound}`, }); }