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 award bounty condition schema into its own workflow schema file

Kiran Ke901ddcf36b7
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/use-add-edit-bounty-form.ts b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/use-add-edit-bounty-form.ts
index 3b181d4159..bb3a34a85c 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/use-add-edit-bounty-form.ts
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/use-add-edit-bounty-form.ts
@@ -1,5 +1,6 @@
"use client";
+import { awardBountyConditionSchema } from "@/lib/api/workflows/award-bounty/schema";
import { isCurrencyAttribute } from "@/lib/api/workflows/utils";
import { generatePerformanceBountyName } from "@/lib/bounty/api/generate-performance-bounty-name";
import {
@@ -11,10 +12,7 @@ import { mutatePrefix } from "@/lib/swr/mutate";
import { useApiMutation } from "@/lib/swr/use-api-mutation";
import useWorkspace from "@/lib/swr/use-workspace";
import { BountyProps } from "@/lib/types";
-import {
- bountyPerformanceConditionSchema,
- bountySocialContentRequirementsSchema,
-} from "@/lib/zod/schemas/bounties";
+import { bountySocialContentRequirementsSchema } from "@/lib/zod/schemas/bounties";
import { formatDate } from "@dub/utils";
import { BountySubmissionFrequency } from "@prisma/client";
import { Dispatch, SetStateAction, useEffect, useMemo, useState } from "react";
@@ -542,7 +540,7 @@ export function useAddEditBountyForm({
numAmount != null && numAmount > 0 ? numAmount * 100 : null;
if (data.type === "performance") {
- const result = bountyPerformanceConditionSchema.safeParse(
+ const result = awardBountyConditionSchema.safeParse(
data.performanceCondition,
);
diff --git a/apps/web/lib/api/workflows/award-bounty/schema.ts b/apps/web/lib/api/workflows/award-bounty/schema.ts
new file mode 100644
index 0000000000..5dbf48a946
--- /dev/null
+++ b/apps/web/lib/api/workflows/award-bounty/schema.ts
@@ -0,0 +1,59 @@
+import * as z from "zod/v4";
+
+export const AWARD_BOUNTY_ATTRIBUTES = [
+ "totalLeads",
+ "totalConversions",
+ "totalSaleAmount",
+ "totalCommissions",
+] as const;
+
+export const AWARD_BOUNTY_OPERATORS = ["gte"] as const;
+
+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_OPERATOR_LABELS: Record<AwardBountyOperator, string> =
+ {
+ gte: "at least",
+ } as const;
+
+export const awardBountyConditionSchema = z.object({
+ attribute: z.enum(AWARD_BOUNTY_ATTRIBUTES),
+ operator: z.enum(AWARD_BOUNTY_OPERATORS).default("gte"),
+ value: z.number(),
+});
+
+export type AwardBountyCondition = z.infer<typeof awardBountyConditionSchema>;
+
+export type AwardBountyAttribute = (typeof AWARD_BOUNTY_ATTRIBUTES)[number];
+
+type AwardBountyOperator = (typeof AWARD_BOUNTY_OPERATORS)[number];
+
+export type AwardBountyAttributeConfig = {
+ label: string;
+ inputType: "number" | "currency";
+ operators: readonly AwardBountyOperator[];
+};
diff --git a/apps/web/lib/bounty/api/generate-performance-bounty-name.ts b/apps/web/lib/bounty/api/generate-performance-bounty-name.ts
index 803770f6eb..aa899530e8 100644
--- a/apps/web/lib/bounty/api/generate-performance-bounty-name.ts
+++ b/apps/web/lib/bounty/api/generate-performance-bounty-name.ts
@@ -1,5 +1,5 @@
import { isCurrencyAttribute } from "@/lib/api/workflows/utils";
-import { BountyPerformanceCondition } from "@/lib/types";
+import { AwardBountyCondition } from "@/lib/types";
import { currencyFormatter, nFormatter } from "@dub/utils";
import { PERFORMANCE_BOUNTY_SCOPE_ATTRIBUTES } from "./performance-bounty-scope-attributes";
@@ -8,7 +8,7 @@ export const generatePerformanceBountyName = ({
condition,
}: {
rewardAmount: number;
- condition: BountyPerformanceCondition;
+ condition: AwardBountyCondition;
}) => {
const isCurrency = isCurrencyAttribute(condition.attribute);
const attributeLabel =
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 0778ebd271..783b5b0ee1 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -71,7 +71,6 @@ import {
import { adminNetworkPartnerSchema } from "./zod/schemas/admin";
import {
BountyListSchema,
- bountyPerformanceConditionSchema,
BountySchema,
bountySocialContentIncrementalBonusSchema,
BountySubmissionExtendedSchema,
@@ -720,9 +719,7 @@ export type SocialMetricsChannel =
export type WorkflowCondition = z.infer<typeof workflowConditionSchema>;
-export type BountyPerformanceCondition = z.infer<
- typeof bountyPerformanceConditionSchema
->;
+export type { AwardBountyCondition } from "./api/workflows/award-bounty/schema";
export type BountySocialMetricsIncrementalBonus = z.infer<
typeof bountySocialContentIncrementalBonusSchema
diff --git a/apps/web/lib/zod/schemas/bounties.ts b/apps/web/lib/zod/schemas/bounties.ts
index 12ea84a8b4..19e9128c85 100644
--- a/apps/web/lib/zod/schemas/bounties.ts
+++ b/apps/web/lib/zod/schemas/bounties.ts
@@ -18,19 +18,13 @@ import {
BountyType,
} from "@prisma/client";
import * as z from "zod/v4";
+import { awardBountyConditionSchema } from "../../api/workflows/award-bounty/schema";
import { CommissionSchema } from "./commissions";
import { GroupSchema } from "./groups";
import { booleanQuerySchema, getPaginationQuerySchema } from "./misc";
import { EnrolledPartnerSchema } from "./partners";
import { UserSchema } from "./users";
import { nullableCountSchema, parseDateSchema } from "./utils";
-import { WORKFLOW_ATTRIBUTES } from "./workflows";
-
-export const bountyPerformanceConditionSchema = z.object({
- attribute: z.enum(WORKFLOW_ATTRIBUTES),
- operator: z.literal("gte").default("gte"),
- value: z.number(),
-});
// Eg: for each additional 1000 views, earn $1, up to $100
export const bountySocialContentIncrementalBonusSchema = z
@@ -123,7 +117,7 @@ export const createBountySchema = z.object({
.nullish(),
submissionRequirements: submissionRequirementsSchema.nullish(),
groupIds: z.array(z.string()).nullable(),
- performanceCondition: bountyPerformanceConditionSchema.nullish(),
+ performanceCondition: awardBountyConditionSchema.nullish(),
performanceScope: z.enum(BountyPerformanceScope).nullish(),
sendNotificationEmails: z.boolean().optional(),
});
@@ -155,9 +149,7 @@ export const BountySchema = z.object({
maxSubmissions: z.number(),
rewardAmount: z.number().nullable(),
rewardDescription: z.string().nullable(),
- performanceCondition: bountyPerformanceConditionSchema
- .nullable()
- .default(null),
+ performanceCondition: awardBountyConditionSchema.nullable().default(null),
performanceScope: z.enum(BountyPerformanceScope).nullable(),
submissionRequirements: submissionRequirementsSchema.nullable().default(null),
socialMetricsLastSyncedAt: z.date().nullable().optional(),
diff --git a/apps/web/lib/zod/schemas/partner-profile.ts b/apps/web/lib/zod/schemas/partner-profile.ts
index 303279daf9..baa1d344f7 100644
--- a/apps/web/lib/zod/schemas/partner-profile.ts
+++ b/apps/web/lib/zod/schemas/partner-profile.ts
@@ -2,6 +2,7 @@ import {
DATE_RANGE_INTERVAL_PRESETS,
DUB_PARTNERS_ANALYTICS_INTERVAL,
} from "@/lib/analytics/constants";
+import { awardBountyConditionSchema } from "@/lib/api/workflows/award-bounty/schema";
import { PARTNER_CUSTOMERS_MAX_PAGE_SIZE } from "@/lib/constants/partner-profile";
import {
CommissionType,
@@ -14,11 +15,7 @@ import {
} from "@prisma/client";
import * as z from "zod/v4";
import { analyticsQuerySchema, eventsQuerySchema } from "./analytics";
-import {
- bountyPerformanceConditionSchema,
- BountySchema,
- BountySubmissionSchema,
-} from "./bounties";
+import { BountySchema, BountySubmissionSchema } from "./bounties";
import {
CommissionSchema,
getCommissionsCountQuerySchema,
@@ -167,9 +164,7 @@ export const PartnerBountySchema = BountySchema.omit({
socialMetricsLastSyncedAt: true,
}).extend({
submissions: z.array(partnerBountySubmissionSchema),
- performanceCondition: bountyPerformanceConditionSchema
- .nullable()
- .default(null),
+ performanceCondition: awardBountyConditionSchema.nullable().default(null),
partner: z.object({
totalClicks: z.number(),
totalLeads: z.number(),