11module . exports = ( cron , fetch ) => {
2+ // Check to see if any events are about to start,
3+ // and if so, open their respective check-ins
4+
5+ const url =
6+ process . env . NODE_ENV === 'prod'
7+ ? 'https://www.vrms.io'
8+ : `http://localhost:${ process . env . BACKEND_PORT } ` ;
9+ const headerToSend = process . env . CUSTOM_REQUEST_HEADER ;
10+
11+ async function fetchEvents ( ) {
12+ try {
13+ const res = await fetch ( `${ url } /api/events` , {
14+ headers : {
15+ 'x-customrequired-header' : headerToSend ,
16+ } ,
17+ } ) ;
18+ const resJson = await res . json ( ) ;
19+
20+ return resJson ;
21+ } catch ( error ) {
22+ console . log ( error ) ;
23+ }
24+ }
25+
26+ async function updateEvents ( eventsToUpdate ) {
27+ try {
28+ const res = await fetch ( `${ url } /api/events/batchUpdate` , {
29+ method : 'PATCH' ,
30+ headers : {
31+ 'Content-Type' : 'application/json' ,
32+ 'x-customrequired-header' : headerToSend ,
33+ } ,
34+ body : JSON . stringify ( eventsToUpdate ) ,
35+ } ) ;
36+ if ( ! res . ok ) throw new Error ( 'Failed to update event' ) ;
37+ return await res . json ( ) ;
38+ } catch ( error ) {
39+ console . error ( 'Error updating event:' , error ) ;
40+ return null ;
41+ }
42+ }
43+ async function sortAndFilterEvents ( ) {
44+ const events = await fetchEvents ( ) ;
45+
46+ // Get current time and set to date variable
47+ const now = Date . now ( ) ;
48+
49+ // Filter events if event date is after now but before thirty minutes from now
50+ if ( events && events . length > 0 ) {
51+ const sortedEvents = events . filter ( ( event ) => {
52+ if ( ! event . date ) {
53+ // handle if event date is null/undefined
54+ // false meaning don't include in sortedEvents
55+ return false ;
56+ }
57+ // Calculate three hours from now
58+ const threeHoursFromStartTime = new Date ( event . date ) . getTime ( ) + 10800000 ;
59+ if ( Number . isNaN ( threeHoursFromStartTime ) ) return false ;
60+ return now >= threeHoursFromStartTime && event . checkInReady === true ;
61+ } ) ;
62+
63+ // console.log('Sorted events: ', sortedEvents);
64+ return sortedEvents ;
65+ }
66+ }
67+
68+ async function closeCheckins ( events ) {
69+ if ( events && events . length > 0 ) {
70+ console . log ( 'Closing check-ins' ) ;
71+ // console.log('Closing event: ', event);
72+ const batchEventsToUpdate = events . map ( ( e ) => ( {
73+ _id : e . _id ,
74+ checkInReady : false ,
75+ } ) ) ;
76+ const updatedEvents = await updateEvents ( batchEventsToUpdate ) ;
77+ if ( updatedEvents ) console . log ( 'Updated events:' , updatedEvents ) ;
78+ console . log ( 'Check-ins closed' ) ;
79+ } else {
80+ console . log ( 'No open events to close' ) ;
81+ }
82+ }
83+
84+ async function runTask ( ) {
85+ const eventsToClose = await sortAndFilterEvents ( ) . catch ( ( err ) => {
86+ console . log ( err ) ;
87+ } ) ;
288
3- // Check to see if any events are about to start,
4- // and if so, open their respective check-ins
5-
6- const url = process . env . NODE_ENV === 'prod' ? 'https://www.vrms.io' : `http://localhost:${ process . env . BACKEND_PORT } ` ;
7- const headerToSend = process . env . CUSTOM_REQUEST_HEADER ;
8-
9- async function fetchEvents ( ) {
10- try {
11- const res = await fetch ( `${ url } /api/events` , {
12- headers : {
13- "x-customrequired-header" : headerToSend
14- }
15- } ) ;
16- const resJson = await res . json ( ) ;
17-
18- return resJson ;
19- } catch ( error ) {
20- console . log ( error ) ;
21- } ;
22- } ;
23-
24- async function sortAndFilterEvents ( ) {
25- const events = await fetchEvents ( ) ;
26-
27- // Filter events if event date is after now but before thirty minutes from now
28- if ( events && events . length > 0 ) {
29-
30- const sortedEvents = events . filter ( event => {
31- if ( ! event . date ) {
32- // handle if event date is null/undefined
33- // false meaning don't include in sortedEvents
34- return false
35- }
36-
37- const currentTimeISO = new Date ( ) . toISOString ( ) ;
38- const threeHoursFromStartTime = new Date ( event . date ) . getTime ( ) + 10800000 ;
39- const threeHoursISO = new Date ( threeHoursFromStartTime ) . toISOString ( ) ;
40-
41- return ( currentTimeISO > threeHoursISO ) && ( event . checkInReady === true ) ;
42- } ) ;
43-
44- // console.log('Sorted events: ', sortedEvents);
45- return sortedEvents ;
46- } ;
47- } ;
48-
49- async function closeCheckins ( events ) {
50- if ( events && events . length > 0 ) {
51- events . forEach ( async event => {
52- // console.log('Closing event: ', event);
53-
54- await fetch ( `${ url } /api/events/${ event . _id } ` , {
55- method : "PATCH" ,
56- headers : {
57- "Content-Type" : "application/json" ,
58- "x-customrequired-header" : headerToSend
59- } ,
60- body : JSON . stringify ( { checkInReady : false } )
61- } )
62- . catch ( err => {
63- console . log ( err ) ;
64- } ) ;
65- } ) ;
66- } ;
67- } ;
68-
69- async function runTask ( ) {
70- console . log ( "Closing check-ins" ) ;
71-
72- const eventsToClose = await sortAndFilterEvents ( )
73- . catch ( err => { console . log ( err ) } ) ;
74-
75- await closeCheckins ( eventsToClose )
76- . catch ( err => { console . log ( err ) } ) ;
77-
78- console . log ( "Check-ins closed" ) ;
79- } ;
80-
81- const scheduledTask = cron . schedule ( '*/30 * * * *' , ( ) => {
82- runTask ( ) ;
89+ await closeCheckins ( eventsToClose ) . catch ( ( err ) => {
90+ console . log ( err ) ;
8391 } ) ;
92+ }
93+
94+ const scheduledTask = cron . schedule ( '*/30 * * * *' , ( ) => {
95+ runTask ( ) ;
96+ } ) ;
8497
85- return scheduledTask ;
86- } ;
98+ return scheduledTask ;
99+ } ;
0 commit comments