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.

Refactor `saleAmount` to use `centsSchemaWithDefault` across analytics, groups, links, and partners schemas for consistent default handling.

Kiran Ke61cc77f52e4
diff --git a/apps/web/lib/zod/schemas/analytics-response.ts b/apps/web/lib/zod/schemas/analytics-response.ts
index 6d2c71b261..9a465ad424 100644
--- a/apps/web/lib/zod/schemas/analytics-response.ts
+++ b/apps/web/lib/zod/schemas/analytics-response.ts
@@ -2,7 +2,7 @@ import { TRIGGER_TYPES } from "@/lib/analytics/constants";
import { CONTINENT_CODES } from "@dub/utils";
import * as z from "zod/v4";
import { LinkTagSchema } from "./tags";
-import { centsSchema } from "./utils";
+import { centsSchemaWithDefault } from "./utils";
const analyticsTriggersResponse = z.object({
trigger: z
@@ -20,9 +20,9 @@ const analyticsTriggersResponse = z.object({
.number()
.describe("The number of sales from this trigger method")
.default(0),
- saleAmount: centsSchema
- .describe("The total amount of sales from this trigger method, in cents")
- .default(0),
+ saleAmount: centsSchemaWithDefault.describe(
+ "The total amount of sales from this trigger method, in cents",
+ ),
});
export const analyticsResponse = {
@@ -30,9 +30,9 @@ export const analyticsResponse = {
clicks: z.coerce.number().describe("The total number of clicks").default(0),
leads: z.coerce.number().describe("The total number of leads").default(0),
sales: z.coerce.number().describe("The total number of sales").default(0),
- saleAmount: centsSchema
- .describe("The total amount of sales, in cents")
- .default(0),
+ saleAmount: centsSchemaWithDefault.describe(
+ "The total amount of sales, in cents",
+ ),
}),
timeseries: z.object({
@@ -49,9 +49,9 @@ export const analyticsResponse = {
.number()
.describe("The number of sales in the interval")
.default(0),
- saleAmount: centsSchema
- .describe("The total amount of sales in the interval, in cents")
- .default(0),
+ saleAmount: centsSchemaWithDefault.describe(
+ "The total amount of sales in the interval, in cents",
+ ),
}),
continents: z.object({
@@ -72,9 +72,9 @@ export const analyticsResponse = {
.number()
.describe("The number of sales from this continent")
.default(0),
- saleAmount: centsSchema
- .describe("The total amount of sales from this continent, in cents")
- .default(0),
+ saleAmount: centsSchemaWithDefault.describe(
+ "The total amount of sales from this continent, in cents",
+ ),
}),
countries: z.object({
@@ -97,9 +97,9 @@ export const analyticsResponse = {
.number()
.describe("The number of sales from this country")
.default(0),
- saleAmount: centsSchema
- .describe("The total amount of sales from this country, in cents")
- .default(0),
+ saleAmount: centsSchemaWithDefault.describe(
+ "The total amount of sales from this country, in cents",
+ ),
}),
regions: z.object({
@@ -124,9 +124,9 @@ export const analyticsResponse = {
.number()
.describe("The number of sales from this region")
.default(0),
- saleAmount: centsSchema
- .describe("The total amount of sales from this region, in cents")
- .default(0),
+ saleAmount: centsSchemaWithDefault.describe(
+ "The total amount of sales from this region, in cents",
+ ),
}),
cities: z.object({
@@ -147,9 +147,9 @@ export const analyticsResponse = {
.default(0),
leads: z.number().describe("The number of leads from this city").default(0),
sales: z.number().describe("The number of sales from this city").default(0),
- saleAmount: centsSchema
- .describe("The total amount of sales from this city, in cents")
- .default(0),
+ saleAmount: centsSchemaWithDefault.describe(
+ "The total amount of sales from this city, in cents",
+ ),
}),
devices: z.object({
@@ -166,9 +166,9 @@ export const analyticsResponse = {
.number()
.describe("The number of sales from this device")
.default(0),
- saleAmount: centsSchema
- .describe("The total amount of sales from this device, in cents")
- .default(0),
+ saleAmount: centsSchemaWithDefault.describe(
+ "The total amount of sales from this device, in cents",
+ ),
}),
browsers: z.object({
@@ -185,9 +185,9 @@ export const analyticsResponse = {
.number()
.describe("The number of sales from this browser")
.default(0),
- saleAmount: centsSchema
- .describe("The total amount of sales from this browser, in cents")
- .default(0),
+ saleAmount: centsSchemaWithDefault.describe(
+ "The total amount of sales from this browser, in cents",
+ ),
}),
os: z.object({
@@ -195,9 +195,9 @@ export const analyticsResponse = {
clicks: z.number().describe("The number of clicks from this OS").default(0),
leads: z.number().describe("The number of leads from this OS").default(0),
sales: z.number().describe("The number of sales from this OS").default(0),
- saleAmount: centsSchema
- .describe("The total amount of sales from this OS, in cents")
- .default(0),
+ saleAmount: centsSchemaWithDefault.describe(
+ "The total amount of sales from this OS, in cents",
+ ),
}),
triggers: analyticsTriggersResponse,
@@ -219,9 +219,9 @@ export const analyticsResponse = {
.number()
.describe("The number of sales from this referer")
.default(0),
- saleAmount: centsSchema
- .describe("The total amount of sales from this referer, in cents")
- .default(0),
+ saleAmount: centsSchemaWithDefault.describe(
+ "The total amount of sales from this referer, in cents",
+ ),
}),
referer_urls: z.object({
@@ -242,11 +242,9 @@ export const analyticsResponse = {
.number()
.describe("The number of sales from this referer to this URL")
.default(0),
- saleAmount: centsSchema
- .describe(
- "The total amount of sales from this referer to this URL, in cents",
- )
- .default(0),
+ saleAmount: centsSchemaWithDefault.describe(
+ "The total amount of sales from this referer to this URL, in cents",
+ ),
}),
top_links: z.object({
@@ -283,9 +281,9 @@ export const analyticsResponse = {
.default(0),
leads: z.number().describe("The number of leads from this link").default(0),
sales: z.number().describe("The number of sales from this link").default(0),
- saleAmount: centsSchema
- .describe("The total amount of sales from this link, in cents")
- .default(0),
+ saleAmount: centsSchemaWithDefault.describe(
+ "The total amount of sales from this link, in cents",
+ ),
}),
top_urls: z.object({
@@ -298,9 +296,9 @@ export const analyticsResponse = {
.default(0),
leads: z.number().describe("The number of leads from this URL").default(0),
sales: z.number().describe("The number of sales from this URL").default(0),
- saleAmount: centsSchema
- .describe("The total amount of sales from this URL, in cents")
- .default(0),
+ saleAmount: centsSchemaWithDefault.describe(
+ "The total amount of sales from this URL, in cents",
+ ),
}),
top_base_urls: z.object({
@@ -319,9 +317,9 @@ export const analyticsResponse = {
.number()
.describe("The number of sales from this base URL")
.default(0),
- saleAmount: centsSchema
- .describe("The total amount of sales from this base URL, in cents")
- .default(0),
+ saleAmount: centsSchemaWithDefault.describe(
+ "The total amount of sales from this base URL, in cents",
+ ),
}),
utm_sources: z.object({
@@ -338,9 +336,9 @@ export const analyticsResponse = {
.number()
.describe("The number of sales with this UTM source")
.default(0),
- saleAmount: centsSchema
- .describe("The total amount of sales with this UTM source, in cents")
- .default(0),
+ saleAmount: centsSchemaWithDefault.describe(
+ "The total amount of sales with this UTM source, in cents",
+ ),
}),
utm_mediums: z.object({
@@ -357,9 +355,9 @@ export const analyticsResponse = {
.number()
.describe("The number of sales with this UTM medium")
.default(0),
- saleAmount: centsSchema
- .describe("The total amount of sales with this UTM medium, in cents")
- .default(0),
+ saleAmount: centsSchemaWithDefault.describe(
+ "The total amount of sales with this UTM medium, in cents",
+ ),
}),
utm_campaigns: z.object({
@@ -376,9 +374,9 @@ export const analyticsResponse = {
.number()
.describe("The number of sales with this UTM campaign")
.default(0),
- saleAmount: centsSchema
- .describe("The total amount of sales with this UTM campaign, in cents")
- .default(0),
+ saleAmount: centsSchemaWithDefault.describe(
+ "The total amount of sales with this UTM campaign, in cents",
+ ),
}),
utm_terms: z.object({
@@ -395,9 +393,9 @@ export const analyticsResponse = {
.number()
.describe("The number of sales with this UTM term")
.default(0),
- saleAmount: centsSchema
- .describe("The total amount of sales with this UTM term, in cents")
- .default(0),
+ saleAmount: centsSchemaWithDefault.describe(
+ "The total amount of sales with this UTM term, in cents",
+ ),
}),
utm_contents: z.object({
@@ -414,9 +412,9 @@ export const analyticsResponse = {
.number()
.describe("The number of sales with this UTM content")
.default(0),
- saleAmount: centsSchema
- .describe("The total amount of sales with this UTM content, in cents")
- .default(0),
+ saleAmount: centsSchemaWithDefault.describe(
+ "The total amount of sales with this UTM content, in cents",
+ ),
}),
top_folders: z.object({
@@ -428,9 +426,9 @@ export const analyticsResponse = {
clicks: z.number().describe("The total number of clicks").default(0),
leads: z.number().describe("The total number of leads").default(0),
sales: z.number().describe("The total number of sales").default(0),
- saleAmount: centsSchema
- .describe("The total amount of sales from this link folder, in cents")
- .default(0),
+ saleAmount: centsSchemaWithDefault.describe(
+ "The total amount of sales from this link folder, in cents",
+ ),
}),
top_link_tags: z.object({
@@ -439,9 +437,9 @@ export const analyticsResponse = {
clicks: z.number().describe("The total number of clicks").default(0),
leads: z.number().describe("The total number of leads").default(0),
sales: z.number().describe("The total number of sales").default(0),
- saleAmount: centsSchema
- .describe("The total amount of sales from this link tag, in cents")
- .default(0),
+ saleAmount: centsSchemaWithDefault.describe(
+ "The total amount of sales from this link tag, in cents",
+ ),
}),
top_domains: z.object({
@@ -449,9 +447,9 @@ export const analyticsResponse = {
clicks: z.number().describe("The total number of clicks").default(0),
leads: z.number().describe("The total number of leads").default(0),
sales: z.number().describe("The total number of sales").default(0),
- saleAmount: centsSchema
- .describe("The total amount of sales from this domain, in cents")
- .default(0),
+ saleAmount: centsSchemaWithDefault.describe(
+ "The total amount of sales from this domain, in cents",
+ ),
}),
top_partners: z.object({
@@ -469,11 +467,9 @@ export const analyticsResponse = {
clicks: z.number().describe("The total number of clicks").default(0),
leads: z.number().describe("The total number of leads").default(0),
sales: z.number().describe("The total number of sales").default(0),
- saleAmount: centsSchema
- .describe(
- "The total amount of sales from this partner for this program, in cents",
- )
- .default(0),
+ saleAmount: centsSchemaWithDefault.describe(
+ "The total amount of sales from this partner for this program, in cents",
+ ),
}),
top_groups: z.object({
@@ -487,8 +483,8 @@ export const analyticsResponse = {
clicks: z.number().describe("The total number of clicks").default(0),
leads: z.number().describe("The total number of leads").default(0),
sales: z.number().describe("The total number of sales").default(0),
- saleAmount: centsSchema
- .describe("The total amount of sales from this group, in cents")
- .default(0),
+ saleAmount: centsSchemaWithDefault.describe(
+ "The total amount of sales from this group, in cents",
+ ),
}),
} as const;
diff --git a/apps/web/lib/zod/schemas/groups.ts b/apps/web/lib/zod/schemas/groups.ts
index 6a1dc3cfb6..ea9399e76a 100644
--- a/apps/web/lib/zod/schemas/groups.ts
+++ b/apps/web/lib/zod/schemas/groups.ts
@@ -10,7 +10,7 @@ import { booleanQuerySchema, getPaginationQuerySchema } from "./misc";
import { programApplicationFormSchema } from "./program-application-form";
import { programLanderSchema } from "./program-lander";
import { RewardSchema } from "./rewards";
-import { parseUrlSchema, centsSchema } from "./utils";
+import { centsSchemaWithDefault, parseUrlSchema } from "./utils";
import { UTMTemplateSchema } from "./utm";
import { workflowConditionSchema } from "./workflows";
@@ -86,10 +86,10 @@ export const GroupSchemaExtended = GroupSchema.extend({
totalClicks: z.number().default(0),
totalLeads: z.number().default(0),
totalSales: z.number().default(0),
- totalSaleAmount: centsSchema.default(0),
+ totalSaleAmount: centsSchemaWithDefault,
totalConversions: z.number().default(0),
- totalCommissions: centsSchema.default(0),
- netRevenue: centsSchema.default(0),
+ totalCommissions: centsSchemaWithDefault,
+ netRevenue: centsSchemaWithDefault,
});
export const PartnerProgramGroupSchema = GroupWithFormDataSchema.pick({
diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.ts
index 012e2c88c9..112e071393 100644
--- a/apps/web/lib/zod/schemas/links.ts
+++ b/apps/web/lib/zod/schemas/links.ts
@@ -9,11 +9,11 @@ import {
} from "./misc";
import { LinkTagSchema } from "./tags";
import {
+ centsSchemaWithDefault,
DESTINATION_URL_MAX_LENGTH,
parseDateSchema,
parseUrlSchema,
parseUrlSchemaAllowEmpty,
- centsSchema,
} from "./utils";
export const getUrlQuerySchema = z.object({
@@ -787,11 +787,9 @@ export const LinkSchema = z
.describe(
"The total number of sales (includes recurring sales) generated by the short link.",
),
- saleAmount: centsSchema
- .default(0)
- .describe(
- "The total dollar value of sales (in cents) generated by the short link.",
- ),
+ saleAmount: centsSchemaWithDefault.describe(
+ "The total dollar value of sales (in cents) generated by the short link.",
+ ),
lastClicked: z
.string()
.nullable()
diff --git a/apps/web/lib/zod/schemas/partners.ts b/apps/web/lib/zod/schemas/partners.ts
index a03a1f195b..91333a4500 100644
--- a/apps/web/lib/zod/schemas/partners.ts
+++ b/apps/web/lib/zod/schemas/partners.ts
@@ -23,7 +23,7 @@ import {
storedR2ImageUrlSchema,
} from "./misc";
import { ProgramEnrollmentSchema } from "./programs";
-import { parseUrlSchema, centsSchema } from "./utils";
+import { centsSchema, centsSchemaWithDefault, parseUrlSchema } from "./utils";
export const PARTNERS_MAX_PAGE_SIZE = 100;
@@ -414,19 +414,15 @@ export const EnrolledPartnerSchema = PartnerSchema.pick({
.describe(
"The total number of sales generated by the partner's links (includes recurring sales)",
),
- totalSaleAmount: centsSchema
- .default(0)
- .describe("Total revenue generated by the partner's links"),
- totalCommissions: centsSchema
- .default(0)
- .describe(
- "The total commissions paid to the partner for their referrals",
- ),
- netRevenue: centsSchema
- .default(0)
- .describe(
- "Net revenue after commissions (`Total Revenue - Total Commissions`)",
- ),
+ totalSaleAmount: centsSchemaWithDefault.describe(
+ "Total revenue generated by the partner's links",
+ ),
+ totalCommissions: centsSchemaWithDefault.describe(
+ "The total commissions paid to the partner for their referrals",
+ ),
+ netRevenue: centsSchemaWithDefault.describe(
+ "Net revenue after commissions (`Total Revenue - Total Commissions`)",
+ ),
earningsPerClick: z
.number()
.nullish()
@@ -511,7 +507,7 @@ export const LeaderboardPartnerSchema = z.object({
id: z.string(),
name: z.string(),
image: z.string(),
- totalCommissions: centsSchema.default(0),
+ totalCommissions: centsSchemaWithDefault,
});
export const getPartnerCustomersQuerySchema = z
diff --git a/apps/web/lib/zod/schemas/utils.ts b/apps/web/lib/zod/schemas/utils.ts
index d37f98e834..fb36cd0c3f 100644
--- a/apps/web/lib/zod/schemas/utils.ts
+++ b/apps/web/lib/zod/schemas/utils.ts
@@ -10,6 +10,13 @@ export const centsSchema = z.preprocess(
z.number(),
);
+/** Same as centsSchema but with a default of 0. The default is on the inner z.number()
+ * so code generators (e.g. Speakeasy) can introspect it through the preprocess layer. */
+export const centsSchemaWithDefault = z.preprocess(
+ (n) => (typeof n === "bigint" ? Number(n) : n),
+ z.number().default(0),
+);
+
/** Accepts number or bigint or null (e.g. from Prisma BigInt?), outputs number | null. */
export const nullableCountSchema = z.preprocess(
(n) => (typeof n === "bigint" ? Number(n) : n),