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.

getCustomerOrThrow

Steven Tey846aa5206543
diff --git a/apps/web/app/api/customers/[id]/route.ts b/apps/web/app/api/customers/[id]/route.ts
index c4b574d211..a77d904f05 100644
--- a/apps/web/app/api/customers/[id]/route.ts
+++ b/apps/web/app/api/customers/[id]/route.ts
@@ -1,3 +1,4 @@
+import { getCustomerOrThrow } from "@/lib/api/customers/get-customer-or-throw";
import { DubApiError } from "@/lib/api/errors";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
@@ -13,20 +14,11 @@ export const GET = withWorkspace(
async ({ workspace, params }) => {
const { id } = params;
- const customer = await prisma.customer.findUnique({
- where: {
- id,
- projectId: workspace.id,
- },
+ const customer = await getCustomerOrThrow({
+ id,
+ workspaceId: workspace.id,
});
- if (!customer) {
- throw new DubApiError({
- code: "not_found",
- message: "Customer not found",
- });
- }
-
return NextResponse.json(CustomerSchema.parse(customer));
},
{
@@ -39,15 +31,19 @@ export const PATCH = withWorkspace(
async ({ workspace, params, req }) => {
const { id } = params;
- const { externalId, name, email, avatar } = updateCustomerBodySchema.parse(
+ const { name, email, avatar, externalId } = updateCustomerBodySchema.parse(
await parseRequestBody(req),
);
+ await getCustomerOrThrow({
+ id,
+ workspaceId: workspace.id,
+ });
+
try {
const customer = await prisma.customer.update({
where: {
id,
- projectId: workspace.id,
},
data: { name, email, avatar, externalId },
});
@@ -83,11 +79,15 @@ export const DELETE = withWorkspace(
async ({ workspace, params }) => {
const { id } = params;
+ await getCustomerOrThrow({
+ id,
+ workspaceId: workspace.id,
+ });
+
try {
await prisma.customer.delete({
where: {
id,
- projectId: workspace.id,
},
});
diff --git a/apps/web/app/api/customers/route.ts b/apps/web/app/api/customers/route.ts
index 3b6142dd00..ecaa9daad3 100644
--- a/apps/web/app/api/customers/route.ts
+++ b/apps/web/app/api/customers/route.ts
@@ -32,7 +32,7 @@ export const GET = withWorkspace(
// POST /api/customers – Create a new customer
export const POST = withWorkspace(
async ({ req, workspace }) => {
- const { externalId, email, name, avatar } = createCustomerBodySchema.parse(
+ const { email, name, avatar, externalId } = createCustomerBodySchema.parse(
await parseRequestBody(req),
);
diff --git a/apps/web/lib/api/customers/get-customer-or-throw.ts b/apps/web/lib/api/customers/get-customer-or-throw.ts
new file mode 100644
index 0000000000..6f78224ad5
--- /dev/null
+++ b/apps/web/lib/api/customers/get-customer-or-throw.ts
@@ -0,0 +1,23 @@
+import { prisma } from "@/lib/prisma";
+import { DubApiError } from "../errors";
+
+export const getCustomerOrThrow = async ({
+ id,
+ workspaceId,
+}: {
+ id: string;
+ workspaceId: string;
+}) => {
+ const customer = await prisma.customer.findUnique({
+ where: { id },
+ });
+
+ if (!customer || customer.projectId !== workspaceId) {
+ throw new DubApiError({
+ code: "not_found",
+ message: "Customer not found",
+ });
+ }
+
+ return customer;
+};
diff --git a/apps/web/lib/zod/schemas/customers.ts b/apps/web/lib/zod/schemas/customers.ts
index 1fd1087705..9755aa575b 100644
--- a/apps/web/lib/zod/schemas/customers.ts
+++ b/apps/web/lib/zod/schemas/customers.ts
@@ -1,9 +1,6 @@
import z from "@/lib/zod";
export const createCustomerBodySchema = z.object({
- externalId: z
- .string()
- .describe("Unique identifier for the customer in the client's app."),
email: z
.string()
.email()
@@ -20,6 +17,9 @@ export const createCustomerBodySchema = z.object({
.url()
.nullish()
.describe("Avatar URL of the customer in the client's app."),
+ externalId: z
+ .string()
+ .describe("Unique identifier for the customer in the client's app."),
});
export const updateCustomerBodySchema = createCustomerBodySchema.partial();