-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathschedule.js
181 lines (156 loc) · 7.64 KB
/
schedule.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
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
let filterNonTestingRooms = (nonTestingRooms, groups, showDivision=false) => {
let final = [];
groups.push("All");
nonTestingRooms.filter(room => arraysMatchOneTerm(room.fields.Group, groups))
.forEach(room => {
let name = room.fields.Name;
if (showDivision) {
let divisionA = room.fields.Group.includes("A");
let divisionAA = room.fields.Group.includes("AA");
if (divisionA) {
name += " - Division A";
} else if (divisionAA) {
name += " - Division AA";
}
}
final.push({
zoomLink: room.fields["Zoom Link"],
name: name,
openTime: room.fields.Start,
closeTime: room.fields.End,
openTimeText: new Date(test.fields["Competition Start Time"]).toLocaleString("CDT", { timeStyle: 'short', dateStyle: 'short', timeZone: "America/Chicago" }),
closeTimeText: new Date(test.fields["Competition End Time"]).toLocaleString("CDT", { timeStyle: 'short', dateStyle: 'short', timeZone: "America/Chicago" }),
id: room.fields.ID
});
});
return final.sort((a, b) => new Date(a.openTime) - new Date(b.openTime));
}
let arraysMatchOneTerm = (arr1, arr2) => {
return arr1.some(r => arr2.includes(r));
}
module.exports = (app, { eventsTable, studentsTable, schoolsTable, testsTable, alertsTable }, websocket, error) => {
app.get('/student/:studentId', async (req, res) => {
const studentId = req.params.studentId;
try {
let student = await studentsTable.find(studentId);
console.log(student);
let testIds = student.fields.Tests;
let school = await schoolsTable.find(student.fields["School"]);
let studentRooms = await Promise.all(testIds.map(async (testId) => {
let test = await testsTable.find(testId),
competitionName = test.fields["Competition Name"][0];
let zoomLink = school.fields["7th Grade Zoom Link"];
if (competitionName.includes("8th")) {
zoomLink = school.fields["8th Grade Zoom Link"];
} else if (competitionName.includes("Creative")) {
zoomLink = school.fields["Creative Thinking Zoom Link"];
}
let subtext = test.fields["Student Names"].length == 1 ? "" : test.fields["Student Names"].join(", ");
let testLink = test.fields["Link To Join"] + `?student=${student.id}`
if (test.fields.Students[0] !== student.id) {
testLink = null;
}
let teamTest = test.fields.Students.length != 1;
return {
testLink,
zoomLink,
subtext,
name: test.fields["Competition Friendly Name"],
openTime: test.fields["Competition Start Time"],
closeTime: test.fields["Competition End Time"],
openTimeText: new Date(test.fields["Competition Start Time"]).toLocaleString("CDT", { timeStyle: 'short', dateStyle: 'short', timeZone: "America/Chicago" }),
closeTimeText: new Date(test.fields["Competition End Time"]).toLocaleString("CDT", { timeStyle: 'short', dateStyle: 'short', timeZone: "America/Chicago" }),
teamTest,
students: test.fields["Student Names"],
};
}));
// let liveAlerts = websocket.getAlerts(alertsTable)
let [nonTestingRooms, liveAlerts] = await Promise.all([eventsTable.read(), websocket.getAlerts(alertsTable)]);
// eventsTable.read()
// screw non testing rooms
let alertsObject = await websocket.getAlertObject(liveAlerts),
alertsHtml = alertsObject.html;
// let filteredNonTestingRooms = filterNonTestingRooms(nonTestingRooms, [school.fields.Division, "Students"])
// studentRooms = studentRooms.concat(filteredNonTestingRooms);
studentRooms.sort((a, b) => new Date(a.openTime) - new Date(b.openTime));
let now = new Date();
studentRooms = studentRooms.map(room => {
return {
...room,
active: (new Date(room.openTime) < now && new Date(room.closeTime) > now)
};
});
res.status(200).render("pages/links.ejs", {
rooms: studentRooms,
name: student.fields.Name,
primary: student.fields.Name,
secondary: "JHMC 2024", // Goes in the <title>
schoolName: school.fields.Name,
// helpLink: nonTestingRooms.find(room => room.fields.ID == "help").fields["Zoom Link"],
helpLink: "https://zoom.us/j/97978208692?pwd=OXFEREZJekVsQ1diRVNwNllFRlk0dz09",
divisionText: "Division " + school.fields.Division,
// alertsHtml: "hi",
alertsHtml
});
} catch (e) {
console.error(e);
error(res);
}
});
app.get('/coaches', async (req, res) => {
let nonTestingRooms = await eventsTable.read();
// screw non-testing rooms
// await eventsTable.read();
let filteredNonTestingRooms = filterNonTestingRooms(nonTestingRooms, ["Coaches"], showDivision=true);
let liveAlerts = await websocket.getAlerts(alertsTable);
let alertsObject = await websocket.getAlertObject(liveAlerts),
alertsHtml = alertsObject.html;
// Activate rooms
let now = new Date();
filteredNonTestingRooms = filteredNonTestingRooms.map(room => {
return {
...room,
active: (new Date(room.openTime) < now && new Date(room.closeTime) > now)
};
});
res.status(200).render("pages/links.ejs", {
rooms: filteredNonTestingRooms,
name: "Coaches",
primary: "Coaches Schedule",
secondary: "JHMC 2024", // Goes in the <title>
schoolName: "",
// helpLink: nonTestingRooms.find(room => room.fields.ID == "help").fields["Zoom Link"],
helpLink: "https://zoom.us/j/97978208692?pwd=OXFEREZJekVsQ1diRVNwNllFRlk0dz09",
divisionText: "",
alertsHtml
});
});
app.get('/parents', async (req, res) => {
let nonTestingRooms = await eventsTable.read();
// screw non-testing rooms
// await eventsTable.read();
let filteredNonTestingRooms = filterNonTestingRooms(nonTestingRooms, ["Parents"], showDivision=true);
let liveAlerts = await websocket.getAlerts(alertsTable);
let alertsObject = await websocket.getAlertObject(liveAlerts),
alertsHtml = alertsObject.html;
// Activate rooms
let now = new Date();
filteredNonTestingRooms = filteredNonTestingRooms.map(room => {
return {
...room,
active: (new Date(room.openTime) < now && new Date(room.closeTime) > now)
};
});
res.status(200).render("pages/links.ejs", {
rooms: filteredNonTestingRooms,
name: "Parents",
primary: "Parents Schedule",
secondary: "JHMC 2024", // Goes in the <title>
schoolName: "",
// helpLink: nonTestingRooms.find(room => room.fields.ID == "help").fields["Zoom Link"],
helpLink: "https://zoom.us/j/97978208692?pwd=OXFEREZJekVsQ1diRVNwNllFRlk0dz09",
divisionText: "",
alertsHtml
});
});
}