Loop through reminders

docs/contribute-to-app-store
Joe Au-Yeung 2022-03-10 14:30:58 -05:00
parent c3e019394d
commit a0b67bf2e3
2 changed files with 32 additions and 12 deletions

View File

@ -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,
},
});
};

View File

@ -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);
}
}
}
}