-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcheckin.controller.js
More file actions
101 lines (88 loc) · 3.29 KB
/
checkin.controller.js
File metadata and controls
101 lines (88 loc) · 3.29 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
"use strict";
const Services = {
Sheets: require('../services/sheets.service'),
Hacker: require('../services/hacker.service'),
Team: require('../services/team.service'),
Account: require('../services/account.service')
};
/**
* @function submitCheckin
* @param {{body: {formData: Object}, user: {id: string}}} req
* @param {*} res
* @return {JSON} Success status
* @description Handles the check-in form submission and adds data to Google Sheets
* Automatically fetches team member emails from the logged-in user's team
*/
async function submitCheckin(req, res) {
try {
// Get logged-in hacker
const hacker = await Services.Hacker.findByAccountId(req.user.id);
if (!hacker) {
return res.status(404).json({
message: "Hacker not found",
data: {}
});
}
// Check hacker has a team
if (!hacker.teamId) {
return res.status(400).json({
message: "You must be part of a team to submit check-in",
data: {}
});
}
// Fetch team data
const team = await Services.Team.findById(hacker.teamId);
if (!team) {
return res.status(404).json({
message: "Team not found",
data: {}
});
}
// Fetch all team member emails
const teamMemberEmails = [];
for (const memberId of team.members) {
const memberHacker = await Services.Hacker.findById(memberId);
if (memberHacker) {
const memberAccount = await Services.Account.findById(memberHacker.accountId);
if (memberAccount) {
teamMemberEmails.push(memberAccount.email);
}
}
}
// Update team's devpostURL in the database if provided
if (req.body.formData.devpostLink) {
await Services.Team.updateOne(hacker.teamId, {
devpostURL: req.body.formData.devpostLink
});
}
// Prepare data for Google Sheets with team member emails
const teamIdString = team._id ? team._id.toString() : hacker.teamId.toString();
const checkinData = {
teamMember1: teamMemberEmails[0] || '',
teamMember2: teamMemberEmails[1] || '',
teamMember3: teamMemberEmails[2] || '',
teamMember4: teamMemberEmails[3] || '',
prizeCategories: req.body.formData.prizeCategories,
sponsorChallenges: req.body.formData.sponsorChallenges,
mlhChallenges: req.body.formData.mlhChallenges,
// workshopsAttended: req.body.formData.workshopsAttended,
discordTag: req.body.formData.discordTag,
devpostLink: req.body.formData.devpostLink,
teamId: teamIdString
};
await Services.Sheets.appendCheckinData(checkinData);
return res.status(200).json({
message: "Check-in data successfully submitted",
data: {}
});
} catch (error) {
console.error('Checkin submission error:', error);
return res.status(500).json({
message: "Error submitting check-in data",
data: {}
});
}
}
module.exports = {
submitCheckin
};