Send email on reschedules

pull/7270/head
Joe Au-Yeung 2023-02-18 15:48:15 -05:00
parent d05fe6f9b8
commit cbdda75ca8
4 changed files with 43 additions and 28 deletions

View File

@ -76,6 +76,9 @@ export const sendScheduledEmails = async (calEvent: CalendarEvent) => {
export const sendRescheduledEmails = async (calEvent: CalendarEvent) => {
const emailsToSend: Promise<unknown>[] = [];
const clonedEvent = cloneDeep(calEvent);
// @TODO: we should obtain who is rescheduling the event and send them a different email
emailsToSend.push(
...calEvent.attendees.map((attendee) => {
@ -93,7 +96,7 @@ export const sendRescheduledEmails = async (calEvent: CalendarEvent) => {
emailsToSend.push(
new Promise((resolve, reject) => {
try {
const scheduledEmail = new OrganizerRescheduledEmail(calEvent);
const scheduledEmail = new OrganizerRescheduledEmail(clonedEvent);
resolve(scheduledEmail.sendEmail());
} catch (e) {
reject(console.error("OrganizerRescheduledEmail.sendEmail failed", e));
@ -101,6 +104,21 @@ export const sendRescheduledEmails = async (calEvent: CalendarEvent) => {
})
);
if (clonedEvent.team) {
for (const teamMember of clonedEvent.team.members) {
emailsToSend.push(
new Promise((resolve, reject) => {
try {
const scheduledEmail = new OrganizerRescheduledEmail(clonedEvent, undefined, teamMember);
resolve(scheduledEmail.sendEmail());
} catch (e) {
reject(console.error("OrganizerRescheduledEmail.sendEmail failed", e));
}
})
);
}
}
await Promise.all(emailsToSend);
};

View File

@ -5,15 +5,7 @@ import OrganizerScheduledEmail from "./organizer-scheduled-email";
export default class OrganizerRescheduledEmail extends OrganizerScheduledEmail {
protected getNodeMailerPayload(): Record<string, unknown> {
const toAddresses = [this.calEvent.organizer.email];
if (this.calEvent.team) {
this.calEvent.team.members.forEach((member) => {
const memberAttendee = this.calEvent.attendees.find((attendee) => attendee.name === member);
if (memberAttendee) {
toAddresses.push(memberAttendee.email);
}
});
}
const toAddresses = [this.teamMember?.email || this.calEvent.organizer.email];
return {
icalEvent: {

View File

@ -60,14 +60,6 @@ export default class OrganizerScheduledEmail extends BaseEmail {
protected getNodeMailerPayload(): Record<string, unknown> {
const toAddresses = [this.teamMember?.email || this.calEvent.organizer.email];
// if (this.calEvent.team) {
// this.calEvent.team.members.forEach((member) => {
// const memberAttendee = this.calEvent.attendees.find((attendee) => attendee.name === member);
// if (memberAttendee) {
// toAddresses.push(memberAttendee.email);
// }
// });
// }
return {
icalEvent: {

View File

@ -332,6 +332,10 @@ async function handler(req: NextApiRequest & { userId?: number | undefined }) {
if (!eventType) throw new HttpError({ statusCode: 404, message: "eventType.notFound" });
const isTeamEventType =
eventType.schedulingType === SchedulingType.COLLECTIVE ||
eventType.schedulingType === SchedulingType.ROUND_ROBIN;
const paymentAppData = getPaymentAppData(eventType);
// Check if required custom inputs exist
@ -491,12 +495,23 @@ async function handler(req: NextApiRequest & { userId?: number | undefined }) {
language: { translate: tAttendees, locale: language ?? "en" },
},
];
const guests = (reqBody.guests || []).map((guest) => ({
email: guest,
name: "",
timeZone: reqBody.timeZone,
language: { translate: tGuests, locale: "en" },
}));
const guests = (reqBody.guests || []).reduce((guestArray, guest) => {
// If it's a team event, remove the team member from guests
if (isTeamEventType) {
if (users.some((user) => user.email === guest)) {
return guestArray;
} else {
guestArray.push({
email: guest,
name: "",
timeZone: reqBody.timeZone,
language: { translate: tGuests, locale: "en" },
});
}
}
return guestArray;
}, [] as typeof invitee);
const seed = `${organizerUser.username}:${dayjs(reqBody.start).utc().format()}:${new Date().getTime()}`;
const uid = translator.fromUUID(uuidv5(seed, uuidv5.URL));
@ -715,10 +730,7 @@ async function handler(req: NextApiRequest & { userId?: number | undefined }) {
});
}
if (
eventType.schedulingType === SchedulingType.COLLECTIVE ||
eventType.schedulingType === SchedulingType.ROUND_ROBIN
) {
if (isTeamEventType) {
evt.team = {
members: teamMembers,
name: eventType.team?.name || "Nameless",
@ -977,6 +989,7 @@ async function handler(req: NextApiRequest & { userId?: number | undefined }) {
evt.description = eventType.description;
results = updateManager.results;
console.log("🚀 ~ file: handleNewBooking.ts:992 ~ handler ~ results", results);
referencesToCreate = updateManager.referencesToCreate;
if (results.length > 0 && results.some((res) => !res.success)) {
const error = {