Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Extract reward metadata field coercion into dedicated module

Kiran Kd637a2ad71d7
diff --git a/apps/web/lib/api/rewards/reward-condition-metadata.ts b/apps/web/lib/api/rewards/reward-condition-metadata.ts
new file mode 100644
index 0000000000..688e3df8f7
--- /dev/null
+++ b/apps/web/lib/api/rewards/reward-condition-metadata.ts
@@ -0,0 +1,84 @@
+import { RewardCondition } from "@/lib/types";
+import {
+ METADATA_CONDITION_OPERATORS,
+ METADATA_NUMBER_CONDITION_OPERATORS,
+} from "@/lib/zod/schemas/rewards";
+
+function toNumber(fieldKey: unknown): number | undefined {
+ if (fieldKey == null) {
+ return undefined;
+ }
+
+ if (typeof fieldKey === "number" && !Number.isNaN(fieldKey)) {
+ return fieldKey;
+ }
+
+ if (typeof fieldKey === "boolean") {
+ return undefined;
+ }
+
+ if (typeof fieldKey === "string") {
+ if (fieldKey.trim() === "" || Number.isNaN(Number(fieldKey))) {
+ return undefined;
+ }
+
+ return Number(fieldKey);
+ }
+
+ const n = Number(fieldKey);
+ return Number.isNaN(n) ? undefined : n;
+}
+
+function toString(fieldKey: unknown) {
+ return typeof fieldKey === "string" ? fieldKey : String(fieldKey);
+}
+
+/**
+ * Normalizes a raw metadata value (from lead/sale JSON) into the type that
+ * `evaluateCondition` expects for the given operator.
+ *
+ * Metadata is stored as loosely typed JSON, so the same field may arrive as a
+ * string, number, or other primitive. Text operators (starts_with, contains,
+ * etc.) always receive a string; numeric operators receive a parsed number or
+ * undefined when parsing fails. For equals_to / not_equals, the return type is
+ * aligned with `condition.value` so strict equality checks compare like types.
+ */
+export function prepareMetadataFieldValue(
+ fieldKey: unknown,
+ condition: RewardCondition,
+): string | number | string[] | number[] | undefined {
+ if (fieldKey == null) {
+ return undefined;
+ }
+
+ const { operator, value: conditionValue } = condition;
+
+ // Metadata conditions do not support in / not_in and other non-metadata operators.
+ if (!METADATA_CONDITION_OPERATORS.includes(operator)) {
+ return undefined;
+ }
+
+ // Pattern text operators always compare against a stringified metadata value.
+ if (
+ operator === "starts_with" ||
+ operator === "ends_with" ||
+ operator === "contains" ||
+ operator === "not_contains"
+ ) {
+ return toString(fieldKey);
+ }
+
+ // Numeric operators require a parsed number, or undefined when coercion fails.
+ if (METADATA_NUMBER_CONDITION_OPERATORS.includes(operator)) {
+ return toNumber(fieldKey);
+ }
+
+ // For equals_to / not_equals with a numeric condition value, prefer number coercion.
+ if (typeof conditionValue === "number") {
+ const numeric = toNumber(fieldKey);
+ return numeric !== undefined ? numeric : toString(fieldKey);
+ }
+
+ // All other operators use string comparison.
+ return toString(fieldKey);
+}
diff --git a/apps/web/lib/partners/evaluate-reward-conditions.ts b/apps/web/lib/partners/evaluate-reward-conditions.ts
index c3642fc7f1..f41ed729fe 100644
--- a/apps/web/lib/partners/evaluate-reward-conditions.ts
+++ b/apps/web/lib/partners/evaluate-reward-conditions.ts
@@ -1,13 +1,10 @@
+import { prepareMetadataFieldValue } from "../api/rewards/reward-condition-metadata";
import {
RewardCondition,
RewardConditions,
RewardConditionsArray,
RewardContext,
} from "../types";
-import {
- METADATA_NUMBER_CONDITION_OPERATORS,
- METADATA_TEXT_CONDITION_OPERATORS,
-} from "../zod/schemas/rewards";
import { getRewardAmount } from "./get-reward-amount";
export const evaluateRewardConditions = ({
@@ -79,95 +76,33 @@ function resolveConditionFieldValue({
context: RewardContext;
}): string | number | string[] | number[] | undefined {
if (condition.attribute === "metadata") {
- const metaKey = condition.metadataField?.trim() ?? "";
- if (!metaKey) return undefined;
-
- if (condition.entity === "lead") {
- const raw = context.lead?.metadata?.[metaKey];
- return prepareMetadataFieldValue(raw, condition);
- }
-
- if (condition.entity === "sale") {
- const raw = context.sale?.metadata?.[metaKey];
- return prepareMetadataFieldValue(raw, condition);
- }
-
- return undefined;
- }
-
- if (condition.entity === "customer") {
- return context.customer?.[condition.attribute];
- }
-
- if (condition.entity === "sale") {
- return context.sale?.[condition.attribute];
- }
-
- if (condition.entity === "partner") {
- return context.partner?.[condition.attribute];
- }
-
- if (condition.entity === "lead") {
- return undefined;
- }
-
- return undefined;
-}
+ const metaKey = condition.metadataField?.trim();
-function parseMetadataNumeric(raw: unknown): number | undefined {
- if (raw == null) {
- return undefined;
- }
-
- if (typeof raw === "number" && !Number.isNaN(raw)) {
- return raw;
- }
-
- if (typeof raw === "string") {
- if (raw.trim() === "" || Number.isNaN(Number(raw))) {
+ if (!metaKey) {
return undefined;
}
- return Number(raw);
- }
-
- if (typeof raw === "boolean") {
- return undefined;
- }
-
- const n = Number(raw);
- return Number.isNaN(n) ? undefined : n;
-}
-
-function metadataRawToString(raw: unknown): string {
- return typeof raw === "string" ? raw : String(raw);
-}
-
-function prepareMetadataFieldValue(
- raw: unknown,
- condition: RewardCondition,
-): string | number | string[] | number[] | undefined {
- if (raw == null) return undefined;
+ const entityMap = {
+ partner: undefined,
+ customer: undefined,
+ lead: context.lead,
+ sale: context.sale,
+ } as const;
- const op = condition.operator;
- const equalsOrNot = op === "equals_to" || op === "not_equals";
-
- if (METADATA_TEXT_CONDITION_OPERATORS.includes(op) && !equalsOrNot) {
- return metadataRawToString(raw);
+ return prepareMetadataFieldValue(
+ entityMap[condition.entity]?.metadata?.[metaKey],
+ condition,
+ );
}
- const ordering = METADATA_NUMBER_CONDITION_OPERATORS.includes(op);
- if (!ordering && !equalsOrNot) return undefined;
+ const entityMap = {
+ partner: context.partner,
+ customer: context.customer,
+ lead: undefined,
+ sale: context.sale,
+ } as const;
- const numeric = parseMetadataNumeric(raw);
- if (ordering) return numeric;
-
- // For equals_to / not_equals, match the coercion to condition.value's type
- // so that strict === in evaluateCondition sees the same type on both sides.
- if (typeof condition.value === "number") {
- return numeric !== undefined ? numeric : metadataRawToString(raw);
- }
- return metadataRawToString(raw);
+ return entityMap[condition.entity]?.[condition.attribute];
}
const evaluateCondition = ({