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.

refactor webhook implementation for stripe integration

Steven Tey651c3fbb6ac1
diff --git a/apps/web/app/api/stripe/connect/webhook/checkout-session-completed.ts b/apps/web/app/api/stripe/connect/webhook/checkout-session-completed.ts
index 61f48cd25b..b85538062d 100644
--- a/apps/web/app/api/stripe/connect/webhook/checkout-session-completed.ts
+++ b/apps/web/app/api/stripe/connect/webhook/checkout-session-completed.ts
@@ -87,7 +87,7 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
return `Link with ID ${linkId} not found, skipping...`;
}
- await Promise.all([
+ const [_sale, _link, workspace] = await Promise.all([
recordSale(saleData),
// update link sales count
@@ -121,30 +121,18 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
]);
waitUntil(
- (async () => {
- const workspace = await prisma.project.findUniqueOrThrow({
- where: {
- id: customer.projectId,
- },
- select: {
- id: true,
- webhookEnabled: true,
- },
- });
-
- sendWorkspaceWebhook({
- trigger: "sale.created",
- workspace,
- data: transformSaleEventData({
- ...saleData,
- link,
- customerId: customer.externalId,
- customerName: customer.name,
- customerEmail: customer.email,
- customerAvatar: customer.avatar,
- }),
- });
- })(),
+ sendWorkspaceWebhook({
+ trigger: "sale.created",
+ workspace,
+ data: transformSaleEventData({
+ ...saleData,
+ link,
+ customerId: customer.externalId,
+ customerName: customer.name,
+ customerEmail: customer.email,
+ customerAvatar: customer.avatar,
+ }),
+ }),
);
return `Checkout session completed for customer with external ID ${dubCustomerId} and invoice ID ${invoiceId}`;
diff --git a/apps/web/app/api/stripe/connect/webhook/customer-created.ts b/apps/web/app/api/stripe/connect/webhook/customer-created.ts
index 5ef2401b6a..08d7870926 100644
--- a/apps/web/app/api/stripe/connect/webhook/customer-created.ts
+++ b/apps/web/app/api/stripe/connect/webhook/customer-created.ts
@@ -77,7 +77,7 @@ export async function customerCreated(event: Stripe.Event) {
customer_id: customer.id,
};
- await Promise.all([
+ const [_customer, _lead, _link, workspace] = await Promise.all([
// Record customer
recordCustomer({
workspace_id: customer.projectId,
@@ -101,33 +101,33 @@ export async function customerCreated(event: Stripe.Event) {
},
},
}),
+
+ // update workspace usage
+ prisma.project.update({
+ where: {
+ id: customer.projectId,
+ },
+ data: {
+ usage: {
+ increment: 1,
+ },
+ },
+ }),
]);
waitUntil(
- (async () => {
- const workspace = await prisma.project.findUniqueOrThrow({
- where: {
- id: customer.projectId,
- },
- select: {
- id: true,
- webhookEnabled: true,
- },
- });
-
- sendWorkspaceWebhook({
- trigger: "lead.created",
- workspace,
- data: transformLeadEventData({
- ...leadData,
- link,
- customerId: customer.externalId,
- customerName: customer.name,
- customerEmail: customer.email,
- customerAvatar: customer.avatar,
- }),
- });
- })(),
+ sendWorkspaceWebhook({
+ trigger: "lead.created",
+ workspace,
+ data: transformLeadEventData({
+ ...leadData,
+ link,
+ customerId: customer.externalId,
+ customerName: customer.name,
+ customerEmail: customer.email,
+ customerAvatar: customer.avatar,
+ }),
+ }),
);
return `Customer created: ${customer.id}`;
diff --git a/apps/web/app/api/stripe/connect/webhook/invoice-paid.ts b/apps/web/app/api/stripe/connect/webhook/invoice-paid.ts
index 43404c2ece..3f5ef81008 100644
--- a/apps/web/app/api/stripe/connect/webhook/invoice-paid.ts
+++ b/apps/web/app/api/stripe/connect/webhook/invoice-paid.ts
@@ -70,7 +70,7 @@ export async function invoicePaid(event: Stripe.Event) {
return `Link with ID ${linkId} not found, skipping...`;
}
- await Promise.all([
+ const [_sale, _link, workspace] = await Promise.all([
recordSale(saleData),
// update link sales count
@@ -104,30 +104,18 @@ export async function invoicePaid(event: Stripe.Event) {
]);
waitUntil(
- (async () => {
- const workspace = await prisma.project.findUniqueOrThrow({
- where: {
- id: customer.projectId,
- },
- select: {
- id: true,
- webhookEnabled: true,
- },
- });
-
- sendWorkspaceWebhook({
- trigger: "sale.created",
- workspace,
- data: transformSaleEventData({
- ...saleData,
- link,
- customerId: customer.externalId,
- customerName: customer.name,
- customerEmail: customer.email,
- customerAvatar: customer.avatar,
- }),
- });
- })(),
+ sendWorkspaceWebhook({
+ trigger: "sale.created",
+ workspace,
+ data: transformSaleEventData({
+ ...saleData,
+ link,
+ customerId: customer.externalId,
+ customerName: customer.name,
+ customerEmail: customer.email,
+ customerAvatar: customer.avatar,
+ }),
+ }),
);
return `Sale recorded for customer ID ${customer.id} and invoice ID ${invoiceId}`;