generated from idea2app/REST-Node-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Evaluation.ts
91 lines (84 loc) · 2.3 KB
/
Evaluation.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
import {
Authorized,
Body,
CurrentUser,
ForbiddenError,
Get,
HttpCode,
JsonController,
NotFoundError,
Param,
Post,
QueryParams
} from 'routing-controllers';
import { ResponseSchema } from 'routing-controllers-openapi';
import {
BaseFilter,
dataSource,
Evaluation,
EvaluationListChunk,
Team,
User
} from '../model';
import { searchConditionOf } from '../utility';
import { ActivityLogController } from './ActivityLog';
import { HackathonController } from './Hackathon';
const store = dataSource.getRepository(Evaluation),
teamStore = dataSource.getRepository(Team);
@JsonController('/hackathon/:name/team/:tid/evaluation')
export class EvaluationController {
@Post()
@Authorized()
@HttpCode(201)
@ResponseSchema(Evaluation)
async createOne(
@CurrentUser() createdBy: User,
@Param('name') name: string,
@Param('tid') tid: number,
@Body() evaluation: Evaluation
) {
const team = await teamStore.findOne({
where: { id: tid },
relations: ['hackathon']
});
if (!team) throw new NotFoundError();
const { hackathon } = team,
now = Date.now();
if (
now < +new Date(hackathon.judgeStartedAt) ||
now > +new Date(hackathon.judgeEndedAt)
)
throw new ForbiddenError('Not in evaluation period');
await HackathonController.ensureJudge(createdBy.id, name);
const saved = await store.save({
...evaluation,
team,
hackathon: team.hackathon,
createdBy
});
await ActivityLogController.logCreate(
createdBy,
'Evaluation',
saved.id
);
return saved;
}
@Get()
@ResponseSchema(EvaluationListChunk)
async getList(
@Param('tid') tid: number,
@QueryParams() { keywords, pageSize, pageIndex }: BaseFilter
) {
const where = searchConditionOf<Evaluation>(
['scores', 'comment'],
keywords,
{ team: { id: tid } }
);
const [list, count] = await store.findAndCount({
where,
skip: pageSize * (pageIndex - 1),
take: pageSize
});
return { list, count };
}
}