Skip to content

Commit 79116bf

Browse files
authored
Merge pull request #2810 from dubinc/add-missing-workflows
Add executeWorkflows to a few spots that were missed earlier
2 parents 0d15b8b + 7cc3d42 commit 79116bf

File tree

5 files changed

+51
-15
lines changed

5 files changed

+51
-15
lines changed

apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
341341
livemode: event.livemode,
342342
});
343343

344-
const commission = await createPartnerCommission({
344+
await createPartnerCommission({
345345
event: "sale",
346346
programId: link.programId,
347347
partnerId: link.partnerId,
@@ -362,15 +362,25 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
362362
},
363363
});
364364

365-
if (commission) {
366-
waitUntil(
365+
waitUntil(
366+
Promise.allSettled([
367367
executeWorkflows({
368368
trigger: WorkflowTrigger.saleRecorded,
369369
programId: link.programId,
370370
partnerId: link.partnerId,
371371
}),
372-
);
373-
}
372+
// same logic as lead.created webhook below:
373+
// if the clickEvent variable exists and there was no existing customer before,
374+
// we need to trigger the leadRecorded workflow
375+
clickEvent &&
376+
!existingCustomer &&
377+
executeWorkflows({
378+
trigger: WorkflowTrigger.leadRecorded,
379+
programId: link.programId,
380+
partnerId: link.partnerId,
381+
}),
382+
]),
383+
);
374384
}
375385

376386
waitUntil(

apps/web/app/(ee)/api/stripe/integration/webhook/invoice-paid.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,13 @@ export async function invoicePaid(event: Stripe.Event) {
209209
},
210210
});
211211

212-
if (commission) {
213-
waitUntil(
214-
executeWorkflows({
215-
trigger: WorkflowTrigger.saleRecorded,
216-
programId: link.programId,
217-
partnerId: link.partnerId,
218-
}),
219-
);
220-
}
212+
waitUntil(
213+
executeWorkflows({
214+
trigger: WorkflowTrigger.saleRecorded,
215+
programId: link.programId,
216+
partnerId: link.partnerId,
217+
}),
218+
);
221219
}
222220

223221
// send workspace webhook

apps/web/lib/actions/partners/create-commission.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { isFirstConversion } from "@/lib/analytics/is-first-conversion";
44
import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
55
import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";
6+
import { executeWorkflows } from "@/lib/api/workflows/execute-workflows";
67
import { createPartnerCommission } from "@/lib/partners/create-partner-commission";
78
import { getLeadEvent } from "@/lib/tinybird";
89
import { recordClick } from "@/lib/tinybird/record-click";
@@ -15,6 +16,7 @@ import { leadEventSchemaTB } from "@/lib/zod/schemas/leads";
1516
import { prisma } from "@dub/prisma";
1617
import { nanoid } from "@dub/utils";
1718
import { COUNTRIES_TO_CONTINENTS } from "@dub/utils/src";
19+
import { WorkflowTrigger } from "@prisma/client";
1820
import { waitUntil } from "@vercel/functions";
1921
import { authActionClient } from "../safe-action";
2022

@@ -200,6 +202,12 @@ export const createCommissionAction = authActionClient
200202
},
201203
},
202204
}),
205+
206+
executeWorkflows({
207+
trigger: WorkflowTrigger.leadRecorded,
208+
programId,
209+
partnerId,
210+
}),
203211
]);
204212
}
205213

@@ -238,6 +246,12 @@ export const createCommissionAction = authActionClient
238246
},
239247
},
240248
}),
249+
250+
executeWorkflows({
251+
trigger: WorkflowTrigger.saleRecorded,
252+
programId,
253+
partnerId,
254+
}),
241255
]);
242256
}
243257

apps/web/lib/integrations/shopify/create-sale.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { isFirstConversion } from "@/lib/analytics/is-first-conversion";
22
import { includeTags } from "@/lib/api/links/include-tags";
3+
import { executeWorkflows } from "@/lib/api/workflows/execute-workflows";
34
import { createPartnerCommission } from "@/lib/partners/create-partner-commission";
45
import { recordSale } from "@/lib/tinybird";
56
import { LeadEventTB } from "@/lib/types";
@@ -8,6 +9,7 @@ import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
89
import { transformSaleEventData } from "@/lib/webhook/transform";
910
import { prisma } from "@dub/prisma";
1011
import { nanoid } from "@dub/utils";
12+
import { WorkflowTrigger } from "@prisma/client";
1113
import { waitUntil } from "@vercel/functions";
1214
import { orderSchema } from "./schema";
1315

@@ -154,5 +156,13 @@ export async function createShopifySale({
154156
},
155157
},
156158
});
159+
160+
waitUntil(
161+
executeWorkflows({
162+
trigger: WorkflowTrigger.saleRecorded,
163+
programId: link.programId,
164+
partnerId: link.partnerId,
165+
}),
166+
);
157167
}
158168
}

apps/web/lib/partners/create-partner-commission.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
EventType,
66
WorkflowTrigger,
77
} from "@dub/prisma/client";
8-
import { log } from "@dub/utils";
8+
import { currencyFormatter, log } from "@dub/utils";
99
import { waitUntil } from "@vercel/functions";
1010
import { differenceInMonths } from "date-fns";
1111
import { recordAuditLog } from "../api/audit-logs/record-audit-log";
@@ -238,6 +238,10 @@ export const createPartnerCommission = async ({
238238
},
239239
});
240240

241+
console.log(
242+
`Created a ${event} commission ${commission.id} (${currencyFormatter(commission.earnings)}) for ${partnerId}: ${JSON.stringify(commission)}`,
243+
);
244+
241245
waitUntil(
242246
(async () => {
243247
const program = await prisma.program.findUniqueOrThrow({

0 commit comments

Comments
 (0)