2022-07-20 18:30:57 +00:00
|
|
|
import { Prisma, WebhookTriggerEvents } from "@prisma/client";
|
2022-07-15 11:46:05 +00:00
|
|
|
import { v4 as uuidv4 } from "uuid";
|
2022-07-14 12:40:53 +00:00
|
|
|
import { z } from "zod";
|
|
|
|
|
2022-09-12 19:07:52 +00:00
|
|
|
import getWebhooks from "@calcom/features/webhooks/utils/getWebhooks";
|
2022-08-03 19:18:26 +00:00
|
|
|
import { sendGenericWebhookPayload } from "@calcom/lib/webhooks/sendPayload";
|
2022-07-22 17:27:06 +00:00
|
|
|
import { TRPCError } from "@calcom/trpc/server";
|
|
|
|
import { createProtectedRouter, createRouter } from "@calcom/trpc/server/createRouter";
|
|
|
|
|
2022-09-02 19:00:41 +00:00
|
|
|
import { isAllowed } from "./lib/isAllowed";
|
2022-07-14 12:40:53 +00:00
|
|
|
import { zodFields, zodRoutes } from "./zod";
|
|
|
|
|
2022-07-19 18:36:39 +00:00
|
|
|
const app_RoutingForms = createRouter()
|
|
|
|
.merge(
|
|
|
|
"public.",
|
|
|
|
createRouter().mutation("response", {
|
|
|
|
input: z.object({
|
|
|
|
formId: z.string(),
|
|
|
|
formFillerId: z.string(),
|
|
|
|
response: z.record(
|
|
|
|
z.object({
|
|
|
|
label: z.string(),
|
|
|
|
value: z.union([z.string(), z.array(z.string())]),
|
|
|
|
})
|
|
|
|
),
|
|
|
|
}),
|
|
|
|
async resolve({ ctx: { prisma }, input }) {
|
|
|
|
try {
|
|
|
|
const { response, formId } = input;
|
|
|
|
const form = await prisma.app_RoutingForms_Form.findFirst({
|
|
|
|
where: {
|
|
|
|
id: formId,
|
2022-07-14 12:40:53 +00:00
|
|
|
},
|
2022-07-20 18:30:57 +00:00
|
|
|
include: {
|
|
|
|
user: true,
|
|
|
|
},
|
2022-07-14 12:40:53 +00:00
|
|
|
});
|
2022-07-19 18:36:39 +00:00
|
|
|
if (!form) {
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "NOT_FOUND",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const fieldsParsed = zodFields.safeParse(form.fields);
|
|
|
|
if (!fieldsParsed.success) {
|
|
|
|
// This should not be possible normally as before saving the form it is verified by zod
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "INTERNAL_SERVER_ERROR",
|
|
|
|
});
|
|
|
|
}
|
2022-07-14 12:40:53 +00:00
|
|
|
|
2022-07-19 18:36:39 +00:00
|
|
|
const fields = fieldsParsed.data;
|
2022-07-14 12:40:53 +00:00
|
|
|
|
2022-07-19 18:36:39 +00:00
|
|
|
if (!fields) {
|
|
|
|
// There is no point in submitting a form that doesn't have fields defined
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "BAD_REQUEST",
|
|
|
|
});
|
|
|
|
}
|
2022-07-14 12:40:53 +00:00
|
|
|
|
2022-07-19 18:36:39 +00:00
|
|
|
const missingFields = fields
|
|
|
|
.filter((field) => !(field.required ? response[field.id]?.value : true))
|
|
|
|
.map((f) => f.label);
|
2022-07-14 12:40:53 +00:00
|
|
|
|
2022-07-19 18:36:39 +00:00
|
|
|
if (missingFields.length) {
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "BAD_REQUEST",
|
|
|
|
message: `Missing required fields ${missingFields.join(", ")}`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const invalidFields = fields
|
|
|
|
.filter((field) => {
|
|
|
|
const fieldValue = response[field.id]?.value;
|
|
|
|
// The field isn't required at this point. Validate only if it's set
|
|
|
|
if (!fieldValue) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let schema;
|
|
|
|
if (field.type === "email") {
|
|
|
|
schema = z.string().email();
|
|
|
|
} else if (field.type === "phone") {
|
|
|
|
schema = z.any();
|
|
|
|
} else {
|
|
|
|
schema = z.any();
|
|
|
|
}
|
|
|
|
return !schema.safeParse(fieldValue).success;
|
|
|
|
})
|
|
|
|
.map((f) => ({ label: f.label, type: f.type }));
|
|
|
|
|
|
|
|
if (invalidFields.length) {
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "BAD_REQUEST",
|
|
|
|
message: `Invalid fields ${invalidFields.map((f) => `${f.label}: ${f.type}`)}`,
|
|
|
|
});
|
|
|
|
}
|
2022-07-14 12:40:53 +00:00
|
|
|
|
2022-07-20 18:30:57 +00:00
|
|
|
const fieldResponsesByName: Record<string, typeof response[keyof typeof response]["value"]> = {};
|
|
|
|
|
|
|
|
for (const [fieldId, fieldResponse] of Object.entries(response)) {
|
|
|
|
// Use the label lowercased as the key to identify a field.
|
|
|
|
const key =
|
|
|
|
fields.find((f) => f.id === fieldId)?.identifier ||
|
|
|
|
(fieldResponse.label as keyof typeof fieldResponsesByName);
|
|
|
|
fieldResponsesByName[key] = fieldResponse.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
const subscriberOptions = {
|
|
|
|
userId: form.user.id,
|
|
|
|
// It isn't an eventType webhook
|
|
|
|
eventTypeId: -1,
|
|
|
|
triggerEvent: WebhookTriggerEvents.FORM_SUBMITTED,
|
|
|
|
};
|
|
|
|
const webhooks = await getWebhooks(subscriberOptions);
|
|
|
|
const promises = webhooks.map((webhook) => {
|
|
|
|
sendGenericWebhookPayload(
|
|
|
|
webhook.secret,
|
|
|
|
"FORM_SUBMITTED",
|
|
|
|
new Date().toISOString(),
|
|
|
|
webhook,
|
|
|
|
fieldResponsesByName
|
|
|
|
).catch((e) => {
|
|
|
|
console.error(`Error executing routing form webhook`, webhook, e);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
await Promise.all(promises);
|
|
|
|
|
2022-07-19 18:36:39 +00:00
|
|
|
return await prisma.app_RoutingForms_FormResponse.create({
|
|
|
|
data: input,
|
2022-07-14 12:40:53 +00:00
|
|
|
});
|
2022-07-19 18:36:39 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
|
|
|
if (e.code === "P2002") {
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "CONFLICT",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw e;
|
2022-07-14 12:40:53 +00:00
|
|
|
}
|
2022-07-19 18:36:39 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.merge(
|
|
|
|
"",
|
|
|
|
createProtectedRouter()
|
|
|
|
.query("forms", {
|
|
|
|
async resolve({ ctx: { user, prisma } }) {
|
|
|
|
return await prisma.app_RoutingForms_Form.findMany({
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
|
|
|
},
|
|
|
|
orderBy: {
|
|
|
|
createdAt: "asc",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.query("form", {
|
|
|
|
input: z.object({
|
|
|
|
id: z.string(),
|
|
|
|
}),
|
2022-08-13 11:04:57 +00:00
|
|
|
async resolve({ ctx: { prisma, user }, input }) {
|
2022-07-19 18:36:39 +00:00
|
|
|
const form = await prisma.app_RoutingForms_Form.findFirst({
|
|
|
|
where: {
|
2022-08-13 11:04:57 +00:00
|
|
|
userId: user.id,
|
2022-07-19 18:36:39 +00:00
|
|
|
id: input.id,
|
|
|
|
},
|
|
|
|
});
|
2022-07-14 12:40:53 +00:00
|
|
|
|
2022-07-19 18:36:39 +00:00
|
|
|
return form;
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.mutation("form", {
|
|
|
|
input: z.object({
|
|
|
|
id: z.string(),
|
|
|
|
name: z.string(),
|
|
|
|
description: z.string().nullable().optional(),
|
|
|
|
disabled: z.boolean().optional(),
|
|
|
|
fields: zodFields,
|
|
|
|
routes: zodRoutes,
|
|
|
|
addFallback: z.boolean().optional(),
|
2022-08-13 11:04:57 +00:00
|
|
|
duplicateFrom: z.string().nullable().optional(),
|
2022-07-19 18:36:39 +00:00
|
|
|
}),
|
|
|
|
async resolve({ ctx: { user, prisma }, input }) {
|
2022-08-13 11:04:57 +00:00
|
|
|
const { name, id, description, disabled, addFallback, duplicateFrom } = input;
|
2022-09-02 19:00:41 +00:00
|
|
|
if (!(await isAllowed({ userId: user.id, formId: id }))) {
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "FORBIDDEN",
|
|
|
|
});
|
|
|
|
}
|
2022-07-19 18:36:39 +00:00
|
|
|
let { routes } = input;
|
|
|
|
let { fields } = input;
|
2022-08-13 11:04:57 +00:00
|
|
|
|
|
|
|
if (duplicateFrom) {
|
|
|
|
const sourceForm = await prisma.app_RoutingForms_Form.findFirst({
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
|
|
|
id: duplicateFrom,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
fields: true,
|
|
|
|
routes: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (!sourceForm) {
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "BAD_REQUEST",
|
|
|
|
message: `Form to duplicate: ${duplicateFrom} not found`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const fieldParsed = zodFields.safeParse(sourceForm.fields);
|
|
|
|
const routesParsed = zodRoutes.safeParse(sourceForm.routes);
|
|
|
|
if (!fieldParsed.success || !routesParsed.success) {
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "INTERNAL_SERVER_ERROR",
|
|
|
|
message: "Could not parse source form's fields or routes",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// Duplicate just routes and fields
|
|
|
|
// We don't want name, description and responses to be copied
|
|
|
|
routes = routesParsed.data;
|
|
|
|
fields = fieldParsed.data;
|
|
|
|
}
|
|
|
|
|
2022-07-19 18:36:39 +00:00
|
|
|
fields = fields || [];
|
|
|
|
|
|
|
|
if (addFallback) {
|
|
|
|
const uuid = uuidv4();
|
|
|
|
routes = routes || [];
|
2022-08-13 11:04:57 +00:00
|
|
|
// Add a fallback route if there is none
|
|
|
|
if (!routes.find((route) => route.isFallback)) {
|
|
|
|
routes.push({
|
|
|
|
id: uuid,
|
|
|
|
isFallback: true,
|
|
|
|
action: {
|
|
|
|
type: "customPageMessage",
|
|
|
|
value: "Thank you for your interest! We will be in touch soon.",
|
|
|
|
},
|
|
|
|
queryValue: { id: uuid, type: "group" },
|
|
|
|
});
|
|
|
|
}
|
2022-07-14 12:40:53 +00:00
|
|
|
}
|
2022-07-19 18:36:39 +00:00
|
|
|
|
|
|
|
return await prisma.app_RoutingForms_Form.upsert({
|
|
|
|
where: {
|
|
|
|
id: id,
|
|
|
|
},
|
|
|
|
create: {
|
|
|
|
user: {
|
|
|
|
connect: {
|
|
|
|
id: user.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
fields: fields,
|
|
|
|
name: name,
|
|
|
|
description,
|
|
|
|
// Prisma doesn't allow setting null value directly for JSON. It recommends using JsonNull for that case.
|
|
|
|
routes: routes === null ? Prisma.JsonNull : routes,
|
|
|
|
id: id,
|
|
|
|
},
|
|
|
|
update: {
|
|
|
|
disabled: disabled,
|
|
|
|
fields: fields,
|
|
|
|
name: name,
|
|
|
|
description,
|
|
|
|
routes: routes === null ? Prisma.JsonNull : routes,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
})
|
|
|
|
// TODO: Can't we use DELETE method on form?
|
|
|
|
.mutation("deleteForm", {
|
|
|
|
input: z.object({
|
|
|
|
id: z.string(),
|
|
|
|
}),
|
2022-09-02 19:00:41 +00:00
|
|
|
async resolve({ ctx: { user, prisma }, input }) {
|
|
|
|
if (!(await isAllowed({ userId: user.id, formId: input.id }))) {
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "FORBIDDEN",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return await prisma.app_RoutingForms_Form.deleteMany({
|
2022-07-19 18:36:39 +00:00
|
|
|
where: {
|
|
|
|
id: input.id,
|
2022-09-02 19:00:41 +00:00
|
|
|
userId: user.id,
|
2022-07-19 18:36:39 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
2022-07-14 12:40:53 +00:00
|
|
|
|
|
|
|
export default app_RoutingForms;
|