* refactor: handleNewBooking #3 * refactor: create booking factor * refactor: handleNewBooking * refactor: seats and rescheduleUId * chore: remove comment * fix: type err * chore: add missing statement * chore: use less params and other improvements * chore: name * chore: improvement * fix: type err * Typo fix * chore: fix type err * refactor: more readable * refactor: improve code * fix: conflicts --------- Co-authored-by: Joe Au-Yeung <j.auyeung419@gmail.com> Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> Co-authored-by: Syed Ali Shahbaz <52925846+alishaz-polymath@users.noreply.github.com>
50 lines
1.8 KiB
TypeScript
50 lines
1.8 KiB
TypeScript
import type z from "zod";
|
|
|
|
import { slugify } from "@calcom/lib/slugify";
|
|
import type { bookingCreateSchemaLegacyPropsForApi } from "@calcom/prisma/zod-utils";
|
|
import type { CalendarEvent } from "@calcom/types/Calendar";
|
|
|
|
import type { getEventTypeResponse } from "./types";
|
|
|
|
type CustomInputs = z.infer<typeof bookingCreateSchemaLegacyPropsForApi>["customInputs"];
|
|
|
|
type RequestBody = {
|
|
responses?: Record<string, object>;
|
|
customInputs?: CustomInputs;
|
|
};
|
|
|
|
function mapCustomInputs(
|
|
customInputs: { label: string; value: CustomInputs[number]["value"] }[]
|
|
): Record<string, CustomInputs[number]["value"]> {
|
|
return customInputs.reduce((acc, { label, value }) => {
|
|
acc[label] = value;
|
|
return acc;
|
|
}, {} as Record<string, CustomInputs[number]["value"]>);
|
|
}
|
|
|
|
function mapResponsesToCustomInputs(
|
|
responses: Record<string, object>,
|
|
eventTypeCustomInputs: getEventTypeResponse["customInputs"]
|
|
): NonNullable<CalendarEvent["customInputs"]> {
|
|
// Backward Compatibility: Map new `responses` to old `customInputs` format so that webhooks can still receive same values.
|
|
return Object.entries(responses).reduce((acc, [fieldName, fieldValue]) => {
|
|
const foundInput = eventTypeCustomInputs.find((input) => slugify(input.label) === fieldName);
|
|
if (foundInput) {
|
|
acc[foundInput.label] = fieldValue;
|
|
}
|
|
return acc;
|
|
}, {} as NonNullable<CalendarEvent["customInputs"]>);
|
|
}
|
|
|
|
export function getCustomInputsResponses(
|
|
reqBody: RequestBody,
|
|
eventTypeCustomInputs: getEventTypeResponse["customInputs"]
|
|
): NonNullable<CalendarEvent["customInputs"]> {
|
|
if (reqBody.customInputs && reqBody.customInputs.length > 0) {
|
|
return mapCustomInputs(reqBody.customInputs);
|
|
}
|
|
|
|
const responses = reqBody.responses || {};
|
|
return mapResponsesToCustomInputs(responses, eventTypeCustomInputs);
|
|
}
|