fix: Custom Event Redirect in Routing Form with Dropdown Based fields (#17870)

Co-authored-by: [email protected] <[email protected]>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
Hariom BalharaandDevin AI authored and GitHub committed 2025-08-25 13:46:18 -04:00
1 parent 8d2a072645
commit 13cbb59e5e
8 files changed
+599 -73

No files matched your search

@@ -0,0 +1,205 @@
import { describe, it, expect } from "vitest";
import type { Field, FormResponse } from "@calcom/app-store/routing-forms/types/types";
import { getHumanReadableFieldResponseValue } from "./getHumanReadableFieldResponseValue";
describe("getHumanReadableFieldResponseValue", () => {
const createSelectField = (
options: Array<{ id: string; label: string }>
): Pick<Field, "type" | "options"> => ({
type: "select",
options,
});
const createMultiselectField = (
options: Array<{ id: string; label: string }>
): Pick<Field, "type" | "options"> => ({
type: "multiselect",
options,
});
const createRadioField = (
options: Array<{ id: string; label: string }>
): Pick<Field, "type" | "options"> => ({
type: "radio",
options,
});
describe("select fields", () => {
it("should return option label for select field with single value", () => {
const field = createSelectField([
{ id: "opt-1", label: "Option One" },
{ id: "opt-2", label: "Option Two" },
]);
const value: FormResponse[string]["value"] = "opt-1";
const result = getHumanReadableFieldResponseValue({ field, value });
expect(result).toEqual(["Option One"]);
});
it("should handle select field when value is already a label (legacy)", () => {
const field = createSelectField([
{ id: "opt-1", label: "Option One" },
{ id: "opt-2", label: "Option Two" },
]);
const value: FormResponse[string]["value"] = "Option Two";
const result = getHumanReadableFieldResponseValue({ field, value });
expect(result).toEqual(["Option Two"]);
});
it("should return value as-is when option not found", () => {
const field = createSelectField([{ id: "opt-1", label: "Option One" }]);
const value: FormResponse[string]["value"] = "unknown-id";
const result = getHumanReadableFieldResponseValue({ field, value });
expect(result).toEqual(["unknown-id"]);
});
});
describe("multiselect fields", () => {
it("should return option labels for multiselect field with array value", () => {
const field = createMultiselectField([
{ id: "opt-1", label: "Option One" },
{ id: "opt-2", label: "Option Two" },
{ id: "opt-3", label: "Option Three" },
]);
const value: FormResponse[string]["value"] = ["opt-1", "opt-3"];
const result = getHumanReadableFieldResponseValue({ field, value });
expect(result).toEqual(["Option One", "Option Three"]);
});
it("should support legacy options with used to have labels directly", () => {
const field = createMultiselectField([
{ id: "opt-1", label: "Option One" },
{ id: "opt-2", label: "Option Two" },
]);
const value: FormResponse[string]["value"] = ["Option One", "Option Two", "unknown"];
const result = getHumanReadableFieldResponseValue({ field, value });
expect(result).toEqual(["Option One", "Option Two", "unknown"]);
});
it("should handle single value for multiselect (edge case)", () => {
const field = createMultiselectField([{ id: "opt-1", label: "Option One" }]);
const value: FormResponse[string]["value"] = "opt-1";
const result = getHumanReadableFieldResponseValue({ field, value });
expect(result).toEqual(["Option One"]);
});
});
describe("radio fields", () => {
it("should return option label for radio field", () => {
const field = createRadioField([
{ id: "yes", label: "Yes" },
{ id: "no", label: "No" },
]);
const value: FormResponse[string]["value"] = "yes";
const result = getHumanReadableFieldResponseValue({ field, value });
expect(result).toEqual(["Yes"]);
});
});
describe("text and other fields", () => {
it("should return value as string for text field", () => {
const field: Pick<Field, "type" | "options"> = {
type: "text",
};
const value: FormResponse[string]["value"] = "John Doe";
const result = getHumanReadableFieldResponseValue({ field, value });
expect(result).toEqual("John Doe");
});
it("should handle number field", () => {
const field: Pick<Field, "type" | "options"> = {
type: "number",
};
const value: FormResponse[string]["value"] = 42;
const result = getHumanReadableFieldResponseValue({ field, value });
expect(result).toEqual(["42"]);
});
it("should handle textarea field", () => {
const field: Pick<Field, "type" | "options"> = {
type: "textarea",
};
const value: FormResponse[string]["value"] = "Multi\nline\ntext";
const result = getHumanReadableFieldResponseValue({ field, value });
expect(result).toEqual("Multi\nline\ntext");
});
it("should handle array values for non-option fields", () => {
const field: Pick<Field, "type" | "options"> = {
type: "text",
};
const value: FormResponse[string]["value"] = ["value1", "value2"];
const result = getHumanReadableFieldResponseValue({ field, value });
expect(result).toEqual(["value1", "value2"]);
});
});
describe("edge cases", () => {
it("should handle numeric values in options", () => {
const field = createSelectField([
{ id: "1", label: "First" },
{ id: "2", label: "Second" },
]);
const value: FormResponse[string]["value"] = 1;
const result = getHumanReadableFieldResponseValue({ field, value });
expect(result).toEqual(["First"]);
});
it("should handle empty string value", () => {
const field: Pick<Field, "type" | "options"> = {
type: "text",
};
const value: FormResponse[string]["value"] = "";
const result = getHumanReadableFieldResponseValue({ field, value });
expect(result).toEqual("");
});
it("should handle field with empty options array", () => {
const field = createSelectField([]);
const value: FormResponse[string]["value"] = "some-value";
const result = getHumanReadableFieldResponseValue({ field, value });
expect(result).toEqual(["some-value"]);
});
it("should handle field with undefined options", () => {
const field: Pick<Field, "type" | "options"> = {
type: "select",
options: undefined,
};
const value: FormResponse[string]["value"] = "some-value";
const result = getHumanReadableFieldResponseValue({ field, value });
expect(result).toEqual("some-value");
});
});
});
@@ -0,0 +1,79 @@
import type { Field, FormResponse } from "@calcom/app-store/routing-forms/types/types";
/**
* Ensures a value is either a string or string array.
* Converts numbers to strings.
*
* @param value - The value to normalize
* @returns String or string array
*/
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()];
}
/**
* Get labels from option IDs for fields with options (select, multiselect, radio).
* Handles legacy cases where the value might already be a label.
*
* @param options - The field options array
* @param optionIds - The option ID(s) to get labels for
* @returns The label(s) corresponding to the option ID(s)
*/
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];
}
}
/**
* Get the human-readable value for a field response.
* For select/multiselect/radio fields, this returns the label of the selected option(s).
* For other fields, it returns the value as-is.
*
* @param field - The field definition with options
* @param value - The response value (typically an option ID for select fields)
* @returns Array of human-readable values
*/
export function getHumanReadableFieldResponseValue({
field,
value,
}: {
field: Pick<Field, "type" | "options">;
value: FormResponse[string]["value"];
}): string | string[] {
// For fields with options (select, multiselect, radio), use the existing getLabelsFromOptionIds function
if (field.options) {
const optionIds = ensureStringOrStringArray(value);
return getLabelsFromOptionIds({ options: field.options, optionIds });
}
return ensureStringOrStringArray(value);
}