2022-08-13 11:04:57 +00:00
|
|
|
import Head from "next/head";
|
|
|
|
import z from "zod";
|
|
|
|
|
2023-08-23 15:12:51 +00:00
|
|
|
import { getSlugOrRequestedSlug } from "@calcom/features/ee/organizations/lib/orgDomains";
|
2023-07-25 09:28:57 +00:00
|
|
|
import { orgDomainConfig } from "@calcom/features/ee/organizations/lib/orgDomains";
|
2023-08-23 15:12:51 +00:00
|
|
|
import logger from "@calcom/lib/logger";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { AppGetServerSidePropsContext, AppPrisma } from "@calcom/types/AppGetServerSideProps";
|
|
|
|
import type { inferSSRProps } from "@calcom/types/inferSSRProps";
|
2022-08-13 11:04:57 +00:00
|
|
|
|
|
|
|
import getFieldIdentifier from "../../lib/getFieldIdentifier";
|
|
|
|
import { getSerializableForm } from "../../lib/getSerializableForm";
|
|
|
|
import { processRoute } from "../../lib/processRoute";
|
2023-06-01 20:29:13 +00:00
|
|
|
import transformResponse from "../../lib/transformResponse";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { Response } from "../../types/types";
|
2022-08-13 11:04:57 +00:00
|
|
|
|
2023-08-23 15:12:51 +00:00
|
|
|
const log = logger.getChildLogger({ prefix: ["[routing-forms]", "[router]"] });
|
2022-08-13 11:04:57 +00:00
|
|
|
export default function Router({ form, message }: inferSSRProps<typeof getServerSideProps>) {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Head>
|
|
|
|
<title>{form.name} | Cal.com Forms</title>
|
|
|
|
</Head>
|
|
|
|
<div className="mx-auto my-0 max-w-3xl md:my-24">
|
|
|
|
<div className="w-full max-w-4xl ltr:mr-2 rtl:ml-2">
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="bg-default -mx-4 rounded-sm border border-neutral-200 p-4 py-6 sm:mx-0 sm:px-8">
|
2022-08-13 11:04:57 +00:00
|
|
|
<div>{message}</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const querySchema = z
|
|
|
|
.object({
|
|
|
|
form: z.string(),
|
|
|
|
slug: z.string(),
|
|
|
|
pages: z.array(z.string()),
|
|
|
|
})
|
2023-08-23 15:12:51 +00:00
|
|
|
.catchall(z.string().or(z.array(z.string())));
|
2022-08-13 11:04:57 +00:00
|
|
|
|
|
|
|
export const getServerSideProps = async function getServerSideProps(
|
|
|
|
context: AppGetServerSidePropsContext,
|
|
|
|
prisma: AppPrisma
|
|
|
|
) {
|
|
|
|
const queryParsed = querySchema.safeParse(context.query);
|
|
|
|
if (!queryParsed.success) {
|
2023-08-23 15:12:51 +00:00
|
|
|
log.warn("Error parsing query", queryParsed.error);
|
2022-08-13 11:04:57 +00:00
|
|
|
return {
|
|
|
|
notFound: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
const { form: formId, slug: _slug, pages: _pages, ...fieldsResponses } = queryParsed.data;
|
2023-07-25 09:28:57 +00:00
|
|
|
const { currentOrgDomain, isValidOrgDomain } = orgDomainConfig(context.req.headers.host ?? "");
|
|
|
|
|
|
|
|
const form = await prisma.app_RoutingForms_Form.findFirst({
|
2022-08-13 11:04:57 +00:00
|
|
|
where: {
|
|
|
|
id: formId,
|
2023-07-25 09:28:57 +00:00
|
|
|
user: {
|
2023-08-23 15:12:51 +00:00
|
|
|
organization: isValidOrgDomain && currentOrgDomain ? getSlugOrRequestedSlug(currentOrgDomain) : null,
|
2023-07-25 09:28:57 +00:00
|
|
|
},
|
2022-08-13 11:04:57 +00:00
|
|
|
},
|
|
|
|
});
|
2023-07-25 09:28:57 +00:00
|
|
|
|
2022-08-13 11:04:57 +00:00
|
|
|
if (!form) {
|
|
|
|
return {
|
|
|
|
notFound: true,
|
|
|
|
};
|
|
|
|
}
|
2023-06-15 08:58:07 +00:00
|
|
|
const serializableForm = await getSerializableForm({ form });
|
2022-08-13 11:04:57 +00:00
|
|
|
|
2023-08-23 15:12:51 +00:00
|
|
|
const response: Response = {};
|
2022-08-13 11:04:57 +00:00
|
|
|
serializableForm.fields?.forEach((field) => {
|
2023-06-01 20:29:13 +00:00
|
|
|
const fieldResponse = fieldsResponses[getFieldIdentifier(field)] || "";
|
2023-08-23 15:12:51 +00:00
|
|
|
|
2022-08-13 11:04:57 +00:00
|
|
|
response[field.id] = {
|
2023-08-23 15:12:51 +00:00
|
|
|
label: field.label,
|
|
|
|
value: transformResponse({ field, value: fieldResponse }),
|
2022-08-13 11:04:57 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
const decidedAction = processRoute({ form: serializableForm, response });
|
|
|
|
|
|
|
|
if (!decidedAction) {
|
|
|
|
throw new Error("No matching route could be found");
|
|
|
|
}
|
|
|
|
|
2023-08-23 15:12:51 +00:00
|
|
|
const { createContext } = await import("@calcom/trpc/server/createContext");
|
|
|
|
const ctx = await createContext(context);
|
|
|
|
|
|
|
|
const { default: trpcRouter } = await import("@calcom/app-store/routing-forms/trpc/_router");
|
|
|
|
const caller = trpcRouter.createCaller(ctx);
|
|
|
|
const { v4: uuidv4 } = await import("uuid");
|
|
|
|
await caller.public.response({
|
|
|
|
formId: form.id,
|
|
|
|
formFillerId: uuidv4(),
|
|
|
|
response: response,
|
|
|
|
});
|
|
|
|
|
2022-08-13 11:04:57 +00:00
|
|
|
//TODO: Maybe take action after successful mutation
|
|
|
|
if (decidedAction.type === "customPageMessage") {
|
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
form: serializableForm,
|
|
|
|
message: decidedAction.value,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} else if (decidedAction.type === "eventTypeRedirectUrl") {
|
|
|
|
return {
|
|
|
|
redirect: {
|
|
|
|
destination: `/${decidedAction.value}`,
|
|
|
|
permanent: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} else if (decidedAction.type === "externalRedirectUrl") {
|
|
|
|
return {
|
|
|
|
redirect: {
|
|
|
|
destination: `${decidedAction.value}`,
|
|
|
|
permanent: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2023-08-23 15:12:51 +00:00
|
|
|
|
2022-08-13 11:04:57 +00:00
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
form: serializableForm,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|