2022-07-14 00:10:45 +00:00
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
2022-07-28 19:58:26 +00:00
|
|
|
import {
|
|
|
|
TimeUnit,
|
|
|
|
WorkflowActions,
|
|
|
|
WorkflowStep,
|
|
|
|
WorkflowTemplates,
|
|
|
|
WorkflowTriggerEvents,
|
|
|
|
} from "@prisma/client";
|
2022-07-14 00:10:45 +00:00
|
|
|
import { isValidPhoneNumber } from "libphonenumber-js";
|
|
|
|
import { useSession } from "next-auth/react";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
import { z } from "zod";
|
|
|
|
|
2023-01-10 15:39:29 +00:00
|
|
|
import Shell from "@calcom/features/shell/Shell";
|
2022-11-23 02:55:25 +00:00
|
|
|
import { classNames } from "@calcom/lib";
|
2022-07-14 00:10:45 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-11-23 02:55:25 +00:00
|
|
|
import { HttpError } from "@calcom/lib/http-error";
|
2022-07-28 19:58:26 +00:00
|
|
|
import { stringOrNumber } from "@calcom/prisma/zod-utils";
|
2022-07-22 17:27:06 +00:00
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2022-11-23 02:55:25 +00:00
|
|
|
import type { MultiSelectCheckboxesOptionType as Option } from "@calcom/ui";
|
2023-01-10 15:39:29 +00:00
|
|
|
import { Alert, Button, Form, showToast } from "@calcom/ui";
|
2022-11-23 02:55:25 +00:00
|
|
|
|
|
|
|
import LicenseRequired from "../../common/components/v2/LicenseRequired";
|
|
|
|
import SkeletonLoader from "../components/SkeletonLoaderEdit";
|
2022-07-28 19:58:26 +00:00
|
|
|
import WorkflowDetailsPage from "../components/WorkflowDetailsPage";
|
2022-11-23 02:55:25 +00:00
|
|
|
import { getTranslatedText, translateVariablesToEnglish } from "../lib/variableTranslations";
|
2022-07-14 00:10:45 +00:00
|
|
|
|
|
|
|
export type FormValues = {
|
|
|
|
name: string;
|
|
|
|
activeOn: Option[];
|
|
|
|
steps: WorkflowStep[];
|
|
|
|
trigger: WorkflowTriggerEvents;
|
|
|
|
time?: number;
|
|
|
|
timeUnit?: TimeUnit;
|
|
|
|
};
|
|
|
|
|
2022-11-23 02:55:25 +00:00
|
|
|
export function onlyLettersNumbersSpaces(str: string) {
|
|
|
|
if (str.length <= 11 && /^[A-Za-z0-9\s]*$/.test(str)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-20 19:48:40 +00:00
|
|
|
const formSchema = z.object({
|
|
|
|
name: z.string(),
|
|
|
|
activeOn: z.object({ value: z.string(), label: z.string() }).array(),
|
2022-07-28 19:58:26 +00:00
|
|
|
trigger: z.nativeEnum(WorkflowTriggerEvents),
|
2022-07-20 19:48:40 +00:00
|
|
|
time: z.number().gte(0).optional(),
|
2022-07-28 19:58:26 +00:00
|
|
|
timeUnit: z.nativeEnum(TimeUnit).optional(),
|
2022-07-20 19:48:40 +00:00
|
|
|
steps: z
|
|
|
|
.object({
|
|
|
|
id: z.number(),
|
|
|
|
stepNumber: z.number(),
|
2022-07-28 19:58:26 +00:00
|
|
|
action: z.nativeEnum(WorkflowActions),
|
2022-07-20 19:48:40 +00:00
|
|
|
workflowId: z.number(),
|
2022-07-28 19:58:26 +00:00
|
|
|
reminderBody: z.string().nullable(),
|
|
|
|
emailSubject: z.string().nullable(),
|
|
|
|
template: z.nativeEnum(WorkflowTemplates),
|
2022-11-23 02:55:25 +00:00
|
|
|
numberRequired: z.boolean().nullable(),
|
2022-07-20 19:48:40 +00:00
|
|
|
sendTo: z
|
|
|
|
.string()
|
2022-11-23 02:55:25 +00:00
|
|
|
.refine((val) => isValidPhoneNumber(val) || val.includes("@"))
|
2022-11-25 14:34:55 +00:00
|
|
|
.optional()
|
2022-11-23 02:55:25 +00:00
|
|
|
.nullable(),
|
|
|
|
sender: z
|
|
|
|
.string()
|
|
|
|
.refine((val) => onlyLettersNumbersSpaces(val))
|
|
|
|
.optional()
|
2022-07-20 19:48:40 +00:00
|
|
|
.nullable(),
|
|
|
|
})
|
|
|
|
.array(),
|
|
|
|
});
|
|
|
|
|
2022-07-28 19:58:26 +00:00
|
|
|
const querySchema = z.object({
|
|
|
|
workflow: stringOrNumber,
|
|
|
|
});
|
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
function WorkflowPage() {
|
2022-11-23 02:55:25 +00:00
|
|
|
const { t, i18n } = useLocale();
|
2022-07-14 00:10:45 +00:00
|
|
|
const session = useSession();
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
const [selectedEventTypes, setSelectedEventTypes] = useState<Option[]>([]);
|
|
|
|
const [isAllDataLoaded, setIsAllDataLoaded] = useState(false);
|
|
|
|
|
|
|
|
const form = useForm<FormValues>({
|
2022-11-23 02:55:25 +00:00
|
|
|
mode: "onBlur",
|
2022-07-14 00:10:45 +00:00
|
|
|
resolver: zodResolver(formSchema),
|
|
|
|
});
|
2022-11-23 02:55:25 +00:00
|
|
|
|
2022-07-28 19:58:26 +00:00
|
|
|
const { workflow: workflowId } = router.isReady ? querySchema.parse(router.query) : { workflow: -1 };
|
2022-11-23 02:55:25 +00:00
|
|
|
const utils = trpc.useContext();
|
2022-07-14 00:10:45 +00:00
|
|
|
|
|
|
|
const {
|
|
|
|
data: workflow,
|
2022-07-21 00:33:35 +00:00
|
|
|
isError,
|
|
|
|
error,
|
2022-11-25 14:34:55 +00:00
|
|
|
isLoading,
|
2022-11-10 23:40:01 +00:00
|
|
|
} = trpc.viewer.workflows.get.useQuery(
|
|
|
|
{ id: +workflowId },
|
|
|
|
{
|
|
|
|
enabled: router.isReady && !!workflowId,
|
|
|
|
}
|
|
|
|
);
|
2022-07-14 00:10:45 +00:00
|
|
|
|
2022-12-15 21:54:40 +00:00
|
|
|
const { data: verifiedNumbers } = trpc.viewer.workflows.getVerifiedNumbers.useQuery();
|
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
useEffect(() => {
|
2022-11-25 14:34:55 +00:00
|
|
|
if (workflow && !isLoading) {
|
2022-07-14 00:10:45 +00:00
|
|
|
setSelectedEventTypes(
|
2022-07-28 19:58:26 +00:00
|
|
|
workflow.activeOn.map((active) => ({
|
|
|
|
value: String(active.eventType.id),
|
|
|
|
label: active.eventType.title,
|
|
|
|
})) || []
|
2022-07-14 00:10:45 +00:00
|
|
|
);
|
|
|
|
const activeOn = workflow.activeOn
|
2022-07-28 19:58:26 +00:00
|
|
|
? workflow.activeOn.map((active) => ({
|
|
|
|
value: active.eventType.id.toString(),
|
|
|
|
label: active.eventType.slug,
|
|
|
|
}))
|
2022-07-14 00:10:45 +00:00
|
|
|
: undefined;
|
2022-11-23 02:55:25 +00:00
|
|
|
|
|
|
|
//translate dynamic variables into local language
|
|
|
|
const steps = workflow.steps.map((step) => {
|
|
|
|
const updatedStep = step;
|
|
|
|
if (step.reminderBody) {
|
|
|
|
updatedStep.reminderBody = getTranslatedText(step.reminderBody || "", {
|
|
|
|
locale: i18n.language,
|
|
|
|
t,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (step.emailSubject) {
|
|
|
|
updatedStep.emailSubject = getTranslatedText(step.emailSubject || "", {
|
|
|
|
locale: i18n.language,
|
|
|
|
t,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return updatedStep;
|
|
|
|
});
|
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
form.setValue("name", workflow.name);
|
2022-11-23 02:55:25 +00:00
|
|
|
form.setValue("steps", steps);
|
2022-07-14 00:10:45 +00:00
|
|
|
form.setValue("trigger", workflow.trigger);
|
|
|
|
form.setValue("time", workflow.time || undefined);
|
|
|
|
form.setValue("timeUnit", workflow.timeUnit || undefined);
|
|
|
|
form.setValue("activeOn", activeOn || []);
|
|
|
|
setIsAllDataLoaded(true);
|
|
|
|
}
|
2022-11-25 14:34:55 +00:00
|
|
|
}, [isLoading]);
|
2022-11-23 02:55:25 +00:00
|
|
|
|
|
|
|
const updateMutation = trpc.viewer.workflows.update.useMutation({
|
|
|
|
onSuccess: async ({ workflow }) => {
|
|
|
|
if (workflow) {
|
|
|
|
utils.viewer.workflows.get.setData({ id: +workflow.id }, workflow);
|
|
|
|
|
|
|
|
showToast(
|
|
|
|
t("workflow_updated_successfully", {
|
|
|
|
workflowName: workflow.name,
|
|
|
|
}),
|
|
|
|
"success"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
await router.push("/workflows");
|
|
|
|
},
|
|
|
|
onError: (err) => {
|
|
|
|
if (err instanceof HttpError) {
|
|
|
|
const message = `${err.statusCode}: ${err.message}`;
|
|
|
|
showToast(message, "error");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return session.data ? (
|
|
|
|
<Form
|
|
|
|
form={form}
|
|
|
|
handleSubmit={async (values) => {
|
|
|
|
let activeOnEventTypeIds: number[] = [];
|
2022-11-25 14:34:55 +00:00
|
|
|
let isEmpty = false;
|
2022-12-15 21:54:40 +00:00
|
|
|
let isVerified = true;
|
2022-11-23 02:55:25 +00:00
|
|
|
|
|
|
|
values.steps.forEach((step) => {
|
2022-11-25 14:34:55 +00:00
|
|
|
const isSMSAction =
|
|
|
|
step.action === WorkflowActions.SMS_ATTENDEE || step.action === WorkflowActions.SMS_NUMBER;
|
|
|
|
|
|
|
|
const strippedHtml = step.reminderBody?.replace(/<[^>]+>/g, "") || "";
|
|
|
|
|
|
|
|
const isBodyEmpty =
|
|
|
|
step.template === WorkflowTemplates.CUSTOM && !isSMSAction && strippedHtml.length <= 1;
|
|
|
|
|
|
|
|
if (isBodyEmpty) {
|
|
|
|
form.setError(`steps.${step.stepNumber - 1}.reminderBody`, {
|
|
|
|
type: "custom",
|
|
|
|
message: t("fill_this_field"),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-23 02:55:25 +00:00
|
|
|
if (step.reminderBody) {
|
|
|
|
step.reminderBody = translateVariablesToEnglish(step.reminderBody, { locale: i18n.language, t });
|
|
|
|
}
|
|
|
|
if (step.emailSubject) {
|
|
|
|
step.emailSubject = translateVariablesToEnglish(step.emailSubject, { locale: i18n.language, t });
|
|
|
|
}
|
2022-11-25 14:34:55 +00:00
|
|
|
isEmpty = !isEmpty ? isBodyEmpty : isEmpty;
|
2022-12-15 21:54:40 +00:00
|
|
|
|
|
|
|
//check if phone number is verified
|
|
|
|
if (
|
|
|
|
step.action === WorkflowActions.SMS_NUMBER &&
|
|
|
|
!verifiedNumbers?.find((verifiedNumber) => verifiedNumber.phoneNumber === step.sendTo)
|
|
|
|
) {
|
|
|
|
isVerified = false;
|
|
|
|
|
|
|
|
form.setError(`steps.${step.stepNumber - 1}.sendTo`, {
|
|
|
|
type: "custom",
|
|
|
|
message: t("not_verified"),
|
|
|
|
});
|
|
|
|
}
|
2022-11-23 02:55:25 +00:00
|
|
|
});
|
|
|
|
|
2022-12-15 21:54:40 +00:00
|
|
|
if (!isEmpty && isVerified) {
|
2022-11-25 14:34:55 +00:00
|
|
|
if (values.activeOn) {
|
|
|
|
activeOnEventTypeIds = values.activeOn.map((option) => {
|
|
|
|
return parseInt(option.value, 10);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
updateMutation.mutate({
|
|
|
|
id: parseInt(router.query.workflow as string, 10),
|
|
|
|
name: values.name,
|
|
|
|
activeOn: activeOnEventTypeIds,
|
|
|
|
steps: values.steps,
|
|
|
|
trigger: values.trigger,
|
|
|
|
time: values.time || null,
|
|
|
|
timeUnit: values.timeUnit || null,
|
2022-11-23 02:55:25 +00:00
|
|
|
});
|
2022-12-15 21:54:40 +00:00
|
|
|
utils.viewer.workflows.getVerifiedNumbers.invalidate();
|
2022-11-23 02:55:25 +00:00
|
|
|
}
|
|
|
|
}}>
|
|
|
|
<Shell
|
|
|
|
backPath="/workflows"
|
|
|
|
title={workflow && workflow.name ? workflow.name : "Untitled"}
|
|
|
|
CTA={
|
|
|
|
<div>
|
|
|
|
<Button type="submit">{t("save")}</Button>
|
2022-07-14 00:10:45 +00:00
|
|
|
</div>
|
2022-11-23 02:55:25 +00:00
|
|
|
}
|
|
|
|
heading={
|
|
|
|
session.data?.hasValidLicense &&
|
|
|
|
isAllDataLoaded && (
|
|
|
|
<div className={classNames(workflow && !workflow.name ? "text-gray-400" : "")}>
|
|
|
|
{workflow && workflow.name ? workflow.name : "untitled"}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}>
|
|
|
|
<LicenseRequired>
|
|
|
|
{!isError ? (
|
|
|
|
<>
|
|
|
|
{isAllDataLoaded ? (
|
|
|
|
<>
|
2022-07-21 00:33:35 +00:00
|
|
|
<WorkflowDetailsPage
|
|
|
|
form={form}
|
|
|
|
workflowId={+workflowId}
|
|
|
|
selectedEventTypes={selectedEventTypes}
|
|
|
|
setSelectedEventTypes={setSelectedEventTypes}
|
|
|
|
/>
|
2022-11-23 02:55:25 +00:00
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<SkeletonLoader />
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<Alert severity="error" title="Something went wrong" message={error.message} />
|
|
|
|
)}
|
|
|
|
</LicenseRequired>
|
|
|
|
</Shell>
|
|
|
|
</Form>
|
|
|
|
) : (
|
|
|
|
<></>
|
2022-07-14 00:10:45 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default WorkflowPage;
|