The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Fix update sale
Steven Tey7f6f3d0d026d
diff --git a/apps/web/app/api/partners/sales/route.ts b/apps/web/app/api/partners/sales/route.tsindex e4e41c5c00..f727f2c290 100644--- a/apps/web/app/api/partners/sales/route.ts+++ b/apps/web/app/api/partners/sales/route.ts@@ -12,9 +12,8 @@ import { NextResponse } from "next/server";// PATCH /api/partners/sales - update a saleexport const PATCH = withWorkspace(async ({ req, workspace }) => {- const { programId, invoiceId, amount } = updatePartnerSaleSchema.parse(- await parseRequestBody(req),- );+ const { programId, invoiceId, amount, modifyAmount } =+ updatePartnerSaleSchema.parse(await parseRequestBody(req));const program = await getProgramOrThrow({workspaceId: workspace.id,@@ -47,7 +46,10 @@ export const PATCH = withWorkspace(});}- const { partner } = sale;+ const { partner, amount: originalAmount } = sale;+ const finalAmount = modifyAmount+ ? originalAmount + modifyAmount+ : amount ?? originalAmount;const reward = await determinePartnerReward({event: "sale",@@ -66,7 +68,7 @@ export const PATCH = withWorkspace(const earnings = calculateSaleEarnings({reward,sale: {- amount,+ amount: finalAmount,quantity: sale.quantity,},});@@ -80,23 +82,36 @@ export const PATCH = withWorkspace(earnings,},});-- // If the sale has already been paid, we need to update the payout- if (sale.status === "processed" && sale.payoutId) {- const earningsDifference = earnings - sale.earnings;-- await prisma.payout.update({+ const earningsDifference = earnings - sale.earnings;+ if (earningsDifference !== 0) {+ // update link sales+ await prisma.link.update({where: {- id: sale.payoutId,+ id: sale.linkId,},data: {- amount: {+ saleAmount: {...(earningsDifference < 0? { decrement: Math.abs(earningsDifference) }: { increment: earningsDifference }),},},});+ // If the sale has already been paid, we need to update the payout+ if (sale.status === "processed" && sale.payoutId) {+ await prisma.payout.update({+ where: {+ id: sale.payoutId,+ },+ data: {+ amount: {+ ...(earningsDifference < 0+ ? { decrement: Math.abs(earningsDifference) }+ : { increment: earningsDifference }),+ },+ },+ });+ }}return NextResponse.json(ProgramSaleSchema.parse(updatedSale));diff --git a/apps/web/lib/zod/schemas/partners.ts b/apps/web/lib/zod/schemas/partners.tsindex 7551f814eb..e5f5890c8a 100644--- a/apps/web/lib/zod/schemas/partners.ts+++ b/apps/web/lib/zod/schemas/partners.ts@@ -288,11 +288,26 @@ export const partnerAnalyticsResponseSchema = {}),} as const;-export const updatePartnerSaleSchema = z.object({- programId: z.string(),- invoiceId: z.string(),- amount: z- .number({ required_error: "Amount is required." })- .min(0)- .describe("The new amount for the sale."),-});+export const updatePartnerSaleSchema = z+ .object({+ programId: z.string(),+ invoiceId: z.string(),+ amount: z+ .number()+ .min(0)+ .describe("The new absolute amount for the sale.")+ .optional(),+ modifyAmount: z+ .number()+ .describe(+ "Modify the current sale amount: use positive values to increase the amount, negative values to decrease it.",+ )+ .optional(),+ })+ .refine(+ (data) => data.amount !== undefined || data.modifyAmount !== undefined,+ {+ message: "Either amount or modifyAmount must be provided.",+ path: ["amount"],+ },+ );