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 commission hold failures and include hold in spend limits

Kiran K4c8965060a95
diff --git a/apps/web/app/(ee)/api/workflows/create-partner-commission/route.ts b/apps/web/app/(ee)/api/workflows/create-partner-commission/route.ts
index 1515088f9d..3262637d41 100644
--- a/apps/web/app/(ee)/api/workflows/create-partner-commission/route.ts
+++ b/apps/web/app/(ee)/api/workflows/create-partner-commission/route.ts
@@ -34,6 +34,7 @@ import {
Link,
Partner,
PartnerGroup,
+ Prisma,
ProgramEnrollment,
Reward,
} from "@prisma/client";
@@ -65,6 +66,18 @@ type StepCreateCommissionOutput = {
isFirstCommission?: boolean;
};
+const commissionInclude: Prisma.CommissionInclude = {
+ customer: true,
+ link: {
+ select: {
+ id: true,
+ shortLink: true,
+ domain: true,
+ key: true,
+ },
+ },
+};
+
// POST /api/workflows/create-partner-commission
export const { POST } = serve<Input>(
async (context) => {
@@ -501,17 +514,7 @@ async function stepRunSideEffects(
where: {
id: _commission.id,
},
- include: {
- customer: true,
- link: {
- select: {
- id: true,
- shortLink: true,
- domain: true,
- key: true,
- },
- },
- },
+ include: commissionInclude,
});
const program = await prisma.program.findUniqueOrThrow({
@@ -624,17 +627,7 @@ async function stepRunSideEffects(
data: {
status: CommissionStatus.hold,
},
- include: {
- customer: true,
- link: {
- select: {
- id: true,
- shortLink: true,
- domain: true,
- key: true,
- },
- },
- },
+ include: commissionInclude,
});
await trackCommissionStatusUpdate({
@@ -643,7 +636,17 @@ async function stepRunSideEffects(
commissions: [commissionBeforeHold],
newStatus: CommissionStatus.hold,
});
- } catch (error) {}
+ } catch (error) {
+ // The update only matches pending commissions; if it fails (e.g. concurrent status change),
+ // re-fetch so side effects use the current status from the database.
+
+ commission = await prisma.commission.findUniqueOrThrow({
+ where: {
+ id: commission.id,
+ },
+ include: commissionInclude,
+ });
+ }
}
}
@@ -766,7 +769,7 @@ async function clampEarningsToSpendLimit({
...(reward.event === "sale" ? { customerId } : {}),
type: reward.event,
status: {
- in: ["pending", "processed", "paid"],
+ in: ["pending", "processed", "paid", "hold"],
},
// only need to filter if not all-time spend limit (no startDate or endDate)
...(startDate && endDate
diff --git a/apps/web/ui/activity-logs/commission-activity.tsx b/apps/web/ui/activity-logs/commission-activity.tsx
index 865a6885e5..12d2b8d88a 100644
--- a/apps/web/ui/activity-logs/commission-activity.tsx
+++ b/apps/web/ui/activity-logs/commission-activity.tsx
@@ -106,8 +106,7 @@ export function CommissionActivity({
children: (
<>
<span className="text-sm text-neutral-700">
- Commission{" "}
- {commission.status === "hold" ? "created" : "imported"} as
+ Commission imported as
</span>
<StatusBadge
icon={null}