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.

add to create and update

Kiran K820bfd2e9f09
diff --git a/apps/web/lib/actions/partners/create-reward.ts b/apps/web/lib/actions/partners/create-reward.ts
index 32322c6306..4b673a02a6 100644
--- a/apps/web/lib/actions/partners/create-reward.ts
+++ b/apps/web/lib/actions/partners/create-reward.ts
@@ -23,6 +23,7 @@ export const createRewardAction = authActionClient
isDefault,
includedPartnerIds,
excludedPartnerIds,
+ modifiers,
} = parsedInput;
includedPartnerIds = includedPartnerIds || [];
@@ -83,6 +84,7 @@ export const createRewardAction = authActionClient
amount,
maxDuration,
default: isDefault,
+ modifiers,
},
});
diff --git a/apps/web/lib/actions/partners/update-reward.ts b/apps/web/lib/actions/partners/update-reward.ts
index 3abf943619..a8334c064f 100644
--- a/apps/web/lib/actions/partners/update-reward.ts
+++ b/apps/web/lib/actions/partners/update-reward.ts
@@ -23,6 +23,7 @@ export const updateRewardAction = authActionClient
type,
includedPartnerIds,
excludedPartnerIds,
+ modifiers,
} = parsedInput;
includedPartnerIds = includedPartnerIds || [];
@@ -66,6 +67,7 @@ export const updateRewardAction = authActionClient
type,
amount,
maxDuration,
+ modifiers,
},
});
diff --git a/apps/web/lib/zod/schemas/rewards.ts b/apps/web/lib/zod/schemas/rewards.ts
index 3df4738146..e3aa847c65 100644
--- a/apps/web/lib/zod/schemas/rewards.ts
+++ b/apps/web/lib/zod/schemas/rewards.ts
@@ -2,6 +2,62 @@ import { EventType, RewardStructure } from "@dub/prisma/client";
import { z } from "zod";
import { getPaginationQuerySchema, maxDurationSchema } from "./misc";
+///// Reward modifier schema //////
+
+const operatorTypeSchema = z.enum([
+ "equals_to",
+ "not_equals",
+ "in",
+ "not_in",
+ "starts_with",
+ "ends_with",
+ "greater_than",
+ "less_than",
+ "greater_than_or_equal",
+ "less_than_or_equal",
+]);
+
+const logicOperatorSchema = z.enum(["AND", "OR"]);
+
+const fieldTypeSchema = z.enum(["customer", "sale"]);
+
+const fieldSchema = z.enum(["country", "productId"]);
+
+// A condition can be met if the field value matches the value
+export const rewardConditionSchema = z.object({
+ type: fieldTypeSchema,
+ field: fieldSchema,
+ operator: operatorTypeSchema,
+ value: z.union([
+ z.string(),
+ z.number(),
+ z.array(z.string()),
+ z.array(z.number()),
+ ]),
+});
+
+// A modifier can have multiple conditions that must be met for the modifier to be applied
+export const rewardModifierSchema = z.object({
+ operator: logicOperatorSchema,
+ amount: z.number().int().min(0),
+ conditions: z.array(rewardConditionSchema).min(1),
+});
+
+// The context in which a reward modifier is evaluated
+export const rewardContextSchema = z.object({
+ customer: z
+ .object({
+ country: z.string().nullable(),
+ })
+ .optional(),
+
+ sale: z
+ .object({
+ productId: z.string().nullable(),
+ })
+ .optional(),
+});
+
export const COMMISSION_TYPES = [
{
value: "one-off",
@@ -25,6 +81,7 @@ export const RewardSchema = z.object({
maxDuration: z.number().nullish(),
maxAmount: z.number().nullish(),
default: z.boolean(),
+ modifiers: z.array(rewardModifierSchema).nullish(),
});
export const createOrUpdateRewardSchema = z.object({
@@ -42,6 +99,7 @@ export const createOrUpdateRewardSchema = z.object({
.array(z.string())
.nullish()
.describe("Only applicable for default rewards"),
+ modifiers: z.array(rewardModifierSchema).optional(),
});
export const createRewardSchema = createOrUpdateRewardSchema.superRefine(
@@ -79,59 +137,3 @@ export const REWARD_EVENT_COLUMN_MAPPING = Object.freeze({
lead: "leadRewardId",
sale: "saleRewardId",
});
-
-///// Reward modifier schema //////
-
-const operatorTypeSchema = z.enum([
- "equals_to",
- "not_equals",
- "in",
- "not_in",
- "starts_with",
- "ends_with",
- "greater_than",
- "less_than",
- "greater_than_or_equal",
- "less_than_or_equal",
-]);
-
-const logicOperatorSchema = z.enum(["AND", "OR"]);
-
-const fieldTypeSchema = z.enum(["customer", "sale"]);
-
-const fieldSchema = z.enum(["country", "productId"]);
-
-// A condition can be met if the field value matches the value
-export const rewardConditionSchema = z.object({
- type: fieldTypeSchema,
- field: fieldSchema,
- operator: operatorTypeSchema,
- value: z.union([
- z.string(),
- z.number(),
- z.array(z.string()),
- z.array(z.number()),
- ]),
-});
-
-// A modifier can have multiple conditions that must be met for the modifier to be applied
-export const rewardModifierSchema = z.object({
- operator: logicOperatorSchema,
- amount: z.number().int().min(0),
- conditions: z.array(rewardConditionSchema).min(1),
-});
-
-// The context in which a reward modifier is evaluated
-export const rewardContextSchema = z.object({
- customer: z
- .object({
- country: z.string().nullable(),
- })
- .optional(),
-
- sale: z
- .object({
- productId: z.string().nullable(),
- })
- .optional(),
-});