Repository navigation
Expand file tree
/
Copy pathUser.ts
More file actions
165 lines (144 loc) · 4.45 KB
/
Copy pathUser.ts
File metadata and controls
165 lines (144 loc) · 4.45 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
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
160
161
162
163
164
165
import {
Authorized,
Body,
CurrentUser,
Delete,
ForbiddenError,
Get,
HttpCode,
JsonController,
OnNull,
OnUndefined,
Param,
Post,
Put,
QueryParams
} from 'routing-controllers';
import { ResponseSchema } from 'routing-controllers-openapi';
import {
BaseFilter,
Enrollment,
HackathonListChunk,
Role,
SignInData,
Staff,
User,
UserFilter,
UserHackathonType,
UserListChunk
} from '../model';
import {
activityLogService,
BaseService,
hackathonService,
sessionService
} from '../service';
const UserHackathonTypeRegExp = Object.values(UserHackathonType).join('|');
@JsonController('/user')
export class UserController {
store = sessionService.userStore;
service = new BaseService(User, ['email', 'mobilePhone', 'name']);
@Get('/session')
@Authorized()
@ResponseSchema(User)
getSession(@CurrentUser() user: User) {
return user;
}
@Post('/session')
@HttpCode(201)
@ResponseSchema(User)
async signIn(@Body() { email, password }: SignInData): Promise<User> {
const user = await this.store.findOneBy({
email,
password: sessionService.encrypt(password)
});
if (!user) throw new ForbiddenError();
return sessionService.sign(user);
}
@Post()
@HttpCode(201)
@ResponseSchema(User)
signUp(@Body() data: SignInData) {
return sessionService.signUp(data);
}
@Put('/:id')
@Authorized()
@ResponseSchema(User)
async updateOne(
@Param('id') id: number,
@CurrentUser() updatedBy: User,
@Body() { password, ...data }: User
) {
if (!updatedBy.roles.includes(Role.Administrator) && id !== updatedBy.id)
throw new ForbiddenError();
await this.store.save({
...data,
password: password && sessionService.encrypt(password),
id
});
await activityLogService.logUpdate(updatedBy, 'User', id);
return sessionService.sign(await this.store.findOneBy({ id }));
}
@Get(`/:id/hackathon/:type(${UserHackathonTypeRegExp})`)
@ResponseSchema(HackathonListChunk)
async getHackathonListByType(
@Param('id') id: number,
@Param('type') type: UserHackathonType,
@QueryParams() { pageSize = 10, pageIndex = 1 }: BaseFilter
): Promise<HackathonListChunk> {
const skip = pageSize * (pageIndex - 1);
if (type === UserHackathonType.Enrollee) {
const [list, count] = await hackathonService.store
.createQueryBuilder('hackathon')
.innerJoin(
Enrollment,
'enrollment',
'enrollment.hackathonId = hackathon.id AND enrollment.createdById = :id',
{ id }
)
.leftJoinAndSelect('hackathon.createdBy', 'createdBy')
.orderBy('hackathon.updatedAt', 'DESC')
.skip(skip)
.take(pageSize)
.getManyAndCount();
return { count, list };
}
if (type === UserHackathonType.Staff) {
const [list, count] = await hackathonService.store
.createQueryBuilder('hackathon')
.innerJoin(
Staff,
'staff',
'staff.hackathonId = hackathon.id AND staff.userId = :id',
{ id }
)
.leftJoinAndSelect('hackathon.createdBy', 'createdBy')
.orderBy('hackathon.updatedAt', 'DESC')
.skip(skip)
.take(pageSize)
.getManyAndCount();
return { count, list };
}
return hackathonService.getList({ createdBy: id, pageSize, pageIndex });
}
@Get('/:id')
@OnNull(404)
@ResponseSchema(User)
getOne(@Param('id') id: number) {
return this.service.getOne(id);
}
@Delete('/:id')
@Authorized()
@OnUndefined(204)
async deleteOne(@Param('id') id: number, @CurrentUser() deletedBy: User) {
if (deletedBy.roles.includes(Role.Administrator) && id == deletedBy.id)
throw new ForbiddenError();
await this.store.softDelete(id);
await activityLogService.logDelete(deletedBy, 'User', id);
}
@Get()
@ResponseSchema(UserListChunk)
getList(@QueryParams() filter: UserFilter) {
return this.service.getList(filter);
}
}