generated from idea2app/REST-Node-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Team.ts
162 lines (140 loc) · 4.3 KB
/
Team.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
import {
Authorized,
Body,
CurrentUser,
Delete,
ForbiddenError,
Get,
HttpCode,
JsonController,
NotFoundError,
OnNull,
OnUndefined,
Param,
Patch,
Post,
QueryParams
} from 'routing-controllers';
import { ResponseSchema } from 'routing-controllers-openapi';
import {
BaseFilter,
dataSource,
Hackathon,
Team,
TeamListChunk,
TeamMemberRole,
TeamMemberStatus,
User
} from '../model';
import { searchConditionOf } from '../utility';
import { ActivityLogController } from './ActivityLog';
import { HackathonController } from './Hackathon';
import { PlatformAdminController } from './PlatformAdmin';
import { TeamMemberController } from './TeamMember';
const store = dataSource.getRepository(Team),
hackathonStore = dataSource.getRepository(Hackathon);
@JsonController('/hackathon/:name/team')
export class TeamController {
static async ensureAdmin(userId: number, teamId: number) {
if (
!(await TeamMemberController.isAdmin(userId, teamId)) &&
!(await PlatformAdminController.isAdmin(userId))
)
throw new ForbiddenError();
}
static async ensureMember(userId: number, teamId: number) {
if (!(await TeamMemberController.isMember(userId, teamId)))
throw new ForbiddenError();
}
@Post()
@Authorized()
@HttpCode(201)
@ResponseSchema(Team)
async createOne(
@CurrentUser() createdBy: User,
@Param('name') name: string,
@Body() team: Team
) {
const hackathon = await hackathonStore.findOne({
where: { name },
relations: ['createdBy']
});
if (!hackathon) throw new NotFoundError();
await HackathonController.ensureEnrolled(createdBy.id, name);
const same = await store.findOneBy({
hackathon: { name },
displayName: team.displayName
});
if (same)
throw new ForbiddenError(`Team ${team.displayName} already exists`);
const saved = await store.save({ ...team, hackathon, createdBy });
await ActivityLogController.logCreate(createdBy, 'Team', saved.id);
await TeamMemberController.addOne({
role: TeamMemberRole.Admin,
user: createdBy,
description: 'Team Creator',
status: TeamMemberStatus.Approved,
team: saved,
hackathon,
createdBy
});
return saved;
}
@Patch('/:id')
@Authorized()
@ResponseSchema(Team)
async updateOne(
@CurrentUser() updatedBy: User,
@Param('name') name: string,
@Param('id') id: number,
@Body() newData: Team
) {
const old = await store.findOne({
where: { id },
relations: ['hackathon', 'createdBy']
});
if (!old) throw new NotFoundError();
await TeamController.ensureMember(updatedBy.id, id);
const saved = await store.save({ ...old, ...newData, updatedBy });
await ActivityLogController.logUpdate(updatedBy, 'Team', old.id);
return saved;
}
@Delete('/:id')
@Authorized()
@OnUndefined(204)
async deleteOne(@CurrentUser() deletedBy: User, @Param('id') id: number) {
const team = await store.findOne({
where: { id },
relations: ['hackathon', 'createdBy']
});
if (!team) throw new NotFoundError();
await TeamController.ensureAdmin(deletedBy.id, id);
await store.save({ ...team, deletedBy });
await store.softDelete(id);
await ActivityLogController.logDelete(deletedBy, 'Team', team.id);
}
@Get('/:id')
@OnNull(404)
@ResponseSchema(Team)
getOne(@Param('id') id: number) {
return store.findOneBy({ id });
}
@Get()
@ResponseSchema(TeamListChunk)
async getList(
@Param('name') name: string,
@QueryParams() { keywords, pageSize, pageIndex }: BaseFilter
) {
const where = searchConditionOf<Team>(
['displayName', 'description'],
keywords,
{ hackathon: { name } }
);
const [list, count] = await store.findAndCount({
where,
skip: pageSize * (pageIndex - 1),
take: pageSize
});
return { list, count };
}
}