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.

Reject conflicting sale fields in Zod when importing Stripe invoices.

Kiran K722cc0ac8310
diff --git a/apps/web/lib/api/commissions/create-manual-commissions.ts b/apps/web/lib/api/commissions/create-manual-commissions.ts
index 4b50d632b2..e88c12f518 100644
--- a/apps/web/lib/api/commissions/create-manual-commissions.ts
+++ b/apps/web/lib/api/commissions/create-manual-commissions.ts
@@ -114,22 +114,9 @@ export async function createManualCommissions(args: CreateCommissionsArgs) {
});
if (type === "sale") {
- const { importStripeInvoices, saleAmount, saleEventDate, productId, sale } =
- args;
-
- const invoiceId = sale?.invoiceId ?? args.invoiceId;
- const hasManualSaleFields =
- saleAmount || saleEventDate || invoiceId || productId;
+ const { importStripeInvoices, sale } = args;
if (importStripeInvoices) {
- if (hasManualSaleFields) {
- throw new DubApiError({
- code: "bad_request",
- message:
- "saleAmount, saleEventDate, invoiceId, and productId cannot be provided when importStripeInvoices is enabled.",
- });
- }
-
if (!workspace.stripeConnectId) {
throw new DubApiError({
code: "bad_request",
@@ -145,6 +132,8 @@ export async function createManualCommissions(args: CreateCommissionsArgs) {
}
}
+ const invoiceId = sale?.invoiceId ?? args.invoiceId;
+
if (invoiceId) {
const commission = await prisma.commission.findUnique({
where: {
diff --git a/apps/web/lib/zod/schemas/commissions.ts b/apps/web/lib/zod/schemas/commissions.ts
index d61cdd8b51..7bcbf3037d 100644
--- a/apps/web/lib/zod/schemas/commissions.ts
+++ b/apps/web/lib/zod/schemas/commissions.ts
@@ -580,73 +580,115 @@ const createLeadCommissionSchema = z.object({
.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."),
+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 }),
-});
+ // 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.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: z.ZodIssueCode.custom,
+ message: `${conflicts.map((field) => `\`${field}\``).join(", ")} cannot be provided when \`importStripeInvoices\` is enabled.`,
+ path: [conflicts[0]],
+ });
+ }
+ return;
+ }
+
+ const saleAmount = data.sale?.amount ?? data.saleAmount;
+
+ 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"],
+ });
+ }
+ });
export const createManualCommissionBodySchema = z
.discriminatedUnion("type", [
@@ -664,33 +706,6 @@ export const createManualCommissionBodySchema = z
path: ["description"],
});
}
- return;
- }
-
- if (data.type === "sale") {
- if (data.importStripeInvoices) {
- return;
- }
-
- const saleAmount = data.sale?.amount ?? data.saleAmount;
-
- 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"],
- });
- }
}
});
diff --git a/apps/web/playwright/api/commissions/commissions.spec.ts b/apps/web/playwright/api/commissions/commissions.spec.ts
index 678b9f2869..de851ce550 100644
--- a/apps/web/playwright/api/commissions/commissions.spec.ts
+++ b/apps/web/playwright/api/commissions/commissions.spec.ts
@@ -597,7 +597,7 @@ test.describe("Sale commissions", () => {
expected: apiError({
code: "unprocessable_entity",
message:
- "custom: sale: `sale` and `date` cannot be provided when `importStripeInvoices` is enabled.",
+ "custom: sale: `sale`, `date`, `invoiceId`, `productId` cannot be provided when `importStripeInvoices` is enabled.",
}),
},
{