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 465865020d..56565269ed 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 @@ -138,16 +138,34 @@ describe('affiliates-controller#V4', () => { }); describe('GET /address', () => { - it('should return address for a valid referral code string', async () => { - const referralCode = 'TempCode123'; + beforeEach(async () => { + await testMocks.seedData(); + await SubaccountUsernamesTable.create(testConstants.defaultSubaccountUsername); + }); + + afterEach(async () => { + await dbHelpers.clearData(); + }); + + it('should return address for a valid referral code', async () => { + const referralCode = testConstants.defaultSubaccountUsername.username; const response: request.Response = await sendRequest({ type: RequestMethod.GET, path: `/v4/affiliates/address?referralCode=${referralCode}`, + expectedStatus: 200, // helper performs expect on status }); - expect(response.status).toBe(200); expect(response.body).toEqual({ - address: 'some_address', + address: testConstants.defaultWallet.address, + }); + }); + + it('should fail when referral code not found', async () => { + const nonExistentReferralCode = 'BadCode123'; + await sendRequest({ + type: RequestMethod.GET, + path: `/v4/affiliates/address?referralCode=${nonExistentReferralCode}`, + expectedStatus: 404, // helper performs expect on status }); }); }); @@ -184,16 +202,39 @@ describe('affiliates-controller#V4', () => { }); describe('GET /total_volume', () => { - it('should return total_volume for a valid address', async () => { - const address = 'some_address'; + beforeEach(async () => { + await testMocks.seedData(); + await WalletTable.update( + { + address: testConstants.defaultWallet.address, + totalVolume: '100000', + totalTradingRewards: '0', + }, + ); + }); + + afterEach(async () => { + await dbHelpers.clearData(); + }); + + it('should return total volume for a valid address', async () => { const response: request.Response = await sendRequest({ type: RequestMethod.GET, - path: `/v4/affiliates/total_volume?address=${address}`, + path: `/v4/affiliates/total_volume?address=${testConstants.defaultWallet.address}`, + expectedStatus: 200, // helper performs expect on status }); - expect(response.status).toBe(200); expect(response.body).toEqual({ - totalVolume: 111.1, + totalVolume: 100000, + }); + }); + + it('should fail if address does not exist', async () => { + const nonExistentAddress = 'adgsakhasgt'; + await sendRequest({ + type: RequestMethod.GET, + path: `/v4/affiliates/metadata?address=${nonExistentAddress}`, + expectedStatus: 404, // 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 278b988422..841d37c300 100644 --- a/indexer/services/comlink/src/controllers/api/v4/affiliates-controller.ts +++ b/indexer/services/comlink/src/controllers/api/v4/affiliates-controller.ts @@ -93,12 +93,24 @@ class AffiliatesController extends Controller { @Get('/address') async getAddress( - @Query() referralCode: string, // eslint-disable-line @typescript-eslint/no-unused-vars + @Query() referralCode: string, ): Promise { - // simulate a delay - await new Promise((resolve) => setTimeout(resolve, 100)); + const usernameRow = await SubaccountUsernamesTable.findByUsername(referralCode); + if (!usernameRow) { + throw new NotFoundError(`Referral code ${referralCode} does not exist`); + } + const subAccountId = usernameRow.subaccountId; + + const subaccountRow = await SubaccountTable.findById(subAccountId); + // subaccountRow should never be undefined because of foreign key constraint between subaccounts + // and subaccount_usernames tables + if (!subaccountRow) { + throw new UnexpectedServerError(`Subaccount ${subAccountId} not found`); + } + const address = subaccountRow.address; + return { - address: 'some_address', + address, }; } @@ -143,12 +155,16 @@ class AffiliatesController extends Controller { @Get('/total_volume') public async getTotalVolume( - @Query() address: string, // eslint-disable-line @typescript-eslint/no-unused-vars + @Query() address: string, ): Promise { - // simulate a delay - await new Promise((resolve) => setTimeout(resolve, 100)); + // Check that the address exists + const walletRow = await WalletTable.findById(address); + if (!walletRow) { + throw new NotFoundError(`Wallet with address ${address} not found`); + } + return { - totalVolume: 111.1, + totalVolume: Number(walletRow.totalVolume), }; } }