Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
transformSaleEventData,
} from "@/lib/webhook/transform";
import { prisma } from "@dub/prisma";
import { Customer, WorkflowTrigger } from "@dub/prisma/client";
import { Customer, Project, WorkflowTrigger } from "@dub/prisma/client";
import { COUNTRIES_TO_CONTINENTS, nanoid, pick } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import type Stripe from "stripe";
Expand Down Expand Up @@ -60,6 +60,22 @@ export async function checkoutSessionCompleted(
let leadEvent: LeadEventTB | undefined;
let linkId: string | undefined;

const workspace = await prisma.project.findUnique({
where: {
stripeConnectId: stripeAccountId,
},
select: {
id: true,
stripeConnectId: true,
defaultProgramId: true,
webhookEnabled: true,
},
});

if (!workspace) {
return `Workspace with stripeConnectId ${stripeAccountId} not found, skipping...`;
}

/*
for stripe checkout links:
- if client_reference_id is a dub_id, we find the click event
Expand All @@ -75,31 +91,21 @@ export async function checkoutSessionCompleted(
return `Click event with dub_id ${dubClickId} not found, skipping...`;
}

const workspace = await prisma.project.findUnique({
where: {
stripeConnectId: stripeAccountId,
},
select: {
id: true,
},
});

if (!workspace) {
return `Workspace with stripeConnectId ${stripeAccountId} not found, skipping...`;
}

existingCustomer = await prisma.customer.findFirst({
where: {
projectId: workspace.id,
// check for existing customer with the same externalId (via clickId or email)
// TODO: should we support checks for email and stripeCustomerId too?
// check for existing customer with the same externalId (via clickId or email) or email or stripeCustomerId
OR: [
{
externalId: clickEvent.click_id,
},
{
externalId: stripeCustomerEmail,
},
...(stripeCustomerEmail
? [
{
externalId: stripeCustomerEmail,
},
]
: []),
],
},
});
Expand Down Expand Up @@ -189,6 +195,7 @@ export async function checkoutSessionCompleted(
const promoCodeResponse = await attributeViaPromoCode({
promotionCodeId,
stripeAccountId,
workspace,
mode,
charge,
});
Expand All @@ -202,9 +209,22 @@ export async function checkoutSessionCompleted(
}
}
} else {
existingCustomer = await prisma.customer.findUnique({
// find customer by stripeCustomerId or email
existingCustomer = await prisma.customer.findFirst({
where: {
stripeCustomerId,
OR: [
{
stripeCustomerId,
},
...(stripeCustomerEmail
? [
{
projectId: workspace.id,
email: stripeCustomerEmail,
},
]
: []),
],
},
});

Expand Down Expand Up @@ -232,6 +252,7 @@ export async function checkoutSessionCompleted(
const promoCodeResponse = await attributeViaPromoCode({
promotionCodeId,
stripeAccountId,
workspace,
mode,
charge,
});
Expand Down Expand Up @@ -350,7 +371,7 @@ export async function checkoutSessionCompleted(
linkId,
});

const [_sale, linkUpdated, workspace] = await Promise.all([
const [_sale, linkUpdated] = await Promise.all([
recordSale(saleData),

// update link stats
Expand Down Expand Up @@ -495,11 +516,16 @@ export async function checkoutSessionCompleted(
async function attributeViaPromoCode({
promotionCodeId,
stripeAccountId,
workspace,
mode,
charge,
}: {
promotionCodeId: string;
stripeAccountId: string;
workspace: Pick<
Project,
"id" | "defaultProgramId" | "stripeConnectId" | "webhookEnabled"
>;
mode: StripeMode;
charge: Stripe.Checkout.Session;
}) {
Expand All @@ -517,26 +543,6 @@ async function attributeViaPromoCode({
return null;
}

// Find the workspace
const workspace = await prisma.project.findUnique({
where: {
stripeConnectId: stripeAccountId,
},
select: {
id: true,
stripeConnectId: true,
defaultProgramId: true,
webhookEnabled: true,
},
});

if (!workspace) {
console.log(
`Workspace with stripeConnectId ${stripeAccountId} not found, skipping...`,
);
return null;
}

if (!workspace.defaultProgramId) {
console.log(
`Workspace with stripeConnectId ${stripeAccountId} has no default program, skipping...`,
Expand Down