From 2a73fd818874d1838fcd6a85f24c61c931b04d74 Mon Sep 17 00:00:00 2001 From: Carina Wollendorfer <30310907+CarinaWolli@users.noreply.github.com> Date: Wed, 3 Aug 2022 18:22:38 -0400 Subject: [PATCH] Variables for custom templates in workflows (#3602) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * improve functionality to update a step * remove console logs * fix issue with active event types * allow null value for time and timeUnit * sort steps asc step number * add action to workflow (frontend) * add phone number input for SMS to specific number * use PhoneInput for number input + input validation * improve invalid input for phone number * improve UI of phoneInput * Improve design and validation * fix undefined error * set default action when adding action * include all team event types * fix phone number input for editing steps * fix update muation to add steps * remove console logs * fix order of steps * functionality to delete steps * add trigger when event is cancelled * add custom email body * sms and email reminder updates * add custom emails * add custom email subject * send reminder email to all attendees * update migration * fix default value for time and timeUnit * save email reminders to database * clean code * add custom template to SMS actions * schedule emails with sendgrid * clean code * add workflow templates * keep custom template saved when changing templates * create reminder template for email * add dot at the end of sentace for email template * fix merge error * fix issue that template was not saved * include sending emails for when event is cancelled * fix bug that email was always sent * add templates to sms reminders * add info that sending sms to attendees won't trigger for already exisitng bookings * only schedule sms for attendees when smsReminderNumber exists * only schedule sms for attendees when smsReminderNumber exists * set scheduled of workflow reminder to false when longer than 72 hours * add cron for email scheduling + fixes for for sms an email scheduling * adjust step number when deleting a step * cast to boolean with !! * update cron job for email reminders * update sms template * send reminder email not to guests * remove sendTo from workflow reminder * fixes sending sms without name + removing sendTo everywhere * fix undefined name in sms template * set user name to undefined for sending sms to a specific number * fix singular and plural for time unit * set to edit mode when changing action and custom template is selected * delete reminders when booking cancelled or not active anymore * fix type errors * fix error that deleted reminders twice * create booking reminders for existing bookings when eventType is set active * improve email and sms templates * use BookingInfo type instead of calendarEvent for reminder emails * schedule emails for already existing bookings * add and remove reminders for new active event types and cancelled events * connect add action button with last step * fix step container width for mobile view * helper functions that return options for select * fix typo and remove comment * clean code * add/improve error messages for forms * fix typo * clean code * improve email template * clean code * fix missing prop * save reference id when scheduling reminder * fix step not added because of changed id for new steps * small fixes + code cleanup * code cleanup * show error message when number is invalid * fix typo * fix phone number input when location is already phone * set multi select checkbox to read only * change email scheduling in cron job from 7 days to 72 hours * show active event types in workflow list * fix trigger information for workflow list * improve layout for small screens in workflow list * remove optional from zod type for workflow name * order workflows by id * use link icon to show active event types * fix plural and add translation for showing nr of active eventtypes * fix text for sms reminder template * add reminders for added steps * remove optional for activeOn * improve reminder templates * improve design of custom input fields * set edit mode to false when phone number isn't needed anymore * set sendTo in workflow step only for SMS_NUMBER action * set email body and subject only when custom template * only delete reminders that belong to workflow steps * improve text for new event book trigger * move reminders folder to workflows * fix issue that save button was sometimes enabled in edit mode * fix form issues for send to * delete all scheduled reminders when workflow is deleted * use enum for method * fix imports for workflow methods * add missing import * fix edit mode * create reminders when event is confirmed * add reminderScheduler to reduce duplicate code * make workflow enterprise and pro only feature * move all files to /ee/ folder * move package.json change to /ee/ folder * add pro badge to shell * set to edit mode to true if email subject is missing when action changes * fix loading bug * add migration * fix old imports * don't schedule reminders for opt-ins * fix style of email body * code clean up * Update yarn.lock * fix isLoading for active on dropdown * update import for prisma Co-authored-by: Omar López * update imports * remove console * use session to check if user has valid license * use defaultHandler * clean up code * Create db-staging-snapshot.yml * move LisenceRequired inside shell * update import for FormValues * fix phone input design * fix disabled save button for edit mode * squah all migration into a single one * use isAfter and isBefore instead of isBetween * import dayjs from @calcom * validate phone number for sms reminders when booking event * Allows auto approvals for crowdin * add dropdown for adding dynamic variables to string * use lighter text colour for disabled button * change textarea for email subject to one row only * add function to create custom template with dynamic variables * add location to dynamic variables * add missing locations to custom template * add variables at cursor position * remove second curly brakets from variables * code cleanup * fix names for attendee and organizer * enable internationaliztion for custom reminders * disabled add workflow button when mutation is loading * fix translation keys * internationalize dates * code clean up * add translation for email subject * skip translation for english * remove language from BookingInfo type * add additional notes as variable * add custom inputs as variables * add information for custom inputs * add translation * Add formated variables in additional input information * fix type errors * code clean up * Update AddVariablesDropdown.tsx * Update variableTranslations.ts * Update variableTranslations.ts * Formatting * Update [workflow].tsx Co-authored-by: CarinaWolli Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> Co-authored-by: zomars --- apps/web/public/static/locales/de/common.json | 9 +- apps/web/public/static/locales/en/common.json | 16 ++ packages/dayjs/index.ts | 26 +++ .../workflows/api/scheduleEmailReminders.ts | 25 +++ .../ee/workflows/api/scheduleSMSReminders.ts | 20 ++ .../components/AddVariablesDropdown.tsx | 49 +++++ .../components/NewWorkflowButton.tsx | 4 +- .../components/WorkflowStepContainer.tsx | 173 +++++++++++++++--- .../lib/reminders/emailReminderManager.ts | 22 +++ .../lib/reminders/smsReminderManager.ts | 27 ++- .../lib/reminders/templates/customTemplate.ts | 91 +++++++++ .../ee/workflows/lib/variableTranslations.ts | 57 ++++++ .../trpc/server/routers/viewer/workflows.tsx | 12 +- packages/ui/Dropdown.tsx | 7 +- 14 files changed, 505 insertions(+), 33 deletions(-) create mode 100644 packages/features/ee/workflows/components/AddVariablesDropdown.tsx create mode 100644 packages/features/ee/workflows/lib/reminders/templates/customTemplate.ts create mode 100644 packages/features/ee/workflows/lib/variableTranslations.ts diff --git a/apps/web/public/static/locales/de/common.json b/apps/web/public/static/locales/de/common.json index 6dc127f50e..6a74b445f7 100644 --- a/apps/web/public/static/locales/de/common.json +++ b/apps/web/public/static/locales/de/common.json @@ -945,5 +945,12 @@ "new_seat_subject": "Neuer Teilnehmer {{name}} beim {{eventType}} am {{date}}", "new_seat_title": "Jemand hat sich zu einem Ereignis hinzugefügt", "app_upgrade_description": "Um diese Funktion nutzen zu können, müssen Sie ein Upgrade auf einen Pro-Account durchführen.", - "invalid_number": "Ungültige Telefonnummer" + "invalid_number": "Ungültige Telefonnummer", + "event_name_workflow": "Event Name", + "organizer_name_workflow": "Organisator Name", + "attendee_name_workflow": "Teilnehmer Name", + "event_date_workflow": "Event Datum", + "event_time_workflow": "Event Zeit", + "location_workflow": "Ort", + "additional_notes_workflow": "Zusätzliche Notizen" } diff --git a/apps/web/public/static/locales/en/common.json b/apps/web/public/static/locales/en/common.json index d135f9d113..a63b4a5fee 100644 --- a/apps/web/public/static/locales/en/common.json +++ b/apps/web/public/static/locales/en/common.json @@ -1000,6 +1000,14 @@ "no_active_event_types": "No active event types", "new_seat_subject": "New Attendee {{name}} on {{eventType}} at {{date}}", "new_seat_title": "Someone has added themselves to an event", + "variable": "Variable", + "event_name_workflow": "Event name", + "organizer_name_workflow": "Organizer name", + "attendee_name_workflow": "Attendee name", + "event_date_workflow": "Event date", + "event_time_workflow": "Event time", + "location_workflow": "Location", + "additional_notes_workflow": "Additional notes", "app_upgrade_description": "In order to use this feature, you need to upgrade to a Pro account.", "invalid_number": "Invalid phone number", "navigate": "Navigate", @@ -1010,5 +1018,13 @@ "embed": "Embed", "new_username": "New username", "current_username": "Current username", + "example_1": "Example 1", + "example_2": "Example 2", + "additonal_input_label": "Additonal input label", + "company_size": "Company size", + "what_help_needed": "What do you need help with?", + "variable_format": "Variable format", + "custom_input_as_variable_info": "Ignore all special characters of the additonal input label (use only letters and numbers), use uppercase for all letters and replace whitespaces with underscores.", + "using_additional_inputs_as_variables": "How to use additional inputs as variables?", "missing_connected_calendar": "No calendar connected" } diff --git a/packages/dayjs/index.ts b/packages/dayjs/index.ts index 62e049f3a0..27b233c9e2 100644 --- a/packages/dayjs/index.ts +++ b/packages/dayjs/index.ts @@ -20,6 +20,32 @@ dayjs.extend(timeZone); dayjs.extend(toArray); dayjs.extend(utc); +require('dayjs/locale/ar'); +require('dayjs/locale/bg'); +require('dayjs/locale/cs'); +require('dayjs/locale/de'); +require('dayjs/locale/es'); +require('dayjs/locale/es-mx'); +require('dayjs/locale/fr'); +require('dayjs/locale/he'); +require('dayjs/locale/hu'); +require('dayjs/locale/it'); +require('dayjs/locale/ja'); +require('dayjs/locale/ko'); +require('dayjs/locale/nl'); +require('dayjs/locale/pl'); +require('dayjs/locale/pt'); +require('dayjs/locale/pt-br'); +require('dayjs/locale/ro'); +require('dayjs/locale/ru'); +require('dayjs/locale/sr'); +require('dayjs/locale/sv'); +require('dayjs/locale/tr'); +require('dayjs/locale/uk'); +require('dayjs/locale/vi'); +require('dayjs/locale/zh-tw'); +require('dayjs/locale/zh-cn'); + export type Dayjs = dayjs.Dayjs; export type { ConfigType } from "dayjs"; diff --git a/packages/features/ee/workflows/api/scheduleEmailReminders.ts b/packages/features/ee/workflows/api/scheduleEmailReminders.ts index 4d92d23029..e95145f924 100644 --- a/packages/features/ee/workflows/api/scheduleEmailReminders.ts +++ b/packages/features/ee/workflows/api/scheduleEmailReminders.ts @@ -8,6 +8,7 @@ import dayjs from "@calcom/dayjs"; import { defaultHandler } from "@calcom/lib/server"; import prisma from "@calcom/prisma"; +import customTemplate, { VariablesType } from "../lib/reminders/templates/customTemplate"; import emailReminderTemplate from "../lib/reminders/templates/emailReminderTemplate"; const sendgridAPIKey = process.env.SENDGRID_API_KEY as string; @@ -103,6 +104,30 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { name || "" ); break; + case WorkflowTemplates.CUSTOM: + const variables: VariablesType = { + eventName: reminder.booking?.eventType?.title || "", + organizerName: reminder.booking?.user?.name || "", + attendeeName: reminder.booking?.attendees[0].name, + eventDate: dayjs(reminder.booking?.startTime).tz(timeZone), + eventTime: dayjs(reminder.booking?.startTime).tz(timeZone), + timeZone: timeZone, + location: reminder.booking?.location || "", + additionalNotes: reminder.booking?.description, + customInputs: reminder.booking?.customInputs, + }; + const emailSubject = await customTemplate( + reminder.workflowStep.emailSubject || "", + variables, + reminder.booking?.user?.locale || "" + ); + emailContent.emailSubject = emailSubject.text; + emailContent.emailBody = await customTemplate( + reminder.workflowStep.reminderBody || "", + variables, + reminder.booking?.user?.locale || "" + ); + break; } if (emailContent.emailSubject.length > 0 && emailContent.emailBody.text.length > 0 && sendTo) { const batchIdResponse = await client.request({ diff --git a/packages/features/ee/workflows/api/scheduleSMSReminders.ts b/packages/features/ee/workflows/api/scheduleSMSReminders.ts index 9afc0245e7..38098fca4a 100644 --- a/packages/features/ee/workflows/api/scheduleSMSReminders.ts +++ b/packages/features/ee/workflows/api/scheduleSMSReminders.ts @@ -7,6 +7,7 @@ import { defaultHandler } from "@calcom/lib/server"; import prisma from "@calcom/prisma"; import * as twilio from "../lib/reminders/smsProviders/twilioProvider"; +import customTemplate, { VariablesType } from "../lib/reminders/templates/customTemplate"; import smsReminderTemplate from "../lib/reminders/templates/smsReminderTemplate"; async function handler(req: NextApiRequest, res: NextApiResponse) { @@ -82,6 +83,25 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { userName ); break; + case WorkflowTemplates.CUSTOM: + const variables: VariablesType = { + eventName: reminder.booking?.eventType?.title, + organizerName: reminder.booking?.user?.name || "", + attendeeName: reminder.booking?.attendees[0].name, + eventDate: dayjs(reminder.booking?.startTime).tz(timeZone), + eventTime: dayjs(reminder.booking?.startTime).tz(timeZone), + timeZone: timeZone, + location: reminder.booking?.location || "", + additionalNotes: reminder.booking?.description, + customInputs: reminder.booking?.customInputs, + }; + const customMessage = await customTemplate( + reminder.workflowStep.reminderBody || "", + variables, + reminder.booking?.user?.locale || "" + ); + message = customMessage.text; + break; } if (message?.length && message?.length > 0 && sendTo) { const scheduledSMS = await twilio.scheduleSMS(sendTo, message, reminder.scheduledDate); diff --git a/packages/features/ee/workflows/components/AddVariablesDropdown.tsx b/packages/features/ee/workflows/components/AddVariablesDropdown.tsx new file mode 100644 index 0000000000..cf41cf714c --- /dev/null +++ b/packages/features/ee/workflows/components/AddVariablesDropdown.tsx @@ -0,0 +1,49 @@ +import classNames from "@calcom/lib/classNames"; +import { useLocale } from "@calcom/lib/hooks/useLocale"; +import { Dropdown, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@calcom/ui/Dropdown"; + +interface IAddVariablesDropdown { + addVariable: (isEmailSubject: boolean, variable: string) => void; + disabled: boolean; + isEmailSubject: boolean; +} + +const variables = [ + "event_name_workflow", + "organizer_name_workflow", + "attendee_name_workflow", + "event_date_workflow", + "event_time_workflow", + "location_workflow", + "additional_notes_workflow", +]; + +export const AddVariablesDropdown = (props: IAddVariablesDropdown) => { + const { t } = useLocale(); + + return ( + + + + {t("variable")} + + + {variables.map((variable) => ( + + + + ))} + + + ); +}; diff --git a/packages/features/ee/workflows/components/NewWorkflowButton.tsx b/packages/features/ee/workflows/components/NewWorkflowButton.tsx index 3d0bcfea3a..a6280c8818 100644 --- a/packages/features/ee/workflows/components/NewWorkflowButton.tsx +++ b/packages/features/ee/workflows/components/NewWorkflowButton.tsx @@ -232,7 +232,9 @@ export function NewWorkflowButton() { )}
- +