Files
calendar/packages/features/ee/workflows/lib/reminders/templates/customTemplate.ts
T
0aca769d06 Allow editing workflow templates (#8028)
* add event end time as variable

* add timezone as new variable

* add first version of template prefill

* set template body when template is updated

* set reminder template body and subject when creating workflow

* set email subject when changes templates

* save emailBody and emailsubject for all templates + fix duplicate template text

* add more flexibility for templates

* remove console.log

* fix {ORAGANIZER} and {ATTENDEE} variable

* make sure to always send reminder body and not default template

* fix import

* remove email body text and match variables in templates

* handle translations of formatted variables

* fix email reminder template

* add cancel and reschedule link as variable

* add cancel and reschedule link for scheduled emails/sms

* make sure empty empty body and subject are set for reminder template

* add info message for testing workflow

* fix typo

* add sms template

* add migration to remove reminderBody and emailSubject

* add branding

* code clean up

* add hide branding everywhere

* fix sms reminder template

* set sms reminder template if sms body is empty

* fix custom inputs variables everywhere

* fix variable translations + other small fixes

* fix some type errors

* fix more type errors

* fix everything missing around cron job scheduling

* make sure to always use custom template for sms messages

* fix type error

* code clean up

* rename link to url

* Add debug logs

* Update handleNewBooking.ts

* Add debug logs

* removed unneded responses

* fix booking questions + UI improvements

* remove html email body when changing to sms action

* code clean up + comments

* code clean up

* code clean up

* remove comment

* more clear info message for timezone variable

---------

Co-authored-by: CarinaWolli <wollencarina@gmail.com>
Co-authored-by: Hariom Balhara <hariombalhara@gmail.com>
Co-authored-by: zomars <zomars@me.com>
Co-authored-by: alannnc <alannnc@gmail.com>
2023-04-18 11:08:09 +01:00

110 lines
4.0 KiB
TypeScript

import { guessEventLocationType } from "@calcom/app-store/locations";
import type { Dayjs } from "@calcom/dayjs";
import { APP_NAME, WEBAPP_URL } from "@calcom/lib/constants";
import type { CalEventResponses } from "@calcom/types/Calendar";
export type VariablesType = {
eventName?: string;
organizerName?: string;
attendeeName?: string;
attendeeEmail?: string;
eventDate?: Dayjs;
eventEndTime?: Dayjs;
timeZone?: string;
location?: string | null;
additionalNotes?: string | null;
responses?: CalEventResponses | null;
meetingUrl?: string;
cancelLink?: string;
rescheduleLink?: string;
};
const customTemplate = (
text: string,
variables: VariablesType,
locale: string,
isBrandingDisabled?: boolean
) => {
const translatedDate = new Intl.DateTimeFormat(locale, {
weekday: "long",
month: "long",
day: "numeric",
year: "numeric",
}).format(variables.eventDate?.toDate());
let locationString = variables.location || "";
if (text.includes("{LOCATION}")) {
locationString = guessEventLocationType(locationString)?.label || locationString;
}
const cancelLink = variables.cancelLink ? `${WEBAPP_URL}${variables.cancelLink}` : "";
const rescheduleLink = variables.rescheduleLink ? `${WEBAPP_URL}${variables.rescheduleLink}` : "";
let dynamicText = text
.replaceAll("{EVENT_NAME}", variables.eventName || "")
.replaceAll("{ORGANIZER}", variables.organizerName || "")
.replaceAll("{ATTENDEE}", variables.attendeeName || "")
.replaceAll("{ORGANIZER_NAME}", variables.organizerName || "") //old variable names
.replaceAll("{ATTENDEE_NAME}", variables.attendeeName || "") //old variable names
.replaceAll("{EVENT_DATE}", translatedDate)
.replaceAll("{EVENT_TIME}", variables.eventDate?.format("H:mmA") || "")
.replaceAll("{EVENT_END_TIME}", variables.eventEndTime?.format("H:mmA") || "")
.replaceAll("{LOCATION}", locationString)
.replaceAll("{ADDITIONAL_NOTES}", variables.additionalNotes || "")
.replaceAll("{ATTENDEE_EMAIL}", variables.attendeeEmail || "")
.replaceAll("{TIMEZONE}", variables.timeZone || "")
.replaceAll("{CANCEL_URL}", cancelLink)
.replaceAll("{RESCHEDULE_URL}", rescheduleLink)
.replaceAll("{MEETING_URL}", variables.meetingUrl || "");
const customInputvariables = dynamicText.match(/\{(.+?)}/g)?.map((variable) => {
return variable.replace("{", "").replace("}", "");
});
// event date/time with formatting
customInputvariables?.forEach((variable) => {
if (variable.startsWith("EVENT_DATE_") || variable.startsWith("EVENT_TIME_")) {
const dateFormat = variable.substring(11, text.length);
const formattedDate = variables.eventDate?.format(dateFormat);
dynamicText = dynamicText.replace(`{${variable}}`, formattedDate || "");
return;
}
if (variable.startsWith("EVENT_END_TIME_")) {
const dateFormat = variable.substring(15, text.length);
const formattedDate = variables.eventEndTime?.format(dateFormat);
dynamicText = dynamicText.replace(`{${variable}}`, formattedDate || "");
return;
}
if (variables.responses) {
Object.keys(variables.responses).forEach((customInput) => {
const formatedToVariable = customInput
.replace(/[^a-zA-Z0-9 ]/g, "")
.trim()
.replaceAll(" ", "_")
.toUpperCase();
if (
variable === formatedToVariable &&
variables.responses &&
variables.responses[customInput as keyof typeof variables.responses].value
) {
dynamicText = dynamicText.replace(
`{${variable}}`,
variables.responses[customInput as keyof typeof variables.responses].value.toString()
);
}
});
}
});
const branding = !isBrandingDisabled ? `<br><br>_<br><br>Scheduling by ${APP_NAME}` : "";
const textHtml = `<body style="white-space: pre-wrap;">${dynamicText}${branding}</body>`;
return { text: dynamicText, html: textHtml };
};
export default customTemplate;