fix: customTemplate() function fails when no locale is given (#10201)

* fallback to en for locale

* remove console.log

* Fix nit changes

---------

Co-authored-by: CarinaWolli <[email protected]>
Co-authored-by: alannnc <[email protected]>
This commit is contained in:
authored and GitHub committed 2023-07-17 16:38:37 -07:00
1 parent 6a8db56dbb
commit c26efcabe4
4 files changed
+28 -18

No files matched your search

@@ -191,22 +191,24 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
cancelLink: `/booking/${reminder.booking.uid}?cancel=true`, cancelLink: `/booking/${reminder.booking.uid}?cancel=true`,
rescheduleLink: `/${reminder.booking.user?.username}/${reminder.booking.eventType?.slug}?rescheduleUid=${reminder.booking.uid}`, rescheduleLink: `/${reminder.booking.user?.username}/${reminder.booking.eventType?.slug}?rescheduleUid=${reminder.booking.uid}`,
}; };
const emailLocale = locale || "en";
const emailSubject = customTemplate( const emailSubject = customTemplate(
reminder.workflowStep.emailSubject || "", reminder.workflowStep.emailSubject || "",
variables, variables,
locale || "", emailLocale,
!!reminder.booking.user?.hideBranding !!reminder.booking.user?.hideBranding
).text; ).text;
emailContent.emailSubject = emailSubject; emailContent.emailSubject = emailSubject;
emailContent.emailBody = customTemplate( emailContent.emailBody = customTemplate(
reminder.workflowStep.reminderBody || "", reminder.workflowStep.reminderBody || "",
variables, variables,
locale || "", emailLocale,
!!reminder.booking.user?.hideBranding !!reminder.booking.user?.hideBranding
).html; ).html;
emailBodyEmpty = emailBodyEmpty =
customTemplate(reminder.workflowStep.reminderBody || "", variables, locale || "").text.length === 0; customTemplate(reminder.workflowStep.reminderBody || "", variables, emailLocale).text.length ===
0;
} else if (reminder.workflowStep.template === WorkflowTemplates.REMINDER) { } else if (reminder.workflowStep.template === WorkflowTemplates.REMINDER) {
emailContent = emailReminderTemplate( emailContent = emailReminderTemplate(
false, false,
@@ -116,7 +116,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
const customMessage = customTemplate( const customMessage = customTemplate(
reminder.workflowStep.reminderBody || "", reminder.workflowStep.reminderBody || "",
variables, variables,
locale || "" locale || "en"
); );
message = customMessage.text; message = customMessage.text;
} else if (reminder.workflowStep.template === WorkflowTemplates.REMINDER) { } else if (reminder.workflowStep.template === WorkflowTemplates.REMINDER) {
@@ -155,6 +155,7 @@ export const activateEventTypeHandler = async ({ ctx, input }: ActivateEventType
}); });
for (const booking of bookingsForReminders) { for (const booking of bookingsForReminders) {
const defaultLocale = "en";
const bookingInfo = { const bookingInfo = {
uid: booking.uid, uid: booking.uid,
attendees: booking.attendees.map((attendee) => { attendees: booking.attendees.map((attendee) => {
@@ -162,7 +163,7 @@ export const activateEventTypeHandler = async ({ ctx, input }: ActivateEventType
name: attendee.name, name: attendee.name,
email: attendee.email, email: attendee.email,
timeZone: attendee.timeZone, timeZone: attendee.timeZone,
language: { locale: attendee.locale || "" }, language: { locale: attendee.locale || defaultLocale },
}; };
}), }),
organizer: booking.user organizer: booking.user
@@ -170,13 +171,13 @@ export const activateEventTypeHandler = async ({ ctx, input }: ActivateEventType
name: booking.user.name || "", name: booking.user.name || "",
email: booking.user.email, email: booking.user.email,
timeZone: booking.user.timeZone, timeZone: booking.user.timeZone,
language: { locale: booking.user.locale || "" }, language: { locale: booking.user.locale || defaultLocale },
} }
: { name: "", email: "", timeZone: "", language: { locale: "" } }, : { name: "", email: "", timeZone: "", language: { locale: "" } },
startTime: booking.startTime.toISOString(), startTime: booking.startTime.toISOString(),
endTime: booking.endTime.toISOString(), endTime: booking.endTime.toISOString(),
title: booking.title, title: booking.title,
language: { locale: booking?.user?.locale || "" }, language: { locale: booking?.user?.locale || defaultLocale },
eventType: { eventType: {
slug: booking.eventType?.slug, slug: booking.eventType?.slug,
}, },
@@ -257,9 +257,13 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
}); });
steps.forEach(async (step) => { steps.forEach(async (step) => {
if (step.action !== WorkflowActions.SMS_ATTENDEE && step.action !== WorkflowActions.WHATSAPP_ATTENDEE) { if (
step.action !== WorkflowActions.SMS_ATTENDEE &&
step.action !== WorkflowActions.WHATSAPP_ATTENDEE
) {
//as we do not have attendees phone number (user is notified about that when setting this action) //as we do not have attendees phone number (user is notified about that when setting this action)
bookingsForReminders.forEach(async (booking) => { bookingsForReminders.forEach(async (booking) => {
const defaultLocale = "en";
const bookingInfo = { const bookingInfo = {
uid: booking.uid, uid: booking.uid,
attendees: booking.attendees.map((attendee) => { attendees: booking.attendees.map((attendee) => {
@@ -267,12 +271,12 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
name: attendee.name, name: attendee.name,
email: attendee.email, email: attendee.email,
timeZone: attendee.timeZone, timeZone: attendee.timeZone,
language: { locale: attendee.locale || "" }, language: { locale: attendee.locale || defaultLocale },
}; };
}), }),
organizer: booking.user organizer: booking.user
? { ? {
language: { locale: booking.user.locale || "" }, language: { locale: booking.user.locale || defaultLocale },
name: booking.user.name || "", name: booking.user.name || "",
email: booking.user.email, email: booking.user.email,
timeZone: booking.user.timeZone, timeZone: booking.user.timeZone,
@@ -281,7 +285,7 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
startTime: booking.startTime.toISOString(), startTime: booking.startTime.toISOString(),
endTime: booking.endTime.toISOString(), endTime: booking.endTime.toISOString(),
title: booking.title, title: booking.title,
language: { locale: booking?.user?.locale || "" }, language: { locale: booking?.user?.locale || defaultLocale },
eventType: { eventType: {
slug: booking.eventType?.slug, slug: booking.eventType?.slug,
}, },
@@ -482,6 +486,7 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
}, },
}); });
bookingsOfEventTypes.forEach(async (booking) => { bookingsOfEventTypes.forEach(async (booking) => {
const defaultLocale = "en";
const bookingInfo = { const bookingInfo = {
uid: booking.uid, uid: booking.uid,
attendees: booking.attendees.map((attendee) => { attendees: booking.attendees.map((attendee) => {
@@ -489,12 +494,12 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
name: attendee.name, name: attendee.name,
email: attendee.email, email: attendee.email,
timeZone: attendee.timeZone, timeZone: attendee.timeZone,
language: { locale: attendee.locale || "" }, language: { locale: attendee.locale || defaultLocale },
}; };
}), }),
organizer: booking.user organizer: booking.user
? { ? {
language: { locale: booking.user.locale || "" }, language: { locale: booking.user.locale || defaultLocale },
name: booking.user.name || "", name: booking.user.name || "",
email: booking.user.email, email: booking.user.email,
timeZone: booking.user.timeZone, timeZone: booking.user.timeZone,
@@ -503,7 +508,7 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
startTime: booking.startTime.toISOString(), startTime: booking.startTime.toISOString(),
endTime: booking.endTime.toISOString(), endTime: booking.endTime.toISOString(),
title: booking.title, title: booking.title,
language: { locale: booking?.user?.locale || "" }, language: { locale: booking?.user?.locale || defaultLocale },
eventType: { eventType: {
slug: booking.eventType?.slug, slug: booking.eventType?.slug,
}, },
@@ -610,7 +615,8 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
if ( if (
(trigger === WorkflowTriggerEvents.BEFORE_EVENT || trigger === WorkflowTriggerEvents.AFTER_EVENT) && (trigger === WorkflowTriggerEvents.BEFORE_EVENT || trigger === WorkflowTriggerEvents.AFTER_EVENT) &&
eventTypesToCreateReminders && eventTypesToCreateReminders &&
step.action !== WorkflowActions.SMS_ATTENDEE && step.action !== WorkflowActions.WHATSAPP_ATTENDEE step.action !== WorkflowActions.SMS_ATTENDEE &&
step.action !== WorkflowActions.WHATSAPP_ATTENDEE
) { ) {
const bookingsForReminders = await ctx.prisma.booking.findMany({ const bookingsForReminders = await ctx.prisma.booking.findMany({
where: { where: {
@@ -627,6 +633,7 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
}, },
}); });
for (const booking of bookingsForReminders) { for (const booking of bookingsForReminders) {
const defaultLocale = "en";
const bookingInfo = { const bookingInfo = {
uid: booking.uid, uid: booking.uid,
attendees: booking.attendees.map((attendee) => { attendees: booking.attendees.map((attendee) => {
@@ -634,7 +641,7 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
name: attendee.name, name: attendee.name,
email: attendee.email, email: attendee.email,
timeZone: attendee.timeZone, timeZone: attendee.timeZone,
language: { locale: attendee.locale || "" }, language: { locale: attendee.locale || defaultLocale },
}; };
}), }),
organizer: booking.user organizer: booking.user
@@ -642,13 +649,13 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
name: booking.user.name || "", name: booking.user.name || "",
email: booking.user.email, email: booking.user.email,
timeZone: booking.user.timeZone, timeZone: booking.user.timeZone,
language: { locale: booking.user.locale || "" }, language: { locale: booking.user.locale || defaultLocale },
} }
: { name: "", email: "", timeZone: "", language: { locale: "" } }, : { name: "", email: "", timeZone: "", language: { locale: "" } },
startTime: booking.startTime.toISOString(), startTime: booking.startTime.toISOString(),
endTime: booking.endTime.toISOString(), endTime: booking.endTime.toISOString(),
title: booking.title, title: booking.title,
language: { locale: booking?.user?.locale || "" }, language: { locale: booking?.user?.locale || defaultLocale },
eventType: { eventType: {
slug: booking.eventType?.slug, slug: booking.eventType?.slug,
}, },