-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathproject.controller.ts
159 lines (151 loc) · 4.31 KB
/
project.controller.ts
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright IBM Corp. and LoopBack contributors 2020. All Rights Reserved.
// Node module: @loopback/example-access-control-migration
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {authenticate} from '@loopback/authentication';
import {authorize} from '@loopback/authorization';
import {repository} from '@loopback/repository';
import {get, getModelSchemaRef, param, patch} from '@loopback/rest';
import _ from 'lodash';
import {assignProjectInstanceId} from '../components/casbin-authorization';
import {Project} from '../models';
import {ProjectRepository} from '../repositories';
// TBD: refactor the ACLs to a separate file
const RESOURCE_NAME = 'project';
const ACL_PROJECT = {
'view-all': {
resource: `${RESOURCE_NAME}*`,
scopes: ['view-all'],
allowedRoles: ['admin'],
},
'show-balance': {
resource: RESOURCE_NAME,
scopes: ['show-balance'],
allowedRoles: ['owner', 'team'],
voters: [assignProjectInstanceId],
},
donate: {
resource: RESOURCE_NAME,
scopes: ['donate'],
allowedRoles: ['admin', 'owner', 'team'],
voters: [assignProjectInstanceId],
},
withdraw: {
resource: RESOURCE_NAME,
scopes: ['withdraw'],
allowedRoles: ['owner'],
voters: [assignProjectInstanceId],
},
};
// TODO: add other CRUD methods and corresponding ACL
export class ProjectController {
constructor(
@repository(ProjectRepository)
public projectRepository: ProjectRepository,
) {}
// LIST PROJECTS (balance is not public)
@get('/list-projects', {
responses: {
'200': {
description: 'List all the project model instances without balance',
content: {
'application/json': {
schema: {
type: 'array',
items: getModelSchemaRef(Project, {
title: 'ProjectPublic',
exclude: ['balance'],
}),
},
},
},
},
},
})
async listProjects(): Promise<Omit<Project, 'balance'>[]> {
const projects = await this.projectRepository.find();
return projects.map(p => _.omit(p, 'balance'));
}
// VIWE ALL PROJECTS (including balance)
@get('/view-all-projects', {
responses: {
'200': {
description: 'Array of all Project model instances including balance',
content: {
'application/json': {
schema: {
type: 'array',
items: getModelSchemaRef(Project),
},
},
},
},
},
})
@authenticate('jwt')
@authorize(ACL_PROJECT['view-all'])
async viewAll(): Promise<Project[]> {
return this.projectRepository.find();
}
// SHOW BALANCE: get project by id
@get('/projects/{id}/show-balance', {
responses: {
'200': {
description: 'show balance of a project',
content: {
'application/json': {
schema: getModelSchemaRef(Project),
},
},
},
},
})
@authenticate('jwt')
@authorize(ACL_PROJECT['show-balance'])
async findById(@param.path.number('id') id: number): Promise<Project> {
return this.projectRepository.findById(id);
}
// DONATE BY ID
@patch('/projects/{id}/donate', {
responses: {
'204': {
description: 'Project donate success',
},
},
})
@authenticate('jwt')
@authorize(ACL_PROJECT.donate)
async donateById(
@param.path.number('id') id: number,
@param.query.number('amount') amount: number,
): Promise<void> {
const project = await this.projectRepository.findById(id);
await this.projectRepository.updateById(id, {
balance: project.balance + amount,
});
// TBD: return new balance
}
// WITHDRAW BY ID
@patch('/projects/{id}/withdraw', {
responses: {
'204': {
description: 'Project withdraw success',
},
},
})
@authenticate('jwt')
@authorize(ACL_PROJECT.withdraw)
async withdrawById(
@param.path.number('id') id: number,
@param.query.number('amount') amount: number,
): Promise<void> {
const project = await this.projectRepository.findById(id);
if (project.balance < amount) {
throw new Error('Balance is not enough.');
}
await this.projectRepository.updateById(id, {
balance: project.balance - amount,
});
// TBD: return new balance
}
}