Skip to content

Commit 221fe5f

Browse files
committed
feat(tatakforms): get count based on current accepting time
- refactor(tatakforms): improve getting current day
1 parent 2e7d345 commit 221fe5f

File tree

2 files changed

+106
-40
lines changed

2 files changed

+106
-40
lines changed

src/api/tatakforms/attendance.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ async function getAttendance(context: ElysiaContext) {
9393
// Get college id
9494
const collegeId = context.params?.collegeId;
9595
// Get attendance count
96-
const count = await TatakFormAttendance.getStudentCountAttendedBySlugAndCollege(slug, Number(collegeId));
96+
const count = await TatakFormAttendance.getStudentCountAttendedBySlugAndCollege(slug, Number(collegeId), true);
9797
// Return response
9898
return response.success("Fetch Successful", count);
9999
}

src/db/models/tatakform/attendance.ts

Lines changed: 105 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ enum Days {
1414
DAY3PM = "day3_pm"
1515
}
1616

17+
enum EventStatus {
18+
NOT_ACCEPTING,
19+
NOT_YET_OPEN,
20+
ALREADY_CLOSED
21+
}
22+
1723
/**
1824
* TatakForm Attendance Model
1925
* @author mavyfaby (Maverick Fabroa)
@@ -28,45 +34,25 @@ class TatakFormAttendance {
2834
*/
2935
public static attendStudent(studentId: string, collegeId: number, event: TatakformModel) {
3036
return new Promise(async (resolve, reject) => {
31-
// Default column name
32-
let columnName = Days.DAY1AM;
33-
// Query
34-
let query = "";
35-
3637
// Get database instance
3738
const db = Database.getInstance();
38-
// Get current date
39-
const currentDate = new Date();
40-
// Get from date
41-
const fromDate = new Date(event.from_date);
42-
// Get to date
43-
const toDate = new Date(event.to_date);
44-
toDate.setDate(toDate.getDate() + 1)
45-
46-
// Check if event is open
47-
if (currentDate >= fromDate && currentDate <= toDate) {
48-
const dayDifference = Math.floor((currentDate.getTime() - fromDate.getTime()) / (1000 * 60 * 60 * 24));
49-
const currentDay = dayDifference + 1;
50-
const isAM = currentDate.getHours() < 12;
51-
52-
if (currentDay === 1 && !isAM) {
53-
columnName = Days.DAY1PM;
54-
} else if (currentDay === 2 && isAM) {
55-
columnName = Days.DAY2AM;
56-
} else if (currentDay === 2 && !isAM) {
57-
columnName = Days.DAY2PM;
58-
} else if (currentDay === 3 && isAM) {
59-
columnName = Days.DAY3AM;
60-
} else if (currentDay === 3 && !isAM) {
61-
columnName = Days.DAY3PM;
62-
}
63-
} else if (currentDate < fromDate) {
64-
return reject("Event is still closed.");
65-
} else {
66-
return reject("Event already ended.");
39+
// Get column name
40+
const columnName = TatakFormAttendance.getCurrentDay(event);
41+
42+
// Switch column name
43+
switch(columnName) {
44+
case EventStatus.NOT_ACCEPTING:
45+
return reject("Event is not accepting attendance.");
46+
case EventStatus.NOT_YET_OPEN:
47+
return reject("Event is not yet open.");
48+
case EventStatus.ALREADY_CLOSED:
49+
return reject("Event is already closed.");
6750
}
68-
51+
6952
try {
53+
// Query
54+
let query = "";
55+
7056
// Check if student has already attended
7157
if (await TatakFormAttendance.hasAttended(studentId, event.id)) {
7258
// Check if student has not yet registered time
@@ -219,7 +205,7 @@ class TatakFormAttendance {
219205
/**
220206
* Get all attendance of students by event and college
221207
*/
222-
public static getStudentsAttendedByEventAndCollege(eventId: number, collegeId: number, day?: Days) {
208+
public static getStudentsAttendedByEventAndCollege(eventId: number, collegeId: number) {
223209
return new Promise(async (resolve, reject) => {
224210
// Get database instance
225211
const db = Database.getInstance();
@@ -235,7 +221,7 @@ class TatakFormAttendance {
235221
INNER JOIN
236222
colleges_courses c ON c.id = s.course_id
237223
WHERE
238-
a.student_id = s.student_id AND event_id = ? and c.college_id = ? ${day ? `AND ${day} IS NOT NULL` : ""}
224+
a.student_id = s.student_id AND event_id = ? and c.college_id = ?
239225
`, [eventId, collegeId]
240226
);
241227

@@ -257,10 +243,27 @@ class TatakFormAttendance {
257243
/**
258244
* Get all attendance count by event and college
259245
*/
260-
public static getStudentCountAttendedBySlugAndCollege(eventSlug: string, collegeId: number) {
246+
public static getStudentCountAttendedBySlugAndCollege(eventSlug: string, collegeId: number, willBaseOnDay?: boolean) {
261247
return new Promise(async (resolve, reject) => {
262248
// Get database instance
263249
const db = Database.getInstance();
250+
// Current day value
251+
let day: Days | EventStatus = EventStatus.NOT_ACCEPTING;
252+
253+
// If will base on day
254+
if (willBaseOnDay) {
255+
// Get current day
256+
day = TatakFormAttendance.getCurrentDay(await Tatakform.getBySlug(eventSlug));
257+
258+
// Switch current day
259+
switch (day) {
260+
case EventStatus.NOT_ACCEPTING:
261+
case EventStatus.NOT_YET_OPEN:
262+
case EventStatus.ALREADY_CLOSED:
263+
return resolve(-1);
264+
}
265+
}
266+
264267

265268
try {
266269
const result = await db.query<{ count: bigint }[]>(
@@ -275,7 +278,7 @@ class TatakFormAttendance {
275278
INNER JOIN
276279
colleges_courses c ON c.id = s.course_id
277280
WHERE
278-
a.student_id = s.student_id AND t.slug = ? and c.college_id = ?
281+
a.student_id = s.student_id AND t.slug = ? and c.college_id = ? ${day ? `AND ${day} IS NOT NULL` : ""}
279282
`, [eventSlug, collegeId]
280283
);
281284

@@ -320,6 +323,69 @@ class TatakFormAttendance {
320323
});
321324
}
322325

326+
/**
327+
* Get current day and return message
328+
*/
329+
private static getCurrentDay(event: TatakformModel): Days | EventStatus {
330+
// Get current date
331+
const currentDate = new Date();
332+
// Get from date
333+
const fromDate = new Date(event.from_date);
334+
// Get to date
335+
const toDate = new Date(event.to_date);
336+
toDate.setDate(toDate.getDate() + 1);
337+
338+
// If event is not yet open
339+
if (currentDate < fromDate) return EventStatus.NOT_YET_OPEN;
340+
// If event is already closed
341+
if (currentDate >= toDate) return EventStatus.ALREADY_CLOSED;
342+
343+
// AM START
344+
const AM_START_HOUR = 8;
345+
// AM END
346+
const AM_END_HOUR = 12;
347+
// PM START
348+
const PM_START_HOUR = 13;
349+
// PM END
350+
const PM_END_HOUR = 17;
351+
352+
// Get day difference
353+
const dayDifference = Math.floor((currentDate.getTime() - fromDate.getTime()) / (1000 * 60 * 60 * 24));
354+
// Get current day
355+
const currentDay = dayDifference + 1;
356+
// Get current hour
357+
const currentHour = currentDate.getHours();
358+
359+
// If between AM start and AM end
360+
if (currentHour >= AM_START_HOUR && currentHour < AM_END_HOUR ) {
361+
// Switch current day
362+
switch (currentDay) {
363+
case 1:
364+
return Days.DAY1AM;
365+
case 2:
366+
return Days.DAY2AM;
367+
case 3:
368+
return Days.DAY3AM;
369+
}
370+
}
371+
372+
// If between PM start and PM end
373+
if (currentHour >= PM_START_HOUR && currentHour < PM_END_HOUR) {
374+
// Switch current day
375+
switch (currentDay) {
376+
case 1:
377+
return Days.DAY1PM;
378+
case 2:
379+
return Days.DAY2PM;
380+
case 3:
381+
return Days.DAY3PM;
382+
}
383+
}
384+
385+
// Event not yet accepting attendance
386+
return EventStatus.NOT_ACCEPTING;
387+
}
388+
323389
/**
324390
* Map day name
325391
*/

0 commit comments

Comments
 (0)