From a0b67bf2e3e963f2af3bc54a590aacb50f4775d1 Mon Sep 17 00:00:00 2001 From: Joe Au-Yeung Date: Thu, 10 Mar 2022 14:30:58 -0500 Subject: [PATCH] Loop through reminders --- apps/web/lib/reminders/reminder-manager.ts | 36 ++++++++++++++++------ apps/web/pages/api/book/event.ts | 8 +++-- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/apps/web/lib/reminders/reminder-manager.ts b/apps/web/lib/reminders/reminder-manager.ts index e65a4ae103..68dae1358d 100644 --- a/apps/web/lib/reminders/reminder-manager.ts +++ b/apps/web/lib/reminders/reminder-manager.ts @@ -1,6 +1,9 @@ import { EventTypeAttendeeReminder } from "@prisma/client/"; +import dayjs from "dayjs"; import twilio from "twilio"; +import prisma from "@lib/prisma"; + const accountSid = process.env.TWILIO_SID; const token = process.env.TWILIO_TOKEN; const senderNumber = process.env.TWILIO_PHONE_NUMBER; @@ -8,23 +11,38 @@ const senderNumber = process.env.TWILIO_PHONE_NUMBER; const client = twilio(accountSid, token); export const scheduleSMSAttendeeReminders = async ( + uid: string, reminderPhone: string, startTime: string, - attendeeReminders: EventTypeAttendeeReminder[] + attendeeReminder: EventTypeAttendeeReminder ) => { // console.log("🚀 ~ file: reminder-manager.ts ~ line 8 ~ startTime", startTime); - // console.log( - // "🚀 ~ file: reminder-manager.ts ~ line 8 ~ scheduleAttendeeReminders ~ eventType", - // attendeeReminders - // ); + console.log( + "🚀 ~ file: reminder-manager.ts ~ line 8 ~ scheduleAttendeeReminders ~ eventType", + attendeeReminder + ); // console.log( // "🚀 ~ file: reminder-manager.ts ~ line 8 ~ scheduleAttendeeReminders ~ bookings", // reminderPhone // ); - await client.messages.create({ - body: "This is a test", - from: senderNumber, - to: reminderPhone, + // await client.messages.create({ + // body: "This is a test", + // from: senderNumber, + // to: reminderPhone, + // }); + + await prisma.attendeeReminder.create({ + data: { + booking: { + connect: { + uid: uid, + }, + }, + method: "SMS", + referenceId: "123", + scheduledFor: dayjs().toISOString(), + scheduled: true, + }, }); }; diff --git a/apps/web/pages/api/book/event.ts b/apps/web/pages/api/book/event.ts index f7ce7158bd..8c1957715d 100644 --- a/apps/web/pages/api/book/event.ts +++ b/apps/web/pages/api/book/event.ts @@ -587,9 +587,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) } await sendScheduledEmails({ ...evt, additionInformation: metadata }); - // Checks to see if there is at least one SMS reminder - if (eventType.attendeeReminders.some((reminders) => reminders.method === "SMS")) { - await scheduleSMSAttendeeReminders(evt.reminderPhone!, evt.startTime, eventType.attendeeReminders); + // Loops through attendee reminders to schedule them + for (const reminder of eventType.attendeeReminders) { + if (reminder.method === "SMS") { + await scheduleSMSAttendeeReminders(evt.uid, evt.reminderPhone!, evt.startTime, reminder); + } } } }