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.

Keep commission metadata user-provided and coerce nested sale amounts.

Kiran Kb2459b29799a
diff --git a/apps/web/lib/api/commissions/create-manual-commissions.ts b/apps/web/lib/api/commissions/create-manual-commissions.ts
index e88c12f518..860ca7f2a3 100644
--- a/apps/web/lib/api/commissions/create-manual-commissions.ts
+++ b/apps/web/lib/api/commissions/create-manual-commissions.ts
@@ -475,6 +475,8 @@ async function recordEvents(args: RecordEventsArgs) {
// Record sale events
let saleEvents: z.infer<typeof saleEventSchemaTBWithTimestamp>[] = [];
+ // Only persist user-provided sale.metadata on the commission (not deprecated top-level productId).
+ let commissionMetadata: Record<string, unknown> | null = null;
if (type === "sale") {
const { importStripeInvoices, sale } = args;
@@ -486,10 +488,13 @@ async function recordEvents(args: RecordEventsArgs) {
const paymentProcessor = sale?.paymentProcessor ?? "custom";
let saleAmount = sale?.amount ?? args.saleAmount;
let currency = sale?.currency ?? "usd";
- const metadata = {
+ // Include productId in Tinybird event metadata for Sale → Product ID reward modifiers.
+ const eventMetadata = {
...(productId ? { productId } : {}),
...(sale?.metadata ?? {}),
};
+ const hasEventMetadata = Object.keys(eventMetadata).length > 0;
+ commissionMetadata = sale?.metadata ?? null;
if (importStripeInvoices) {
saleEvents = stripeCustomerInvoices.map((invoice) =>
@@ -530,7 +535,7 @@ async function recordEvents(args: RecordEventsArgs) {
payment_processor: paymentProcessor,
currency,
timestamp: new Date(saleEventDate).toISOString(),
- metadata: metadata ? JSON.stringify(metadata) : "",
+ metadata: hasEventMetadata ? JSON.stringify(eventMetadata) : "",
}),
];
}
@@ -556,7 +561,7 @@ async function recordEvents(args: RecordEventsArgs) {
(invoice) => invoice.id === saleEvent.invoice_id,
);
- const metadata = saleEvent.metadata
+ const eventMetadata = saleEvent.metadata
? JSON.parse(saleEvent.metadata)
: null;
@@ -566,9 +571,9 @@ async function recordEvents(args: RecordEventsArgs) {
amount: saleEvent.amount,
currency: saleEvent.currency,
invoiceId: saleEvent.invoice_id,
- productId: metadata?.productId,
+ productId: eventMetadata?.productId,
// Only persist user-provided metadata on the commission.
- metadata: stripeInvoice ? null : metadata,
+ metadata: stripeInvoice ? null : commissionMetadata,
...(stripeInvoice?.refunded && {
status: "refunded" as const,
}),
diff --git a/apps/web/lib/zod/schemas/commissions.ts b/apps/web/lib/zod/schemas/commissions.ts
index e42bbdf8fc..008a16d916 100644
--- a/apps/web/lib/zod/schemas/commissions.ts
+++ b/apps/web/lib/zod/schemas/commissions.ts
@@ -612,7 +612,12 @@ const createSaleCommissionSchema = z
),
sale: z
.object({
- amount: trackSaleRequestSchema.shape.amount.nullish(),
+ amount: centsSchema
+ .pipe(z.number().int().min(0))
+ .nullish()
+ .describe(
+ "The amount of the sale in cents (for all two-decimal currencies). If the sale is in a zero-decimal currency, pass the full integer value (e.g. `1580` JPY). Learn more: https://d.to/currency",
+ ),
currency: trackSaleRequestSchema.shape.currency,
eventName: trackSaleRequestSchema.shape.eventName,
paymentProcessor: trackSaleRequestSchema.shape.paymentProcessor,
diff --git a/apps/web/playwright/api/commissions/commissions.spec.ts b/apps/web/playwright/api/commissions/commissions.spec.ts
index 6507df9474..8e6677d596 100644
--- a/apps/web/playwright/api/commissions/commissions.spec.ts
+++ b/apps/web/playwright/api/commissions/commissions.spec.ts
@@ -130,6 +130,7 @@ test.describe("Lead commissions", () => {
partnerId,
programId: program.id,
type: "lead",
+ expectedMetadata: null,
});
});
});
@@ -189,6 +190,7 @@ test.describe("Lead commissions", () => {
programId: program.id,
type: "lead",
expectedCreatedAt: leadEventDate,
+ expectedMetadata: null,
});
});
});
@@ -257,6 +259,7 @@ test.describe("Sale commissions", () => {
programId: program.id,
type: "sale",
invoiceId,
+ expectedMetadata: null,
});
});
});
@@ -297,6 +300,34 @@ test.describe("Sale commissions", () => {
});
});
+ test("creates using nested sale with string amount", async ({
+ api,
+ program,
+ }) => {
+ const invoiceId = `INV_${nanoid()}`;
+
+ await withCommissionPartner(api, program, async (partnerId) => {
+ expect(
+ await api.post("/api/commissions", {
+ type: "sale",
+ partnerId,
+ sale: {
+ amount: "1000",
+ invoiceId,
+ },
+ customer: customerBody(),
+ }),
+ ).toEqual(expectedQueuedResponse);
+
+ await expectCommissionCreated({
+ partnerId,
+ programId: program.id,
+ type: "sale",
+ invoiceId,
+ });
+ });
+ });
+
test("converts nested sale.currency to USD", async ({ api, program }) => {
const invoiceId = `INV_${nanoid()}`;
const previousEurRate = await redis.hget("fxRates:usd", "EUR");
@@ -360,6 +391,8 @@ test.describe("Sale commissions", () => {
expectedCreatedAt: saleEventDate,
expectedEarnings:
TEST_COMMISSION_REWARDS.sale.modifiers[0].amountInCents,
+ // Deprecated top-level productId must not leak into commission.metadata.
+ expectedMetadata: null,
});
});
});
@@ -453,6 +486,42 @@ test.describe("Sale commissions", () => {
expectedCreatedAt: date,
expectedEarnings:
TEST_COMMISSION_REWARDS.sale.modifiers[0].amountInCents,
+ // User-provided sale.metadata persists; deprecated productId does not.
+ expectedMetadata: { productId: "sku_pro" },
+ });
+ });
+ });
+
+ test("coerces non-string sale.metadata.productId in reward context", async ({
+ api,
+ program,
+ }) => {
+ const invoiceId = `INV_${nanoid()}`;
+ const metadata = { productId: 12345 };
+
+ await withCommissionPartner(api, program, async (partnerId) => {
+ expect(
+ await api.post("/api/commissions", {
+ type: "sale",
+ partnerId,
+ sale: {
+ amount: 1000,
+ invoiceId,
+ metadata,
+ },
+ customer: customerBody(),
+ }),
+ ).toEqual(expectedQueuedResponse);
+
+ await expectCommissionCreated({
+ partnerId,
+ programId: program.id,
+ type: "sale",
+ invoiceId,
+ // Non-string productId is dropped from reward context (no productId modifier).
+ expectedEarnings: TEST_COMMISSION_REWARDS.sale.amountInCents,
+ // Original metadata still persists on the commission.
+ expectedMetadata: metadata,
});
});
});
@@ -585,6 +654,20 @@ test.describe("Sale commissions", () => {
message: "custom: sale.amount: Sale amount cannot be 0.",
}),
},
+ {
+ name: "rejects non-integer sale.amount",
+ body: {
+ type: "sale",
+ partnerId: "pn_test",
+ customerId: "cus_test",
+ sale: { amount: 10.5 },
+ },
+ expected: apiError({
+ code: "unprocessable_entity",
+ message:
+ "invalid_type: sale.amount: Invalid input: expected int, received number",
+ }),
+ },
{
name: "rejects nested sale fields when importing Stripe invoices",
body: {
diff --git a/apps/web/playwright/api/commissions/helpers.ts b/apps/web/playwright/api/commissions/helpers.ts
index 532b44101e..4acbf07508 100644
--- a/apps/web/playwright/api/commissions/helpers.ts
+++ b/apps/web/playwright/api/commissions/helpers.ts
@@ -114,9 +114,7 @@ export async function expectCommissionCreated({
invoiceId: commission.invoiceId,
currency: commission.currency,
createdAt: commission.createdAt.toISOString(),
- ...(expectedMetadata !== undefined && {
- metadata: commission.metadata,
- }),
+ metadata: commission.metadata,
};
})
.toEqual({
@@ -132,6 +130,6 @@ export async function expectCommissionCreated({
createdAt: expectedCreatedAt
? expectedCreatedAt.toISOString()
: expect.any(String),
- ...(expectedMetadata !== undefined && { metadata: expectedMetadata }),
+ metadata: expectedMetadata === undefined ? null : expectedMetadata,
});
}
diff --git a/apps/web/scripts/migrations/backfill-commissions-metadata.ts b/apps/web/scripts/migrations/backfill-commissions-metadata.ts
index fbf931f412..bdaa01d81c 100644
--- a/apps/web/scripts/migrations/backfill-commissions-metadata.ts
+++ b/apps/web/scripts/migrations/backfill-commissions-metadata.ts
@@ -1,3 +1,4 @@
+import { formatUTCDateTimeClickhouse } from "@/lib/analytics/utils/format-utc-datetime-clickhouse";
import { prisma } from "@/lib/prisma";
import { tb } from "@/lib/tinybird/client";
import { CommissionType, Prisma } from "@prisma/client";
@@ -12,23 +13,29 @@ Create this once in Tinybird before running:
SELECT event_id, metadata
FROM dub_lead_events_mv
WHERE event_id IN {{ Array(eventIds, 'String') }}
+ AND timestamp BETWEEN {{ DateTime(start) }} AND {{ DateTime(end) }}
AND metadata != ''
UNION ALL
SELECT event_id, metadata
FROM dub_sale_events_mv
WHERE event_id IN {{ Array(eventIds, 'String') }}
+ AND timestamp BETWEEN {{ DateTime(start) }} AND {{ DateTime(end) }}
AND metadata != ''
*/
const DRY_RUN = true;
-const BATCH_SIZE = 100;
+const BATCH_SIZE = 1000;
const THROTTLE_MS = 1000;
const LAST_CURSOR_ID: string | null = null; // Paste the last printed cursor id here to resume after a crash.
+const USER_METADATA_MAX_CHARS = 10_000; // Matches metadataSchema in lib/zod/schemas/misc.ts
+const TIMESTAMP_PAD_MS = 86_400_000; // ±1 day: commission createdAt can lag Tinybird event timestamp
const getEventsMetadata = tb.buildPipe({
pipe: "internal_get_events_metadata",
parameters: z.object({
eventIds: z.string().array(),
+ start: z.string(),
+ end: z.string(),
}),
data: z.object({
event_id: z.string(),
@@ -40,14 +47,62 @@ async function sleep(ms: number) {
await new Promise((resolve) => setTimeout(resolve, ms));
}
-function parseMetadataObject(raw: string): Record<string, unknown> | null {
- const parsed = JSON.parse(raw);
+function isPlainObject(value: unknown): value is Record<string, unknown> {
+ return value !== null && typeof value === "object" && !Array.isArray(value);
+}
+
+// Tinybird event metadata is not always user-provided. Stripe/Shopify/importers
+// store full internal payloads; Commission.metadata is a public API field.
+// Match nested resource shapes (not just key names) to avoid false positives
+// from coincidental user metadata like { invoice: "in_123" }.
+function looksLikeInternalEventPayload(metadata: Record<string, unknown>) {
+ // Stripe webhook: { invoice } or { checkoutSession } — nested Stripe objects
+ if (
+ isPlainObject(metadata.invoice) ||
+ isPlainObject(metadata.checkoutSession)
+ ) {
+ return true;
+ }
+
+ // Shopify order body
+ if (
+ typeof metadata.checkout_token === "string" &&
+ typeof metadata.confirmation_number === "string" &&
+ isPlainObject(metadata.current_subtotal_price_set)
+ ) {
+ return true;
+ }
+
+ // Skip Rewardful/Tapfiliate/Tolt/PartnerStack fingerprints: oversized dumps
+ // are already dropped by USER_METADATA_MAX_CHARS, and small payloads are
+ // allowed through (avoids rejecting coincidental user keys).
+
+ return false;
+}
+
+function parseUserProvidedMetadata(
+ raw: string,
+): Record<string, unknown> | null {
+ if (raw.length > USER_METADATA_MAX_CHARS) {
+ return null;
+ }
+
+ const parsed: unknown = JSON.parse(raw);
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
return null;
}
- return parsed as Record<string, unknown>;
+ const metadata = parsed as Record<string, unknown>;
+
+ if (
+ Object.keys(metadata).length === 0 ||
+ looksLikeInternalEventPayload(metadata)
+ ) {
+ return null;
+ }
+
+ return metadata;
}
async function main() {
@@ -87,6 +142,7 @@ async function main() {
id: true,
eventId: true,
type: true,
+ createdAt: true,
},
take: BATCH_SIZE,
orderBy: {
@@ -104,7 +160,21 @@ async function main() {
.map((c) => c.eventId)
.filter((id): id is string => Boolean(id));
- const { data: tbRows } = await getEventsMetadata({ eventIds });
+ const start = new Date(
+ Math.min(...commissions.map((c) => c.createdAt.getTime())) -
+ TIMESTAMP_PAD_MS,
+ );
+
+ const end = new Date(
+ Math.max(...commissions.map((c) => c.createdAt.getTime())) +
+ TIMESTAMP_PAD_MS,
+ );
+
+ const { data: tbRows } = await getEventsMetadata({
+ eventIds,
+ start: formatUTCDateTimeClickhouse(start),
+ end: formatUTCDateTimeClickhouse(end),
+ });
const metadataByEventId = new Map(
tbRows.map((row) => [row.event_id, row.metadata]),
);
@@ -124,12 +194,13 @@ async function main() {
}
try {
- const parsed = parseMetadataObject(raw);
- if (!parsed) {
+ const metadata = parseUserProvidedMetadata(raw);
+ if (!metadata) {
batchSkipped++;
continue;
}
- updates.push({ id: commission.id, metadata: parsed });
+
+ updates.push({ id: commission.id, metadata });
} catch (error) {
batchErrors++;
console.error(
@@ -170,8 +241,7 @@ async function main() {
),
" ",
)}
- END,
- updatedAt = NOW()
+ END
WHERE id IN (${Prisma.join(updates.map((u) => u.id))})
AND metadata IS NULL
`;