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

[feature] allowed accounts view loan managers #924

Merged
merged 3 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/api/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,6 @@ export default {
* Variables for restricted actions
*/
admin: {
authorisedAddresses: validatedEnv.ADMIN_AUTHORISED_ADDRESSES
authorisedAddresses: validatedEnv.ADMIN_AUTHORISED_ADDRESSES.split(',').map(a => a.trim())
}
};
2 changes: 1 addition & 1 deletion packages/api/src/config/validatenv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function validateEnv() {
LEARN_AND_EARN_CONTRACT_ADDRESS: str({ devDefault: onlyOnTestEnv('xyz') }),
AWS_LAMBDA: bool({ default: false }),
SIGNATURE_EXPIRATION: num({ default: 15 }),
ADMIN_AUTHORISED_ADDRESSES: str({ default: '0x0' }),
ADMIN_AUTHORISED_ADDRESSES: str({ default: '' }),
SUBGRAPH_URL: str({ devDefault: onlyOnTestEnv('xyz') }),
// attestation service (ASv2)
ATTESTATION_ISSUER_PRIVATE_KEY: str({
Expand Down
24 changes: 19 additions & 5 deletions packages/api/src/controllers/v2/microcredit/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import {
ListApplicationsRequestSchema,
ListBorrowersRequestSchema,
RepaymentHistoryRequestSchema
} from '../../../validators/microcredit';
import { RequestWithUser } from '../../../middlewares/core';
import { ValidatedRequest } from '../../../utils/queryValidator';
import { standardResponse } from '../../../utils/api';
} from '~validators/microcredit';
import { RequestWithUser } from '~middlewares/core';
import { ValidatedRequest } from '~utils/queryValidator';
import { standardResponse } from '~utils/api';
import config from '~config/index';

class MicroCreditController {
private microCreditService: services.MicroCredit.List;
Expand All @@ -28,8 +29,21 @@ class MicroCreditController {
});
return;
}

if (req.query.loanManagerAddress) {
if (!config.admin.authorisedAddresses.includes(req.user.address)) {
standardResponse(res, 400, false, '', {
error: {
name: 'USER_NOT_AUTHORIZED',
message: 'User not authorized!'
}
});
return;
}
}

this.microCreditService
.listBorrowers({ ...req.query, addedBy: req.user.address })
.listBorrowers({ ...req.query, addedBy: req.query.loanManagerAddress ?? req.user.address })
.then(r => standardResponse(res, 200, true, r))
.catch(e => standardResponse(res, 400, false, '', { error: e }));
};
Expand Down
5 changes: 5 additions & 0 deletions packages/api/src/middlewares/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ export const onlyAuthorizedRoles =
}
}

if (config.admin.authorisedAddresses.includes(req.user.address)) {
next();
return;
}

res.status(401).json({
success: false,
error: {
Expand Down
10 changes: 8 additions & 2 deletions packages/api/src/routes/v2/microcredit/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ export default (route: Router): void => {
* enum: [amount, amount:asc, amount:desc, period, period:asc, period:desc, lastRepayment, lastRepayment:asc, lastRepayment:desc, lastDebt, lastDebt:asc, lastDebt:desc, performance, performance:asc, performance:desc]
* required: false
* description: order by
* - in: query
* name: loanManagerAddress
* schema:
* type: string
* required: false
* description: loan manager address used to query from an authorized account
* responses:
* "200":
* description: OK
Expand All @@ -60,8 +66,8 @@ export default (route: Router): void => {
route.get(
'/borrowers/:query?',
authenticateToken,
verifySignature,
cache(cacheIntervals.tenMinutes, true),
// verifySignature,
// cache(cacheIntervals.tenMinutes, true),
dev-jotape marked this conversation as resolved.
Show resolved Hide resolved
onlyAuthorizedRoles(['loanManager']),
listBorrowersValidator,
controller.listBorrowers
Expand Down
4 changes: 3 additions & 1 deletion packages/api/src/validators/microcredit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type ListBorrowersType = {
| 'performance'
| 'performance:asc'
| 'performance:desc';
loanManagerAddress?: string;
};

type ListApplicationsType = {
Expand Down Expand Up @@ -56,7 +57,8 @@ const queryListBorrowersSchema = defaultSchema.object<ListBorrowersType>({
'performance',
'performance:asc',
'performance:desc'
)
),
loanManagerAddress: Joi.string().optional()
});

const queryListApplicationsSchema = defaultSchema.object<ListApplicationsType>({
Expand Down
Loading