Skip to content

Commit

Permalink
update endpoint validators
Browse files Browse the repository at this point in the history
  • Loading branch information
Joao Pedro da Silva committed Nov 24, 2023
1 parent 7b448c1 commit 2536e07
Show file tree
Hide file tree
Showing 13 changed files with 298 additions and 145 deletions.
60 changes: 18 additions & 42 deletions packages/api/src/controllers/v2/community/details.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request, Response } from 'express';
import { config, services } from '@impactmarket/core';
import { getAddress } from '@ethersproject/address';
import { services } from '@impactmarket/core';

import { RequestWithUser } from '../../../middlewares/core';
import { standardResponse } from '../../../utils/api';
Expand All @@ -16,30 +16,18 @@ class CommunityController {
getManagers = (req: RequestWithUser, res: Response) => {
const community = req.params.id;
const { search } = req.query;
let { state, offset, limit, orderBy } = req.query;
if (state === undefined || typeof state !== 'string') {
state = undefined;
}
if (offset === undefined || typeof offset !== 'string') {
offset = '0';
}
if (limit === undefined || typeof limit !== 'string') {
limit = '5';
}
if (orderBy === undefined || typeof orderBy !== 'string') {
orderBy = undefined;
}
const { state, offset, limit, orderBy } = req.query;

this.detailsService
.listManagers(
parseInt(community, 10),
parseInt(offset, 10),
parseInt(limit, 10),
offset ? parseInt(offset as string, 10) : config.defaultOffset,
limit ? parseInt(limit as string, 10) : config.defaultLimit,
{
state: state ? parseInt(state, 10) : undefined
state: state ? parseInt(state as string, 10) : undefined
},
search !== undefined && typeof search === 'string' ? search : undefined,
orderBy,
orderBy as string,
req.user?.address
)
.then(r => standardResponse(res, 200, true, r))
Expand All @@ -57,48 +45,31 @@ class CommunityController {
return;
}
const { suspect, inactivity, unidentified, loginInactivity, search, lastActivity_lt } = req.query;
let { state, offset, limit, orderBy } = req.query;
if (state === undefined || typeof state !== 'string') {
state = undefined;
}
if (offset === undefined || typeof offset !== 'string') {
offset = '0';
}
if (limit === undefined || typeof limit !== 'string') {
limit = '5';
}
if (orderBy === undefined || typeof orderBy !== 'string') {
orderBy = undefined;
}
const { state, offset, limit, orderBy } = req.query;
this.detailsService
.listBeneficiaries(
req.user.address,
parseInt(req.params.id, 10),
parseInt(offset, 10),
parseInt(limit, 10),
offset ? parseInt(offset as string, 10) : config.defaultOffset,
limit ? parseInt(limit as string, 10) : config.defaultLimit,
{
state: state ? parseInt(state, 10) : undefined,
state: state ? parseInt(state as string, 10) : undefined,
suspect: suspect ? suspect === 'true' : undefined,
inactivity: inactivity ? inactivity === 'true' : undefined,
unidentified: unidentified ? unidentified === 'true' : undefined,
loginInactivity: loginInactivity ? loginInactivity === 'true' : undefined
},
search !== undefined && typeof search === 'string' ? search : undefined,
orderBy,
lastActivity_lt && typeof lastActivity_lt === 'string' ? parseInt(lastActivity_lt, 10) : undefined
orderBy as string,
lastActivity_lt ? parseInt(lastActivity_lt as string, 10) : undefined
)
.then(r => standardResponse(res, 200, true, r))
.catch(e => standardResponse(res, 400, false, '', { error: e }));
};

findBy = (req: RequestWithUser, res: Response) => {
const { idOrAddress } = req.params;
if (idOrAddress.startsWith('0x')) {
this.detailsService
.findByContractAddress(getAddress(idOrAddress), req.user?.address, req.query)
.then(community => standardResponse(res, 200, !!community, community))
.catch(e => standardResponse(res, 400, false, '', { error: e }));
} else {
if (typeof idOrAddress === 'number') {
const communityId = parseInt(idOrAddress, 10);
if (!communityId) {
standardResponse(res, 400, false, '', {
Expand All @@ -114,6 +85,11 @@ class CommunityController {
.findById(communityId, req.user?.address, req.query)
.then(community => standardResponse(res, 200, true, community))
.catch(e => standardResponse(res, 400, false, '', { error: e }));
} else {
this.detailsService
.findByContractAddress(getAddress(idOrAddress), req.user?.address, req.query)
.then(community => standardResponse(res, 200, !!community, community))
.catch(e => standardResponse(res, 400, false, '', { error: e }));
}
};

Expand Down
10 changes: 5 additions & 5 deletions packages/api/src/routes/v2/community/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Router } from 'express';

import { CommunityController } from '../../../controllers/v2/community/create';
import { authenticateToken, verifySignature } from '../../../middlewares';
import CommunityValidator from '../../../validators/community';
import { create, edit, editSubmission, review } from '../../../validators/community';

export default (route: Router): void => {
const controller = new CommunityController();
Expand Down Expand Up @@ -96,7 +96,7 @@ export default (route: Router): void => {
* security:
* - BearerToken: []
*/
route.post('/', authenticateToken, CommunityValidator.create, controller.create);
route.post('/', authenticateToken, create, controller.create);

/**
* @swagger
Expand Down Expand Up @@ -179,7 +179,7 @@ export default (route: Router): void => {
* security:
* - BearerToken: []
*/
route.patch('/:id', authenticateToken, CommunityValidator.editSubmission, controller.editSubmission);
route.patch('/:id', authenticateToken, editSubmission, controller.editSubmission);

/**
* @swagger
Expand Down Expand Up @@ -213,7 +213,7 @@ export default (route: Router): void => {
* security:
* - BearerToken: []
*/
route.put('/:id/review', authenticateToken, CommunityValidator.review, controller.review);
route.put('/:id/review', authenticateToken, review, controller.review);

/**
* @swagger
Expand Down Expand Up @@ -254,5 +254,5 @@ export default (route: Router): void => {
* - SignatureMessage: []
* - Signature: []
*/
route.put('/:id', authenticateToken, verifySignature, CommunityValidator.edit, controller.edit);
route.put('/:id', authenticateToken, verifySignature, edit, controller.edit);
};
24 changes: 15 additions & 9 deletions packages/api/src/routes/v2/community/details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { CommunityController } from '../../../controllers/v2/community/details';
import { authenticateToken, optionalAuthentication, verifySignature } from '../../../middlewares';
import { cache } from '../../../middlewares/cache-redis';
import { cacheIntervals } from '../../../utils/api';
import {
getBeneficiariesValidator,
getCommunityByIDOrAddressValidator,
getCommunityValidator
} from '../../../validators/community';

const upload = multer({
storage: multer.memoryStorage(),
Expand Down Expand Up @@ -123,7 +128,7 @@ export default (route: Router): void => {
* schema:
* $ref: '#/components/schemas/getManagersResponse'
*/
route.get('/:id/managers/:query?', optionalAuthentication, controller.getManagers);
route.get('/:id/managers/:query?', optionalAuthentication, getCommunityValidator, controller.getManagers);

/**
* @swagger
Expand All @@ -148,7 +153,7 @@ export default (route: Router): void => {
* schema:
* $ref: '#/components/schemas/UbiCommunityContract'
*/
route.get('/:id/contract', controller.getContract);
route.get('/:id/contract', getCommunityValidator, controller.getContract);

/**
* @swagger
Expand All @@ -173,7 +178,7 @@ export default (route: Router): void => {
* schema:
* $ref: '#/components/schemas/UbiCommunityContract'
*/
route.get('/:id/ambassador', optionalAuthentication, controller.getAmbassador);
route.get('/:id/ambassador', getCommunityValidator, optionalAuthentication, controller.getAmbassador);

/**
* @swagger
Expand All @@ -194,7 +199,7 @@ export default (route: Router): void => {
* "200":
* description: OK
*/
route.get('/:id/merchant', controller.getMerchant);
route.get('/:id/merchant', getCommunityValidator, controller.getMerchant);

/**
* @swagger
Expand Down Expand Up @@ -259,7 +264,8 @@ export default (route: Router): void => {
route.get(
'/:id/beneficiaries/:query?',
authenticateToken,
cache(cacheIntervals.halfHour),
// cache(cacheIntervals.halfHour),
getBeneficiariesValidator,
controller.getBeneficiaries
);

Expand Down Expand Up @@ -293,7 +299,7 @@ export default (route: Router): void => {
* longitude:
* type: integer
*/
route.get('/:id/claims-location', cache(cacheIntervals.oneDay), controller.getClaimLocation);
route.get('/:id/claims-location', cache(cacheIntervals.oneDay), getCommunityValidator, controller.getClaimLocation);

/**
* @swagger
Expand Down Expand Up @@ -341,7 +347,7 @@ export default (route: Router): void => {
* schema:
* $ref: '#/components/schemas/UbiPromoter'
*/
route.get('/:id/promoter', controller.getPromoter);
route.get('/:id/promoter', getCommunityValidator, controller.getPromoter);

/**
* @swagger
Expand Down Expand Up @@ -401,7 +407,7 @@ export default (route: Router): void => {
* schema:
* $ref: '#/components/schemas/UbiCommunityCampaign'
*/
route.get('/:id/campaign', cache(cacheIntervals.oneHour), controller.getCampaign);
route.get('/:id/campaign', cache(cacheIntervals.oneHour), getCommunityValidator, controller.getCampaign);

/**
* @swagger
Expand Down Expand Up @@ -433,5 +439,5 @@ export default (route: Router): void => {
* schema:
* $ref: '#/components/schemas/UbiCommunity'
*/
route.get('/:idOrAddress/:query?', optionalAuthentication, controller.findBy);
route.get('/:idOrAddress/:query?', optionalAuthentication, getCommunityByIDOrAddressValidator, controller.findBy);
};
26 changes: 14 additions & 12 deletions packages/api/src/routes/v2/learnAndEarn.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { Router } from 'express';

import {
answer,
createLevel,
listLessonsValidator,
listLevels,
registerClaimRewards,
startLesson
} from '../../validators/learnAndEarn';
import { authenticateToken, optionalAuthentication } from '../../middlewares';
import { cache } from '../../middlewares/cache-redis';
import { cacheIntervals } from '../../utils/api';
import LearnAndEarnController from '../../controllers/v2/learnAndEarn';
import LearnAndEarnValidator from '../../validators/learnAndEarn';

export default (app: Router): void => {
const learnAndEarnController = new LearnAndEarnController();
const learnAndEarnValidator = new LearnAndEarnValidator();
const route = Router();
app.use('/learn-and-earn', route);

Expand Down Expand Up @@ -68,7 +74,7 @@ export default (app: Router): void => {
'/levels',
optionalAuthentication,
cache(cacheIntervals.halfHour, true),
learnAndEarnValidator.listLevels,
listLevels,
learnAndEarnController.listLevels
);

Expand Down Expand Up @@ -102,14 +108,9 @@ export default (app: Router): void => {
* security:
* - BearerToken: []
*/
route.post('/levels', authenticateToken, learnAndEarnValidator.createLevel, learnAndEarnController.createLevel);
route.post('/levels', authenticateToken, createLevel, learnAndEarnController.createLevel);

route.put(
'/levels',
authenticateToken,
learnAndEarnValidator.registerClaimRewards,
learnAndEarnController.registerClaimRewards
);
route.put('/levels', authenticateToken, registerClaimRewards, learnAndEarnController.registerClaimRewards);

/**
* @swagger
Expand All @@ -136,6 +137,7 @@ export default (app: Router): void => {
'/levels/:id',
optionalAuthentication,
cache(cacheIntervals.halfHour, true),
listLessonsValidator,
learnAndEarnController.listLessons
);

Expand Down Expand Up @@ -165,7 +167,7 @@ export default (app: Router): void => {
* security:
* - BearerToken: []
*/
route.put('/lessons', authenticateToken, learnAndEarnValidator.answer, learnAndEarnController.answer);
route.put('/lessons', authenticateToken, answer, learnAndEarnController.answer);

/**
* @swagger
Expand All @@ -189,7 +191,7 @@ export default (app: Router): void => {
* security:
* - BearerToken: []
*/
route.post('/lessons', authenticateToken, learnAndEarnValidator.startLesson, learnAndEarnController.startLesson);
route.post('/lessons', authenticateToken, startLesson, learnAndEarnController.startLesson);

/**
* trigger update
Expand Down
11 changes: 9 additions & 2 deletions packages/api/src/routes/v2/microcredit/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { authenticateToken, onlyAuthorizedRoles, verifySignature } from '../../.
import { cache } from '../../../middlewares/cache-redis';
import { cacheIntervals } from '../../../utils/api';
import {
getFormValidator,
listApplicationsValidator,
listBorrowersValidator,
listManagersByCountryValidator,
queryGetBorrowerValidator,
repaymentsHistoryValidator
} from '../../../validators/microcredit';
Expand Down Expand Up @@ -287,7 +289,7 @@ export default (route: Router): void => {
* security:
* - BearerToken: []
*/
route.get('/form/:id', authenticateToken, controller.getUserForm);
route.get('/form/:id', authenticateToken, getFormValidator, controller.getUserForm);

/**
* @swagger
Expand All @@ -309,7 +311,12 @@ export default (route: Router): void => {
* "200":
* description: OK
*/
route.get('/managers/:country', cache(cacheIntervals.oneHour), controller.getLoanManagersByCountry);
route.get(
'/managers/:country',
cache(cacheIntervals.oneHour),
listManagersByCountryValidator,
controller.getLoanManagersByCountry
);

/**
* @swagger
Expand Down
3 changes: 2 additions & 1 deletion packages/api/src/routes/v2/referrals/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Router } from 'express';
import { authenticateToken, verifyTypedSignature } from '~middlewares/index';
import { cache } from '~middlewares/cache-redis';
import { cacheIntervals } from '~utils/api';
import { referralPostValidator } from '~validators/referral';
import { getReferralCodeValidator, referralPostValidator } from '~validators/referral';
import ReferralController from '~controllers/v2/referral';

export default (app: Router): void => {
Expand Down Expand Up @@ -61,6 +61,7 @@ export default (app: Router): void => {
authenticateToken,
verifyTypedSignature,
cache(cacheIntervals.fiveMinutes),
getReferralCodeValidator,
controller.getByCampaign
);

Expand Down
Loading

0 comments on commit 2536e07

Please sign in to comment.