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.

remove createCustomer and getCustomer

Steven Teyd2b2e1e39740
diff --git a/apps/web/app/api/campaigns/webhooks/stripe/route.ts b/apps/web/app/api/campaigns/webhooks/stripe/route.ts
index abd259040a..99b6a2faf5 100644
--- a/apps/web/app/api/campaigns/webhooks/stripe/route.ts
+++ b/apps/web/app/api/campaigns/webhooks/stripe/route.ts
@@ -1,4 +1,4 @@
-import { getCustomer } from "@/lib/planetscale";
+import { prismaEdge } from "@/lib/prisma/edge";
import { stripe } from "@/lib/stripe";
import { getLeadEvent, recordSale } from "@/lib/tinybird";
import { nanoid } from "@dub/utils";
@@ -94,7 +94,14 @@ async function customerCreated(event: Stripe.Event) {
return;
}
- const customer = await getCustomer({ externalId, projectConnectId });
+ const customer = await prismaEdge.customer.findUnique({
+ where: {
+ projectConnectId_externalId: {
+ projectConnectId,
+ externalId,
+ },
+ },
+ });
if (!customer) {
console.error("No customer found for `dubCustomerId`", externalId);
diff --git a/apps/web/app/api/track/lead/route.ts b/apps/web/app/api/track/lead/route.ts
index 4cb522c66e..562fe79927 100644
--- a/apps/web/app/api/track/lead/route.ts
+++ b/apps/web/app/api/track/lead/route.ts
@@ -1,7 +1,7 @@
import { DubApiError } from "@/lib/api/errors";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspaceEdge } from "@/lib/auth/workspace-edge";
-import { createCustomer, getCustomer } from "@/lib/planetscale";
+import { prismaEdge } from "@/lib/prisma/edge";
import { getClickEvent, recordCustomer, recordLead } from "@/lib/tinybird";
import { clickEventSchemaTB, trackLeadRequestSchema } from "@/lib/zod/schemas";
import { nanoid } from "@dub/utils";
@@ -24,9 +24,13 @@ export const POST = withWorkspaceEdge(
} = trackLeadRequestSchema.parse(await parseRequestBody(req));
if (externalId) {
- const customer = await getCustomer({
- externalId,
- workspaceId: workspace.id,
+ const customer = await prismaEdge.customer.findUnique({
+ where: {
+ projectId_externalId: {
+ projectId: workspace.id,
+ externalId,
+ },
+ },
});
if (customer) {
@@ -59,24 +63,28 @@ export const POST = withWorkspaceEdge(
metadata,
}),
- workspace.stripeConnectId &&
- externalId &&
- (createCustomer({
- id: customerId,
- name: customerName || "",
- email: customerEmail || "",
- avatar: customerAvatar || "",
- externalId,
- projectId: workspace.id,
- projectConnectId: workspace.stripeConnectId,
- }),
- recordCustomer({
- customer_id: customerId,
- name: customerName,
- email: customerEmail,
- avatar: customerAvatar,
- workspace_id: workspace.id,
- })),
+ ...(workspace.stripeConnectId && externalId
+ ? [
+ prismaEdge.customer.create({
+ data: {
+ id: customerId,
+ name: customerName || "", // TODO: generate random name if not provided
+ email: customerEmail,
+ avatar: customerAvatar,
+ externalId,
+ projectId: workspace.id,
+ projectConnectId: workspace.stripeConnectId,
+ },
+ }),
+ recordCustomer({
+ customer_id: customerId,
+ name: customerName,
+ email: customerEmail,
+ avatar: customerAvatar,
+ workspace_id: workspace.id,
+ }),
+ ]
+ : []),
]);
})(),
);
diff --git a/apps/web/lib/planetscale.ts b/apps/web/lib/planetscale.ts
index 64471ddee4..2650bc061d 100644
--- a/apps/web/lib/planetscale.ts
+++ b/apps/web/lib/planetscale.ts
@@ -1,6 +1,5 @@
import { nanoid, punyEncode } from "@dub/utils";
import { connect } from "@planetscale/database";
-import { Customer } from "@prisma/client";
import { DomainProps, WorkspaceProps } from "./types";
export const DATABASE_URL =
@@ -140,51 +139,3 @@ export async function getRandomKey({
return key;
}
}
-
-export const createCustomer = async ({
- id,
- name,
- email,
- avatar,
- externalId,
- projectId,
- projectConnectId,
-}: Omit<Customer, "createdAt" | "updatedAt">) => {
- return await conn.execute(
- "INSERT INTO Customer (id, name, email, avatar, externalId, projectId, projectConnectId, updatedAt) VALUES (?, ?, ?, ?, ?, ? ,?, ?)",
- [
- id,
- name,
- email,
- avatar,
- externalId,
- projectId,
- projectConnectId,
- new Date(),
- ],
- );
-};
-
-export const getCustomer = async (params: GetCustomerParams) => {
- const { externalId } = params;
-
- // By workspaceId
- if ("workspaceId" in params) {
- const { rows } = await conn.execute<Customer>(
- "SELECT * FROM Customer WHERE externalId = ? AND projectId = ? LIMIT 1",
- [externalId, params.workspaceId],
- );
-
- return rows && Array.isArray(rows) && rows.length > 0 ? rows[0] : null;
- }
-
- // By projectConnectId
- if ("projectConnectId" in params) {
- const { rows } = await conn.execute<Customer>(
- "SELECT * FROM Customer WHERE externalId = ? AND projectConnectId = ? LIMIT 1",
- [externalId, params.projectConnectId],
- );
-
- return rows && Array.isArray(rows) && rows.length > 0 ? rows[0] : null;
- }
-};