The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Merge branch 'main' into commissions-api-metadata
Steven Tey9be9efd1682c
diff --cc apps/web/lib/zod/schemas/commissions.tsindex 9df7369a5a,261da9727d..e42bbdf8fc--- a/apps/web/lib/zod/schemas/commissions.ts+++ b/apps/web/lib/zod/schemas/commissions.ts@@@ -485,217 -450,163 +485,220 @@@ export const createPartnerCommissionSch),});-export const createManualCommissionBodySchema = z- .discriminatedUnion("type", [- // Custom commission (negative amount = clawback)- z.object({- type: z.literal("custom"),- partnerId: z- .string()- .describe("The ID of the partner to create the commission for."),- amount: centsSchema- .pipe(- z.number().refine((n) => n !== 0, {- message: "Amount cannot be 0.",- }),- )- .describe(- "The commission amount in cents. Use a negative amount to create a clawback.",- ),- date: parseDateSchema- .nullish()- .describe("If not provided, the current date will be used."),- description: z- .string()- .max(190)- .nullish()- .describe(- [- "The description of the commission. Required for clawbacks (negative `amount`).",- "May be a known clawback reason (`order_canceled`, `fraud`, `terms_violation`, `tracking_error`, `payment_failed`, `ineligible_partner`, `duplicate_commission`) or an arbitrary string (max 190 characters).",- ].join("\n"),- ),- }),+// Custom commission (negative amount = clawback)+const createCustomCommissionSchema = z.object({+ type: z.literal("custom"),+ partnerId: z+ .string()+ .describe("The ID of the partner to create the commission for."),+ amount: centsSchema+ .pipe(+ z.number().refine((n) => n !== 0, {+ message: "Amount cannot be 0.",+ }),+ )+ .describe(+ "The commission amount in cents. Use a negative amount to create a clawback.",+ ),+ date: parseDateSchema+ .nullish()+ .describe("If not provided, the current date will be used."),+ description: z+ .string()+ .max(190)+ .nullish()+ .describe(- "The description of the commission. Required for clawbacks (negative `amount`). May be a known clawback reason (`order_canceled`, `fraud`, `terms_violation`, `tracking_error`, `payment_failed`, `ineligible_partner`, `duplicate_commission`, `other`) or any other string.",++ [++ "The description of the commission. Required for clawbacks (negative `amount`).",++ "May be a known clawback reason (`order_canceled`, `fraud`, `terms_violation`, `tracking_error`, `payment_failed`, `ineligible_partner`, `duplicate_commission`) or an arbitrary string (max 190 characters).",++ ].join(" "),+ ),+});- // Lead commission- z.object({- type: z.literal("lead"),- partnerId: z- .string()- .describe("The ID of the partner to create the commission for."),- customerId: z- .string()- .nullish()- .describe(- "The customer ID to associate the commission with. Useful if the customer was already created in a prior operation and you want to associate the commission with it.",- ),- customer: createCustomerBodySchema- .nullish()- .describe(- "The full customer object to associate the commission with. Useful for creating the customer on demand.",- ),- linkId: z- .string()- .nullish()- .describe(- "The partner link ID to associate the commission with. If not provided, default to the link with the most revenue.",- ),- leadEventDate: parseDateSchema- .nullish()- .describe(- "The date and time of the lead event. If not provided, defaults to the current date and time.",- ),- leadEventName: z+const createLeadCommissionSchema = z.object({+ type: z.literal("lead"),+ partnerId: z+ .string()+ .describe("The ID of the partner to create the commission for."),+ customerId: z+ .string()+ .nullish()+ .describe(+ "The customer ID to associate the commission with. Useful if the customer was already created in a prior operation and you want to associate the commission with it.",+ ),+ customer: createCustomerBodySchema+ .nullish()+ .describe(+ "The full customer object to associate the commission with. Useful for creating the customer on demand.",+ ),+ linkId: z+ .string()+ .nullish()+ .describe(+ "The partner link ID to associate the commission with. If not provided, default to the link with the most revenue.",+ ),+ date: parseDateSchema+ .nullish()+ .describe(+ "The date and time of the lead event. If not provided, defaults to the current date and time.",+ ),+ lead: z+ .object({+ eventName: z.string()+ .trim()+ .max(255).nullish()- .default("Sign up").describe(- "The name of the lead event. If not provided, defaults to 'Sign up'.",- ),- }),+ "The name of the lead event to track. If not provided, defaults to 'Sign up'.",+ )+ .meta({ example: "Sign up" }),+ metadata: trackLeadRequestSchema.shape.metadata,+ })+ .nullish()+ .describe("The lead event object to associate the commission with."),- // Sale commission- z.object({- type: z.literal("sale"),- partnerId: z- .string()- .describe("The ID of the partner to create the commission for."),- customerId: z- .string()- .nullish()- .describe(- "The customer ID to associate the commission with. Useful if the customer was already created in a prior operation and you want to associate the commission with it.",- ),- customer: createCustomerBodySchema- .nullish()- .describe(- "The full customer object to associate the commission with. Useful for creating the customer on demand.",- ),- linkId: z- .string()- .nullish()- .describe(- "The partner link ID to associate the commission with. If not provided, default to the link with the most revenue.",- ),- importStripeInvoices: z- .boolean()- .nullish()- .default(false)- .describe(- "When `true`, import all unimported paid Stripe invoices for the customer and create a commission for each. When `false`, create a single manual sale event using `saleAmount`.",- ),- saleAmount: centsSchema- .pipe(z.number().min(0))- .nullish()- .describe(- "Required when `importStripeInvoices` is `false`. The sale amount in cents for the manual sale event. Ignored when importing from Stripe.",- ),- saleEventDate: parseDateSchema- .nullish()- .describe(- "Only used when `importStripeInvoices` is `false`. The date of the manual sale event. Defaults to the current date and time if not provided.",- ),- invoiceId: z- .string()- .nullish()- .describe(- "Only used when `importStripeInvoices` is `false`. An optional invoice ID to attach to the generated sale event and commission entry for deduplication.",- ),- productId: z- .string()- .nullish()- .describe(- "Only used when `importStripeInvoices` is `false`. An optional product ID stored on the sale event metadata – will also impact commission earnings calculation (if a `Sale` `Product ID` modifier is set).",- ),- }),- ])+ // Deprecated fields+ leadEventDate: parseDateSchema+ .nullish()+ .describe(+ "Deprecated: Use `date` instead. The date and time of the lead event. If not provided, defaults to the current date and time.",+ )+ .meta({ deprecated: true }),+ leadEventName: z+ .string()+ .nullish()+ .default("Sign up")+ .describe(+ "Deprecated: Use `lead.eventName` instead. The name of the lead event. If not provided, defaults to 'Sign up'.",+ )+ .meta({ deprecated: true }),+});++const createSaleCommissionSchema = z+ .object({+ type: z.literal("sale"),+ partnerId: z+ .string()+ .describe("The ID of the partner to create the commission for."),+ customerId: z+ .string()+ .nullish()+ .describe(+ "The customer ID to associate the commission with. Useful if the customer was already created in a prior operation and you want to associate the commission with it.",+ ),+ customer: createCustomerBodySchema+ .nullish()+ .describe(+ "The full customer object to associate the commission with. Useful for creating the customer on demand.",+ ),+ linkId: z+ .string()+ .nullish()+ .describe(+ "The partner link ID to associate the commission with. If not provided, default to the link with the most revenue.",+ ),+ importStripeInvoices: z+ .boolean()+ .nullish()+ .default(false)+ .describe(+ "When `true`, import all unimported paid Stripe invoices for the customer and create a commission for each. When `false`, create a single manual sale event using `sale.amount` (or deprecated `saleAmount`).",+ ),+ date: parseDateSchema+ .nullish()+ .describe(+ "Only used when `importStripeInvoices` is `false`. The date of the manual sale event. Defaults to the current date and time if not provided.",+ ),+ sale: z+ .object({+ amount: trackSaleRequestSchema.shape.amount.nullish(),+ currency: trackSaleRequestSchema.shape.currency,+ eventName: trackSaleRequestSchema.shape.eventName,+ paymentProcessor: trackSaleRequestSchema.shape.paymentProcessor,+ invoiceId: trackSaleRequestSchema.shape.invoiceId,+ metadata: trackSaleRequestSchema.shape.metadata,+ })+ .nullish()+ .describe("The sale event object to associate the commission with."),++ // Deprecated fields+ saleEventDate: parseDateSchema+ .nullish()+ .describe("Deprecated: Use `date` instead.")+ .meta({ deprecated: true }),+ saleAmount: centsSchema+ .pipe(z.number().min(0))+ .nullish()+ .describe("Deprecated: Use `sale.amount` instead.")+ .meta({ deprecated: true }),+ invoiceId: z+ .string()+ .nullish()+ .describe("Deprecated: Use `sale.invoiceId` instead.")+ .meta({ deprecated: true }),+ productId: z+ .string()+ .nullish()+ .describe("Deprecated: Use `sale.metadata.productId` instead.")+ .meta({ deprecated: true }),+ }).superRefine((data, ctx) => {- if (data.type === "custom") {- if (data.amount < 0 && !data.description?.trim()) {+ if (data.importStripeInvoices) {+ const conflicts = [+ data.sale != null && "sale",+ data.date != null && "date",+ data.saleAmount != null && "saleAmount",+ data.saleEventDate != null && "saleEventDate",+ (data.invoiceId || data.sale?.invoiceId) && "invoiceId",+ (data.productId || data.sale?.metadata?.productId) && "productId",+ ].filter((field): field is string => Boolean(field));++ if (conflicts.length > 0) {ctx.addIssue({- code: "custom",- message:- "`description` is required when creating a clawback (negative amount).",- path: ["description"],+ code: z.ZodIssueCode.custom,+ message: `${conflicts.map((field) => `\`${field}\``).join(", ")} cannot be provided when \`importStripeInvoices\` is enabled.`,+ path: [conflicts[0]],});}return;}- if (data.type === "sale") {- if (data.importStripeInvoices) {- return;- }+ const saleAmount = data.sale?.amount ?? data.saleAmount;- if (data.saleAmount == null) {- ctx.addIssue({- code: "custom",- message:- "`saleAmount` is required when `importStripeInvoices` is false.",- path: ["saleAmount"],- });- return;- }+ if (saleAmount == null) {+ ctx.addIssue({+ code: z.ZodIssueCode.custom,+ message:+ "`sale.amount` or `saleAmount` is required when `importStripeInvoices` is false.",+ path: data.sale ? ["sale", "amount"] : ["saleAmount"],+ });+ return;+ }++ if (saleAmount === 0) {+ ctx.addIssue({+ code: z.ZodIssueCode.custom,+ message: "Sale amount cannot be 0.",+ path: data.sale?.amount != null ? ["sale", "amount"] : ["saleAmount"],+ });+ }+ });- if (data.saleAmount === 0) {+export const createManualCommissionBodySchema = z+ .discriminatedUnion("type", [+ createCustomCommissionSchema,+ createLeadCommissionSchema,+ createSaleCommissionSchema,+ ])+ .superRefine((data, ctx) => {+ if (data.type === "custom") {+ if (data.amount < 0 && !data.description?.trim()) {ctx.addIssue({- code: z.ZodIssueCode.custom,+ code: "custom",- message: "Sale amount cannot be 0.",- path: ["saleAmount"],+ message:+ "`description` is required when creating a clawback (negative amount).",+ path: ["description"],});}}