Files
calendar/packages/app-store/routing-forms/lib/substituteVariables.ts
T
Hariom BalharaGitHubDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>hariom@cal.com <hariom@cal.com>
bbcd0b7e78 test: add unit tests to various important fns in routing flow (#21902)
* Add unit tests to routing flow

* fix: resolve TypeScript errors in routing forms test files

- Fix mock form structure to match TargetRoutingFormForResponse type
- Add missing properties like selectText, deleted, updatedById to form fields
- Correct route action types to use valid RouteActionType enum values
- Fix response objects to include required label property
- Update mock return values for findTeamMembersMatchingAttributeLogic
- Use proper type assertions for complex route objects

Co-Authored-By: hariom@cal.com <hariom@cal.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: hariom@cal.com <hariom@cal.com>
2025-06-18 14:14:14 +00:00

40 lines
1.3 KiB
TypeScript

import slugify from "@calcom/lib/slugify";
import type { FormResponse, NonRouterRoute, Field } from "../types/types";
import getFieldIdentifier from "./getFieldIdentifier";
/**
* Substitues variables in the target URL identified by routeValue with values from response
* e.g. {firstName} is replaced with value of the field with identifier firstName
*
* @param routeValue - The target URL with variables to be substituted
* @param response - The form response containing the values to be substituted
* @param fields - The fields of the form
* @returns The URL with variables substituted
*/
export const substituteVariables = (
routeValue: NonRouterRoute["action"]["value"],
response: FormResponse,
fields: Field[]
) => {
const regex = /\{([^\}]+)\}/g;
const variables: string[] = routeValue.match(regex)?.map((match: string) => match.slice(1, -1)) || [];
let eventTypeUrl = routeValue;
variables.forEach((variable) => {
for (const key in response) {
const field = fields.find((field) => field.id === key);
if (!field) {
continue;
}
const identifier = getFieldIdentifier(field);
if (identifier.toLowerCase() === variable.toLowerCase()) {
eventTypeUrl = eventTypeUrl.replace(`{${variable}}`, slugify(response[key].value.toString() || ""));
}
}
});
return eventTypeUrl;
};