From 9e124ecf0dc435ae81b940fca3934bc8849dd820 Mon Sep 17 00:00:00 2001 From: Trillium Smith Date: Mon, 30 Oct 2023 16:37:52 -0700 Subject: [PATCH 1/2] feat: Move logic to helper function & resolve bug - Base event time off of event.startTime rather than event.date --- backend/workers/createRecurringEvents.js | 74 +++++++++++++----------- 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/backend/workers/createRecurringEvents.js b/backend/workers/createRecurringEvents.js index a1228c36..4867ac90 100644 --- a/backend/workers/createRecurringEvents.js +++ b/backend/workers/createRecurringEvents.js @@ -60,45 +60,14 @@ module.exports = (cron, fetch) => { // forEach function with async/await. for (filteredEvent of filteredEvents) { const eventExists = await checkIfEventExists(filteredEvent.name); - // console.log('Event exists? ', eventExists); - const eventDate = new Date(filteredEvent.date); if (eventExists) { + //Do nothing console.log("Not going to run ceateEvent"); } else { // Create new event - const hours = eventDate.getHours(); - const minutes = eventDate.getMinutes(); - const seconds = eventDate.getSeconds(); - const milliseconds = eventDate.getMilliseconds(); - - const yearToday = TODAY_DATE.getFullYear(); - const monthToday = TODAY_DATE.getMonth(); - const dateToday = TODAY_DATE.getDate(); - - const newEventDate = new Date(yearToday, monthToday, dateToday, hours, minutes, seconds, milliseconds); - - const newEndTime = new Date(yearToday, monthToday, dateToday, hours + filteredEvent.hours, minutes, seconds, milliseconds) - - const eventToCreate = { - name: filteredEvent.name && filteredEvent.name, - hacknight: filteredEvent.hacknight && filteredEvent.hacknight, - eventType: filteredEvent.eventType && filteredEvent.eventType, - description: filteredEvent.eventDescription && filteredEvent.eventDescription, - project: filteredEvent.project && filteredEvent.project, - date: filteredEvent.date && newEventDate, - startTime: filteredEvent.startTime && newEventDate, - endTime: filteredEvent.endTime && newEndTime, - hours: filteredEvent.hours && filteredEvent.hours - } - if (filteredEvent.hasOwnProperty("location")) { - eventToCreate.location = { - city: filteredEvent.location.city ? filteredEvent.location.city : 'REMOTE', - state: filteredEvent.location.state ? filteredEvent.location.state : 'REMOTE', - country: filteredEvent.location.country ? filteredEvent.location.country : 'REMOTE' - }; - } - + const eventToCreate = generateEventData(filteredEvent); + const created = await createEvent(eventToCreate); console.log(created); }; @@ -106,6 +75,43 @@ module.exports = (cron, fetch) => { }; }; + function generateEventData(eventData, TODAY_DATE = new Date()) { + const eventDate = new Date(eventData.startTime); + // Create new event + const hours = eventDate.getHours(); + const minutes = eventDate.getMinutes(); + const seconds = eventDate.getSeconds(); + const milliseconds = eventDate.getMilliseconds(); + + const yearToday = TODAY_DATE.getFullYear(); + const monthToday = TODAY_DATE.getMonth(); + const dateToday = TODAY_DATE.getDate(); + + const newEventDate = new Date(yearToday, monthToday, dateToday, hours, minutes, seconds, milliseconds); + + const newEndTime = new Date(yearToday, monthToday, dateToday, hours + filteredEvent.hours, minutes, seconds, milliseconds) + + const eventToCreate = { + name: eventData.name && eventData.name, + hacknight: eventData.hacknight && eventData.hacknight, + eventType: eventData.eventType && eventData.eventType, + description: eventData.eventDescription && eventData.eventDescription, + project: eventData.project && eventData.project, + date: eventData.date && newEventDate, + startTime: eventData.startTime && newEventDate, + endTime: eventData.endTime && newEndTime, + hours: eventData.hours && eventData.hours + } + if (eventData.hasOwnProperty("location")) { + eventToCreate.location = { + city: eventData.location.city ? eventData.location.city : 'REMOTE', + state: eventData.location.state ? eventData.location.state : 'REMOTE', + country: eventData.location.country ? eventData.location.country : 'REMOTE' + }; + } + return eventToCreate + }; + async function checkIfEventExists(eventName) { const events = EVENTS; // const today = new Date(); From 6d373a32a6e67bedcae2427e32cc7287bf66d7a4 Mon Sep 17 00:00:00 2001 From: Trillium Smith Date: Sun, 5 Nov 2023 13:57:38 -0800 Subject: [PATCH 2/2] feat: Add emojis to create/not create console.logs --- backend/workers/createRecurringEvents.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/workers/createRecurringEvents.js b/backend/workers/createRecurringEvents.js index 4867ac90..3eef5fbf 100644 --- a/backend/workers/createRecurringEvents.js +++ b/backend/workers/createRecurringEvents.js @@ -63,13 +63,13 @@ module.exports = (cron, fetch) => { if (eventExists) { //Do nothing - console.log("Not going to run ceateEvent"); + console.log("➖ Not going to run ceateEvent"); } else { // Create new event const eventToCreate = generateEventData(filteredEvent); const created = await createEvent(eventToCreate); - console.log(created); + console.log("➕", created); }; }; };