generated from hackforla/.github-hackforla-base-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathcreateRecurringEvents.js
141 lines (118 loc) · 4.64 KB
/
createRecurringEvents.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
const { generateEventData } = require('./lib/generateEventData');
module.exports = (cron, fetch) => {
// Check to see if any recurring events are happening today,
// and if so, check to see if an event has already been created
// for it. If not, create one.
let EVENTS;
let RECURRING_EVENTS;
let TODAY_DATE;
let TODAY;
const URL = process.env.NODE_ENV === 'prod' ? 'https://www.vrms.io' : `http://localhost:${process.env.BACKEND_PORT}`;
const headerToSend = process.env.CUSTOM_REQUEST_HEADER;
const fetchEvents = async () => {
try {
const res = await fetch(`${URL}/api/events/`, {
headers: {
"x-customrequired-header": headerToSend
}
});
EVENTS = await res.json();
// return EVENTS;
} catch(error) {
console.log(error);
};
};
const fetchRecurringEvents = async () => {
try {
const res = await fetch(`${URL}/api/recurringevents/`, {
headers: {
"x-customrequired-header": headerToSend
}
});
RECURRING_EVENTS = await res.json();
// return resJson;
} catch(error) {
console.log(error);
};
};
async function filterAndCreateEvents() {
TODAY_DATE = new Date();
TODAY = TODAY_DATE.getDay();
console.log("Date: ", TODAY_DATE, "Day: ", TODAY);
const recurringEvents = RECURRING_EVENTS;
// console.log("Today Day: ", TODAY);
// Filter recurring events where the event date is today
if (recurringEvents && recurringEvents.length > 0) {
const filteredEvents = recurringEvents.filter(event => {
const eventDay = new Date(event.date).getDay();
// console.log("Event Day: ", eventDay);
return (eventDay === TODAY);
});
// For each recurring event, check to see if an event already
// exists for it and do something if true/false. Can't use
// forEach function with async/await.
for (filteredEvent of filteredEvents) {
const eventExists = await checkIfEventExists(filteredEvent.name);
if (eventExists) {
//Do nothing
console.log("➖ Not going to run ceateEvent");
} else {
// Create new event
const eventToCreate = generateEventData(filteredEvent);
const created = await createEvent(eventToCreate);
console.log("➕", created);
};
};
};
};
async function checkIfEventExists(eventName) {
const events = EVENTS;
// const today = new Date();
if (events && events.length > 0) {
const filteredEvents = events.filter(event => {
const eventDate = new Date(event.date);
const year = eventDate.getFullYear();
const month = eventDate.getMonth();
const date = eventDate.getDate();
const yearToday = TODAY_DATE.getFullYear();
const monthToday = TODAY_DATE.getMonth();
const dateToday = TODAY_DATE.getDate();
return (year === yearToday && month === monthToday && date === dateToday && eventName === event.name);
});
console.log("Events already created: ", filteredEvents);
return filteredEvents.length > 0 ? true : false;
};
};
const createEvent = async (event) => {
if(event) {
const jsonEvent = JSON.stringify(event);
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-customrequired-header": headerToSend
},
body: jsonEvent
}
console.log('Running createEvent: ', jsonEvent);
try {
const response = await fetch(`${URL}/api/events/`, options);
const resJson = await response.json();
return resJson;
} catch (error) {
console.log(error);
};
};
};
async function runTask() {
console.log("Creating today's events");
await fetchEvents();
await fetchRecurringEvents();
await filterAndCreateEvents();
console.log("Today's events are created");
};
const scheduledTask = cron.schedule('*/30 * * * *', () => {
runTask();
});
return scheduledTask;
};