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.

update the exsiting sale payout if exists

Kiran K2ed22ba379f0
diff --git a/apps/web/lib/partners/create-sales-payout.ts b/apps/web/lib/partners/create-sales-payout.ts
index d6a293cec5..9149205c1d 100644
--- a/apps/web/lib/partners/create-sales-payout.ts
+++ b/apps/web/lib/partners/create-sales-payout.ts
@@ -1,5 +1,6 @@
import { createId } from "@/lib/api/utils";
import { prisma } from "@dub/prisma";
+import { Payout } from "@dub/prisma/client";
// Calculate the commission earned for the partner for the given program
export const createSalesPayout = async ({
@@ -42,29 +43,69 @@ export const createSalesPayout = async ({
}
const amount = sales.reduce((total, sale) => total + sale.earnings, 0);
+ const quantity = sales.length;
+ let payout: Payout | null = null;
- // Create the payout
- const payout = await tx.payout.create({
- data: {
- id: createId({ prefix: "po_" }),
+ // Check if the partner has another pending payout
+ payout = await tx.payout.findFirst({
+ where: {
programId,
partnerId,
- amount,
- periodStart,
- periodEnd,
- quantity: sales.length,
- description,
+ status: "pending",
+ type: "sales",
},
});
+ // Update the existing payout
+ if (payout) {
+ await tx.payout.update({
+ where: {
+ id: payout.id,
+ },
+ data: {
+ amount: {
+ increment: amount,
+ },
+ quantity: {
+ increment: quantity,
+ },
+ periodEnd,
+ },
+ });
+
+ console.info("Payout updated", payout);
+ }
+
+ // Create the payout
+ else {
+ payout = await tx.payout.create({
+ data: {
+ id: createId({ prefix: "po_" }),
+ programId,
+ partnerId,
+ amount,
+ periodStart,
+ periodEnd,
+ quantity,
+ description,
+ },
+ });
+
+ console.info("Payout created", payout);
+ }
+
+ if (!payout) {
+ throw new Error(
+ `Failed to create payout for ${partnerId} in ${programId}.`,
+ );
+ }
+
// Update the sales records
await tx.sale.updateMany({
where: { id: { in: sales.map((sale) => sale.id) } },
data: { payoutId: payout.id, status: "processed" },
});
- console.info("Payout created", payout);
-
return payout;
});
};