Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OTE-761] Affiliates comlink total volume and address #2254

Merged
merged 9 commits into from
Sep 13, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
});
});
Expand Down Expand Up @@ -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
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<AffiliateAddressResponse> {
// 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,
};
}

Expand Down Expand Up @@ -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<AffiliateTotalVolumeResponse> {
// 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),
};
}
}
Expand Down
Loading