@@ -14,6 +14,12 @@ enum Days {
14
14
DAY3PM = "day3_pm"
15
15
}
16
16
17
+ enum EventStatus {
18
+ NOT_ACCEPTING ,
19
+ NOT_YET_OPEN ,
20
+ ALREADY_CLOSED
21
+ }
22
+
17
23
/**
18
24
* TatakForm Attendance Model
19
25
* @author mavyfaby (Maverick Fabroa)
@@ -28,45 +34,25 @@ class TatakFormAttendance {
28
34
*/
29
35
public static attendStudent ( studentId : string , collegeId : number , event : TatakformModel ) {
30
36
return new Promise ( async ( resolve , reject ) => {
31
- // Default column name
32
- let columnName = Days . DAY1AM ;
33
- // Query
34
- let query = "" ;
35
-
36
37
// Get database instance
37
38
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." ) ;
67
50
}
68
-
51
+
69
52
try {
53
+ // Query
54
+ let query = "" ;
55
+
70
56
// Check if student has already attended
71
57
if ( await TatakFormAttendance . hasAttended ( studentId , event . id ) ) {
72
58
// Check if student has not yet registered time
@@ -219,7 +205,7 @@ class TatakFormAttendance {
219
205
/**
220
206
* Get all attendance of students by event and college
221
207
*/
222
- public static getStudentsAttendedByEventAndCollege ( eventId : number , collegeId : number , day ?: Days ) {
208
+ public static getStudentsAttendedByEventAndCollege ( eventId : number , collegeId : number ) {
223
209
return new Promise ( async ( resolve , reject ) => {
224
210
// Get database instance
225
211
const db = Database . getInstance ( ) ;
@@ -235,7 +221,7 @@ class TatakFormAttendance {
235
221
INNER JOIN
236
222
colleges_courses c ON c.id = s.course_id
237
223
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 = ?
239
225
` , [ eventId , collegeId ]
240
226
) ;
241
227
@@ -257,10 +243,27 @@ class TatakFormAttendance {
257
243
/**
258
244
* Get all attendance count by event and college
259
245
*/
260
- public static getStudentCountAttendedBySlugAndCollege ( eventSlug : string , collegeId : number ) {
246
+ public static getStudentCountAttendedBySlugAndCollege ( eventSlug : string , collegeId : number , willBaseOnDay ?: boolean ) {
261
247
return new Promise ( async ( resolve , reject ) => {
262
248
// Get database instance
263
249
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
+
264
267
265
268
try {
266
269
const result = await db . query < { count : bigint } [ ] > (
@@ -275,7 +278,7 @@ class TatakFormAttendance {
275
278
INNER JOIN
276
279
colleges_courses c ON c.id = s.course_id
277
280
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` : "" }
279
282
` , [ eventSlug , collegeId ]
280
283
) ;
281
284
@@ -320,6 +323,69 @@ class TatakFormAttendance {
320
323
} ) ;
321
324
}
322
325
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
+
323
389
/**
324
390
* Map day name
325
391
*/
0 commit comments