generated from idea2app/REST-Node-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Staff.ts
171 lines (151 loc) · 4.54 KB
/
Staff.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
160
161
162
163
164
165
166
167
168
169
170
171
import {
Authorized,
Body,
CurrentUser,
Delete,
ForbiddenError,
Get,
HttpCode,
JsonController,
NotFoundError,
OnUndefined,
Param,
Patch,
Put,
QueryParams
} from 'routing-controllers';
import { ResponseSchema } from 'routing-controllers-openapi';
import {
Base,
dataSource,
Hackathon,
Staff,
StaffFilter,
StaffListChunk,
StaffType,
User
} from '../model';
import { searchConditionOf } from '../utility';
import { ActivityLogController } from './ActivityLog';
import { HackathonController } from './Hackathon';
const store = dataSource.getRepository(Staff),
userStore = dataSource.getRepository(User),
hackathonStore = dataSource.getRepository(Hackathon);
@JsonController('/hackathon/:name/:type')
export class StaffController {
static async isAdmin(userId: number, hackathonName: string) {
const staff = await store.findOneBy({
hackathon: { name: hackathonName },
user: { id: userId },
type: StaffType.Admin
});
return !!staff;
}
static async isJudge(userId: number, hackathonName: string) {
const staff = await store.findOneBy({
hackathon: { name: hackathonName },
user: { id: userId },
type: StaffType.Judge
});
return !!staff;
}
static async addOne(staff: Omit<Staff, keyof Base>) {
const saved = await store.save(staff);
await ActivityLogController.logCreate(
staff.createdBy,
'Staff',
saved.id
);
return saved;
}
@Put('/:uid')
@HttpCode(201)
@Authorized()
@ResponseSchema(Staff)
async createOne(
@CurrentUser() createdBy: User,
@Param('name') name: string,
@Param('type') type: StaffType,
@Param('uid') uid: number,
@Body() staff: Staff
) {
const [user, hackathon] = await Promise.all([
userStore.findOneBy({ id: uid }),
hackathonStore.findOneBy({ name })
]);
if (!user || !hackathon || !StaffType[type]) throw new NotFoundError();
if (createdBy.id === uid) throw new ForbiddenError();
await HackathonController.ensureAdmin(createdBy.id, name);
return StaffController.addOne({
...staff,
type,
user,
hackathon,
createdBy
});
}
@Patch('/:uid')
@Authorized()
@ResponseSchema(Staff)
async updateOne(
@CurrentUser() updatedBy: User,
@Param('name') name: string,
@Param('type') type: StaffType,
@Param('uid') uid: number,
@Body() { description }: Staff
) {
const staff = await store.findOne({
where: { hackathon: { name }, type, user: { id: uid } },
relations: ['hackathon']
});
if (!staff) throw new NotFoundError();
await HackathonController.ensureAdmin(updatedBy.id, name);
const saved = await store.save({
...staff,
description,
updatedBy
});
await ActivityLogController.logUpdate(updatedBy, 'Staff', staff.id);
return saved;
}
@Delete('/:uid')
@Authorized()
@OnUndefined(204)
async deleteOne(
@CurrentUser() deletedBy: User,
@Param('name') name: string,
@Param('type') type: StaffType,
@Param('uid') uid: number
) {
const staff = await store.findOne({
where: { hackathon: { name }, type, user: { id: uid } },
relations: ['hackathon']
});
if (!staff) throw new NotFoundError();
if (deletedBy.id === uid) throw new ForbiddenError();
await HackathonController.ensureAdmin(deletedBy.id, name);
await store.save({ ...staff, deletedBy });
await store.softDelete(staff.id);
await ActivityLogController.logDelete(deletedBy, 'Staff', staff.id);
}
@Get()
@ResponseSchema(StaffListChunk)
async getList(
@Param('name') name: string,
@Param('type') type: StaffType,
@QueryParams() { keywords, pageSize, pageIndex, user }: StaffFilter
) {
const where = searchConditionOf<Staff>(['description'], keywords, {
hackathon: { name },
type,
user: { id: user }
});
const [list, count] = await store.findAndCount({
where,
relations: ['hackathon', 'user'],
skip: pageSize * (pageIndex - 1),
take: pageSize
});
return { list, count };
}
}