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.

handle charge-refunded and PATCH /commissions endpoint

Steven Teyc9ce13a3482c
diff --git a/apps/web/app/(ee)/api/commissions/[commissionId]/route.ts b/apps/web/app/(ee)/api/commissions/[commissionId]/route.ts
index 977ef4f104..01d677e0e8 100644
--- a/apps/web/app/(ee)/api/commissions/[commissionId]/route.ts
+++ b/apps/web/app/(ee)/api/commissions/[commissionId]/route.ts
@@ -1,5 +1,6 @@
import { convertCurrency } from "@/lib/analytics/convert-currency";
import { DubApiError } from "@/lib/api/errors";
+import { syncTotalCommissions } from "@/lib/api/partners/sync-total-commissions";
import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
import { calculateSaleEarnings } from "@/lib/api/sales/calculate-sale-earnings";
import { parseRequestBody } from "@/lib/api/utils";
@@ -22,13 +23,14 @@ export const PATCH = withWorkspace(async ({ workspace, params, req }) => {
const commission = await prisma.commission.findUnique({
where: {
id: commissionId,
+ programId,
},
include: {
partner: true,
},
});
- if (!commission || commission.programId !== programId) {
+ if (!commission) {
throw new DubApiError({
code: "not_found",
message: `Commission ${commissionId} not found.`,
@@ -149,5 +151,12 @@ export const PATCH = withWorkspace(async ({ workspace, params, req }) => {
);
}
+ waitUntil(
+ syncTotalCommissions({
+ partnerId: commission.partnerId,
+ programId: commission.programId,
+ }),
+ );
+
return NextResponse.json(CommissionSchema.parse(updatedCommission));
});
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/charge-refunded.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/charge-refunded.ts
index 397360a6be..04eda5d532 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/charge-refunded.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/charge-refunded.ts
@@ -61,6 +61,8 @@ export async function chargeRefunded(event: Stripe.Event) {
status: true,
payoutId: true,
earnings: true,
+ partnerId: true,
+ programId: true,
},
});
@@ -92,16 +94,31 @@ export async function chargeRefunded(event: Stripe.Event) {
}
}
- // update the commission status to refunded
- await prisma.commission.update({
- where: {
- id: commission.id,
- },
- data: {
- status: "refunded",
- payoutId: null,
- },
- });
+ await Promise.all([
+ // update the commission status to refunded
+ prisma.commission.update({
+ where: {
+ id: commission.id,
+ },
+ data: {
+ status: "refunded",
+ payoutId: null,
+ },
+ }),
+ prisma.programEnrollment.update({
+ where: {
+ partnerId_programId: {
+ partnerId: commission.partnerId,
+ programId: commission.programId,
+ },
+ },
+ data: {
+ totalCommissions: {
+ decrement: commission.earnings,
+ },
+ },
+ }),
+ ]);
return `Commission ${commission.id} updated to status "refunded"`;
}
diff --git a/apps/web/lib/api/partners/sync-total-commissions.ts b/apps/web/lib/api/partners/sync-total-commissions.ts
index e98f75c91f..3226df3730 100644
--- a/apps/web/lib/api/partners/sync-total-commissions.ts
+++ b/apps/web/lib/api/partners/sync-total-commissions.ts
@@ -15,6 +15,9 @@ export const syncTotalCommissions = async ({
},
programId,
partnerId,
+ status: {
+ in: ["pending", "processed", "paid"],
+ },
},
_sum: { earnings: true },
});