This repository was archived by the owner on Oct 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathteam_info.ts
More file actions
225 lines (185 loc) · 7.47 KB
/
team_info.ts
File metadata and controls
225 lines (185 loc) · 7.47 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { Request, Response } from 'express';
import { getApplicationAttribute, setApplicationAttribute } from "./common";
import { IApplication } from '../models/Application.d';
import Application from '../models/Application';
import { HACKATHON_YEAR } from '../constants';
import { mergeWith, pickBy, without } from "lodash";
export function getTeamInfo(req: Request, res: Response) {
return getApplicationAttribute(req, res, (e: IApplication) => {
return e.forms.team_info || {};
}, true);
}
export function setTeamInfo(req: Request, res: Response) {
return setApplicationAttribute(req, res,
(e: IApplication) => {
e.forms.team_info = req.body;
},
e => e.forms.team_info
);
}
// parse existing teammate list, or create one with only user if doesn't exist
function parseList(list: string | null, email: string): Record<string, number> {
try {
return JSON.parse(list);
} catch (_) {
return { [email]: 1 };
}
}
// filter out pending teammates from team list
function filterPending(list: Record<string, number>) {
return pickBy(list, value => value !== 0);
}
// filter out approved teammates from team list
function filterApproved(list: Record<string, number>) {
return pickBy(list, value => value !== 1);
}
// combine team lists, preferring a confirmed teammate (a number 1) over a pending teammate (a number 0)
function combineLists(one: Record<string, number>, two: Record<string, number>): Record<string, number> {
return mergeWith(one, two, (oneValue, twoValue) => oneValue || twoValue);
}
function applicationByEmail(email: string): PromiseLike<IApplication | null> {
return Application.findOne(
{ "user.email": email, year: HACKATHON_YEAR },
{ __v: 0, reviews: 0 }
);
}
// add each teammate from `two` to the teamList of each user in `one`
async function combineTeams(one: Record<string, number>, two: Record<string, number>) {
for (const email of Object.keys(one)) {
const teammate = await applicationByEmail(email);
if (!teammate) {
throw new Error("Teammate not found.");
}
const teammateList = parseList(teammate.forms.team_info.teamList.toString(), email);
const newTeam = combineLists(teammateList, two);
teammate.forms.team_info.teamList = JSON.stringify(newTeam);
await teammate.save();
}
}
export async function addTeammate(req: Request, res: Response) {
// find request user's application
const user: IApplication | null = await Application.findOne(
{ "user.id": req.params.userId },
{ __v: 0, reviews: 0 }
);
if (!user) {
res.status(404).json({message: "User application not found."});
return;
}
// find requested teammate user's application
const teammate = await applicationByEmail(req.body.email);
if (!teammate) {
res.status(404).json({message: "Teammate application not found."});
return;
}
// filter out user's pending teammates
const userList = parseList(user.forms.team_info.teamList.toString(), user.user.email);
const userConfirmed = filterPending(userList);
// filter out teammate's pending teammates
const teammateList = parseList(teammate.forms.team_info.teamList.toString(), teammate.user.email);
const teammateConfirmed = filterPending(teammateList);
// requested teammate hasn't added user
if (!teammateList.hasOwnProperty(user.user.email)) {
// don't allow user to have more than 8 pending teammates
if (Object.keys(filterApproved(userList)).length > 8) {
res.status(400).json({message: "Too many pending teammates."});
return;
}
userList[teammate.user.email] = 0;
user.forms.team_info.teamList = JSON.stringify(userList);
await user.save();
res.status(200).json(user.forms.team_info);
return;
}
// check combined teams at most four people
const combinedConfirmed = combineLists(userConfirmed, teammateConfirmed);
if (Object.keys(combinedConfirmed).length > 4) {
res.status(400).json({message: "Too many combined teammates."});
return;
}
// for each of user's current teammate, combine with requested teammate
try {
await combineTeams(userConfirmed, teammateConfirmed);
} catch (e) {
if (e instanceof Error) {
res.status(404).json({message: e.message});
return;
}
}
// for each of requeste teammates's current teammate, combine with user
try {
await combineTeams(teammateConfirmed, userConfirmed);
} catch (e) {
if (e instanceof Error) {
res.status(404).json({message: e.message});
return;
}
}
res.status(200).json(user.forms.team_info);
}
// remove an email `removed` from each confirmed teammate in `team`
async function removeTeammateFromAll(team: Record<string, number>, removed: string) {
for (const email of without(Object.keys(team), removed)) {
const teammate = await applicationByEmail(email);
if (!teammate) {
throw new Error("Teammate not found.");
}
const teammateList = parseList(teammate.forms.team_info.teamList.toString(), email);
delete teammateList[removed];
teammate.forms.team_info.teamList = JSON.stringify(teammateList);
await teammate.save();
}
}
export async function removeTeammate(req: Request, res: Response) {
// find request user's application
const user: IApplication | null = await Application.findOne(
{ "user.id": req.params.userId },
{ __v: 0, reviews: 0 }
);
if (!user) {
res.status(404).json({message: "User application not found."});
return;
}
// find removed teammate user's application
const teammate = await applicationByEmail(req.body.email);
if (!teammate) {
res.status(404).json({message: "Teammate application not found."});
return;
}
// filter out user's pending teammates
const userList = parseList(user.forms.team_info.teamList.toString(), user.user.email);
const userConfirmed = filterPending(userList);
// ensure email exists in team
if (!userList.hasOwnProperty(teammate.user.email)) {
res.status(400).json({message: "Removed teammate is not in user's team list."});
return;
}
// if pending, only remove from user's team list
if (!userConfirmed.hasOwnProperty(teammate.user.email)) {
delete userList[teammate.user.email];
user.forms.team_info.teamList = JSON.stringify(userList);
await user.save()
res.status(200).json(user.forms.team_info);
return;
}
// delete all emails except self and pending teammates in deleted user's team list
const teammateList = parseList(teammate.forms.team_info.teamList.toString(), teammate.user.email);
for (const email of without(Object.keys(teammateList), teammate.user.email)) {
if (teammateList[email] === 1) {
delete teammateList[email];
}
}
teammate.forms.team_info.teamList = JSON.stringify(teammateList);
await teammate.save();
// for each user in the team except the deleted user,
// remove the deleted user's email
try {
await removeTeammateFromAll(userConfirmed, teammate.user.email);
} catch (e) {
if (e instanceof Error) {
res.status(404).json({message: e.message});
return;
}
}
res.status(200).json(user.forms.team_info);
}