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.

Convert saleAmount to BigInt

Steven Tey28e45bd9d759
diff --git a/apps/web/app/(ee)/api/admin/links/[linkId]/route.ts b/apps/web/app/(ee)/api/admin/links/[linkId]/route.ts
index d72e40ae6f..8015a0a997 100644
--- a/apps/web/app/(ee)/api/admin/links/[linkId]/route.ts
+++ b/apps/web/app/(ee)/api/admin/links/[linkId]/route.ts
@@ -1,4 +1,5 @@
import { withAdmin } from "@/lib/auth";
+import { serializeForJson } from "@/lib/utils/serialize-for-json";
import { prisma } from "@dub/prisma";
import { NextResponse } from "next/server";
@@ -16,5 +17,5 @@ export const GET = withAdmin(async ({ params }) => {
return NextResponse.json({ error: "Link not found" }, { status: 404 });
}
- return NextResponse.json(link);
+ return NextResponse.json(serializeForJson(link));
});
diff --git a/apps/web/app/(ee)/api/admin/links/route.ts b/apps/web/app/(ee)/api/admin/links/route.ts
index cdf7060920..3cf803aaf3 100644
--- a/apps/web/app/(ee)/api/admin/links/route.ts
+++ b/apps/web/app/(ee)/api/admin/links/route.ts
@@ -1,4 +1,5 @@
import { withAdmin } from "@/lib/auth";
+import { serializeForJson } from "@/lib/utils/serialize-for-json";
import { prisma } from "@dub/prisma";
import { DUB_DOMAINS_ARRAY, LEGAL_USER_ID } from "@dub/utils";
import { NextResponse } from "next/server";
@@ -85,5 +86,5 @@ export const GET = withAdmin(async ({ searchParams }) => {
tags: link.tags.map(({ tag }) => tag),
}));
- return NextResponse.json(links);
+ return NextResponse.json(serializeForJson(links));
});
diff --git a/apps/web/app/(ee)/api/cron/partners/merge-accounts/route.ts b/apps/web/app/(ee)/api/cron/partners/merge-accounts/route.ts
index c8eef8114a..624a2b65c4 100644
--- a/apps/web/app/(ee)/api/cron/partners/merge-accounts/route.ts
+++ b/apps/web/app/(ee)/api/cron/partners/merge-accounts/route.ts
@@ -11,7 +11,7 @@ import { redis } from "@/lib/upstash";
import { sendBatchEmail } from "@dub/email";
import PartnerAccountMerged from "@dub/email/templates/partner-account-merged";
import { prisma } from "@dub/prisma";
-import { log, prettyPrint, R2_URL } from "@dub/utils";
+import { log, prettyPrint, R2_URL, toCentsNumber } from "@dub/utils";
import * as z from "zod/v4";
export const dynamic = "force-dynamic";
@@ -264,8 +264,8 @@ export async function POST(req: Request) {
.then((res) => ({
totalClicks: res._sum.totalClicks ?? 0,
totalLeads: res._sum.totalLeads ?? 0,
- totalRevenue: res._sum.totalSaleAmount ?? 0,
- totalEarnings: res._sum.totalCommissions ?? 0,
+ totalRevenue: toCentsNumber(res._sum.totalSaleAmount ?? 0),
+ totalEarnings: toCentsNumber(res._sum.totalCommissions ?? 0),
}));
await prisma.partnerRewind.upsert({
diff --git a/apps/web/lib/api/links/utils/transform-link.ts b/apps/web/lib/api/links/utils/transform-link.ts
index 2132f55aff..e884e6284d 100644
--- a/apps/web/lib/api/links/utils/transform-link.ts
+++ b/apps/web/lib/api/links/utils/transform-link.ts
@@ -6,6 +6,7 @@ import {
import { Dashboard, Link, Tag } from "@dub/prisma/client";
import { prefixWorkspaceId } from "../../workspaces/workspace-id";
import { decodeLinkIfCaseSensitive } from "../case-sensitivity";
+import { toCentsNumber } from "@dub/utils";
// used in API (e.g. transformLink)
// TODO: standardize this with ExpandedLinkProps
@@ -52,6 +53,7 @@ export const transformLink = (
return {
...rest,
+ saleAmount: toCentsNumber(rest.saleAmount),
identifier: null, // backwards compatibility
tagId: tags?.[0]?.id ?? null, // backwards compatibility
tags,
diff --git a/apps/web/lib/utils/serialize-for-json.ts b/apps/web/lib/utils/serialize-for-json.ts
new file mode 100644
index 0000000000..5a22f15ef8
--- /dev/null
+++ b/apps/web/lib/utils/serialize-for-json.ts
@@ -0,0 +1,24 @@
+/**
+ * Recursively convert bigint values to number in an object so it can be safely
+ * passed to JSON.stringify (which throws on bigint by default).
+ * Use before returning API responses that may contain Prisma BigInt fields.
+ */
+export function serializeForJson<T>(obj: T): T {
+ if (obj === null || obj === undefined) {
+ return obj;
+ }
+ if (typeof obj === "bigint") {
+ return Number(obj) as T;
+ }
+ if (Array.isArray(obj)) {
+ return obj.map(serializeForJson) as T;
+ }
+ if (typeof obj === "object" && obj.constructor === Object) {
+ const result: Record<string, unknown> = {};
+ for (const [key, value] of Object.entries(obj)) {
+ result[key] = serializeForJson(value);
+ }
+ return result as T;
+ }
+ return obj;
+}
diff --git a/apps/web/lib/zod/schemas/analytics-response.ts b/apps/web/lib/zod/schemas/analytics-response.ts
index 455315f9ad..6d2c71b261 100644
--- a/apps/web/lib/zod/schemas/analytics-response.ts
+++ b/apps/web/lib/zod/schemas/analytics-response.ts
@@ -2,6 +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";
const analyticsTriggersResponse = z.object({
trigger: z
@@ -19,8 +20,7 @@ const analyticsTriggersResponse = z.object({
.number()
.describe("The number of sales from this trigger method")
.default(0),
- saleAmount: z
- .number()
+ saleAmount: centsSchema
.describe("The total amount of sales from this trigger method, in cents")
.default(0),
});
@@ -30,8 +30,7 @@ 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: z.coerce
- .number()
+ saleAmount: centsSchema
.describe("The total amount of sales, in cents")
.default(0),
}),
@@ -50,8 +49,7 @@ export const analyticsResponse = {
.number()
.describe("The number of sales in the interval")
.default(0),
- saleAmount: z
- .number()
+ saleAmount: centsSchema
.describe("The total amount of sales in the interval, in cents")
.default(0),
}),
@@ -74,8 +72,7 @@ export const analyticsResponse = {
.number()
.describe("The number of sales from this continent")
.default(0),
- saleAmount: z
- .number()
+ saleAmount: centsSchema
.describe("The total amount of sales from this continent, in cents")
.default(0),
}),
@@ -100,8 +97,7 @@ export const analyticsResponse = {
.number()
.describe("The number of sales from this country")
.default(0),
- saleAmount: z
- .number()
+ saleAmount: centsSchema
.describe("The total amount of sales from this country, in cents")
.default(0),
}),
@@ -128,8 +124,7 @@ export const analyticsResponse = {
.number()
.describe("The number of sales from this region")
.default(0),
- saleAmount: z
- .number()
+ saleAmount: centsSchema
.describe("The total amount of sales from this region, in cents")
.default(0),
}),
@@ -152,8 +147,7 @@ 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: z
- .number()
+ saleAmount: centsSchema
.describe("The total amount of sales from this city, in cents")
.default(0),
}),
@@ -172,8 +166,7 @@ export const analyticsResponse = {
.number()
.describe("The number of sales from this device")
.default(0),
- saleAmount: z
- .number()
+ saleAmount: centsSchema
.describe("The total amount of sales from this device, in cents")
.default(0),
}),
@@ -192,8 +185,7 @@ export const analyticsResponse = {
.number()
.describe("The number of sales from this browser")
.default(0),
- saleAmount: z
- .number()
+ saleAmount: centsSchema
.describe("The total amount of sales from this browser, in cents")
.default(0),
}),
@@ -203,8 +195,7 @@ 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: z
- .number()
+ saleAmount: centsSchema
.describe("The total amount of sales from this OS, in cents")
.default(0),
}),
@@ -228,8 +219,7 @@ export const analyticsResponse = {
.number()
.describe("The number of sales from this referer")
.default(0),
- saleAmount: z
- .number()
+ saleAmount: centsSchema
.describe("The total amount of sales from this referer, in cents")
.default(0),
}),
@@ -252,8 +242,7 @@ export const analyticsResponse = {
.number()
.describe("The number of sales from this referer to this URL")
.default(0),
- saleAmount: z
- .number()
+ saleAmount: centsSchema
.describe(
"The total amount of sales from this referer to this URL, in cents",
)
@@ -294,8 +283,7 @@ 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: z
- .number()
+ saleAmount: centsSchema
.describe("The total amount of sales from this link, in cents")
.default(0),
}),
@@ -310,8 +298,7 @@ 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: z
- .number()
+ saleAmount: centsSchema
.describe("The total amount of sales from this URL, in cents")
.default(0),
}),
@@ -332,8 +319,7 @@ export const analyticsResponse = {
.number()
.describe("The number of sales from this base URL")
.default(0),
- saleAmount: z
- .number()
+ saleAmount: centsSchema
.describe("The total amount of sales from this base URL, in cents")
.default(0),
}),
@@ -352,8 +338,7 @@ export const analyticsResponse = {
.number()
.describe("The number of sales with this UTM source")
.default(0),
- saleAmount: z
- .number()
+ saleAmount: centsSchema
.describe("The total amount of sales with this UTM source, in cents")
.default(0),
}),
@@ -372,8 +357,7 @@ export const analyticsResponse = {
.number()
.describe("The number of sales with this UTM medium")
.default(0),
- saleAmount: z
- .number()
+ saleAmount: centsSchema
.describe("The total amount of sales with this UTM medium, in cents")
.default(0),
}),
@@ -392,8 +376,7 @@ export const analyticsResponse = {
.number()
.describe("The number of sales with this UTM campaign")
.default(0),
- saleAmount: z
- .number()
+ saleAmount: centsSchema
.describe("The total amount of sales with this UTM campaign, in cents")
.default(0),
}),
@@ -412,8 +395,7 @@ export const analyticsResponse = {
.number()
.describe("The number of sales with this UTM term")
.default(0),
- saleAmount: z
- .number()
+ saleAmount: centsSchema
.describe("The total amount of sales with this UTM term, in cents")
.default(0),
}),
@@ -432,8 +414,7 @@ export const analyticsResponse = {
.number()
.describe("The number of sales with this UTM content")
.default(0),
- saleAmount: z
- .number()
+ saleAmount: centsSchema
.describe("The total amount of sales with this UTM content, in cents")
.default(0),
}),
@@ -447,8 +428,7 @@ 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: z
- .number()
+ saleAmount: centsSchema
.describe("The total amount of sales from this link folder, in cents")
.default(0),
}),
@@ -459,8 +439,7 @@ 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: z
- .number()
+ saleAmount: centsSchema
.describe("The total amount of sales from this link tag, in cents")
.default(0),
}),
@@ -470,8 +449,7 @@ 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: z
- .number()
+ saleAmount: centsSchema
.describe("The total amount of sales from this domain, in cents")
.default(0),
}),
@@ -491,8 +469,7 @@ 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: z
- .number()
+ saleAmount: centsSchema
.describe(
"The total amount of sales from this partner for this program, in cents",
)
@@ -510,8 +487,7 @@ 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: z
- .number()
+ saleAmount: centsSchema
.describe("The total amount of sales from this group, in cents")
.default(0),
}),
diff --git a/apps/web/lib/zod/schemas/commissions.ts b/apps/web/lib/zod/schemas/commissions.ts
index 0104c368f3..592cbd5f98 100644
--- a/apps/web/lib/zod/schemas/commissions.ts
+++ b/apps/web/lib/zod/schemas/commissions.ts
@@ -5,7 +5,7 @@ import { CustomerSchema } from "./customers";
import { LinkSchema } from "./links";
import { getPaginationQuerySchema } from "./misc";
import { EnrolledPartnerSchema, WebhookPartnerSchema } from "./partners";
-import { parseDateSchema } from "./utils";
+import { centsSchema, parseDateSchema } from "./utils";
export const CommissionSchema = z.object({
id: z.string().describe("The commission's unique ID on Dub.").meta({
@@ -146,7 +146,7 @@ export const createCommissionSchema = z.object({
// Sale
saleEventDate: parseDateSchema.nullish(),
- saleAmount: z.number().min(0).nullish(),
+ saleAmount: centsSchema.min(0).nullish(),
invoiceId: z.string().nullish(),
productId: z.string().nullish(),
});
diff --git a/apps/web/lib/zod/schemas/customers.ts b/apps/web/lib/zod/schemas/customers.ts
index 3980e66b11..aa3c16dccb 100644
--- a/apps/web/lib/zod/schemas/customers.ts
+++ b/apps/web/lib/zod/schemas/customers.ts
@@ -3,6 +3,7 @@ import { DiscountSchema } from "./discount";
import { LinkSchema } from "./links";
import { booleanQuerySchema, getPaginationQuerySchema } from "./misc";
import { PartnerSchema } from "./partners";
+import { centsSchema } from "./utils";
export const CUSTOMERS_MAX_PAGE_SIZE = 100;
@@ -141,8 +142,7 @@ export const CustomerSchema = z.object({
.number()
.nullish()
.describe("Total number of sales for the customer."),
- saleAmount: z
- .number()
+ saleAmount: centsSchema
.nullish()
.describe("Total amount of sales for the customer."),
createdAt: z
diff --git a/apps/web/lib/zod/schemas/groups.ts b/apps/web/lib/zod/schemas/groups.ts
index 8aad59967a..6a1dc3cfb6 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 } from "./utils";
+import { parseUrlSchema, centsSchema } 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: z.number().default(0),
+ totalSaleAmount: centsSchema.default(0),
totalConversions: z.number().default(0),
- totalCommissions: z.number().default(0),
- netRevenue: z.number().default(0),
+ totalCommissions: centsSchema.default(0),
+ netRevenue: centsSchema.default(0),
});
export const PartnerProgramGroupSchema = GroupWithFormDataSchema.pick({
diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.ts
index 1ff2800b6f..012e2c88c9 100644
--- a/apps/web/lib/zod/schemas/links.ts
+++ b/apps/web/lib/zod/schemas/links.ts
@@ -13,6 +13,7 @@ import {
parseDateSchema,
parseUrlSchema,
parseUrlSchemaAllowEmpty,
+ centsSchema,
} from "./utils";
export const getUrlQuerySchema = z.object({
@@ -786,8 +787,7 @@ export const LinkSchema = z
.describe(
"The total number of sales (includes recurring sales) generated by the short link.",
),
- saleAmount: z
- .number()
+ saleAmount: centsSchema
.default(0)
.describe(
"The total dollar value of sales (in cents) generated by the short link.",
diff --git a/apps/web/lib/zod/schemas/partner-profile.ts b/apps/web/lib/zod/schemas/partner-profile.ts
index 0ad18dfbfc..60816b3cd2 100644
--- a/apps/web/lib/zod/schemas/partner-profile.ts
+++ b/apps/web/lib/zod/schemas/partner-profile.ts
@@ -29,6 +29,7 @@ import { LinkSchema } from "./links";
import { getPaginationQuerySchema } from "./misc";
import { payoutsQuerySchema } from "./payouts";
import { referralFormDataSchema } from "./referral-form";
+import { centsSchema } from "./utils";
export const PartnerEarningsSchema = CommissionSchema.omit({
userId: true,
@@ -167,8 +168,8 @@ export const PartnerBountySchema = BountySchema.omit({
totalLeads: z.number(),
totalConversions: z.number(),
totalSales: z.number(),
- totalSaleAmount: z.number(),
- totalCommissions: z.number(),
+ totalSaleAmount: centsSchema,
+ totalCommissions: centsSchema,
}),
});
diff --git a/apps/web/lib/zod/schemas/partners.ts b/apps/web/lib/zod/schemas/partners.ts
index f4b6f7d45c..8980902d4b 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 } from "./utils";
+import { parseUrlSchema, centsSchema } from "./utils";
export const PARTNERS_MAX_PAGE_SIZE = 100;
@@ -413,18 +413,15 @@ export const EnrolledPartnerSchema = PartnerSchema.pick({
.describe(
"The total number of sales generated by the partner's links (includes recurring sales)",
),
- totalSaleAmount: z
- .number()
+ totalSaleAmount: centsSchema
.default(0)
.describe("Total revenue generated by the partner's links"),
- totalCommissions: z
- .number()
+ totalCommissions: centsSchema
.default(0)
.describe(
"The total commissions paid to the partner for their referrals",
),
- netRevenue: z
- .number()
+ netRevenue: centsSchema
.default(0)
.describe(
"Net revenue after commissions (`Total Revenue - Total Commissions`)",
@@ -504,15 +501,15 @@ export const WebhookPartnerSchema = PartnerSchema.pick({
totalLeads: z.number(),
totalConversions: z.number(),
totalSales: z.number(),
- totalSaleAmount: z.number(),
- totalCommissions: z.number(),
+ totalSaleAmount: centsSchema,
+ totalCommissions: centsSchema,
});
export const LeaderboardPartnerSchema = z.object({
id: z.string(),
name: z.string(),
image: z.string(),
- totalCommissions: z.number().default(0),
+ totalCommissions: centsSchema.default(0),
});
export const getPartnerCustomersQuerySchema = z
diff --git a/apps/web/lib/zod/schemas/programs.ts b/apps/web/lib/zod/schemas/programs.ts
index 56f0c42c59..bb1756b1fb 100644
--- a/apps/web/lib/zod/schemas/programs.ts
+++ b/apps/web/lib/zod/schemas/programs.ts
@@ -23,7 +23,7 @@ import { programInviteEmailDataSchema } from "./program-invite-email";
import { referralFormSchema } from "./referral-form";
import { RewardSchema } from "./rewards";
import { UserSchema } from "./users";
-import { parseDateSchema } from "./utils";
+import { parseDateSchema, centsSchema } from "./utils";
export const ProgramSchema = z.object({
id: z.string(),
@@ -111,7 +111,7 @@ export const ProgramEnrollmentSchema = z.object({
.array(ProgramPartnerLinkSchema)
.nullable()
.describe("The partner's referral links in this program."),
- totalCommissions: z.number().default(0),
+ totalCommissions: centsSchema.default(0),
rewards: z.array(RewardSchema).nullish(),
clickRewardId: z.string().nullish(),
leadRewardId: z.string().nullish(),
diff --git a/apps/web/lib/zod/schemas/referrals.ts b/apps/web/lib/zod/schemas/referrals.ts
index db734e24a9..54878427b1 100644
--- a/apps/web/lib/zod/schemas/referrals.ts
+++ b/apps/web/lib/zod/schemas/referrals.ts
@@ -3,6 +3,7 @@ import * as z from "zod/v4";
import { getPaginationQuerySchema } from "./misc";
import { PartnerSchema } from "./partners";
import { referralFormDataSchema } from "./referral-form";
+import { centsSchema } from "./utils";
export const referralSchema = z.object({
id: z.string(),
@@ -105,9 +106,10 @@ export const updateReferralStatusSchema = z.discriminatedUnion("status", [
updateReferralStatusBaseSchema.extend({
status: z.literal("closedWon"),
- saleAmount: z
- .number()
- .min(0, "Sale amount must be greater than or equal to 0"),
+ saleAmount: centsSchema.min(
+ 0,
+ "Sale amount must be greater than or equal to 0",
+ ),
stripeCustomerId: z.string().optional(),
}),
diff --git a/apps/web/lib/zod/schemas/rewards.ts b/apps/web/lib/zod/schemas/rewards.ts
index 3b1cdd8490..437ee2b178 100644
--- a/apps/web/lib/zod/schemas/rewards.ts
+++ b/apps/web/lib/zod/schemas/rewards.ts
@@ -1,6 +1,7 @@
import { EventType, RewardStructure } from "@dub/prisma/client";
import * as z from "zod/v4";
import { getPaginationQuerySchema, maxDurationSchema } from "./misc";
+import { centsSchema } from "./utils";
export const COMMISSION_TYPES = [
{
@@ -372,8 +373,8 @@ export const rewardContextSchema = z.object({
totalClicks: z.number().nullish(),
totalLeads: z.number().nullish(),
totalConversions: z.number().nullish(),
- totalSaleAmount: z.number().nullish(),
- totalCommissions: z.number().nullish(),
+ totalSaleAmount: centsSchema.nullish(),
+ totalCommissions: centsSchema.nullish(),
})
.optional(),
});
diff --git a/apps/web/lib/zod/schemas/sales.ts b/apps/web/lib/zod/schemas/sales.ts
index 6639a576cf..ae087e23cc 100644
--- a/apps/web/lib/zod/schemas/sales.ts
+++ b/apps/web/lib/zod/schemas/sales.ts
@@ -3,6 +3,7 @@ import { clickEventSchema, clickEventSchemaTB } from "./clicks";
import { CustomerSchema } from "./customers";
import { commonDeprecatedEventFields } from "./deprecated";
import { linkEventSchema } from "./links";
+import { centsSchema } from "./utils";
export const trackSaleRequestSchema = z.object({
customerExternalId: z
@@ -142,7 +143,7 @@ export const saleEventSchemaTBEndpoint = z.object({
customer_id: z.string(),
payment_processor: z.string(),
invoice_id: z.string(),
- saleAmount: z.number(),
+ saleAmount: centsSchema,
click_id: z.string(),
link_id: z.string(),
url: z.string(),
@@ -182,8 +183,7 @@ export const saleEventResponseSchema = z
click: clickEventSchema,
customer: CustomerSchema,
// deprecated fields
- saleAmount: z
- .number()
+ saleAmount: centsSchema
.describe("Deprecated: Use `sale.amount` instead.")
.meta({ deprecated: true }),
invoice_id: z
diff --git a/apps/web/lib/zod/schemas/utils.ts b/apps/web/lib/zod/schemas/utils.ts
index b9f781e3b8..dd516c42dd 100644
--- a/apps/web/lib/zod/schemas/utils.ts
+++ b/apps/web/lib/zod/schemas/utils.ts
@@ -4,6 +4,11 @@ import * as z from "zod/v4";
// This is the default max length for URL validation
export const DESTINATION_URL_MAX_LENGTH = 32000;
+/** Accepts number (before migration) or bigint (after), outputs number. */
+export const centsSchema = z
+ .union([z.number(), z.bigint()])
+ .transform((n) => (typeof n === "bigint" ? Number(n) : n));
+
export const parseUrlSchema = z
.string()
.max(DESTINATION_URL_MAX_LENGTH, {
diff --git a/apps/web/scripts/dub-partner-rewind.ts b/apps/web/scripts/dub-partner-rewind.ts
index 30f1c31adb..d7d06e65f0 100644
--- a/apps/web/scripts/dub-partner-rewind.ts
+++ b/apps/web/scripts/dub-partner-rewind.ts
@@ -1,6 +1,6 @@
import { EXCLUDED_PROGRAM_IDS } from "@/lib/constants/partner-profile";
import { prisma } from "@dub/prisma";
-import { chunk } from "@dub/utils";
+import { chunk, toCentsNumber } from "@dub/utils";
import "dotenv-flow/config";
const REWIND_EARNINGS_MINIMUM = 1_00; // $1
@@ -35,8 +35,8 @@ async function main() {
year: 2025,
totalClicks: _sum.totalClicks ?? 0,
totalLeads: _sum.totalLeads ?? 0,
- totalRevenue: _sum.totalSaleAmount ?? 0,
- totalEarnings: _sum.totalCommissions ?? 0,
+ totalRevenue: toCentsNumber(_sum.totalSaleAmount ?? 0),
+ totalEarnings: toCentsNumber(_sum.totalCommissions ?? 0),
}));
console.table(payloads.slice(0, 10));
diff --git a/packages/prisma/schema/customer.prisma b/packages/prisma/schema/customer.prisma
index 4467554367..45d94383a8 100644
--- a/packages/prisma/schema/customer.prisma
+++ b/packages/prisma/schema/customer.prisma
@@ -12,7 +12,7 @@ model Customer {
country String?
sales Int @default(0)
- saleAmount Int @default(0)
+ saleAmount BigInt @default(0)
firstSaleAt DateTime?
subscriptionCanceledAt DateTime?
diff --git a/packages/prisma/schema/discount.prisma b/packages/prisma/schema/discount.prisma
index 154bd8d073..05d9a6089c 100644
--- a/packages/prisma/schema/discount.prisma
+++ b/packages/prisma/schema/discount.prisma
@@ -1,15 +1,15 @@
model Discount {
- id String @id @default(cuid())
- programId String
- amount Int @default(0)
- type RewardStructure @default(percentage)
- maxDuration Int? // in months (0 -> one-time purchase, 1, 3, 6, 12, 18, 24, 36, 48 -> months, null -> lifetime)
- description String?
- couponId String?
- couponTestId String?
+ id String @id @default(cuid())
+ programId String
+ amount Int @default(0)
+ type RewardStructure @default(percentage)
+ maxDuration Int? // in months (0 -> one-time purchase, 1, 3, 6, 12, 18, 24, 36, 48 -> months, null -> lifetime)
+ description String?
+ couponId String?
+ couponTestId String?
autoProvisionEnabledAt DateTime? // Auto create discount codes for partners when they join the group
- createdAt DateTime @default(now())
- updatedAt DateTime @updatedAt
+ createdAt DateTime @default(now())
+ updatedAt DateTime @updatedAt
programEnrollments ProgramEnrollment[]
program Program @relation("ProgramDiscounts", fields: [programId], references: [id])
diff --git a/packages/prisma/schema/link.prisma b/packages/prisma/schema/link.prisma
index e9c3f7502c..cd5c7d5a91 100644
--- a/packages/prisma/schema/link.prisma
+++ b/packages/prisma/schema/link.prisma
@@ -58,7 +58,7 @@ model Link {
leads Int @default(0) // number of leads
conversions Int @default(0) // number of leads that converted to sales
sales Int @default(0) // number of sales (includes recurring sales)
- saleAmount Int @default(0) // total revenue from sales (in cents)
+ saleAmount BigInt @default(0) // total revenue from sales (in cents)
lastClicked DateTime? // when the link was last clicked
lastLeadAt DateTime? // when the link last brought in a new lead
diff --git a/packages/prisma/schema/program.prisma b/packages/prisma/schema/program.prisma
index b4b406f034..10612677a1 100644
--- a/packages/prisma/schema/program.prisma
+++ b/packages/prisma/schema/program.prisma
@@ -107,15 +107,15 @@ model ProgramEnrollment {
status ProgramEnrollmentStatus @default(pending)
// partner stats
- totalClicks Int @default(0) // total clicks
- totalLeads Int @default(0) // total leads
- totalConversions Int @default(0) // total conversions
- totalSales Int @default(0) // total sales
- totalSaleAmount Int @default(0) // total sale amount (in cents)
- totalCommissions Int @default(0) // total commissions earned by the partner (in cents)
+ totalClicks Int @default(0) // total clicks
+ totalLeads Int @default(0) // total leads
+ totalConversions Int @default(0) // total conversions
+ totalSales Int @default(0) // total sales
+ totalSaleAmount BigInt @default(0) // total sale amount (in cents)
+ totalCommissions BigInt @default(0) // total commissions earned by the partner (in cents)
// calculated stats
- netRevenue Int @default(0) // totalSaleAmount - totalCommissions
+ netRevenue BigInt @default(0) // totalSaleAmount - totalCommissions
earningsPerClick Float @default(0) // totalSaleAmount / totalClicks
averageLifetimeValue Float? // totalSaleAmount / totalConversions (in cents)
clickToLeadRate Float? // totalLeads / totalClicks
diff --git a/packages/utils/src/functions/cents.ts b/packages/utils/src/functions/cents.ts
new file mode 100644
index 0000000000..5f16eb731e
--- /dev/null
+++ b/packages/utils/src/functions/cents.ts
@@ -0,0 +1,8 @@
+/**
+ * Normalize a cents value from Prisma (number before migration, bigint after) to number.
+ * Use for display, JSON serialization, and anywhere a number is required.
+ */
+export function toCentsNumber(value: number | bigint | null | undefined): number {
+ if (value == null) return 0;
+ return typeof value === "bigint" ? Number(value) : value;
+}
diff --git a/packages/utils/src/functions/currency-formatter.ts b/packages/utils/src/functions/currency-formatter.ts
index 894d1f0ba6..0a05da236a 100644
--- a/packages/utils/src/functions/currency-formatter.ts
+++ b/packages/utils/src/functions/currency-formatter.ts
@@ -1,3 +1,4 @@
+import { toCentsNumber } from "./cents";
import { isZeroDecimalCurrency } from "./currency-zero-decimal";
interface CurrencyFormatterOptions extends Intl.NumberFormatOptions {
@@ -5,9 +6,10 @@ interface CurrencyFormatterOptions extends Intl.NumberFormatOptions {
}
export const currencyFormatter = (
- valueInCents: number,
+ valueInCents: number | bigint,
options?: CurrencyFormatterOptions,
) => {
+ const cents = toCentsNumber(valueInCents);
const currency = options?.currency || "USD";
return Intl.NumberFormat("en-US", {
style: "currency",
@@ -17,6 +19,6 @@ export const currencyFormatter = (
: "auto",
...options,
} as CurrencyFormatterOptions).format(
- isZeroDecimalCurrency(currency) ? valueInCents : valueInCents / 100,
+ isZeroDecimalCurrency(currency) ? cents : cents / 100,
);
};
diff --git a/packages/utils/src/functions/index.ts b/packages/utils/src/functions/index.ts
index f2a577251a..a81d92b261 100644
--- a/packages/utils/src/functions/index.ts
+++ b/packages/utils/src/functions/index.ts
@@ -5,6 +5,7 @@ export * from "./chunk";
export * from "./cn";
export * from "./combine-words";
export * from "./construct-metadata";
+export * from "./cents";
export * from "./currency-formatter";
export * from "./currency-zero-decimal";
export * from "./datetime";