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.

Share workflow attribute and operator definitions across schemas

Kiran Kd833f69eeadd
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/bounty-logic.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/bounty-logic.tsx
index 70a7059e7a..1e73ca4e69 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/bounty-logic.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/bounty-logic.tsx
@@ -1,8 +1,11 @@
"use client";
+import {
+ AWARD_BOUNTY_ATTRIBUTE_KEYS,
+ AWARD_BOUNTY_OPERATORS,
+} from "@/lib/api/workflows/award-bounty/schema";
import { isCurrencyAttribute } from "@/lib/api/workflows/utils";
import { PERFORMANCE_BOUNTY_SCOPE_ATTRIBUTES } from "@/lib/bounty/api/performance-bounty-scope-attributes";
-import { WORKFLOW_ATTRIBUTES } from "@/lib/zod/schemas/workflows";
import {
InlineBadgePopover,
InlineBadgePopoverMenu,
@@ -80,9 +83,7 @@ export function BountyLogic({ className }: { className?: string }) {
<InlineBadgePopoverMenu
selectedValue={field.value}
onSelect={field.onChange}
- items={WORKFLOW_ATTRIBUTES.filter(
- (attr) => PERFORMANCE_BOUNTY_SCOPE_ATTRIBUTES[attr],
- ).map((attribute) => ({
+ items={AWARD_BOUNTY_ATTRIBUTE_KEYS.map((attribute) => ({
text: PERFORMANCE_BOUNTY_SCOPE_ATTRIBUTES[
attribute
].toLowerCase(),
@@ -96,7 +97,7 @@ export function BountyLogic({ className }: { className?: string }) {
{attribute && (
<>
{" "}
- is at least{" "}
+ is {AWARD_BOUNTY_OPERATORS.gte.label}{" "}
<InlineBadgePopover
text={
value
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/transactional-campaign-logic.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/transactional-campaign-logic.tsx
index c633347c93..629a1d1bb1 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/transactional-campaign-logic.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/transactional-campaign-logic.tsx
@@ -1,5 +1,5 @@
import {
- SEND_CAMPAIGN_ATTRIBUTE_CONFIG,
+ SEND_CAMPAIGN_ATTRIBUTE_KEYS,
SEND_CAMPAIGN_ATTRIBUTES,
} from "@/lib/api/workflows/send-campaign/schema";
import { handleMoneyInputChange, handleMoneyKeyDown } from "@/lib/form-utils";
@@ -21,7 +21,7 @@ export function TransactionalCampaignLogic() {
const value = watch("triggerCondition.value");
const prevAttributeRef = useRef(attribute);
- const config = attribute ? SEND_CAMPAIGN_ATTRIBUTE_CONFIG[attribute] : null;
+ const config = attribute ? SEND_CAMPAIGN_ATTRIBUTES[attribute] : null;
// Reset value when attribute changes
useEffect(() => {
@@ -55,7 +55,7 @@ export function TransactionalCampaignLogic() {
<InlineBadgePopover
text={
field.value
- ? SEND_CAMPAIGN_ATTRIBUTE_CONFIG[field.value].label
+ ? SEND_CAMPAIGN_ATTRIBUTES[field.value].label
: "activity"
}
invalid={!field.value}
@@ -63,8 +63,8 @@ export function TransactionalCampaignLogic() {
<InlineBadgePopoverMenu
selectedValue={field.value}
onSelect={field.onChange}
- items={SEND_CAMPAIGN_ATTRIBUTES.map((attr) => ({
- text: SEND_CAMPAIGN_ATTRIBUTE_CONFIG[attr].label,
+ items={SEND_CAMPAIGN_ATTRIBUTE_KEYS.map((attr) => ({
+ text: SEND_CAMPAIGN_ATTRIBUTES[attr].label,
value: attr,
}))}
/>
@@ -91,7 +91,7 @@ export function TransactionalCampaignLogic() {
function DropdownValueInput({
config,
}: {
- config: { dropdownValues?: number[] };
+ config: { dropdownValues?: readonly number[] };
}) {
const { control } = useCampaignFormContext();
@@ -112,7 +112,9 @@ function DropdownValueInput({
<DurationPopoverContent
value={field.value ?? undefined}
onChange={field.onChange}
- presetDurations={config.dropdownValues || []}
+ presetDurations={
+ config.dropdownValues ? Array.from(config.dropdownValues) : []
+ }
presetsOnly
unit="days"
minValue={1}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/utils.ts b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/utils.ts
index e4babc6f80..2c54b3f02d 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/utils.ts
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/utils.ts
@@ -1,4 +1,4 @@
-import { SEND_CAMPAIGN_ATTRIBUTE_CONFIG } from "@/lib/api/workflows/send-campaign/schema";
+import { SEND_CAMPAIGN_ATTRIBUTES } from "@/lib/api/workflows/send-campaign/schema";
import type { SendCampaignCondition } from "@/lib/api/workflows/send-campaign/types";
export function isValidTriggerCondition(
@@ -14,7 +14,7 @@ export function isValidTriggerCondition(
return false;
}
- const config = SEND_CAMPAIGN_ATTRIBUTE_CONFIG[triggerCondition.attribute];
+ const config = SEND_CAMPAIGN_ATTRIBUTES[triggerCondition.attribute];
// If attribute doesn't exist in config, invalid
if (!config) {
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/settings/group-move-rules.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/settings/group-move-rules.tsx
index 63a45392fc..f4f56e2783 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/settings/group-move-rules.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/settings/group-move-rules.tsx
@@ -1,12 +1,12 @@
"use client";
import {
- GROUP_MOVE_ATTRIBUTE_CONFIG,
+ GROUP_MOVE_ATTRIBUTE_KEYS,
GROUP_MOVE_ATTRIBUTES,
} from "@/lib/api/workflows/move-group/schema";
import type {
GroupMoveAttribute,
- GroupMoveAttributeConfig,
+ GroupMoveAttributeKey,
GroupMoveCondition,
GroupMoveRules as GroupMoveRulesForm,
} from "@/lib/api/workflows/move-group/types";
@@ -64,7 +64,7 @@ export function GroupMoveRules() {
);
const disableAddRuleButton =
- ruleFields.length >= GROUP_MOVE_ATTRIBUTES.length;
+ ruleFields.length >= GROUP_MOVE_ATTRIBUTE_KEYS.length;
const { canUseGroupMoveRule } = getPlanCapabilities(plan);
@@ -83,7 +83,7 @@ export function GroupMoveRules() {
}
// Filter out attributes already used by other rules
- const availableAttributes = GROUP_MOVE_ATTRIBUTES.filter(
+ const availableAttributes = GROUP_MOVE_ATTRIBUTE_KEYS.filter(
(attribute) =>
attribute === rule.attribute ||
!usedAttributes?.includes(attribute),
@@ -150,11 +150,11 @@ function GroupRule({
onUpdate: (updates: Partial<GroupMoveCondition>) => void;
onRemove: () => void;
index: number;
- availableAttributes: GroupMoveAttribute[];
+ availableAttributes: GroupMoveAttributeKey[];
}) {
const isFirst = index === 0;
const attributeConfig = rule.attribute
- ? GROUP_MOVE_ATTRIBUTE_CONFIG[rule.attribute]
+ ? GROUP_MOVE_ATTRIBUTES[rule.attribute]
: undefined;
const attributeType = attributeConfig?.inputType || "number";
@@ -215,7 +215,7 @@ function GroupRule({
<InlineBadgePopoverMenu
items={availableAttributes.map((attribute) => ({
value: attribute,
- text: GROUP_MOVE_ATTRIBUTE_CONFIG[attribute].label,
+ text: GROUP_MOVE_ATTRIBUTES[attribute].label,
}))}
selectedValue={rule.attribute}
onSelect={(value) => {
@@ -368,7 +368,7 @@ function ValueInput({
}: {
index: number;
rule: GroupMoveCondition;
- attributeType: GroupMoveAttributeConfig["inputType"];
+ attributeType: GroupMoveAttribute["inputType"];
part: "min" | "max";
onUpdate: (updates: Partial<GroupMoveCondition>) => void;
}) {
@@ -539,7 +539,7 @@ const convertFromDisplayValue = (
// Format the value based on the attribute type
const formatValue = (
value: ValueType,
- type: GroupMoveAttributeConfig["inputType"] | undefined,
+ type: GroupMoveAttribute["inputType"] | undefined,
part: "min" | "max" = "min",
) => {
const numValue = part === "min" ? getMinValue(value) : getMaxValue(value);
diff --git a/apps/web/lib/api/groups/validate-group-move-rules.ts b/apps/web/lib/api/groups/validate-group-move-rules.ts
index 27deb2200f..578dc1d4dd 100644
--- a/apps/web/lib/api/groups/validate-group-move-rules.ts
+++ b/apps/web/lib/api/groups/validate-group-move-rules.ts
@@ -1,13 +1,13 @@
import {
- GROUP_MOVE_ATTRIBUTE_CONFIG,
- GROUP_MOVE_OPERATOR_LABELS,
+ GROUP_MOVE_ATTRIBUTES,
+ GROUP_MOVE_OPERATORS,
} from "@/lib/api/workflows/move-group/schema";
import type {
- GroupMoveAttribute,
+ GroupMoveAttributeKey,
GroupMoveCondition,
GroupMoveRules,
} from "@/lib/api/workflows/move-group/types";
-import { COMPARISON_OPERATORS } from "@/lib/api/workflows/operators";
+import { WORKFLOW_OPERATORS } from "@/lib/api/workflows/operator-definitions";
type GroupMoveAttributeValidator = (params: {
rule: GroupMoveCondition;
@@ -18,7 +18,7 @@ type GroupMoveAttributeValidator = (params: {
// Add business rules for each attribute
// NOTE: Keeping this empty for now, next PR will use this
const GROUP_MOVE_ATTRIBUTE_VALIDATORS: Record<
- GroupMoveAttribute,
+ GroupMoveAttributeKey,
GroupMoveAttributeValidator
> = {
// Operator validators already cover performance value shape/constraints.
@@ -63,10 +63,10 @@ const validateRule = ({
}
// Check if operator is valid for the attribute
- const allowedOperators = GROUP_MOVE_ATTRIBUTE_CONFIG[rule.attribute]
+ const allowedOperators = GROUP_MOVE_ATTRIBUTES[rule.attribute]
.operators as readonly string[];
- const operatorLabel = GROUP_MOVE_OPERATOR_LABELS[rule.operator];
+ const operatorLabel = GROUP_MOVE_OPERATORS[rule.operator].label;
if (!allowedOperators.includes(rule.operator)) {
throw new Error(
@@ -80,7 +80,7 @@ const validateRule = ({
}
// Validate value shape/constraints for the selected operator
- const operator = COMPARISON_OPERATORS[rule.operator];
+ const operator = WORKFLOW_OPERATORS[rule.operator];
if (operator) {
try {
diff --git a/apps/web/lib/api/workflows/attribute-definitions.ts b/apps/web/lib/api/workflows/attribute-definitions.ts
new file mode 100644
index 0000000000..850fc4d78d
--- /dev/null
+++ b/apps/web/lib/api/workflows/attribute-definitions.ts
@@ -0,0 +1,45 @@
+/**
+ * Private to lib/api/workflows.
+ * Do not import from UI or other packages — use AWARD_BOUNTY_* /
+ * GROUP_MOVE_* / SEND_CAMPAIGN_* from the workflow schema instead.
+ */
+
+export const WORKFLOW_ATTRIBUTES = {
+ totalLeads: {
+ name: "totalLeads",
+ label: "total leads",
+ inputType: "number",
+ operators: ["gte"],
+ },
+ totalConversions: {
+ name: "totalConversions",
+ label: "total conversions",
+ inputType: "number",
+ operators: ["gte"],
+ },
+ totalSaleAmount: {
+ name: "totalSaleAmount",
+ label: "total revenue",
+ inputType: "currency",
+ operators: ["gte"],
+ },
+ totalCommissions: {
+ name: "totalCommissions",
+ label: "total commissions",
+ inputType: "currency",
+ operators: ["gte"],
+ },
+ partnerEnrolledDays: {
+ name: "partnerEnrolledDays",
+ label: "enrollment duration",
+ inputType: "dropdown",
+ operators: ["gte"],
+ dropdownValues: [1, 3, 7, 14, 30],
+ },
+ partnerJoined: {
+ name: "partnerJoined",
+ label: "joins the program",
+ inputType: "none",
+ operators: ["gte"],
+ },
+} as const;
diff --git a/apps/web/lib/api/workflows/award-bounty/schema.ts b/apps/web/lib/api/workflows/award-bounty/schema.ts
index db76169222..2a1b19813c 100644
--- a/apps/web/lib/api/workflows/award-bounty/schema.ts
+++ b/apps/web/lib/api/workflows/award-bounty/schema.ts
@@ -1,57 +1,28 @@
+import { WORKFLOW_OPERATORS } from "@/lib/api/workflows/operator-definitions";
import * as z from "zod/v4";
+import { WORKFLOW_ATTRIBUTES } from "../attribute-definitions";
-export const AWARD_BOUNTY_ATTRIBUTES = [
- "totalLeads",
- "totalConversions",
- "totalSaleAmount",
- "totalCommissions",
-] as const;
-
-export const AWARD_BOUNTY_OPERATORS = ["gte"] as const;
-
-type AwardBountyAttribute = (typeof AWARD_BOUNTY_ATTRIBUTES)[number];
-
-type AwardBountyOperator = (typeof AWARD_BOUNTY_OPERATORS)[number];
-
-type AwardBountyAttributeConfig = {
- label: string;
- inputType: "number" | "currency";
- operators: readonly AwardBountyOperator[];
+const AWARD_BOUNTY_ATTRIBUTES = {
+ totalLeads: WORKFLOW_ATTRIBUTES.totalLeads,
+ totalConversions: WORKFLOW_ATTRIBUTES.totalConversions,
+ totalSaleAmount: WORKFLOW_ATTRIBUTES.totalSaleAmount,
+ totalCommissions: WORKFLOW_ATTRIBUTES.totalCommissions,
};
-export const AWARD_BOUNTY_ATTRIBUTE_CONFIG: Record<
- AwardBountyAttribute,
- AwardBountyAttributeConfig
-> = {
- totalLeads: {
- label: "total leads",
- inputType: "number",
- operators: ["gte"],
- },
- totalConversions: {
- label: "total conversions",
- inputType: "number",
- operators: ["gte"],
- },
- totalSaleAmount: {
- label: "total revenue",
- inputType: "currency",
- operators: ["gte"],
- },
- totalCommissions: {
- label: "total commissions",
- inputType: "currency",
- operators: ["gte"],
- },
+export const AWARD_BOUNTY_ATTRIBUTE_KEYS = Object.keys(
+ AWARD_BOUNTY_ATTRIBUTES,
+) as readonly (keyof typeof AWARD_BOUNTY_ATTRIBUTES)[];
+
+export const AWARD_BOUNTY_OPERATORS = {
+ gte: WORKFLOW_OPERATORS.gte,
};
-export const AWARD_BOUNTY_OPERATOR_LABELS: Record<AwardBountyOperator, string> =
- {
- gte: "at least",
- } as const;
+const AWARD_BOUNTY_OPERATOR_KEYS = Object.keys(
+ AWARD_BOUNTY_OPERATORS,
+) as readonly (keyof typeof AWARD_BOUNTY_OPERATORS)[];
export const awardBountyConditionSchema = z.object({
- attribute: z.enum(AWARD_BOUNTY_ATTRIBUTES),
- operator: z.enum(AWARD_BOUNTY_OPERATORS).default("gte"),
+ attribute: z.enum(AWARD_BOUNTY_ATTRIBUTE_KEYS),
+ operator: z.enum(AWARD_BOUNTY_OPERATOR_KEYS).default("gte"),
value: z.number(),
});
diff --git a/apps/web/lib/api/workflows/evaluate-workflow-conditions.ts b/apps/web/lib/api/workflows/evaluate-workflow-conditions.ts
index 86f27dc478..5c32c708e7 100644
--- a/apps/web/lib/api/workflows/evaluate-workflow-conditions.ts
+++ b/apps/web/lib/api/workflows/evaluate-workflow-conditions.ts
@@ -2,7 +2,7 @@ import {
WorkflowCondition,
WorkflowConditionAttribute,
} from "@/lib/api/workflows/types";
-import { COMPARISON_OPERATORS } from "./operators";
+import { WORKFLOW_OPERATORS } from "./operator-definitions";
export function evaluateWorkflowConditions({
conditions,
@@ -14,7 +14,7 @@ export function evaluateWorkflowConditions({
if (conditions.length === 0) return false;
for (const condition of conditions) {
- const operator = COMPARISON_OPERATORS[condition.operator];
+ const operator = WORKFLOW_OPERATORS[condition.operator];
if (!operator) {
console.error(`Operator ${condition.operator} is not supported.`);
diff --git a/apps/web/lib/api/workflows/move-group/schema.ts b/apps/web/lib/api/workflows/move-group/schema.ts
index b8aa462ef7..f58593dc2a 100644
--- a/apps/web/lib/api/workflows/move-group/schema.ts
+++ b/apps/web/lib/api/workflows/move-group/schema.ts
@@ -1,58 +1,42 @@
+import { WORKFLOW_OPERATORS } from "@/lib/api/workflows/operator-definitions";
import * as z from "zod/v4";
+import { WORKFLOW_ATTRIBUTES } from "../attribute-definitions";
-export const GROUP_MOVE_ATTRIBUTES = [
- "totalLeads",
- "totalConversions",
- "totalSaleAmount",
- "totalCommissions",
-] as const;
-
-export const GROUP_MOVE_OPERATORS = ["gte", "between"] as const;
-
-type GroupMoveAttribute = (typeof GROUP_MOVE_ATTRIBUTES)[number];
-
-type GroupMoveOperator = (typeof GROUP_MOVE_OPERATORS)[number];
-
-type GroupMoveAttributeConfig = {
- label: string;
- inputType: "number" | "currency";
- operators: readonly GroupMoveOperator[];
-};
-
-export const GROUP_MOVE_ATTRIBUTE_CONFIG: Record<
- GroupMoveAttribute,
- GroupMoveAttributeConfig
-> = {
+export const GROUP_MOVE_ATTRIBUTES = {
totalLeads: {
- label: "total leads",
- inputType: "number",
- operators: ["gte", "between"],
+ ...WORKFLOW_ATTRIBUTES.totalLeads,
+ operators: ["gte", "between"] as const,
},
totalConversions: {
- label: "total conversions",
- inputType: "number",
- operators: ["gte", "between"],
+ ...WORKFLOW_ATTRIBUTES.totalConversions,
+ operators: ["gte", "between"] as const,
},
totalSaleAmount: {
- label: "total revenue",
- inputType: "currency",
- operators: ["gte", "between"],
+ ...WORKFLOW_ATTRIBUTES.totalSaleAmount,
+ operators: ["gte", "between"] as const,
},
totalCommissions: {
- label: "total commissions",
- inputType: "currency",
- operators: ["gte", "between"],
+ ...WORKFLOW_ATTRIBUTES.totalCommissions,
+ operators: ["gte", "between"] as const,
},
};
-export const GROUP_MOVE_OPERATOR_LABELS: Record<GroupMoveOperator, string> = {
- gte: "more than",
- between: "between",
-} as const;
+export const GROUP_MOVE_ATTRIBUTE_KEYS = Object.keys(
+ GROUP_MOVE_ATTRIBUTES,
+) as readonly (keyof typeof GROUP_MOVE_ATTRIBUTES)[];
+
+export const GROUP_MOVE_OPERATORS = {
+ gte: WORKFLOW_OPERATORS.gte,
+ between: WORKFLOW_OPERATORS.between,
+};
+
+const GROUP_MOVE_OPERATOR_KEYS = Object.keys(
+ GROUP_MOVE_OPERATORS,
+) as readonly (keyof typeof GROUP_MOVE_OPERATORS)[];
-export const groupMoveConditionSchema = z.object({
- attribute: z.enum(GROUP_MOVE_ATTRIBUTES),
- operator: z.enum(GROUP_MOVE_OPERATORS).default("gte"),
+const groupMoveConditionSchema = z.object({
+ attribute: z.enum(GROUP_MOVE_ATTRIBUTE_KEYS),
+ operator: z.enum(GROUP_MOVE_OPERATOR_KEYS).default("gte"),
value: z.union([
z.number(),
z.object({
diff --git a/apps/web/lib/api/workflows/move-group/types.ts b/apps/web/lib/api/workflows/move-group/types.ts
index c0b4f6b932..8c0919213e 100644
--- a/apps/web/lib/api/workflows/move-group/types.ts
+++ b/apps/web/lib/api/workflows/move-group/types.ts
@@ -1,22 +1,12 @@
import type * as z from "zod/v4";
-import type {
- GROUP_MOVE_ATTRIBUTES,
- GROUP_MOVE_OPERATORS,
- groupMoveConditionSchema,
- groupMoveRulesSchema,
-} from "./schema";
-
-export type GroupMoveCondition = z.infer<typeof groupMoveConditionSchema>;
+import type { GROUP_MOVE_ATTRIBUTES, groupMoveRulesSchema } from "./schema";
export type GroupMoveRules = z.infer<typeof groupMoveRulesSchema>;
-export type GroupMoveAttribute = (typeof GROUP_MOVE_ATTRIBUTES)[number];
+export type GroupMoveCondition = GroupMoveRules[number];
-type GroupMoveOperator = (typeof GROUP_MOVE_OPERATORS)[number];
+export type GroupMoveAttributeKey = keyof typeof GROUP_MOVE_ATTRIBUTES;
-export type GroupMoveAttributeConfig = {
- label: string;
- inputType: "number" | "currency";
- operators: readonly GroupMoveOperator[];
-};
+export type GroupMoveAttribute =
+ (typeof GROUP_MOVE_ATTRIBUTES)[GroupMoveAttributeKey];
diff --git a/apps/web/lib/api/workflows/operators.ts b/apps/web/lib/api/workflows/operator-definitions.ts
similarity index 88%
rename from apps/web/lib/api/workflows/operators.ts
rename to apps/web/lib/api/workflows/operator-definitions.ts
index dc350657d7..b4cbbc71c9 100644
--- a/apps/web/lib/api/workflows/operators.ts
+++ b/apps/web/lib/api/workflows/operator-definitions.ts
@@ -2,17 +2,21 @@ import { WorkflowComparisonOperator } from "@/lib/api/workflows/types";
type ConditionValue = number | { min: number; max?: number };
-type ComparisonOperator = {
+type WorkflowOperator = {
+ name: WorkflowComparisonOperator;
+ label: string;
validate: (value: ConditionValue) => void;
evaluate: (attributeValue: number, conditionValue: ConditionValue) => boolean;
};
-export const COMPARISON_OPERATORS: Record<
+export const WORKFLOW_OPERATORS: Record<
WorkflowComparisonOperator,
- ComparisonOperator
+ WorkflowOperator
> = {
// Greater than or equal to
gte: {
+ name: "gte",
+ label: "at least",
validate(value) {
if (typeof value !== "number" || isNaN(value) || value <= 0) {
throw new Error("Please enter a value greater than 0.");
@@ -29,6 +33,8 @@ export const COMPARISON_OPERATORS: Record<
// Between (inclusive)
between: {
+ name: "between",
+ label: "between",
validate(value) {
if (typeof value !== "object" || value === null) {
throw new Error("Please enter a valid value.");
diff --git a/apps/web/lib/api/workflows/send-campaign/schema.ts b/apps/web/lib/api/workflows/send-campaign/schema.ts
index 52d4804690..8b04698ba0 100644
--- a/apps/web/lib/api/workflows/send-campaign/schema.ts
+++ b/apps/web/lib/api/workflows/send-campaign/schema.ts
@@ -1,73 +1,30 @@
+import { WORKFLOW_OPERATORS } from "@/lib/api/workflows/operator-definitions";
import * as z from "zod/v4";
-
-export const SEND_CAMPAIGN_ATTRIBUTES = [
- "totalLeads",
- "totalConversions",
- "totalSaleAmount",
- "totalCommissions",
- "partnerEnrolledDays",
- "partnerJoined",
-] as const;
-
-export const SEND_CAMPAIGN_OPERATORS = ["gte"] as const;
-
-type SendCampaignAttribute = (typeof SEND_CAMPAIGN_ATTRIBUTES)[number];
-
-type SendCampaignOperator = (typeof SEND_CAMPAIGN_OPERATORS)[number];
-
-type SendCampaignAttributeConfig = {
- label: string;
- inputType: "number" | "currency" | "dropdown" | "none";
- operators: readonly SendCampaignOperator[];
- dropdownValues?: number[];
+import { WORKFLOW_ATTRIBUTES } from "../attribute-definitions";
+
+export const SEND_CAMPAIGN_ATTRIBUTES = {
+ totalLeads: WORKFLOW_ATTRIBUTES.totalLeads,
+ totalConversions: WORKFLOW_ATTRIBUTES.totalConversions,
+ totalSaleAmount: WORKFLOW_ATTRIBUTES.totalSaleAmount,
+ totalCommissions: WORKFLOW_ATTRIBUTES.totalCommissions,
+ partnerEnrolledDays: WORKFLOW_ATTRIBUTES.partnerEnrolledDays,
+ partnerJoined: WORKFLOW_ATTRIBUTES.partnerJoined,
};
-export const SEND_CAMPAIGN_ATTRIBUTE_CONFIG: Record<
- SendCampaignAttribute,
- SendCampaignAttributeConfig
-> = {
- totalLeads: {
- label: "total leads",
- inputType: "number",
- operators: ["gte"],
- },
- totalConversions: {
- label: "total conversions",
- inputType: "number",
- operators: ["gte"],
- },
- totalSaleAmount: {
- label: "total revenue",
- inputType: "currency",
- operators: ["gte"],
- },
- totalCommissions: {
- label: "total commissions",
- inputType: "currency",
- operators: ["gte"],
- },
- partnerEnrolledDays: {
- label: "enrollment duration",
- inputType: "dropdown",
- operators: ["gte"],
- dropdownValues: [1, 3, 7, 14, 30],
- },
- partnerJoined: {
- label: "joins the program",
- inputType: "none",
- operators: ["gte"],
- },
+export const SEND_CAMPAIGN_ATTRIBUTE_KEYS = Object.keys(
+ SEND_CAMPAIGN_ATTRIBUTES,
+) as readonly (keyof typeof SEND_CAMPAIGN_ATTRIBUTES)[];
+
+const SEND_CAMPAIGN_OPERATORS = {
+ gte: WORKFLOW_OPERATORS.gte,
};
-export const SEND_CAMPAIGN_OPERATOR_LABELS: Record<
- SendCampaignOperator,
- string
-> = {
- gte: "at least",
-} as const;
+const SEND_CAMPAIGN_OPERATOR_KEYS = Object.keys(
+ SEND_CAMPAIGN_OPERATORS,
+) as readonly (keyof typeof SEND_CAMPAIGN_OPERATORS)[];
export const sendCampaignConditionSchema = z.object({
- attribute: z.enum(SEND_CAMPAIGN_ATTRIBUTES),
- operator: z.enum(SEND_CAMPAIGN_OPERATORS).default("gte"),
+ attribute: z.enum(SEND_CAMPAIGN_ATTRIBUTE_KEYS),
+ operator: z.enum(SEND_CAMPAIGN_OPERATOR_KEYS).default("gte"),
value: z.number(),
});
diff --git a/apps/web/tests/workflows/comparison-operators.test.ts b/apps/web/tests/workflows/comparison-operators.test.ts
index e90a52912a..89ed2abd0c 100644
--- a/apps/web/tests/workflows/comparison-operators.test.ts
+++ b/apps/web/tests/workflows/comparison-operators.test.ts
@@ -1,8 +1,8 @@
-import { COMPARISON_OPERATORS } from "@/lib/api/workflows/operators";
+import { WORKFLOW_OPERATORS } from "@/lib/api/workflows/operator-definitions";
import { describe, expect, it } from "vitest";
-describe("COMPARISON_OPERATORS.gte.evaluate", () => {
- const { evaluate } = COMPARISON_OPERATORS.gte;
+describe("WORKFLOW_OPERATORS.gte.evaluate", () => {
+ const { evaluate } = WORKFLOW_OPERATORS.gte;
it("returns true when attribute value is greater than or equal to condition", () => {
expect(evaluate(10, 10)).toBe(true);
@@ -18,8 +18,8 @@ describe("COMPARISON_OPERATORS.gte.evaluate", () => {
});
});
-describe("COMPARISON_OPERATORS.between.evaluate", () => {
- const { evaluate } = COMPARISON_OPERATORS.between;
+describe("WORKFLOW_OPERATORS.between.evaluate", () => {
+ const { evaluate } = WORKFLOW_OPERATORS.between;
it("returns true when attribute value is within inclusive range", () => {
expect(evaluate(1, { min: 1, max: 5 })).toBe(true);
@@ -41,8 +41,8 @@ describe("COMPARISON_OPERATORS.between.evaluate", () => {
});
});
-describe("COMPARISON_OPERATORS.gte.validate", () => {
- const { validate } = COMPARISON_OPERATORS.gte;
+describe("WORKFLOW_OPERATORS.gte.validate", () => {
+ const { validate } = WORKFLOW_OPERATORS.gte;
it("accepts a positive number", () => {
expect(() => validate(1)).not.toThrow();
@@ -59,8 +59,8 @@ describe("COMPARISON_OPERATORS.gte.validate", () => {
});
});
-describe("COMPARISON_OPERATORS.between.validate", () => {
- const { validate } = COMPARISON_OPERATORS.between;
+describe("WORKFLOW_OPERATORS.between.validate", () => {
+ const { validate } = WORKFLOW_OPERATORS.between;
it("accepts a valid min/max range", () => {
expect(() => validate({ min: 1, max: 5 })).not.toThrow();