-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add stub endpoint for affiliates/referral_code
- Loading branch information
1 parent
372f645
commit 897e8d4
Showing
6 changed files
with
263 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
indexer/services/comlink/__tests__/controllers/api/v4/affiliates-controller.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { RequestMethod } from '../../../../src/types'; | ||
import request from 'supertest'; | ||
import { sendRequest } from '../../../helpers/helpers'; | ||
|
||
describe('test-controller#V4', () => { | ||
|
||
describe('GET /referral_code', () => { | ||
|
||
it('should return referral code for a valid address string', async () => { | ||
const address = 'some_address'; | ||
const response: request.Response = await sendRequest({ | ||
type: RequestMethod.GET, | ||
path: `/v4/affiliates/referral_code?address=${address}`, | ||
}); | ||
|
||
expect(response.status).toBe(200); | ||
expect(response.body).toEqual({ | ||
referralCode: `${address}-referral_code`, | ||
}); | ||
|
||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
indexer/services/comlink/src/controllers/api/v4/affiliates-controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { stats } from '@dydxprotocol-indexer/base'; | ||
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 { handleControllerError } from '../../../lib/helpers'; | ||
import { rateLimiterMiddleware } from '../../../lib/rate-limit'; | ||
import ExportResponseCodeStats from '../../../request-helpers/export-response-code-stats'; | ||
import { AffiliateReferralCodeRequest, AffiliateReferralCodeResponse } from '../../../types'; | ||
|
||
const router: express.Router = express.Router(); | ||
const controllerName: string = 'affiliates-controller'; | ||
|
||
@Route('affiliates') | ||
class AffiliatesController extends Controller { | ||
@Get('/referral_code') | ||
async getReferralCode( | ||
@Query() address: string, | ||
): Promise<AffiliateReferralCodeResponse> { | ||
|
||
// simulate a delay | ||
await new Promise((resolve) => setTimeout(resolve, 100)); | ||
|
||
return { | ||
referralCode: `${address}-referral_code`, | ||
}; | ||
} | ||
} | ||
|
||
router.get( | ||
'/referral_code', | ||
rateLimiterMiddleware(getReqRateLimiter), | ||
...checkSchema({ | ||
address: { | ||
in: ['query'], | ||
isString: true, | ||
errorMessage: 'address must be a valid string', | ||
}, | ||
}), | ||
ExportResponseCodeStats({ controllerName }), | ||
async (req: express.Request, res: express.Response) => { | ||
const start: number = Date.now(); | ||
const { | ||
address, | ||
}: AffiliateReferralCodeRequest = matchedData(req) as AffiliateReferralCodeRequest; | ||
|
||
try { | ||
const controller: AffiliatesController = new AffiliatesController(); | ||
const response: AffiliateReferralCodeResponse = await controller.getReferralCode(address); | ||
return res.send(response); | ||
} catch (error) { | ||
return handleControllerError( | ||
'AffiliatesController GET /referral_code', | ||
'Affiliates refferal code error', | ||
error, | ||
req, | ||
res, | ||
); | ||
} finally { | ||
stats.timing( | ||
`${config.SERVICE_NAME}.${controllerName}.get_referral_code.timing`, | ||
Date.now() - start, | ||
); | ||
} | ||
}, | ||
); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters