fix: Invalid options for Select and MultiSelect when both legacy and new options(i.e. with and without id are present) (#16509)
* Fix invalid select options * Add test for multiselect filling and fix bug with csv reporting * Fix options getting duplicated * Add getQueryBuilderConfig tests
This commit is contained in:
1 parent
6e54c459a4
commit
ef0ba48f49
14 files changed
+439
-134
No files matched your search
@@ -319,10 +319,14 @@ export const createUsersFixture = (
|
||||
}
|
||||
|
||||
if (scenario.seedRoutingForms) {
|
||||
const option2Uuid = "d1302635-9f12-17b1-9153-c3a854649182";
|
||||
const option1Uuid = "d1292635-9f12-17b1-9153-c3a854649182";
|
||||
const multiSelectOption2Uuid = "d1302635-9f12-17b1-9153-c3a854649182";
|
||||
const multiSelectOption1Uuid = "d1292635-9f12-17b1-9153-c3a854649182";
|
||||
const selectOption1Uuid = "d0292635-9f12-17b1-9153-c3a854649182";
|
||||
const selectOption2Uuid = "d0302635-9f12-17b1-9153-c3a854649182";
|
||||
const multiSelectLegacyFieldUuid = "d4292635-9f12-17b1-9153-c3a854649182";
|
||||
const multiSelectFieldUuid = "d9892635-9f12-17b1-9153-c3a854649182";
|
||||
const selectFieldUuid = "d1302635-9f12-17b1-9153-c3a854649182";
|
||||
const legacySelectFieldUuid = "f0292635-9f12-17b1-9153-c3a854649182";
|
||||
await prisma.app_RoutingForms_Form.create({
|
||||
data: {
|
||||
routes: [
|
||||
@@ -417,7 +421,7 @@ export const createUsersFixture = (
|
||||
type: "rule",
|
||||
properties: {
|
||||
field: multiSelectFieldUuid,
|
||||
value: [[option2Uuid]],
|
||||
value: [[multiSelectOption2Uuid]],
|
||||
operator: "multiselect_equals",
|
||||
valueSrc: ["value"],
|
||||
valueType: ["multiselect"],
|
||||
@@ -455,11 +459,36 @@ export const createUsersFixture = (
|
||||
identifier: "multi-new-format",
|
||||
options: [
|
||||
{
|
||||
id: option1Uuid,
|
||||
id: multiSelectOption1Uuid,
|
||||
label: "Option-1",
|
||||
},
|
||||
{
|
||||
id: option2Uuid,
|
||||
id: multiSelectOption2Uuid,
|
||||
label: "Option-2",
|
||||
},
|
||||
],
|
||||
required: false,
|
||||
},
|
||||
{
|
||||
id: legacySelectFieldUuid,
|
||||
type: "select",
|
||||
label: "Legacy Select",
|
||||
identifier: "test-select",
|
||||
selectText: "Option-1\nOption-2",
|
||||
required: false,
|
||||
},
|
||||
{
|
||||
id: selectFieldUuid,
|
||||
type: "select",
|
||||
label: "Select",
|
||||
identifier: "test-select-new-format",
|
||||
options: [
|
||||
{
|
||||
id: selectOption1Uuid,
|
||||
label: "Option-1",
|
||||
},
|
||||
{
|
||||
id: selectOption2Uuid,
|
||||
label: "Option-2",
|
||||
},
|
||||
],
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import type { Page } from "@playwright/test";
|
||||
|
||||
export const selectInteractions = {
|
||||
async chooseOption({
|
||||
page,
|
||||
selector,
|
||||
optionText,
|
||||
}: {
|
||||
page: Page;
|
||||
selector: string;
|
||||
optionText: string;
|
||||
}): Promise<void> {
|
||||
// Open dropdown
|
||||
await page.locator(selector).click();
|
||||
const selectParentWithOptions = page.locator(`:has(> ${selector})`);
|
||||
// Select option
|
||||
await selectParentWithOptions.getByText(optionText, { exact: true }).click();
|
||||
},
|
||||
};
|
||||
@@ -6,15 +6,30 @@ import { entityPrismaWhereClause, canEditEntity } from "@calcom/lib/entityPermis
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { getSerializableForm } from "../../lib/getSerializableForm";
|
||||
import { ensureStringOrStringArray, getLabelsFromOptionIds } from "../../lib/reportingUtils";
|
||||
import type { FormResponse, SerializableForm } from "../../types/types";
|
||||
|
||||
type Fields = NonNullable<SerializableForm<App_RoutingForms_Form>["fields"]>;
|
||||
function escapeCsvText(str: string) {
|
||||
return str.replace(/,/, "%2C");
|
||||
}
|
||||
async function* getResponses(
|
||||
formId: string,
|
||||
headerFields: NonNullable<SerializableForm<App_RoutingForms_Form>["fields"]>
|
||||
) {
|
||||
|
||||
function getHumanReadableFieldResponseValue({
|
||||
field,
|
||||
value,
|
||||
}: {
|
||||
field: Fields[number];
|
||||
value: string | number | string[];
|
||||
}) {
|
||||
if (field.options) {
|
||||
const optionIds = ensureStringOrStringArray(value);
|
||||
return getLabelsFromOptionIds({ options: field.options, optionIds });
|
||||
} else {
|
||||
return (value instanceof Array ? value : [value]).map(String);
|
||||
}
|
||||
}
|
||||
|
||||
async function* getResponses(formId: string, fields: Fields) {
|
||||
let responses;
|
||||
let skip = 0;
|
||||
// Keep it small enough to be in Vercel limits of Serverless Function in terms of memory.
|
||||
@@ -34,16 +49,12 @@ async function* getResponses(
|
||||
responses.forEach((response) => {
|
||||
const fieldResponses = response.response as FormResponse;
|
||||
const csvCells: string[] = [];
|
||||
headerFields.forEach((headerField) => {
|
||||
const fieldResponse = fieldResponses[headerField.id];
|
||||
fields.forEach((field) => {
|
||||
const fieldResponse = fieldResponses[field.id];
|
||||
const value = fieldResponse?.value || "";
|
||||
let serializedValue = "";
|
||||
if (value instanceof Array) {
|
||||
serializedValue = value.map((val) => escapeCsvText(val)).join(" | ");
|
||||
} else {
|
||||
// value can be a number as well for type Number field
|
||||
serializedValue = escapeCsvText(String(value));
|
||||
}
|
||||
const serializedValue = getHumanReadableFieldResponseValue({ field, value })
|
||||
.map((value) => escapeCsvText(value))
|
||||
.join(" | ");
|
||||
csvCells.push(serializedValue);
|
||||
});
|
||||
csvCells.push(response.createdAt.toISOString());
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
export const FieldTypes = [
|
||||
{
|
||||
label: "Short Text",
|
||||
value: "text",
|
||||
},
|
||||
{
|
||||
label: "Number",
|
||||
value: "number",
|
||||
},
|
||||
{
|
||||
label: "Long Text",
|
||||
value: "textarea",
|
||||
},
|
||||
{
|
||||
label: "Single Selection",
|
||||
value: "select",
|
||||
},
|
||||
{
|
||||
label: "Multiple Selection",
|
||||
value: "multiselect",
|
||||
},
|
||||
{
|
||||
label: "Phone",
|
||||
value: "phone",
|
||||
},
|
||||
{
|
||||
label: "Email",
|
||||
value: "email",
|
||||
},
|
||||
] as const;
|
||||
@@ -0,0 +1,164 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { vi } from "vitest";
|
||||
|
||||
import type { RoutingForm } from "../../types/types";
|
||||
import { InitialConfig } from "../InitialConfig";
|
||||
import { getQueryBuilderConfig } from "../getQueryBuilderConfig";
|
||||
|
||||
type MockedForm = Pick<RoutingForm, "fields">;
|
||||
vi.mock("../InitialConfig", () => ({
|
||||
InitialConfig: {
|
||||
widgets: {
|
||||
text: { type: "text" },
|
||||
select: { type: "select" },
|
||||
multiselect: { type: "multiselect" },
|
||||
},
|
||||
operators: {
|
||||
is_empty: {},
|
||||
is_not_empty: {},
|
||||
between: {},
|
||||
not_between: {},
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
describe("getQueryBuilderConfig", () => {
|
||||
const mockForm: MockedForm = {
|
||||
fields: [
|
||||
{
|
||||
id: "field1",
|
||||
label: "Text Field",
|
||||
type: "text",
|
||||
},
|
||||
{
|
||||
id: "field2",
|
||||
label: "Select Field",
|
||||
type: "select",
|
||||
selectText: "Option 1\nOption 2",
|
||||
},
|
||||
{
|
||||
id: "field3",
|
||||
label: "MultiSelect Field",
|
||||
type: "multiselect",
|
||||
selectText: "Option A\nOption B\nOption C",
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
it("should generate correct config for all field types", () => {
|
||||
const config = getQueryBuilderConfig(mockForm);
|
||||
|
||||
expect(config.fields).toHaveProperty("field1");
|
||||
expect(config.fields).toHaveProperty("field2");
|
||||
expect(config.fields).toHaveProperty("field3");
|
||||
|
||||
expect(config.fields.field1).toEqual({
|
||||
label: "Text Field",
|
||||
type: "text",
|
||||
valueSources: ["value"],
|
||||
fieldSettings: {},
|
||||
});
|
||||
|
||||
expect(config.fields.field2).toEqual({
|
||||
label: "Select Field",
|
||||
type: "select",
|
||||
valueSources: ["value"],
|
||||
fieldSettings: {
|
||||
listValues: [
|
||||
{ value: "Option 1", title: "Option 1" },
|
||||
{ value: "Option 2", title: "Option 2" },
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
expect(config.fields.field3).toEqual({
|
||||
label: "MultiSelect Field",
|
||||
type: "multiselect",
|
||||
valueSources: ["value"],
|
||||
fieldSettings: {
|
||||
listValues: [
|
||||
{ value: "Option A", title: "Option A" },
|
||||
{ value: "Option B", title: "Option B" },
|
||||
{ value: "Option C", title: "Option C" },
|
||||
],
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle router fields correctly", () => {
|
||||
const formWithRouterField: MockedForm = {
|
||||
...mockForm,
|
||||
fields: [
|
||||
{
|
||||
id: "routerField",
|
||||
type: "router",
|
||||
label: "Router Field",
|
||||
routerId: "innerField",
|
||||
routerField: {
|
||||
id: "innerField",
|
||||
label: "Router Field",
|
||||
type: "text",
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const config = getQueryBuilderConfig(formWithRouterField);
|
||||
|
||||
expect(config.fields).toHaveProperty("innerField");
|
||||
expect(config.fields.innerField).toEqual({
|
||||
label: "Router Field",
|
||||
type: "text",
|
||||
valueSources: ["value"],
|
||||
fieldSettings: {},
|
||||
});
|
||||
});
|
||||
|
||||
it("should throw an error for unsupported field types", () => {
|
||||
const formWithUnsupportedField: MockedForm = {
|
||||
...mockForm,
|
||||
fields: [
|
||||
{
|
||||
id: "unsupportedField",
|
||||
label: "Unsupported Field",
|
||||
type: "unsupported" as any,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
expect(() => getQueryBuilderConfig(formWithUnsupportedField)).toThrow(
|
||||
"Unsupported field type:unsupported"
|
||||
);
|
||||
});
|
||||
|
||||
it("should remove specific operators when forReporting is true", () => {
|
||||
const config = getQueryBuilderConfig(mockForm, true);
|
||||
|
||||
expect(config.operators).not.toHaveProperty("is_empty");
|
||||
expect(config.operators).not.toHaveProperty("is_not_empty");
|
||||
expect(config.operators).not.toHaveProperty("between");
|
||||
expect(config.operators).not.toHaveProperty("not_between");
|
||||
expect(config.operators.__calReporting).toBe(true);
|
||||
});
|
||||
|
||||
it("should include all operators when forReporting is false", () => {
|
||||
const config = getQueryBuilderConfig(mockForm, false);
|
||||
|
||||
expect(config.operators).toHaveProperty("is_empty");
|
||||
expect(config.operators).toHaveProperty("is_not_empty");
|
||||
expect(config.operators).toHaveProperty("between");
|
||||
expect(config.operators).toHaveProperty("not_between");
|
||||
expect(config.operators.__calReporting).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should use InitialConfig as base for the returned config", () => {
|
||||
const config = getQueryBuilderConfig(mockForm);
|
||||
|
||||
expect(config).toEqual(
|
||||
expect.objectContaining({
|
||||
...InitialConfig,
|
||||
fields: expect.any(Object),
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -1,9 +1,9 @@
|
||||
import { FieldTypes } from "../pages/form-edit/[...appPages]";
|
||||
import type { QueryBuilderUpdatedConfig, RoutingForm } from "../types/types";
|
||||
import { FieldTypes } from "./FieldTypes";
|
||||
import { InitialConfig } from "./InitialConfig";
|
||||
import { getUIOptionsForSelect } from "./selectOptions";
|
||||
|
||||
export function getQueryBuilderConfig(form: RoutingForm, forReporting = false) {
|
||||
export function getQueryBuilderConfig(form: Pick<RoutingForm, "fields">, forReporting = false) {
|
||||
const fields: Record<
|
||||
string,
|
||||
{
|
||||
@@ -22,10 +22,12 @@ export function getQueryBuilderConfig(form: RoutingForm, forReporting = false) {
|
||||
if ("routerField" in field) {
|
||||
field = field.routerField;
|
||||
}
|
||||
if (FieldTypes.map((f) => f.value).includes(field.type)) {
|
||||
// We can assert the type because otherwise we throw 'Unsupported field type' error
|
||||
const fieldType = field.type as (typeof FieldTypes)[number]["value"];
|
||||
if (FieldTypes.map((f) => f.value).includes(fieldType)) {
|
||||
const options = getUIOptionsForSelect(field);
|
||||
|
||||
const widget = InitialConfig.widgets[field.type];
|
||||
const widget = InitialConfig.widgets[fieldType];
|
||||
const widgetType = widget.type;
|
||||
|
||||
fields[field.id] = {
|
||||
@@ -33,9 +35,9 @@ export function getQueryBuilderConfig(form: RoutingForm, forReporting = false) {
|
||||
type: widgetType,
|
||||
valueSources: ["value"],
|
||||
fieldSettings: {
|
||||
listValues: options,
|
||||
// IMPORTANT: listValues must be undefined for non-select/multiselect fields otherwise RAQB doesn't like it. It ends up considering all the text values as per the listValues too which could be empty as well making all values invalid
|
||||
listValues: fieldType === "select" || fieldType === "multiselect" ? options : undefined,
|
||||
},
|
||||
// preferWidgets: field.type === "textarea" ? ["textarea"] : [],
|
||||
};
|
||||
} else {
|
||||
throw new Error(`Unsupported field type:${field.type}`);
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import type { z } from "zod";
|
||||
|
||||
import type { zodNonRouterField } from "../zod";
|
||||
|
||||
type Field = z.infer<typeof zodNonRouterField>;
|
||||
|
||||
export function ensureStringOrStringArray(value: string | number | (string | number)[]): string | string[] {
|
||||
if (typeof value === "string") {
|
||||
return value;
|
||||
} else if (value instanceof Array) {
|
||||
return value.map((v) => v.toString());
|
||||
}
|
||||
return [value.toString()];
|
||||
}
|
||||
|
||||
export function getLabelsFromOptionIds({
|
||||
options,
|
||||
optionIds,
|
||||
}: {
|
||||
options: NonNullable<Field["options"]>;
|
||||
optionIds: string | string[];
|
||||
}) {
|
||||
if (optionIds instanceof Array) {
|
||||
const labels = optionIds.map((optionId) => {
|
||||
const foundOption = options.find((option) => option.id === optionId);
|
||||
// It would mean that the optionId is actually a label which is why it isn't matching any option id.
|
||||
// Fallback to optionId(i.e. label) which was the case with legacy options
|
||||
if (!foundOption) {
|
||||
return optionId;
|
||||
}
|
||||
return foundOption.label;
|
||||
});
|
||||
return labels;
|
||||
} else {
|
||||
const foundOption = options.find((option) => option.id === optionIds);
|
||||
if (!foundOption) {
|
||||
return [optionIds];
|
||||
}
|
||||
return [foundOption.label];
|
||||
}
|
||||
}
|
||||
@@ -35,14 +35,28 @@ export const getFieldWithOptions = <T extends Field>(field: T) => {
|
||||
}
|
||||
return {
|
||||
...field,
|
||||
};
|
||||
} as typeof field & z.BRAND<"FIELD_WITH_OPTIONS">;
|
||||
};
|
||||
|
||||
export function areSelectOptionsInLegacyFormat(
|
||||
field: Pick<Field, "options"> & z.BRAND<"FIELD_WITH_OPTIONS">
|
||||
) {
|
||||
const options = field.options || [];
|
||||
return !!options.find((option) => !option.id);
|
||||
}
|
||||
|
||||
export function getUIOptionsForSelect(field: Field) {
|
||||
return getFieldWithOptions(field).options?.map((option) => {
|
||||
const fieldWithOptions = getFieldWithOptions(field);
|
||||
const options = fieldWithOptions.options || [];
|
||||
const areOptionsInLegacyFormat = areSelectOptionsInLegacyFormat(
|
||||
fieldWithOptions as typeof fieldWithOptions & z.BRAND<"FIELD_WITH_OPTIONS">
|
||||
);
|
||||
// Because for legacy saved options, routes must have labels in them instead of ids
|
||||
const shouldUseLabelAsValue = areOptionsInLegacyFormat;
|
||||
return options.map((option) => {
|
||||
// We prefer option.id as that doesn't change when we change the option text/label.
|
||||
// Fallback to option.label for fields saved in DB in old format which didn't have `options`
|
||||
const value = option.id ?? option.label;
|
||||
const value = shouldUseLabelAsValue ? option.label : option.id ?? option.label;
|
||||
return {
|
||||
value,
|
||||
title: option.label,
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import type { z } from "zod";
|
||||
|
||||
import type { Field, FormResponse } from "../types/types";
|
||||
import { areSelectOptionsInLegacyFormat } from "./selectOptions";
|
||||
|
||||
/**
|
||||
* It takes care of correctly transforming the input to label or id depending on various cases
|
||||
@@ -17,9 +20,10 @@ function transformSelectValue({
|
||||
if (!options) {
|
||||
return idOrLabel;
|
||||
}
|
||||
const areOptionsInLegacyFormat = !!options.find((option) => !option.id);
|
||||
// Because for legacy saved options, routes must have labels in them instead of ids
|
||||
const shouldUseLabelAsValue = areOptionsInLegacyFormat;
|
||||
const shouldUseLabelAsValue = areSelectOptionsInLegacyFormat(
|
||||
field as typeof field & z.BRAND<"FIELD_WITH_OPTIONS">
|
||||
);
|
||||
const foundOptionById = options.find((option) => option.id === idOrLabel);
|
||||
if (foundOptionById) {
|
||||
if (shouldUseLabelAsValue) {
|
||||
|
||||
@@ -27,6 +27,7 @@ import type { RoutingFormWithResponseCount } from "../../components/SingleForm";
|
||||
import SingleForm, {
|
||||
getServerSidePropsForSingleFormView as getServerSideProps,
|
||||
} from "../../components/SingleForm";
|
||||
import { FieldTypes } from "../../lib/FieldTypes";
|
||||
|
||||
export { getServerSideProps };
|
||||
type SelectOption = { label: string; id: string | null };
|
||||
@@ -50,37 +51,6 @@ const appendArray = <T,>({
|
||||
|
||||
const PASTE_OPTIONS_SEPARATOR_REGEX = /\n+/;
|
||||
|
||||
export const FieldTypes = [
|
||||
{
|
||||
label: "Short Text",
|
||||
value: "text",
|
||||
},
|
||||
{
|
||||
label: "Number",
|
||||
value: "number",
|
||||
},
|
||||
{
|
||||
label: "Long Text",
|
||||
value: "textarea",
|
||||
},
|
||||
{
|
||||
label: "Single Selection",
|
||||
value: "select",
|
||||
},
|
||||
{
|
||||
label: "Multiple Selection",
|
||||
value: "multiselect",
|
||||
},
|
||||
{
|
||||
label: "Phone",
|
||||
value: "phone",
|
||||
},
|
||||
{
|
||||
label: "Email",
|
||||
value: "email",
|
||||
},
|
||||
];
|
||||
|
||||
function Field({
|
||||
hookForm,
|
||||
hookFieldNamespace,
|
||||
@@ -298,7 +268,8 @@ function Field({
|
||||
<ul ref={animationRef}>
|
||||
{watchedOptions.map((option, index) => (
|
||||
<li
|
||||
key={`select-option-${option.id}`}
|
||||
// We can't use option.id here as it is undefined and would make keys non-unique causing duplicate items
|
||||
key={`select-option-${index}`}
|
||||
className="group mt-2 flex items-center gap-2"
|
||||
onPaste={(event: ClipboardEvent) =>
|
||||
handlePasteInOptionAtIndex({ event, optionIndex: index })
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import React, { useCallback, useRef, useState } from "react";
|
||||
import React, { useCallback, useRef, useState, useEffect } from "react";
|
||||
import type {
|
||||
BuilderProps,
|
||||
Config,
|
||||
@@ -166,7 +166,7 @@ const Reporter = ({ form }: { form: inferSSRProps<typeof getServerSideProps>["fo
|
||||
[]
|
||||
);
|
||||
return (
|
||||
<div className="cal-query-builder bg-default fixed inset-0 w-full overflow-scroll pt-12 ltr:mr-2 rtl:ml-2 sm:pt-0">
|
||||
<div className="cal-query-builder">
|
||||
<Query
|
||||
{...config}
|
||||
value={query.state.tree}
|
||||
@@ -184,13 +184,22 @@ export default function ReporterWrapper({
|
||||
form,
|
||||
appUrl,
|
||||
}: inferSSRProps<typeof getServerSideProps> & { appUrl: string }) {
|
||||
const [isClient, setIsClient] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
// It isn't possible to render Reporter without hydration errors if it is rendered on the server.
|
||||
// This is because the RAQB generates some dynamic ids on elements which change b/w client and server.
|
||||
// This is a workaround to render the Reporter on the client only.
|
||||
setIsClient(true);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<SingleForm
|
||||
form={form}
|
||||
appUrl={appUrl}
|
||||
Page={({ form }) => (
|
||||
<div className="route-config">
|
||||
<Reporter form={form} />
|
||||
<div className="route-config bg-default fixed inset-0 w-full overflow-scroll pt-12 ltr:mr-2 rtl:ml-2 sm:pt-0">
|
||||
{isClient && <Reporter form={form} />}
|
||||
</div>
|
||||
)}
|
||||
/>
|
||||
|
||||
@@ -3,6 +3,7 @@ import { expect } from "@playwright/test";
|
||||
|
||||
import type { Fixtures } from "@calcom/web/playwright/lib/fixtures";
|
||||
import { test } from "@calcom/web/playwright/lib/fixtures";
|
||||
import { selectInteractions } from "@calcom/web/playwright/lib/pageObject";
|
||||
import { gotoRoutingLink } from "@calcom/web/playwright/lib/testUtils";
|
||||
|
||||
import {
|
||||
@@ -20,6 +21,8 @@ function todo(title: string) {
|
||||
const Identifiers = {
|
||||
multi: "multi",
|
||||
multiNewFormat: "multi-new-format",
|
||||
select: "test-select",
|
||||
selectNewFormat: "test-select-new-format",
|
||||
};
|
||||
|
||||
test.describe("Routing Forms", () => {
|
||||
@@ -235,11 +238,10 @@ test.describe("Routing Forms", () => {
|
||||
await page.goto(`/routing-forms/reporting/${routingForm.id}`);
|
||||
|
||||
const headerEls = page.locator("[data-testid='reporting-header'] th");
|
||||
// Once the response is there, React would soon render it, so 500ms is enough
|
||||
// FIXME: Sometimes it takes more than 500ms, so added a timeout of 1000ms for now. There might be something wrong with rendering.
|
||||
await headerEls.first().waitFor({
|
||||
timeout: 1000,
|
||||
});
|
||||
|
||||
// Wait for the headers to be visible(will automaically wait for getting response from backend) along with it the rows are rendered.
|
||||
await headerEls.first().waitFor();
|
||||
|
||||
const numHeaderEls = await headerEls.count();
|
||||
const headers = [];
|
||||
for (let i = 0; i < numHeaderEls; i++) {
|
||||
@@ -259,11 +261,17 @@ test.describe("Routing Forms", () => {
|
||||
responses.push(rowResponses);
|
||||
}
|
||||
|
||||
expect(headers).toEqual(["Test field", "Multi Select(with Legacy `selectText`)", "Multi Select"]);
|
||||
expect(headers).toEqual([
|
||||
"Test field",
|
||||
"Multi Select(with Legacy `selectText`)",
|
||||
"Multi Select",
|
||||
"Legacy Select",
|
||||
"Select",
|
||||
]);
|
||||
expect(responses).toEqual([
|
||||
["event-routing", "", ""],
|
||||
["external-redirect", "", ""],
|
||||
["custom-page", "", ""],
|
||||
["event-routing", "Option-2", "Option-2", "Option-2", "Option-2"],
|
||||
["external-redirect", "Option-2", "Option-2", "Option-2", "Option-2"],
|
||||
["custom-page", "Option-2", "Option-2", "Option-2", "Option-2"],
|
||||
]);
|
||||
|
||||
await page.goto(`apps/routing-forms/route-builder/${routingForm.id}`);
|
||||
@@ -287,20 +295,26 @@ test.describe("Routing Forms", () => {
|
||||
const csvRows = csv.trim().split("\n");
|
||||
const csvHeaderRow = csvRows[0];
|
||||
expect(csvHeaderRow).toEqual(
|
||||
"Test field,Multi Select(with Legacy `selectText`),Multi Select,Submission Time"
|
||||
"Test field,Multi Select(with Legacy `selectText`),Multi Select,Legacy Select,Select,Submission Time"
|
||||
);
|
||||
|
||||
const firstResponseCells = csvRows[1].split(",");
|
||||
const secondResponseCells = csvRows[2].split(",");
|
||||
const thirdResponseCells = csvRows[3].split(",");
|
||||
|
||||
expect(firstResponseCells.slice(0, -1).join(",")).toEqual("event-routing,,");
|
||||
expect(firstResponseCells.slice(0, -1).join(",")).toEqual(
|
||||
"event-routing,Option-2,Option-2,Option-2,Option-2"
|
||||
);
|
||||
expect(new Date(firstResponseCells.at(-1) as string).getDay()).toEqual(new Date().getDay());
|
||||
|
||||
expect(secondResponseCells.slice(0, -1).join(",")).toEqual("external-redirect,,");
|
||||
expect(secondResponseCells.slice(0, -1).join(",")).toEqual(
|
||||
"external-redirect,Option-2,Option-2,Option-2,Option-2"
|
||||
);
|
||||
expect(new Date(secondResponseCells.at(-1) as string).getDay()).toEqual(new Date().getDay());
|
||||
|
||||
expect(thirdResponseCells.slice(0, -1).join(",")).toEqual("custom-page,,");
|
||||
expect(thirdResponseCells.slice(0, -1).join(",")).toEqual(
|
||||
"custom-page,Option-2,Option-2,Option-2,Option-2"
|
||||
);
|
||||
expect(new Date(thirdResponseCells.at(-1) as string).getDay()).toEqual(new Date().getDay());
|
||||
});
|
||||
|
||||
@@ -436,24 +450,57 @@ async function expectCurrentFormToHaveFields(
|
||||
|
||||
async function fillSeededForm(page: Page, routingFormId: string) {
|
||||
await gotoRoutingLink({ page, formId: routingFormId });
|
||||
await page.fill('[data-testid="form-field-Test field"]', "event-routing");
|
||||
page.click('button[type="submit"]');
|
||||
|
||||
await page.waitForURL((url) => {
|
||||
return url.pathname.endsWith("/pro/30min");
|
||||
});
|
||||
await (async function firstResponse() {
|
||||
await page.fill('[data-testid="form-field-Test field"]', "event-routing");
|
||||
await fillAllOptionsBasedFields();
|
||||
page.click('button[type="submit"]');
|
||||
|
||||
await page.waitForURL((url) => {
|
||||
return url.pathname.endsWith("/pro/30min");
|
||||
});
|
||||
})();
|
||||
|
||||
await gotoRoutingLink({ page, formId: routingFormId });
|
||||
await page.fill('[data-testid="form-field-Test field"]', "external-redirect");
|
||||
page.click('button[type="submit"]');
|
||||
await page.waitForURL((url) => {
|
||||
return url.hostname.includes("google.com");
|
||||
});
|
||||
await (async function secondResponse() {
|
||||
await page.fill('[data-testid="form-field-Test field"]', "external-redirect");
|
||||
await fillAllOptionsBasedFields();
|
||||
page.click('button[type="submit"]');
|
||||
await page.waitForURL((url) => {
|
||||
return url.hostname.includes("google.com");
|
||||
});
|
||||
})();
|
||||
|
||||
await gotoRoutingLink({ page, formId: routingFormId });
|
||||
await page.fill('[data-testid="form-field-Test field"]', "custom-page");
|
||||
await page.click('button[type="submit"]');
|
||||
await expect(page.locator("text=Custom Page Result")).toBeVisible();
|
||||
await (async function thirdResponse() {
|
||||
await page.fill('[data-testid="form-field-Test field"]', "custom-page");
|
||||
await fillAllOptionsBasedFields();
|
||||
page.click('button[type="submit"]');
|
||||
await expect(page.locator("text=Custom Page Result")).toBeVisible();
|
||||
})();
|
||||
|
||||
async function fillAllOptionsBasedFields() {
|
||||
await selectInteractions.chooseOption({
|
||||
selector: `[data-testid="form-field-${Identifiers.multiNewFormat}"]`,
|
||||
optionText: "Option-2",
|
||||
page,
|
||||
});
|
||||
await selectInteractions.chooseOption({
|
||||
selector: `[data-testid="form-field-${Identifiers.multi}"]`,
|
||||
optionText: "Option-2",
|
||||
page,
|
||||
});
|
||||
await selectInteractions.chooseOption({
|
||||
selector: `[data-testid="form-field-${Identifiers.selectNewFormat}"]`,
|
||||
optionText: "Option-2",
|
||||
page,
|
||||
});
|
||||
await selectInteractions.chooseOption({
|
||||
selector: `[data-testid="form-field-${Identifiers.select}"]`,
|
||||
optionText: "Option-2",
|
||||
page,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function addAllTypesOfFieldsAndSaveForm(
|
||||
|
||||
@@ -7,8 +7,9 @@ import { TRPCError } from "@calcom/trpc/server";
|
||||
|
||||
import { jsonLogicToPrisma } from "../jsonLogicToPrisma";
|
||||
import { getSerializableForm } from "../lib/getSerializableForm";
|
||||
import { ensureStringOrStringArray, getLabelsFromOptionIds } from "../lib/reportingUtils";
|
||||
import type { FormResponse } from "../types/types";
|
||||
import type { zodNonRouterField, zodFieldView } from "../zod";
|
||||
import type { zodFieldView } from "../zod";
|
||||
import type { TReportInputSchema } from "./report.schema";
|
||||
|
||||
interface ReportHandlerOptions {
|
||||
@@ -18,44 +19,6 @@ interface ReportHandlerOptions {
|
||||
input: TReportInputSchema;
|
||||
}
|
||||
|
||||
type Field = z.infer<typeof zodNonRouterField>;
|
||||
|
||||
function ensureStringOrStringArray(value: string | number | (string | number)[]): string | string[] {
|
||||
if (typeof value === "string") {
|
||||
return value;
|
||||
} else if (value instanceof Array) {
|
||||
return value.map((v) => v.toString());
|
||||
}
|
||||
return [value.toString()];
|
||||
}
|
||||
|
||||
function getLabelsFromOptionIds({
|
||||
options,
|
||||
optionIds,
|
||||
}: {
|
||||
options: NonNullable<Field["options"]>;
|
||||
optionIds: string | string[];
|
||||
}) {
|
||||
if (optionIds instanceof Array) {
|
||||
const labels = optionIds.map((optionId) => {
|
||||
const foundOption = options.find((option) => option.id === optionId);
|
||||
// It would mean that the optionId is actually a label which is why it isn't matching any option id.
|
||||
// Fallback to optionId(i.e. label) which was the case with legacy options
|
||||
if (!foundOption) {
|
||||
return optionId;
|
||||
}
|
||||
return foundOption.label;
|
||||
});
|
||||
return labels;
|
||||
} else {
|
||||
const foundOption = options.find((option) => option.id === optionIds);
|
||||
if (!foundOption) {
|
||||
return [optionIds];
|
||||
}
|
||||
return [foundOption.label];
|
||||
}
|
||||
}
|
||||
|
||||
export const reportHandler = async ({ ctx: { prisma }, input }: ReportHandlerOptions) => {
|
||||
// Can be any prisma `where` clause
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
|
||||
@@ -39,6 +39,7 @@ async function seedAppData() {
|
||||
return;
|
||||
}
|
||||
|
||||
const multiSelectLegacyFieldId = "d2292635-9f12-17b1-9153-c3a854649182";
|
||||
await prisma.app_RoutingForms_Form.create({
|
||||
data: {
|
||||
id: seededForm.id,
|
||||
@@ -113,7 +114,7 @@ async function seedAppData() {
|
||||
"b98a8abb-cdef-4012-b456-718262343d27": {
|
||||
type: "rule",
|
||||
properties: {
|
||||
field: "d4292635-9f12-17b1-9153-c3a854649182",
|
||||
field: multiSelectLegacyFieldId,
|
||||
value: [["Option-2"]],
|
||||
operator: "multiselect_equals",
|
||||
valueSrc: ["value"],
|
||||
@@ -131,9 +132,9 @@ async function seedAppData() {
|
||||
},
|
||||
],
|
||||
fields: [
|
||||
{ id: "c4296635-9f12-47b1-8153-c3a854649182", type: "text", label: "Test field", required: true },
|
||||
{ id: "c1296635-9f12-47b1-8153-c3a854649182", type: "text", label: "Test field", required: true },
|
||||
{
|
||||
id: "d4292635-9f12-17b1-9153-c3a854649182",
|
||||
id: multiSelectLegacyFieldId,
|
||||
type: "multiselect",
|
||||
label: "Multi Select(with legacy `selectText`)",
|
||||
identifier: "multi",
|
||||
@@ -141,7 +142,7 @@ async function seedAppData() {
|
||||
required: false,
|
||||
},
|
||||
{
|
||||
id: "d4292635-9f12-17b1-9153-c3a854649182",
|
||||
id: "d3292635-9f12-17b1-9153-c3a854649182",
|
||||
type: "multiselect",
|
||||
label: "Multi Select",
|
||||
identifier: "multi",
|
||||
|
||||
Reference in new issue
Block a user