forked from fidlabs/allocator-rkh-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeta-allocator.controller.ts
More file actions
75 lines (68 loc) · 2.88 KB
/
meta-allocator.controller.ts
File metadata and controls
75 lines (68 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import {
controller,
httpGet,
httpPost,
request,
requestBody,
requestParam,
response,
} from 'inversify-express-utils';
import { Request, Response } from 'express';
import { badPermissions, badRequest, ok } from '@src/api/http/processors/response';
import { MetaAllocatorService } from '@src/application/services/meta-allocator.service';
import { TYPES } from '@src/types';
import { inject } from 'inversify';
import { LOG_MESSAGES, RESPONSE_MESSAGES } from '@src/constants';
import { ICommandBus, Logger } from '@filecoin-plus/core';
import { validateGovernanceReview } from '../validators';
import { validateRequest } from '../middleware/validate-request.middleware';
import { GovernanceReviewDto } from '@src/application/dtos/GovernanceReviewDto';
import { RoleService } from '@src/application/services/role.service';
import { RejectRefreshCommand } from '@src/application/use-cases/refresh-issues/reject-refesh.command';
import {
SignatureGuard,
SignatureType,
WalletType,
} from '@src/patterns/decorators/signature-guard.decorator';
const LOG = LOG_MESSAGES.MA_CONTROLLER;
const RES = RESPONSE_MESSAGES.MA_CONTROLLER;
@controller('/api/v1/ma')
export class MetaAllocatorController {
constructor(
@inject(TYPES.MetaAllocatorService) private readonly metaAllocatorService: MetaAllocatorService,
@inject(TYPES.Logger) private readonly logger: Logger,
@inject(TYPES.RoleService) private readonly roleService: RoleService,
@inject(TYPES.CommandBus) private readonly commandBus: ICommandBus,
) {}
@httpGet('')
async getMetaAllocatorAddresses(@request() req: Request, @response() res: Response) {
this.logger.info(LOG.FETCHING_MA_ADDRESSES);
return res.json(ok(RES.MA_ADDRESSES_RETRIEVED, this.metaAllocatorService.getAll()));
}
@httpPost('/:githubIssueNumber/reject', validateRequest(validateGovernanceReview))
@SignatureGuard(SignatureType.MetaAllocatorReject, WalletType.MetaMask)
async rejectMetaAllocator(
@requestParam('githubIssueNumber') githubIssueNumber: string,
@requestBody() rejectMetaAllocatorDto: GovernanceReviewDto,
@response() res: Response,
) {
this.logger.info(LOG.REJECTING_REFRESH_AS_META_ALLOCATOR);
const id = parseInt(githubIssueNumber);
const {
result,
details: { reviewerAddress },
} = rejectMetaAllocatorDto;
const role = this.roleService.getRole(reviewerAddress);
if (!['GOVERNANCE_TEAM', 'METADATA_ALLOCATOR'].includes(role as string)) {
this.logger.error(
`Cannot reject refresh: ${role as string} for address ${reviewerAddress} not authorised`,
);
return res.status(403).json(badPermissions());
}
const refreshResult = await this.commandBus.send(new RejectRefreshCommand(id));
if (!refreshResult.success) {
return res.status(400).json(badRequest(refreshResult.error.message));
}
return res.json(ok(RES.REJECTED_AS_META_ALLOCATOR, result));
}
}