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.

improve createManualCommission to create commissions sequentially

Steven Teyeaa76a84cce5
diff --git a/apps/web/lib/actions/partners/create-manual-commission.ts b/apps/web/lib/actions/partners/create-manual-commission.ts
index df799f7182..c0e6fe5812 100644
--- a/apps/web/lib/actions/partners/create-manual-commission.ts
+++ b/apps/web/lib/actions/partners/create-manual-commission.ts
@@ -192,6 +192,15 @@ export const createManualCommissionAction = authActionClient
);
}
+ if (stripeCustomerInvoices.length > 12) {
+ throw new Error(
+ `Too many Stripe invoices found for customer ${customer.email} (${stripeCustomerInvoices.length}). Please import the invoices manually.`,
+ );
+ }
+
+ console.log("Stripe invoices to import: ");
+ console.table(stripeCustomerInvoices);
+
// use the first ever stripe invoice created date as the last lead and conversion dates
lastLeadAt = stripeCustomerInvoices[0].createdAt;
lastConversionAt = stripeCustomerInvoices[0].createdAt;
@@ -371,16 +380,11 @@ export const createManualCommissionAction = authActionClient
const tbRes = await Promise.allSettled(tbEventsToRecord.map((fn) => fn()));
console.log("Recorded events in Tinybird: ", prettyPrint(tbRes));
- // create partner commissions
- await Promise.allSettled(
- commissionsToCreate.map((commission) =>
- createPartnerCommission(commission),
- ),
- );
-
- console.log(
- `Created ${commissionsToCreate.length} commissions: ${prettyPrint(commissionsToCreate)}`,
- );
+ // create partner commissions (use a for loop to make sure the commissions are created in the correct order)
+ for (const c of commissionsToCreate) {
+ const { commission } = await createPartnerCommission(c);
+ console.log(`Commission created: ${prettyPrint(commission)}`);
+ }
waitUntil(
(async () => {
diff --git a/apps/web/lib/partners/create-partner-commission.ts b/apps/web/lib/partners/create-partner-commission.ts
index 5036dd96f1..d2776f9c5b 100644
--- a/apps/web/lib/partners/create-partner-commission.ts
+++ b/apps/web/lib/partners/create-partner-commission.ts
@@ -426,11 +426,14 @@ export const createPartnerCommission = async ({
} catch (error) {
console.error("Error creating commission", error);
- await log({
- message: `Error creating commission - ${error.message}`,
- type: "errors",
- mention: true,
- });
+ // only log to Slack if the error is not a unique constraint violation
+ if (error.code !== "P2002") {
+ await log({
+ message: `Error creating commission - ${error.message}`,
+ type: "errors",
+ mention: true,
+ });
+ }
return {
commission: null,