-
Notifications
You must be signed in to change notification settings - Fork 0
/
team.js
71 lines (69 loc) · 2.18 KB
/
team.js
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
export async function updatePoints(req, res) {
const { id } = req.params;
let mainQuest = [];
let sideQuest = [];
try {
let { score, hintId } = req.body;
const team = await TeamModel.findById(id).populate("route").exec();
if (!team) return res.send(createResponse(DATA_NOT_FOUND));
// console.log(team);
mainQuest =
Array.isArray(team.mainQuest) && team.mainQuest.length > 0
? team.mainQuest
: [];
sideQuest =
Array.isArray(team.sideQuest) && team.sideQuest.length > 0
? team.sideQuest
: [];
const quests = team.route;
// console.log(hintId);
// check if main quest
// console.log(quests.hints.length);
if (
quests.hints.length > team.numMain &&
quests.hints[team.numMain].toString() === hintId
) {
mainQuest = [...mainQuest, hintId];
score = 100;
console.log("Hint added ", mainQuest);
} else {
// console.log(hintId);
const hint = await HintsModel.findOne({ _id: hintId, type: "side" });
// console.log(hint);
// if side quest is found
if (hint) {
if (team.sideQuest.includes(hintId)) {
return res.send(createResponse(QUEST_ALREADY_EXISTS));
} else {
sideQuest = [...sideQuest, hintId];
score = 50;
}
} else {
return res.send(createResponse(INVALID_QUEST)); // invalid hint
}
}
const updatedTeam = await TeamModel.findByIdAndUpdate(
id,
{
score: team.score + score,
numMain: mainQuest.length,
numSide: sideQuest.length,
mainQuest: mainQuest,
sideQuest: sideQuest,
},
{ new: true }
)
.populate("mainQuest")
.populate("sideQuest")
.select("-route -teamMembers -teamLead")
.exec();
const hint = await HintsModel.findById({ _id: hintId }).exec();
if (!updatedTeam) return res.send(createResponse(DATA_NOT_FOUND));
if (!hint) return res.send(createResponse(DATA_NOT_FOUND));
// res.send(createResponse(TEAM_UPDATED, updatedTeam));
res.send(createResponse(TEAM_UPDATED, hint));
} catch (error) {
console.log(error);
res.send(createResponse(INTERNAL_SERVER_ERROR));
}
}