Files
calendar/packages/app-store/ee/routing-forms/lib/getSerializableForm.ts
T
Hariom BalharaGitHubkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>Peer Richelsen
deeb8f38e5 Support header in Routing Form CSV (#5133)
* Dont actually delete fields and now add headers to CSV which is possible

* Fix TS issues

* Handle cases due to soft deletion

* Add submission time as a column

* Fix bug with fields getting deleted

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
Co-authored-by: Peer Richelsen <peeroke@gmail.com>
2022-11-08 14:21:53 +00:00

46 lines
1.4 KiB
TypeScript

import { App_RoutingForms_Form } from "@prisma/client";
import { RoutingFormSettings } from "@calcom/prisma/zod-utils";
import { SerializableForm } from "../types/types";
import { zodFields, zodRoutes } from "../zod";
/**
* Doesn't have deleted fields by default
*/
export function getSerializableForm<TForm extends App_RoutingForms_Form>(
form: TForm,
withDeletedFields = false
) {
const routesParsed = zodRoutes.safeParse(form.routes);
if (!routesParsed.success) {
throw new Error("Error parsing routes");
}
const fieldsParsed = zodFields.safeParse(form.fields);
if (!fieldsParsed.success) {
throw new Error("Error parsing fields");
}
const settings = RoutingFormSettings.parse(
form.settings || {
// Would have really loved to do it using zod. But adding .default(true) throws type error in prisma/zod/app_routingforms_form.ts
emailOwnerOnSubmission: true,
}
);
// Ideally we shouldb't have needed to explicitly type it but due to some reason it's not working reliably with VSCode TypeCheck
const serializableForm: SerializableForm<TForm> = {
...form,
settings: settings,
fields: fieldsParsed.data
? withDeletedFields
? fieldsParsed.data
: fieldsParsed.data.filter((f) => !f.deleted)
: [],
routes: routesParsed.data,
createdAt: form.createdAt.toString(),
updatedAt: form.updatedAt.toString(),
};
return serializableForm;
}