refactor: split raqbUtils into server and client modules to fix RAQB import in server context (#26112)
* refactor: add server and client raqbUtils modules Add two new modules to split RAQB utilities: - raqbUtils.server.ts: Server-safe utilities without RAQB runtime - raqbUtils.client.ts: Client-only utilities requiring RAQB runtime This enables server-side code to import RAQB utilities without pulling in the react-awesome-query-builder runtime library. Co-Authored-By: Volnei Munhoz <[email protected]> * refactor: update imports to use server/client raqbUtils modules and fix lint warnings Co-Authored-By: Volnei Munhoz <[email protected]> * fix: fix type errors in getLuckyUser.ts and remove dead code in RouteBuilder.tsx Co-Authored-By: Volnei Munhoz <[email protected]> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
7 files changed
+292
-313
No files matched your search
@@ -0,0 +1,34 @@
|
||||
"use client";
|
||||
|
||||
/**
|
||||
* Client-only RAQB utilities that require the react-awesome-query-builder runtime.
|
||||
* These functions should only be imported in client-side React components.
|
||||
*/
|
||||
import type { JsonTree } from "react-awesome-query-builder";
|
||||
import type { Config } from "react-awesome-query-builder";
|
||||
import { Utils as QbUtils } from "react-awesome-query-builder";
|
||||
|
||||
export function buildEmptyQueryValue() {
|
||||
return { id: QbUtils.uuid(), type: "group" as const };
|
||||
}
|
||||
|
||||
export const buildStateFromQueryValue = ({
|
||||
queryValue,
|
||||
config,
|
||||
}: {
|
||||
/**
|
||||
* Allow null as the queryValue as initially there could be no queryValue and without that we can't build the state and can't show the UI
|
||||
*/
|
||||
queryValue: JsonTree | null;
|
||||
config: Config;
|
||||
}) => {
|
||||
const queryValueToUse = queryValue || buildEmptyQueryValue();
|
||||
const immutableTree = QbUtils.checkTree(QbUtils.loadTree(queryValueToUse), config);
|
||||
return {
|
||||
state: {
|
||||
tree: immutableTree,
|
||||
config,
|
||||
},
|
||||
queryValue: QbUtils.getTree(immutableTree),
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,201 @@
|
||||
/**
|
||||
* Server-safe RAQB utilities that don't require the react-awesome-query-builder runtime.
|
||||
* These functions can be safely imported in server-side code (cron jobs, API routes, etc.)
|
||||
* without pulling in the client-only RAQB library.
|
||||
*/
|
||||
import type { JsonGroup, JsonItem, JsonRule, JsonTree } from "react-awesome-query-builder";
|
||||
|
||||
import { getQueryBuilderConfigForAttributes } from "@calcom/app-store/routing-forms/lib/getQueryBuilderConfig";
|
||||
import type { LocalRoute } from "@calcom/app-store/routing-forms/types/types";
|
||||
import { resolveQueryValue } from "@calcom/lib/raqb/resolveQueryValue";
|
||||
import type { dynamicFieldValueOperands } from "@calcom/lib/raqb/types";
|
||||
import { caseInsensitive } from "@calcom/lib/raqb/utils";
|
||||
import { safeStringify } from "@calcom/lib/safeStringify";
|
||||
import type {
|
||||
AttributeOptionValueWithType,
|
||||
AttributeOptionValue,
|
||||
Attribute,
|
||||
} from "@calcom/lib/service/attribute/server/getAttributes";
|
||||
import { AttributeType } from "@calcom/prisma/enums";
|
||||
|
||||
function ensureArray(value: string | string[]) {
|
||||
return typeof value === "string" ? [value] : value;
|
||||
}
|
||||
|
||||
export const raqbQueryValueUtils = {
|
||||
isQueryValueARuleGroup: function isQueryValueARuleGroup(queryValue: JsonTree): queryValue is JsonGroup {
|
||||
return queryValue.type === "group";
|
||||
},
|
||||
isARule: function isARule(rule: JsonItem): rule is JsonRule {
|
||||
return rule.type === "rule";
|
||||
},
|
||||
getValueTypeFromAttributesQueryValueForRaqbField:
|
||||
function getValueTypeFromAttributesQueryValueForRaqbField({
|
||||
attributesQueryValue,
|
||||
raqbFieldId,
|
||||
}: {
|
||||
attributesQueryValue: JsonTree;
|
||||
raqbFieldId: string;
|
||||
}) {
|
||||
if (!raqbQueryValueUtils.isQueryValueARuleGroup(attributesQueryValue)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!attributesQueryValue.children1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let raqbFieldValueType = null;
|
||||
|
||||
Object.values(attributesQueryValue.children1).reduce((acc, rule) => {
|
||||
if (!raqbQueryValueUtils.isARule(rule)) {
|
||||
return acc;
|
||||
}
|
||||
const ruleProperties = rule?.properties;
|
||||
if (!ruleProperties) {
|
||||
return acc;
|
||||
}
|
||||
if (ruleProperties.field === raqbFieldId) {
|
||||
raqbFieldValueType = ruleProperties.valueType?.[0];
|
||||
}
|
||||
return acc;
|
||||
}, null);
|
||||
|
||||
return raqbFieldValueType;
|
||||
},
|
||||
isQueryValueEmpty: function isQueryValueEmpty(queryValue: JsonTree | null): queryValue is null {
|
||||
if (!queryValue) {
|
||||
return true;
|
||||
}
|
||||
return !queryValue.children1;
|
||||
},
|
||||
};
|
||||
|
||||
export function getValueOfAttributeOption(
|
||||
attributeOptions:
|
||||
| Pick<AttributeOptionValue, "isGroup" | "contains" | "value">
|
||||
| Pick<AttributeOptionValue, "isGroup" | "contains" | "value">[]
|
||||
) {
|
||||
if (!(attributeOptions instanceof Array)) {
|
||||
return transformAttributeOption(attributeOptions);
|
||||
}
|
||||
return attributeOptions
|
||||
.map(transformAttributeOption)
|
||||
.flat()
|
||||
.filter((value, index, self) => self.indexOf(value) === index);
|
||||
|
||||
function transformAttributeOption(
|
||||
attributeOption: Pick<AttributeOptionValue, "isGroup" | "contains" | "value">
|
||||
) {
|
||||
if (attributeOption.isGroup) {
|
||||
const subOptions = attributeOption.contains.map((option) => option.value);
|
||||
console.log("A group option found. Using all its sub-options instead", safeStringify(subOptions));
|
||||
return subOptions;
|
||||
}
|
||||
return attributeOption.value;
|
||||
}
|
||||
}
|
||||
|
||||
function getAttributesData({
|
||||
attributesData,
|
||||
}: {
|
||||
attributesData: Record<string, AttributeOptionValueWithType>;
|
||||
attributesQueryValue: NonNullable<LocalRoute["attributesQueryValue"]>;
|
||||
}) {
|
||||
return Object.entries(attributesData).reduce(
|
||||
(acc, [attributeId, { type: attributeType, attributeOption }]) => {
|
||||
const compatibleValueForAttributeAndFormFieldMatching = caseInsensitive(
|
||||
getValueOfAttributeOption(attributeOption)
|
||||
);
|
||||
|
||||
acc[attributeId] =
|
||||
// multiselect attribute's value must be an array as all the operators multiselect_some_in, multiselect_all_in and their respective not operators expect an array
|
||||
// If we add an operator that doesn't expect an array, we need to somehow make it operator based.
|
||||
attributeType === AttributeType.MULTI_SELECT
|
||||
? ensureArray(compatibleValueForAttributeAndFormFieldMatching)
|
||||
: compatibleValueForAttributeAndFormFieldMatching;
|
||||
|
||||
return acc;
|
||||
},
|
||||
{} as Record<string, string | string[]>
|
||||
);
|
||||
}
|
||||
/** Gets the attributes that were used to generate the chosen route and their values */
|
||||
function getAttributesQueryValue({
|
||||
attributesQueryValue,
|
||||
attributes,
|
||||
dynamicFieldValueOperands,
|
||||
}: {
|
||||
attributesQueryValue: LocalRoute["attributesQueryValue"] | null;
|
||||
attributes: Attribute[];
|
||||
dynamicFieldValueOperands?: dynamicFieldValueOperands;
|
||||
}) {
|
||||
if (!attributesQueryValue) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const resolvedQueryValue = resolveQueryValue({
|
||||
queryValue: attributesQueryValue,
|
||||
attributes,
|
||||
dynamicFieldValueOperands,
|
||||
});
|
||||
|
||||
return resolvedQueryValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns attributesQueryBuilderConfig with the list of labels instead of list of ids.
|
||||
*/
|
||||
export function getAttributesQueryBuilderConfigHavingListofLabels({
|
||||
dynamicFieldValueOperands,
|
||||
attributes,
|
||||
}: {
|
||||
dynamicFieldValueOperands?: dynamicFieldValueOperands;
|
||||
attributes: Attribute[];
|
||||
}) {
|
||||
const attributesQueryBuilderConfig = getQueryBuilderConfigForAttributes({
|
||||
attributes,
|
||||
dynamicOperandFields: dynamicFieldValueOperands?.fields,
|
||||
});
|
||||
|
||||
const attributesQueryBuilderConfigFieldsWithCompatibleListValues = Object.fromEntries(
|
||||
Object.entries(attributesQueryBuilderConfig.fields).map(([raqbFieldId, raqbField]) => {
|
||||
const raqbFieldType = raqbField.type;
|
||||
|
||||
return [
|
||||
raqbFieldId,
|
||||
{
|
||||
...raqbField,
|
||||
type: raqbFieldType,
|
||||
fieldSettings: {
|
||||
...raqbField.fieldSettings,
|
||||
listValues: raqbField.fieldSettings.listValues?.map((option) => {
|
||||
return {
|
||||
...option,
|
||||
// Use the title(which is the attributeOption.value) as the value of the raqb field so that it can be compatible for matching with the form field value
|
||||
value: caseInsensitive(option.title),
|
||||
};
|
||||
}),
|
||||
},
|
||||
},
|
||||
];
|
||||
})
|
||||
);
|
||||
|
||||
const attributesQueryBuilderConfigWithCompatibleListValues = {
|
||||
...attributesQueryBuilderConfig,
|
||||
fields: attributesQueryBuilderConfigFieldsWithCompatibleListValues,
|
||||
};
|
||||
|
||||
return attributesQueryBuilderConfigWithCompatibleListValues;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utilities to establish compatibility between formFieldQueryValue and attributeQueryValue
|
||||
*/
|
||||
export const acrossQueryValueCompatiblity = {
|
||||
getAttributesQueryBuilderConfigHavingListofLabels,
|
||||
getAttributesQueryValue,
|
||||
getAttributesData,
|
||||
resolveQueryValue,
|
||||
};
|
||||
@@ -1,223 +1,24 @@
|
||||
import type { JsonGroup, JsonItem, JsonRule, JsonTree } from "react-awesome-query-builder";
|
||||
import type { Config } from "react-awesome-query-builder";
|
||||
import { Utils as QbUtils } from "react-awesome-query-builder";
|
||||
|
||||
import { getQueryBuilderConfigForAttributes } from "@calcom/app-store/routing-forms/lib/getQueryBuilderConfig";
|
||||
import type { LocalRoute } from "@calcom/app-store/routing-forms/types/types";
|
||||
import { resolveQueryValue } from "@calcom/lib/raqb/resolveQueryValue";
|
||||
import type { dynamicFieldValueOperands } from "@calcom/lib/raqb/types";
|
||||
import { caseInsensitive } from "@calcom/lib/raqb/utils";
|
||||
import { safeStringify } from "@calcom/lib/safeStringify";
|
||||
import type {
|
||||
AttributeOptionValueWithType,
|
||||
AttributeOptionValue,
|
||||
Attribute,
|
||||
} from "@calcom/lib/service/attribute/server/getAttributes";
|
||||
import { AttributeType } from "@calcom/prisma/enums";
|
||||
|
||||
function ensureArray(value: string | string[]) {
|
||||
return typeof value === "string" ? [value] : value;
|
||||
}
|
||||
|
||||
export const raqbQueryValueUtils = {
|
||||
isQueryValueARuleGroup: function isQueryValueARuleGroup(queryValue: JsonTree): queryValue is JsonGroup {
|
||||
return queryValue.type === "group";
|
||||
},
|
||||
isARule: function isARule(rule: JsonItem): rule is JsonRule {
|
||||
return rule.type === "rule";
|
||||
},
|
||||
getValueTypeFromAttributesQueryValueForRaqbField:
|
||||
function getValueTypeFromAttributesQueryValueForRaqbField({
|
||||
attributesQueryValue,
|
||||
raqbFieldId,
|
||||
}: {
|
||||
attributesQueryValue: JsonTree;
|
||||
raqbFieldId: string;
|
||||
}) {
|
||||
if (!raqbQueryValueUtils.isQueryValueARuleGroup(attributesQueryValue)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!attributesQueryValue.children1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let raqbFieldValueType = null;
|
||||
|
||||
Object.values(attributesQueryValue.children1).reduce((acc, rule) => {
|
||||
if (!raqbQueryValueUtils.isARule(rule)) {
|
||||
return acc;
|
||||
}
|
||||
const ruleProperties = rule?.properties;
|
||||
if (!ruleProperties) {
|
||||
return acc;
|
||||
}
|
||||
if (ruleProperties.field === raqbFieldId) {
|
||||
raqbFieldValueType = ruleProperties.valueType?.[0];
|
||||
}
|
||||
return acc;
|
||||
}, null);
|
||||
|
||||
return raqbFieldValueType;
|
||||
},
|
||||
isQueryValueEmpty: function isQueryValueEmpty(queryValue: JsonTree | null): queryValue is null {
|
||||
if (!queryValue) {
|
||||
return true;
|
||||
}
|
||||
return !queryValue.children1;
|
||||
},
|
||||
};
|
||||
|
||||
export function buildEmptyQueryValue() {
|
||||
return { id: QbUtils.uuid(), type: "group" as const };
|
||||
}
|
||||
|
||||
export const buildStateFromQueryValue = ({
|
||||
queryValue,
|
||||
config,
|
||||
}: {
|
||||
/**
|
||||
* Allow null as the queryValue as initially there could be no queryValue and without that we can't build the state and can't show the UI
|
||||
*/
|
||||
queryValue: JsonTree | null;
|
||||
config: Config;
|
||||
}) => {
|
||||
const queryValueToUse = queryValue || buildEmptyQueryValue();
|
||||
const immutableTree = QbUtils.checkTree(QbUtils.loadTree(queryValueToUse), config);
|
||||
return {
|
||||
state: {
|
||||
tree: immutableTree,
|
||||
config,
|
||||
},
|
||||
queryValue: QbUtils.getTree(immutableTree),
|
||||
};
|
||||
};
|
||||
|
||||
export function getValueOfAttributeOption(
|
||||
attributeOptions:
|
||||
| Pick<AttributeOptionValue, "isGroup" | "contains" | "value">
|
||||
| Pick<AttributeOptionValue, "isGroup" | "contains" | "value">[]
|
||||
) {
|
||||
if (!(attributeOptions instanceof Array)) {
|
||||
return transformAttributeOption(attributeOptions);
|
||||
}
|
||||
return attributeOptions
|
||||
.map(transformAttributeOption)
|
||||
.flat()
|
||||
.filter((value, index, self) => self.indexOf(value) === index);
|
||||
|
||||
function transformAttributeOption(
|
||||
attributeOption: Pick<AttributeOptionValue, "isGroup" | "contains" | "value">
|
||||
) {
|
||||
if (attributeOption.isGroup) {
|
||||
const subOptions = attributeOption.contains.map((option) => option.value);
|
||||
console.log("A group option found. Using all its sub-options instead", safeStringify(subOptions));
|
||||
return subOptions;
|
||||
}
|
||||
return attributeOption.value;
|
||||
}
|
||||
}
|
||||
|
||||
function getAttributesData({
|
||||
attributesData,
|
||||
}: {
|
||||
attributesData: Record<string, AttributeOptionValueWithType>;
|
||||
attributesQueryValue: NonNullable<LocalRoute["attributesQueryValue"]>;
|
||||
}) {
|
||||
return Object.entries(attributesData).reduce(
|
||||
(acc, [attributeId, { type: attributeType, attributeOption }]) => {
|
||||
const compatibleValueForAttributeAndFormFieldMatching = caseInsensitive(
|
||||
getValueOfAttributeOption(attributeOption)
|
||||
);
|
||||
|
||||
acc[attributeId] =
|
||||
// multiselect attribute's value must be an array as all the operators multiselect_some_in, multiselect_all_in and their respective not operators expect an array
|
||||
// If we add an operator that doesn't expect an array, we need to somehow make it operator based.
|
||||
attributeType === AttributeType.MULTI_SELECT
|
||||
? ensureArray(compatibleValueForAttributeAndFormFieldMatching)
|
||||
: compatibleValueForAttributeAndFormFieldMatching;
|
||||
|
||||
return acc;
|
||||
},
|
||||
{} as Record<string, string | string[]>
|
||||
);
|
||||
}
|
||||
/** Gets the attributes that were used to generate the chosen route and their values */
|
||||
function getAttributesQueryValue({
|
||||
attributesQueryValue,
|
||||
attributes,
|
||||
dynamicFieldValueOperands,
|
||||
}: {
|
||||
attributesQueryValue: LocalRoute["attributesQueryValue"] | null;
|
||||
attributes: Attribute[];
|
||||
dynamicFieldValueOperands?: dynamicFieldValueOperands;
|
||||
}) {
|
||||
if (!attributesQueryValue) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const resolvedQueryValue = resolveQueryValue({
|
||||
queryValue: attributesQueryValue,
|
||||
attributes,
|
||||
dynamicFieldValueOperands,
|
||||
});
|
||||
|
||||
return resolvedQueryValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns attributesQueryBuilderConfig with the list of labels instead of list of ids.
|
||||
* Barrel file that re-exports from both server and client modules for backward compatibility.
|
||||
*
|
||||
* IMPORTANT: This file imports from raqbUtils.client.ts which requires the react-awesome-query-builder runtime.
|
||||
* For server-side code (cron jobs, API routes, etc.), import directly from raqbUtils.server.ts instead
|
||||
* to avoid pulling in the client-only RAQB library.
|
||||
*
|
||||
* Server-side imports should use:
|
||||
* import { acrossQueryValueCompatiblity, raqbQueryValueUtils } from "@calcom/app-store/_utils/raqb/raqbUtils.server";
|
||||
*
|
||||
* Client-side imports can use this file or import directly from raqbUtils.client.ts:
|
||||
* import { buildStateFromQueryValue, buildEmptyQueryValue } from "@calcom/app-store/_utils/raqb/raqbUtils.client";
|
||||
*/
|
||||
export function getAttributesQueryBuilderConfigHavingListofLabels({
|
||||
dynamicFieldValueOperands,
|
||||
attributes,
|
||||
}: {
|
||||
dynamicFieldValueOperands?: dynamicFieldValueOperands;
|
||||
attributes: Attribute[];
|
||||
}) {
|
||||
const attributesQueryBuilderConfig = getQueryBuilderConfigForAttributes({
|
||||
attributes,
|
||||
dynamicOperandFields: dynamicFieldValueOperands?.fields,
|
||||
});
|
||||
|
||||
const attributesQueryBuilderConfigFieldsWithCompatibleListValues = Object.fromEntries(
|
||||
Object.entries(attributesQueryBuilderConfig.fields).map(([raqbFieldId, raqbField]) => {
|
||||
const raqbFieldType = raqbField.type;
|
||||
|
||||
return [
|
||||
raqbFieldId,
|
||||
{
|
||||
...raqbField,
|
||||
type: raqbFieldType,
|
||||
fieldSettings: {
|
||||
...raqbField.fieldSettings,
|
||||
listValues: raqbField.fieldSettings.listValues?.map((option) => {
|
||||
return {
|
||||
...option,
|
||||
// Use the title(which is the attributeOption.value) as the value of the raqb field so that it can be compatible for matching with the form field value
|
||||
value: caseInsensitive(option.title),
|
||||
};
|
||||
}),
|
||||
},
|
||||
},
|
||||
];
|
||||
})
|
||||
);
|
||||
|
||||
const attributesQueryBuilderConfigWithCompatibleListValues = {
|
||||
...attributesQueryBuilderConfig,
|
||||
fields: attributesQueryBuilderConfigFieldsWithCompatibleListValues,
|
||||
};
|
||||
|
||||
return attributesQueryBuilderConfigWithCompatibleListValues;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utilities to establish compatibility between formFieldQueryValue and attributeQueryValue
|
||||
*/
|
||||
export const acrossQueryValueCompatiblity = {
|
||||
// Re-export server-safe utilities
|
||||
export {
|
||||
raqbQueryValueUtils,
|
||||
getValueOfAttributeOption,
|
||||
getAttributesQueryBuilderConfigHavingListofLabels,
|
||||
getAttributesQueryValue,
|
||||
getAttributesData,
|
||||
resolveQueryValue,
|
||||
};
|
||||
acrossQueryValueCompatiblity,
|
||||
} from "./raqbUtils.server";
|
||||
|
||||
// Re-export client-only utilities (requires react-awesome-query-builder runtime)
|
||||
export { buildEmptyQueryValue, buildStateFromQueryValue } from "./raqbUtils.client";
|
||||
Reference in new issue
Block a user