generated from idea2app/REST-Node-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enrollment.ts
135 lines (121 loc) · 3.46 KB
/
Enrollment.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
import {
Authorized,
Body,
CurrentUser,
ForbiddenError,
Get,
HttpCode,
JsonController,
NotFoundError,
Param,
Patch,
Post,
QueryParams
} from 'routing-controllers';
import { ResponseSchema } from 'routing-controllers-openapi';
import {
dataSource,
Enrollment,
EnrollmentFilter,
EnrollmentListChunk,
EnrollmentStatus,
Hackathon,
User
} from '../model';
import { searchConditionOf } from '../utility';
import { ActivityLogController } from './ActivityLog';
import { HackathonController } from './Hackathon';
const store = dataSource.getRepository(Enrollment),
hackathonStore = dataSource.getRepository(Hackathon);
@JsonController('/hackathon/:name/enrollment')
export class EnrollmentController {
static async isEnrolled(userId: number, hackathonName: string) {
const enrollment = await store.findOneBy({
hackathon: { name: hackathonName },
createdBy: { id: userId }
});
return !!enrollment;
}
@Get('/session')
@Authorized()
@ResponseSchema(Enrollment)
getSessionOne(@CurrentUser() createdBy: User) {
return store.findOneBy({ createdBy });
}
@Patch('/:id')
@Authorized()
@ResponseSchema(Enrollment)
async updateOne(
@CurrentUser() updatedBy: User,
@Param('id') id: number,
@Body() { status }: Enrollment
) {
const old = await store.findOne({
where: { id },
relations: ['hackathon']
});
if (!old) throw new NotFoundError();
await HackathonController.ensureAdmin(updatedBy.id, old.hackathon.name);
const saved = await store.save({ ...old, status, updatedBy });
await ActivityLogController.logUpdate(updatedBy, 'Enrollment', old.id);
return saved;
}
@Post()
@Authorized()
@HttpCode(201)
@ResponseSchema(Enrollment)
async createOne(
@CurrentUser() createdBy: User,
@Param('name') name: string,
@Body() { form }: Enrollment
) {
const hackathon = await hackathonStore.findOneBy({ name }),
now = Date.now();
if (!hackathon) throw new NotFoundError();
if (
now < +new Date(hackathon.enrollmentStartedAt) ||
now > +new Date(hackathon.enrollmentEndedAt)
)
throw new ForbiddenError('Not in enrollment period');
const saved = await store.save({
createdBy,
hackathon,
form,
status: hackathon.autoApprove
? EnrollmentStatus.Approved
: EnrollmentStatus.PendingApproval
});
await ActivityLogController.logCreate(
createdBy,
'Enrollment',
saved.id
);
return saved;
}
@Get()
@ResponseSchema(EnrollmentListChunk)
async getList(
@QueryParams()
{
keywords,
pageSize,
pageIndex,
status,
createdBy,
updatedBy
}: EnrollmentFilter
) {
const where = searchConditionOf<Enrollment>(['form'], keywords, {
status,
createdBy: { id: createdBy },
updatedBy: { id: updatedBy }
});
const [list, count] = await store.findAndCount({
where,
relations: ['createdBy'],
skip: pageSize * (pageIndex - 1),
take: pageSize
});
return { list, count };
}
}