Skip to content

Commit 702b610

Browse files
committed
date to string functionality, and create events before validation
1 parent c0f2085 commit 702b610

File tree

1 file changed

+43
-32
lines changed

1 file changed

+43
-32
lines changed

src/hooks/calendar.ts

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ const TEST_DATE = undefined;
4545
const today = new Date(TEST_DATE ?? new Date().toLocaleDateString());
4646
const nextWeek = new Date(today);
4747
nextWeek.setDate(today.getDate() + 8);
48+
console.log("Today: ", today, "\nNext Week: ", nextWeek);
49+
4850
const url = `https://www.googleapis.com/calendar/v3/calendars/${
4951
config.GOOGLE_CALENDAR_ID
5052
}/events?key=${
@@ -102,6 +104,14 @@ function removeHTMLTags(str: string) {
102104
}
103105
}
104106

107+
// date obj to string
108+
function dateToString(date: Date) {
109+
const day = date.getDate();
110+
const month = date.getMonth() + 1;
111+
const year = date.getFullYear();
112+
return month + "/" + day + "/" + year;
113+
}
114+
105115
// Date Interface
106116
interface DateProps {
107117
start: string;
@@ -125,15 +135,11 @@ function getDateProps(
125135
// if the root cause of this issue is found, you can take steps to fix it
126136
const newDate = new Date(date1);
127137
newDate.setHours(newDate.getHours());
128-
const month = (newDate.getMonth() + 1).toString();
129-
const day = newDate.getDate().toString();
130-
const year = newDate.getFullYear().toString();
138+
const startDateString = dateToString(newDate);
131139

132140
const newDate2 = new Date(date2);
133141
newDate2.setHours(newDate2.getHours());
134-
const month2 = (newDate2.getMonth() + 1).toString();
135-
const day2 = newDate2.getDate().toString();
136-
const year2 = newDate2.getFullYear().toString();
142+
const endDateString = dateToString(newDate2);
137143

138144
console.log("CHECKING DATE PROPS:", event, newDate, newDate2);
139145

@@ -155,20 +161,28 @@ function getDateProps(
155161

156162
dateObject.start = formattedHours1 + ":" + minutes1 + " " + period1;
157163
dateObject.end = formattedHours2 + ":" + minutes2 + " " + period2;
158-
dateObject.date = month + "/" + day + "/" + year;
164+
dateObject.date = startDateString;
159165
} else {
160166
// Set start and end to dates
161-
dateObject.start = month + "/" + day + "/" + year;
162-
dateObject.end = month2 + "/" + day2 + "/" + year2;
167+
dateObject.start = startDateString;
168+
dateObject.end = endDateString;
163169
}
164170

165171
return dateObject;
166172
}
167173

168174
// Function to create discord event
169-
async function createDiscordEvents(events: Messages[], client: Client) {
175+
async function createDiscordEvents(
176+
events: GoogleCalendarDataProps[],
177+
client: Client) {
178+
179+
// filter out operations meetings, as they mess with event creation
180+
const filteredEvents = events
181+
.filter((event) => event.summary !== "Operations Meeting");
182+
170183
// Iterate through the next week events
171-
events.map(async (event) => {
184+
filteredEvents.map(async (event) => {
185+
console.log(event.summary);
172186
// Create a guild event for each event using the discord client
173187
for (const guild of client.guilds.cache.values()) {
174188
try {
@@ -209,9 +223,7 @@ async function createDiscordEvents(events: Messages[], client: Client) {
209223
}
210224

211225
// Grab all events that are today, tomorrow, or in a week
212-
async function getValidEvents() {
213-
const data = await fetchEvents(url);
214-
226+
async function getValidEvents(data: GoogleCalendarDataProps[]) {
215227
// sets all consts to a new date with the local timezone.
216228
const today = new Date(TEST_DATE ?? new Date().toLocaleDateString());
217229
const tomorrow = new Date(today);
@@ -223,13 +235,9 @@ async function getValidEvents() {
223235

224236
const validEvents: Messages[] = [];
225237
// filters out "cancelled" events from the initial data and objects that are recurring, and obj.recurrence is not null
226-
const allEvents = data.items
227-
.filter((event) => event.status !== "cancelled")
228-
.filter((event) => event.recurrence !== null)
229-
.filter((event) => event.summary !== "Kickstart Meeting")
230-
// remove elements with the same summary, keeping the greatest date
231-
// this is to prevent duplicate events from being displayed
232-
.reduce(
238+
// remove elements with the same summary, keeping the greatest date
239+
// this is to prevent duplicate events from being displayed
240+
const allEvents = data.reduce(
233241
(
234242
acc: GoogleCalendarDataProps[],
235243
event: GoogleCalendarDataProps
@@ -296,24 +304,27 @@ async function getValidEvents() {
296304
}
297305

298306
async function hookLogic(client: Client, webhook: WebhookClient) {
299-
console.log("Checking for events...");
300-
const events = await getValidEvents();
307+
const data = await fetchEvents(url);
308+
309+
const allEvents = data.items
310+
.filter((event) => event.status !== "cancelled")
311+
.filter((event) => event.recurrence !== null)
312+
.filter((event) => event.summary !== "Kickstart Meeting");
301313

302-
const todayDate = new Date();
303-
const todayDay = todayDate.getDate();
304-
const todayMonth = todayDate.getMonth() + 1;
305-
const todayYear = todayDate.getFullYear();
306-
const todayDateString = todayMonth + "/" + todayDay + "/" + todayYear;
314+
// Create event on Discord for the upcoming event
315+
createDiscordEvents(allEvents, client);
316+
317+
console.log("Checking for events...");
318+
const events = await getValidEvents(allEvents);
307319

308320
if (events.length === 0) {
309321
return;
310322
} else if (events.length >= 1) {
323+
const messageDay = new Date(today);
324+
messageDay.setDate(today.getDate() + 1);
311325
webhook.send(
312-
`Hey @everyone, it's ${todayDateString}, here are some reminders about our upcoming events!\n`
326+
`Hey @everyone, it's ${dateToString(messageDay)}, here are some reminders about our upcoming events!\n`
313327
);
314-
315-
// Create event on Discord for the upcoming event
316-
createDiscordEvents(events, client);
317328
}
318329

319330
// Sort events by today, then tomorrow, then next week

0 commit comments

Comments
 (0)