feat: Headless router - queue recording booking response (#21805)
* Add queued booking response table * Create `RoutingFormResponseRepository` * Pass `queueFormResponse` param * Queue up form response if param is passed * Forward queued form response parma to booker * Pass `queuedFormResponse` from booker to `handleNewBooking` * Write queued routing form response * Type fixes * Clean up * Allow dry run to work which wont have any QueuedFormResponse or FormResponse * Support passing the time when the modal was actually shown to the user and consider that time as the time of form submission * fix ts error * Queue -> Response through separate endpoint that would be triggered by embed * Make queueResponseId a non-guessable uid * Change queueFormResponse query param * fix ts error * Support useQueuedResponse to record new response data * revert handleNewBooking * Remove dead code formResponse * Refactor use repository * Unify migration files * refactor: moved api endpoint to app dir Signed-off-by: Omar López <[email protected]> * Update formResponse.ts * Refactor use-queued-response for test * Add tests * Fix ts error and unit test. recordFormResponse cant return nullish response * fix schema * feat: Support full reuse of preloaded iframe (#21803) * feat: support updating cal video settings in API v2 (#21784) * feat: support updating cal video settings in API v2 * chore: update descriptio * feat: support create event type * test: add test for updating event type * test: add test for create event type * chore: undo openapi * chore: bump libraries * Revert "chore: bump libraries" This reverts commit bdf36d09b021fc531497a7b7ea66ab9c52b7d136. * chore: bump libraries --------- Co-authored-by: Lauris Skraucis <[email protected]> Co-authored-by: supalarry <[email protected]> * fix tests and ts * Fix tests * wip-useQueuedResponseEndpoint * Add one more test * Change queueFormResponse query param * wip * Support useQueuedResponse to record new response data * Use the update useQueuedResponse endpoint * self-review addressed * Use queuedResponse if available in slots/utils * Add documentation * Remove use-queued-response from critical-path --------- Co-authored-by: Udit Takkar <[email protected]> Co-authored-by: Lauris Skraucis <[email protected]> Co-authored-by: supalarry <[email protected]> * Update schema.prisma * refactor: renamed to avoid react hooks confusion Signed-off-by: Omar López <[email protected]> --------- Signed-off-by: Omar López <[email protected]> Co-authored-by: Hariom Balhara <[email protected]> Co-authored-by: Omar López <[email protected]> Co-authored-by: Udit Takkar <[email protected]> Co-authored-by: Lauris Skraucis <[email protected]> Co-authored-by: supalarry <[email protected]> Co-authored-by: Peer Richelsen <[email protected]>
This commit is contained in:
28 files changed
+1408
-424
No files matched your search
@@ -0,0 +1,72 @@
|
||||
import type { Prisma } from "@prisma/client";
|
||||
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import type { App_RoutingForms_Form } from "@calcom/prisma/client";
|
||||
import { RoutingFormSettings } from "@calcom/prisma/zod-utils";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import { onFormSubmission } from "../trpc/utils";
|
||||
import type { FormResponse, SerializableForm } from "../types/types";
|
||||
|
||||
export type TargetRoutingFormForResponse = SerializableForm<
|
||||
App_RoutingForms_Form & {
|
||||
user: {
|
||||
id: number;
|
||||
email: string;
|
||||
};
|
||||
team: {
|
||||
parentId: number | null;
|
||||
} | null;
|
||||
}
|
||||
>;
|
||||
|
||||
/**
|
||||
* A wrapper over onFormSubmission that handles building the data needed for onFormSubmission
|
||||
*/
|
||||
export const onSubmissionOfFormResponse = async ({
|
||||
form,
|
||||
formResponseInDb,
|
||||
chosenRouteAction,
|
||||
}: {
|
||||
form: TargetRoutingFormForResponse;
|
||||
formResponseInDb: { id: number; response: Prisma.JsonValue };
|
||||
chosenRouteAction: {
|
||||
type: "customPageMessage" | "externalRedirectUrl" | "eventTypeRedirectUrl";
|
||||
value: string;
|
||||
} | null;
|
||||
}) => {
|
||||
if (!form.fields) {
|
||||
// There is no point in submitting a form that doesn't have fields defined
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
});
|
||||
}
|
||||
const settings = RoutingFormSettings.parse(form.settings);
|
||||
let userWithEmails: string[] = [];
|
||||
|
||||
if (form.teamId && (settings?.sendToAll || settings?.sendUpdatesTo?.length)) {
|
||||
const whereClause: Prisma.MembershipWhereInput = { teamId: form.teamId };
|
||||
if (!settings?.sendToAll) {
|
||||
whereClause.userId = { in: settings.sendUpdatesTo };
|
||||
}
|
||||
const userEmails = await prisma.membership.findMany({
|
||||
where: whereClause,
|
||||
select: {
|
||||
user: {
|
||||
select: {
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
userWithEmails = userEmails.map((userEmail) => userEmail.user.email);
|
||||
}
|
||||
|
||||
await onFormSubmission(
|
||||
{ ...form, fields: form.fields, userWithEmails },
|
||||
formResponseInDb.response as FormResponse,
|
||||
formResponseInDb.id,
|
||||
chosenRouteAction ?? undefined
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,26 @@
|
||||
import type { z } from "zod";
|
||||
|
||||
import getFieldIdentifier from "@calcom/app-store/routing-forms/lib/getFieldIdentifier";
|
||||
import { getFieldResponseForJsonLogic } from "@calcom/app-store/routing-forms/lib/transformResponse";
|
||||
import type { FormResponse } from "@calcom/app-store/routing-forms/types/types";
|
||||
|
||||
import type { zodFields } from "../zod";
|
||||
|
||||
export const getResponseToStore = ({
|
||||
formFields,
|
||||
fieldsResponses,
|
||||
}: {
|
||||
formFields: NonNullable<z.infer<typeof zodFields>>;
|
||||
fieldsResponses: Record<string, string | string[]>;
|
||||
}) => {
|
||||
const response: FormResponse = {};
|
||||
formFields.forEach((field) => {
|
||||
const fieldResponse = fieldsResponses[getFieldIdentifier(field)] || "";
|
||||
|
||||
response[field.id] = {
|
||||
label: field.label,
|
||||
value: getFieldResponseForJsonLogic({ field, value: fieldResponse }),
|
||||
};
|
||||
});
|
||||
return response;
|
||||
};
|
||||
@@ -6,29 +6,14 @@ import logger from "@calcom/lib/logger";
|
||||
import { findTeamMembersMatchingAttributeLogic } from "@calcom/lib/raqb/findTeamMembersMatchingAttributeLogic";
|
||||
import { safeStringify } from "@calcom/lib/safeStringify";
|
||||
import { withReporting } from "@calcom/lib/sentryWrapper";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import type { App_RoutingForms_Form } from "@calcom/prisma/client";
|
||||
import { RoutingFormSettings } from "@calcom/prisma/zod-utils";
|
||||
import { RoutingFormResponseRepository } from "@calcom/lib/server/repository/formResponse";
|
||||
import type { ZResponseInputSchema } from "@calcom/trpc/server/routers/viewer/routing-forms/response.schema";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import isRouter from "../lib/isRouter";
|
||||
import { onFormSubmission } from "../trpc/utils";
|
||||
import type { FormResponse, SerializableForm } from "../types/types";
|
||||
import routerGetCrmContactOwnerEmail from "./crmRouting/routerGetCrmContactOwnerEmail";
|
||||
|
||||
export type Form = SerializableForm<
|
||||
App_RoutingForms_Form & {
|
||||
user: {
|
||||
id: number;
|
||||
email: string;
|
||||
};
|
||||
team: {
|
||||
parentId: number | null;
|
||||
} | null;
|
||||
}
|
||||
>;
|
||||
import { onSubmissionOfFormResponse, type TargetRoutingFormForResponse } from "./formSubmissionUtils";
|
||||
|
||||
const moduleLogger = logger.getSubLogger({ prefix: ["routing-forms/lib/handleResponse"] });
|
||||
|
||||
@@ -39,12 +24,14 @@ const _handleResponse = async ({
|
||||
// formFillerId,
|
||||
chosenRouteId,
|
||||
isPreview,
|
||||
queueFormResponse,
|
||||
}: {
|
||||
response: z.infer<typeof ZResponseInputSchema>["response"];
|
||||
form: Form;
|
||||
form: TargetRoutingFormForResponse;
|
||||
formFillerId: string;
|
||||
chosenRouteId: string | null;
|
||||
isPreview: boolean;
|
||||
queueFormResponse?: boolean;
|
||||
}) => {
|
||||
try {
|
||||
if (!form.fields) {
|
||||
@@ -99,26 +86,6 @@ const _handleResponse = async ({
|
||||
});
|
||||
}
|
||||
|
||||
const settings = RoutingFormSettings.parse(form.settings);
|
||||
let userWithEmails: string[] = [];
|
||||
if (form.teamId && (settings?.sendToAll || settings?.sendUpdatesTo?.length)) {
|
||||
const whereClause: Prisma.MembershipWhereInput = { teamId: form.teamId };
|
||||
if (!settings?.sendToAll) {
|
||||
whereClause.userId = { in: settings.sendUpdatesTo };
|
||||
}
|
||||
const userEmails = await prisma.membership.findMany({
|
||||
where: whereClause,
|
||||
select: {
|
||||
user: {
|
||||
select: {
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
userWithEmails = userEmails.map((userEmail) => userEmail.user.email);
|
||||
}
|
||||
|
||||
const chosenRoute = serializableFormWithFields.routes?.find((route) => route.id === chosenRouteId);
|
||||
let teamMemberIdsMatchingAttributeLogic: number[] | null = null;
|
||||
let crmContactOwnerEmail: string | null = null;
|
||||
@@ -185,25 +152,29 @@ const _handleResponse = async ({
|
||||
} else {
|
||||
// It currently happens for a Router route. Such a route id isn't present in the form.routes
|
||||
}
|
||||
|
||||
let dbFormResponse;
|
||||
let dbFormResponse, queuedFormResponse;
|
||||
if (!isPreview) {
|
||||
dbFormResponse = await prisma.app_RoutingForms_FormResponse.create({
|
||||
data: {
|
||||
// TODO: Why do we not save formFillerId available in the input?
|
||||
// formFillerId,
|
||||
if (queueFormResponse) {
|
||||
queuedFormResponse = await RoutingFormResponseRepository.recordQueuedFormResponse({
|
||||
formId: form.id,
|
||||
response: response,
|
||||
response,
|
||||
chosenRouteId,
|
||||
},
|
||||
});
|
||||
});
|
||||
dbFormResponse = null;
|
||||
} else {
|
||||
dbFormResponse = await RoutingFormResponseRepository.recordFormResponse({
|
||||
formId: form.id,
|
||||
response,
|
||||
chosenRouteId,
|
||||
});
|
||||
queuedFormResponse = null;
|
||||
|
||||
await onFormSubmission(
|
||||
{ ...serializableFormWithFields, userWithEmails },
|
||||
dbFormResponse.response as FormResponse,
|
||||
dbFormResponse.id,
|
||||
chosenRoute ? ("action" in chosenRoute ? chosenRoute.action : undefined) : undefined
|
||||
);
|
||||
await onSubmissionOfFormResponse({
|
||||
form: serializableFormWithFields,
|
||||
formResponseInDb: dbFormResponse,
|
||||
chosenRouteAction: chosenRoute ? ("action" in chosenRoute ? chosenRoute.action : null) : null,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
moduleLogger.debug("Dry run mode - Form response not stored and also webhooks and emails not sent");
|
||||
// Create a mock response for dry run
|
||||
@@ -216,10 +187,10 @@ const _handleResponse = async ({
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
isPreview: !!isPreview,
|
||||
formResponse: dbFormResponse,
|
||||
queuedFormResponse,
|
||||
teamMembersMatchingAttributeLogic: teamMemberIdsMatchingAttributeLogic,
|
||||
crmContactOwnerEmail,
|
||||
crmContactOwnerRecordType,
|
||||
|
||||
@@ -101,6 +101,7 @@ function RoutingForm({ form, profile, ...restProps }: Props) {
|
||||
const {
|
||||
teamMembersMatchingAttributeLogic,
|
||||
formResponse,
|
||||
queuedFormResponse,
|
||||
attributeRoutingConfig,
|
||||
crmContactOwnerEmail,
|
||||
crmContactOwnerRecordType,
|
||||
@@ -116,7 +117,8 @@ function RoutingForm({ form, profile, ...restProps }: Props) {
|
||||
}
|
||||
const allURLSearchParams = getUrlSearchParamsToForward({
|
||||
formResponse: chosenRouteWithFormResponse.response,
|
||||
formResponseId: formResponse.id,
|
||||
formResponseId: formResponse?.id ?? null,
|
||||
queuedFormResponseId: queuedFormResponse?.id ?? null,
|
||||
fields,
|
||||
searchParams: new URLSearchParams(window.location.search),
|
||||
teamMembersMatchingAttributeLogic,
|
||||
|
||||
+7
@@ -66,6 +66,7 @@ describe("getUrlSearchParamsToForward", () => {
|
||||
searchParams,
|
||||
teamMembersMatchingAttributeLogic: null,
|
||||
formResponseId: 1,
|
||||
queuedFormResponseId: null,
|
||||
attributeRoutingConfig: null,
|
||||
});
|
||||
expect(fromEntriesWithDuplicateKeys(result.entries())).toEqual(expectedParams);
|
||||
@@ -100,6 +101,7 @@ describe("getUrlSearchParamsToForward", () => {
|
||||
searchParams,
|
||||
teamMembersMatchingAttributeLogic: null,
|
||||
formResponseId: 1,
|
||||
queuedFormResponseId: null,
|
||||
attributeRoutingConfig: null,
|
||||
});
|
||||
expect(fromEntriesWithDuplicateKeys(result.entries())).toEqual(expectedParams);
|
||||
@@ -151,6 +153,7 @@ describe("getUrlSearchParamsToForward", () => {
|
||||
searchParams,
|
||||
teamMembersMatchingAttributeLogic: null,
|
||||
formResponseId: 1,
|
||||
queuedFormResponseId: null,
|
||||
attributeRoutingConfig: null,
|
||||
});
|
||||
expect(fromEntriesWithDuplicateKeys(result.entries())).toEqual(expectedParams);
|
||||
@@ -202,6 +205,7 @@ describe("getUrlSearchParamsToForward", () => {
|
||||
searchParams,
|
||||
teamMembersMatchingAttributeLogic: null,
|
||||
formResponseId: 1,
|
||||
queuedFormResponseId: null,
|
||||
attributeRoutingConfig: null,
|
||||
});
|
||||
expect(fromEntriesWithDuplicateKeys(result.entries())).toEqual(expectedParams);
|
||||
@@ -229,6 +233,7 @@ describe("getUrlSearchParamsToForward", () => {
|
||||
searchParams,
|
||||
teamMembersMatchingAttributeLogic: null,
|
||||
formResponseId: 1,
|
||||
queuedFormResponseId: null,
|
||||
attributeRoutingConfig: null,
|
||||
});
|
||||
expect(fromEntriesWithDuplicateKeys(result.entries())).toEqual(expectedParams);
|
||||
@@ -272,6 +277,7 @@ describe("getUrlSearchParamsToForward", () => {
|
||||
searchParams,
|
||||
teamMembersMatchingAttributeLogic: null,
|
||||
formResponseId: 1,
|
||||
queuedFormResponseId: null,
|
||||
attributeRoutingConfig: {
|
||||
skipContactOwner: true,
|
||||
},
|
||||
@@ -317,6 +323,7 @@ describe("getUrlSearchParamsToForward", () => {
|
||||
searchParams,
|
||||
teamMembersMatchingAttributeLogic: [1, 2],
|
||||
formResponseId: 1,
|
||||
queuedFormResponseId: null,
|
||||
attributeRoutingConfig: null,
|
||||
});
|
||||
expect(fromEntriesWithDuplicateKeys(result.entries())).toEqual(expectedParams);
|
||||
|
||||
@@ -21,6 +21,7 @@ type GetUrlSearchParamsToForwardOptions = {
|
||||
>[];
|
||||
searchParams: URLSearchParams;
|
||||
formResponseId: number | null;
|
||||
queuedFormResponseId: string | null;
|
||||
teamMembersMatchingAttributeLogic: number[] | null;
|
||||
attributeRoutingConfig: AttributeRoutingConfig | null;
|
||||
reroutingFormResponses?: FormResponseValueOnly;
|
||||
@@ -37,6 +38,7 @@ export function getUrlSearchParamsToForward({
|
||||
searchParams,
|
||||
teamMembersMatchingAttributeLogic,
|
||||
formResponseId,
|
||||
queuedFormResponseId,
|
||||
attributeRoutingConfig,
|
||||
reroutingFormResponses,
|
||||
teamId,
|
||||
@@ -122,7 +124,8 @@ export function getUrlSearchParamsToForward({
|
||||
...(teamMembersMatchingAttributeLogic
|
||||
? { ["cal.routedTeamMemberIds"]: teamMembersMatchingAttributeLogic.join(",") }
|
||||
: null),
|
||||
[ROUTING_FORM_RESPONSE_ID_QUERY_STRING]: String(formResponseId),
|
||||
...(formResponseId ? { [ROUTING_FORM_RESPONSE_ID_QUERY_STRING]: String(formResponseId) } : null),
|
||||
...(queuedFormResponseId ? { ["cal.queuedFormResponseId"]: queuedFormResponseId } : null),
|
||||
...attributeRoutingConfigParams,
|
||||
...(reroutingFormResponses
|
||||
? { ["cal.reroutingFormResponses"]: JSON.stringify(reroutingFormResponses) }
|
||||
@@ -151,7 +154,7 @@ export function getUrlSearchParamsToForwardForReroute({
|
||||
attributeRoutingConfig,
|
||||
rescheduleUid,
|
||||
reroutingFormResponses,
|
||||
}: GetUrlSearchParamsToForwardOptions & {
|
||||
}: Omit<GetUrlSearchParamsToForwardOptions, "queuedFormResponseId"> & {
|
||||
rescheduleUid: string;
|
||||
reroutingFormResponses: FormResponseValueOnly;
|
||||
}) {
|
||||
@@ -160,6 +163,8 @@ export function getUrlSearchParamsToForwardForReroute({
|
||||
return getUrlSearchParamsToForward({
|
||||
formResponse,
|
||||
formResponseId,
|
||||
// Queued form response id is not available in rerouting
|
||||
queuedFormResponseId: null,
|
||||
fields,
|
||||
searchParams,
|
||||
teamMembersMatchingAttributeLogic,
|
||||
@@ -187,6 +192,8 @@ export function getUrlSearchParamsToForwardForTestPreview({
|
||||
teamMembersMatchingAttributeLogic,
|
||||
// There is no form response being stored in test preview
|
||||
formResponseId: null,
|
||||
// Queued form response id is not available in test preview
|
||||
queuedFormResponseId: null,
|
||||
searchParams,
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user