forked from FreeCodeCamp-Chengdu/HOP-service
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHackathon.ts
More file actions
180 lines (158 loc) · 4.7 KB
/
Hackathon.ts
File metadata and controls
180 lines (158 loc) · 4.7 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import {
Authorized,
Body,
CurrentUser,
Delete,
ForbiddenError,
Get,
HttpCode,
JsonController,
NotFoundError,
OnNull,
OnUndefined,
Param,
Post,
Put,
QueryParams
} from 'routing-controllers';
import { ResponseSchema } from 'routing-controllers-openapi';
import {
dataSource,
Hackathon,
HackathonFilter,
HackathonListChunk,
StaffType,
User
} from '../model';
import { searchConditionOf } from '../utility';
import { ActivityLogController } from './ActivityLog';
import { EnrollmentController } from './Enrollment';
import { PlatformAdminController } from './PlatformAdmin';
import { StaffController } from './Staff';
const store = dataSource.getRepository(Hackathon);
@JsonController('/hackathon')
export class HackathonController {
static async ensureAdmin(userId: number, hackathonName: string) {
if (
!(await StaffController.isAdmin(userId, hackathonName)) &&
!(await PlatformAdminController.isAdmin(userId))
)
throw new ForbiddenError();
}
static async ensureJudge(userId: number, hackathonName: string) {
if (!(await StaffController.isJudge(userId, hackathonName)))
throw new ForbiddenError();
}
static async ensureEnrolled(userId: number, hackathonName: string) {
if (!(await EnrollmentController.isEnrolled(userId, hackathonName)))
throw new ForbiddenError();
}
@Put('/:name')
@Authorized()
@ResponseSchema(Hackathon)
async updateOne(
@CurrentUser() updatedBy: User,
@Param('name') name: string,
@Body() newData: Hackathon
) {
const old = await store.findOne({
where: { name },
relations: ['createdBy']
});
if (!old) throw new NotFoundError();
await HackathonController.ensureAdmin(updatedBy.id, name);
const saved = await store.save({ ...old, ...newData, updatedBy });
await ActivityLogController.logUpdate(updatedBy, 'Hackathon', old.id);
return saved;
}
@Get('/:name')
@ResponseSchema(Hackathon)
@OnNull(404)
async getOne(@CurrentUser() user: User, @Param('name') name: string) {
const hackathon = await store.findOne({
where: { name },
relations: ['createdBy']
});
if (user) {
const hid = hackathon.id,
uid = user.id;
hackathon.roles = {
isAdmin: await StaffController.isAdmin(uid, name),
isJudge: await StaffController.isJudge(uid, name),
isEnrolled: await EnrollmentController.isEnrolled(uid, name)
};
}
return hackathon;
}
@Delete('/:name')
@Authorized()
@OnUndefined(204)
async deleteOne(
@CurrentUser() deletedBy: User,
@Param('name') name: string
) {
const old = await store.findOneBy({ name });
if (!old) throw new NotFoundError();
await HackathonController.ensureAdmin(deletedBy.id, name);
await store.save({ ...old, deletedBy });
await store.softDelete(old.id);
await ActivityLogController.logDelete(deletedBy, 'Hackathon', old.id);
}
@Post()
@Authorized()
@HttpCode(201)
@ResponseSchema(Hackathon)
async createOne(
@CurrentUser() createdBy: User,
@Body() hackathon: Hackathon
) {
const saved = await store.save({ ...hackathon, createdBy });
await ActivityLogController.logCreate(createdBy, 'Hackathon', saved.id);
await StaffController.addOne({
type: StaffType.Admin,
user: createdBy,
description: 'Hackathon Creator',
hackathon: saved,
createdBy
});
return saved;
}
@Get()
@ResponseSchema(HackathonListChunk)
async getList(
@QueryParams()
{
keywords,
pageSize,
pageIndex,
createdBy,
updatedBy,
...filter
}: HackathonFilter
) {
const where = searchConditionOf<Hackathon>(
[
'name',
'displayName',
'ribbon',
'summary',
'detail',
'location',
'tags'
],
keywords,
{
...filter,
createdBy: { id: createdBy },
updatedBy: { id: updatedBy }
}
);
const [list, count] = await store.findAndCount({
where,
relations: ['createdBy'],
skip: pageSize * (pageIndex - 1),
take: pageSize
});
return { list, count };
}
}