2022-07-14 00:10:45 +00:00
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
2023-02-27 07:24:43 +00:00
|
|
|
import type { WorkflowStep } from "@prisma/client";
|
2022-07-28 19:58:26 +00:00
|
|
|
import {
|
|
|
|
TimeUnit,
|
|
|
|
WorkflowActions,
|
|
|
|
WorkflowTemplates,
|
|
|
|
WorkflowTriggerEvents,
|
2023-02-27 07:24:43 +00:00
|
|
|
MembershipRole,
|
2022-07-28 19:58:26 +00:00
|
|
|
} 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";
|
2023-01-18 14:32:39 +00:00
|
|
|
import { SENDER_ID } from "@calcom/lib/constants";
|
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-02-27 07:24:43 +00:00
|
|
|
import { Alert, Button, Form, showToast, Badge } from "@calcom/ui";
|
2022-11-23 02:55:25 +00:00
|
|
|
|
2023-04-19 14:15:08 +00:00
|
|
|
import LicenseRequired from "../../common/components/LicenseRequired";
|
2022-11-23 02:55:25 +00:00
|
|
|
import SkeletonLoader from "../components/SkeletonLoaderEdit";
|
2022-07-28 19:58:26 +00:00
|
|
|
import WorkflowDetailsPage from "../components/WorkflowDetailsPage";
|
2023-04-18 10:08:09 +00:00
|
|
|
import { isSMSAction } from "../lib/actionHelperFunctions";
|
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[];
|
2023-01-18 14:32:39 +00:00
|
|
|
steps: (WorkflowStep & { senderName: string | null })[];
|
2022-07-14 00:10:45 +00:00
|
|
|
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(),
|
2023-01-18 14:32:39 +00:00
|
|
|
senderName: z.string().optional().nullable(),
|
2022-07-20 19:48:40 +00:00
|
|
|
})
|
|
|
|
.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);
|
2023-02-27 07:24:43 +00:00
|
|
|
const [isMixedEventType, setIsMixedEventType] = useState(false); //for old event types before team workflows existed
|
2022-07-14 00:10:45 +00:00
|
|
|
|
|
|
|
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,
|
2023-02-13 12:39:06 +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
|
|
|
|
2023-02-27 07:24:43 +00:00
|
|
|
const { data: verifiedNumbers } = trpc.viewer.workflows.getVerifiedNumbers.useQuery(
|
|
|
|
{ teamId: workflow?.team?.id },
|
|
|
|
{
|
|
|
|
enabled: !!workflow?.id,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const readOnly =
|
|
|
|
workflow?.team?.members?.find((member) => member.userId === session.data?.user.id)?.role ===
|
|
|
|
MembershipRole.MEMBER;
|
2022-12-15 21:54:40 +00:00
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
useEffect(() => {
|
2023-02-13 12:39:06 +00:00
|
|
|
if (workflow && !isLoading) {
|
2023-02-27 07:24:43 +00:00
|
|
|
if (workflow.userId && workflow.activeOn.find((active) => !!active.eventType.teamId)) {
|
|
|
|
setIsMixedEventType(true);
|
|
|
|
}
|
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) => {
|
2023-01-18 14:32:39 +00:00
|
|
|
const updatedStep = {
|
|
|
|
...step,
|
|
|
|
senderName: step.sender,
|
|
|
|
sender: isSMSAction(step.action) ? step.sender : SENDER_ID,
|
|
|
|
};
|
2022-11-23 02:55:25 +00:00
|
|
|
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);
|
|
|
|
}
|
2023-02-13 12:39:06 +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 strippedHtml = step.reminderBody?.replace(/<[^>]+>/g, "") || "";
|
|
|
|
|
2023-04-18 10:08:09 +00:00
|
|
|
const isBodyEmpty = !isSMSAction(step.action) && strippedHtml.length <= 1;
|
2022-11-25 14:34:55 +00:00
|
|
|
|
|
|
|
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>
|
2023-02-27 07:24:43 +00:00
|
|
|
<Button type="submit" disabled={readOnly}>
|
|
|
|
{t("save")}
|
|
|
|
</Button>
|
2022-07-14 00:10:45 +00:00
|
|
|
</div>
|
2022-11-23 02:55:25 +00:00
|
|
|
}
|
2023-04-19 20:17:54 +00:00
|
|
|
hideHeadingOnMobile
|
2022-11-23 02:55:25 +00:00
|
|
|
heading={
|
|
|
|
session.data?.hasValidLicense &&
|
|
|
|
isAllDataLoaded && (
|
2023-02-27 07:24:43 +00:00
|
|
|
<div className="flex">
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className={classNames(workflow && !workflow.name ? "text-muted" : "")}>
|
2023-02-27 07:24:43 +00:00
|
|
|
{workflow && workflow.name ? workflow.name : "untitled"}
|
|
|
|
</div>
|
|
|
|
{workflow && workflow.team && (
|
|
|
|
<Badge className="mt-1 ml-4" variant="gray">
|
|
|
|
{workflow.team.slug}
|
|
|
|
</Badge>
|
|
|
|
)}
|
2022-11-23 02:55:25 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}>
|
|
|
|
<LicenseRequired>
|
|
|
|
{!isError ? (
|
|
|
|
<>
|
|
|
|
{isAllDataLoaded ? (
|
|
|
|
<>
|
2022-07-21 00:33:35 +00:00
|
|
|
<WorkflowDetailsPage
|
|
|
|
form={form}
|
|
|
|
workflowId={+workflowId}
|
|
|
|
selectedEventTypes={selectedEventTypes}
|
|
|
|
setSelectedEventTypes={setSelectedEventTypes}
|
2023-02-27 07:24:43 +00:00
|
|
|
teamId={workflow ? workflow.teamId || undefined : undefined}
|
|
|
|
isMixedEventType={isMixedEventType}
|
2022-07-21 00:33:35 +00:00
|
|
|
/>
|
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;
|