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 customer stats for recordSaleWithTimestamp

Steven Teycd34724d3831
diff --git a/apps/web/lib/actions/partners/create-commission.ts b/apps/web/lib/actions/partners/create-commission.ts
index e604a33b1e..2db6f7f8e3 100644
--- a/apps/web/lib/actions/partners/create-commission.ts
+++ b/apps/web/lib/actions/partners/create-commission.ts
@@ -9,6 +9,7 @@ import { clickEventSchemaTB } from "@/lib/zod/schemas/clicks";
import { createCommissionSchema } from "@/lib/zod/schemas/commissions";
import { prisma } from "@dub/prisma";
import { nanoid } from "@dub/utils";
+import { waitUntil } from "@vercel/functions";
import { authActionClient } from "../safe-action";
export const createCommissionAction = authActionClient
@@ -115,7 +116,7 @@ export const createCommissionAction = authActionClient
const leadEventId = nanoid(16);
const shouldUpdateCustomer = !customer.linkId && clickData;
- await Promise.all([
+ await Promise.allSettled([
recordLeadWithTimestamp({
...clickEvent,
event_id: leadEventId,
@@ -135,18 +136,6 @@ export const createCommissionAction = authActionClient
quantity: 1,
createdAt: finalLeadEventDate,
}),
-
- shouldUpdateCustomer &&
- prisma.customer.update({
- where: {
- id: customerId,
- },
- data: {
- linkId,
- clickId: clickData.click_id,
- clickedAt: clickData.timestamp,
- },
- }),
]);
// Record sale
@@ -161,7 +150,7 @@ export const createCommissionAction = authActionClient
const saleEventId = nanoid(16);
- await Promise.all([
+ await Promise.allSettled([
recordSaleWithTimestamp({
...clickEvent,
event_id: saleEventId,
@@ -189,23 +178,51 @@ export const createCommissionAction = authActionClient
]);
}
- // Update link stats
- await prisma.link.update({
- where: {
- id: linkId,
- },
- data: {
- leads: {
- increment: 1,
- },
- ...(shouldRecordSale && {
- sales: {
- increment: 1,
+ // link & customer updates
+ waitUntil(
+ Promise.allSettled([
+ // Update link stats
+ prisma.link.update({
+ where: {
+ id: linkId,
},
- saleAmount: {
- increment: saleAmount,
+ data: {
+ leads: {
+ increment: 1,
+ },
+ ...(shouldRecordSale && {
+ sales: {
+ increment: 1,
+ },
+ saleAmount: {
+ increment: saleAmount,
+ },
+ }),
},
}),
- },
- });
+
+ // Update customer details / stats
+ (shouldUpdateCustomer || shouldRecordSale) &&
+ prisma.customer.update({
+ where: {
+ id: customerId,
+ },
+ data: {
+ ...(shouldUpdateCustomer && {
+ linkId,
+ clickId: clickData.click_id,
+ clickedAt: clickData.timestamp,
+ }),
+ ...(shouldRecordSale && {
+ sales: {
+ increment: 1,
+ },
+ saleAmount: {
+ increment: saleAmount,
+ },
+ }),
+ },
+ }),
+ ]),
+ );
});
diff --git a/apps/web/lib/rewardful/import-commissions.ts b/apps/web/lib/rewardful/import-commissions.ts
index ffbe2534c0..b7b684070b 100644
--- a/apps/web/lib/rewardful/import-commissions.ts
+++ b/apps/web/lib/rewardful/import-commissions.ts
@@ -250,6 +250,7 @@ async function createCommission({
timestamp: new Date(sale.created_at).toISOString(),
}),
+ // update link stats
prisma.link.update({
where: { id: customerFound.linkId },
data: {
@@ -257,5 +258,14 @@ async function createCommission({
saleAmount: { increment: sale.sale_amount_cents },
},
}),
+
+ // update customer stats
+ prisma.customer.update({
+ where: { id: customerFound.id },
+ data: {
+ sales: { increment: 1 },
+ saleAmount: { increment: sale.sale_amount_cents },
+ },
+ }),
]);
}