The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
add list
Steven Tey10c6a193e335
diff --git a/apps/web/app/api/customers/[id]/route.ts b/apps/web/app/api/customers/[id]/route.tsindex 5a3fc12bef..f9883ec178 100644--- a/apps/web/app/api/customers/[id]/route.ts+++ b/apps/web/app/api/customers/[id]/route.ts@@ -9,51 +9,23 @@ import {import { NextResponse } from "next/server";// GET /api/customers/:id – Get a customer by external ID-export const GET = withWorkspace(async ({ workspace, params }) => {- const { id: externalId } = params;- const customer = await prisma.customer.findUnique({- where: {- projectId_externalId: {- projectId: workspace.id,- externalId,- },- },- });- if (!customer) {- throw new DubApiError({- code: "not_found",- message: "Customer not found",- });- }-- return NextResponse.json(- CustomerSchema.parse({- ...customer,- id: customer.externalId,- }),- );-});--// PATCH /api/customers/:id – Update a customer by external ID-export const PATCH = withWorkspace(async ({ workspace, params, req }) => {- const { id: externalId } = params;- const {- id: newExternalId,- name,- email,- avatar,- } = updateCustomerBodySchema.parse(await parseRequestBody(req));-- try {- const customer = await prisma.customer.update({+export const GET = withWorkspace(+ async ({ workspace, params }) => {+ const { id: externalId } = params;+ const customer = await prisma.customer.findUnique({where: {projectId_externalId: {projectId: workspace.id,externalId,},},- data: { name, email, avatar, externalId: newExternalId },});+ if (!customer) {+ throw new DubApiError({+ code: "not_found",+ message: "Customer not found",+ });+ }return NextResponse.json(CustomerSchema.parse({@@ -61,53 +33,96 @@ export const PATCH = withWorkspace(async ({ workspace, params, req }) => {id: customer.externalId,}),);- } catch (error) {- if (error.code === "P2002") {- throw new DubApiError({- code: "conflict",- message: "A customer with this external ID already exists.",+ },+ {+ requiredAddOn: "conversion",+ },+);++// PATCH /api/customers/:id – Update a customer by external ID+export const PATCH = withWorkspace(+ async ({ workspace, params, req }) => {+ const { id: externalId } = params;+ const {+ id: newExternalId,+ name,+ email,+ avatar,+ } = updateCustomerBodySchema.parse(await parseRequestBody(req));++ try {+ const customer = await prisma.customer.update({+ where: {+ projectId_externalId: {+ projectId: workspace.id,+ externalId,+ },+ },+ data: { name, email, avatar, externalId: newExternalId },});- } else if (error.code === "P2025") {++ return NextResponse.json(+ CustomerSchema.parse({+ ...customer,+ id: customer.externalId,+ }),+ );+ } catch (error) {+ if (error.code === "P2002") {+ throw new DubApiError({+ code: "conflict",+ message: "A customer with this external ID already exists.",+ });+ } else if (error.code === "P2025") {+ throw new DubApiError({+ code: "not_found",+ message: "Customer not found",+ });+ }+throw new DubApiError({- code: "not_found",- message: "Customer not found",+ code: "unprocessable_entity",+ message: error.message,});}-- throw new DubApiError({- code: "unprocessable_entity",- message: error.message,- });- }-});+ },+ {+ requiredAddOn: "conversion",+ },+);// DELETE /api/customers/:id – Delete a customer by external ID-export const DELETE = withWorkspace(async ({ workspace, params }) => {- const { id: externalId } = params;- try {- const customer = await prisma.customer.delete({- where: {- projectId_externalId: {- projectId: workspace.id,- externalId,+export const DELETE = withWorkspace(+ async ({ workspace, params }) => {+ const { id: externalId } = params;+ try {+ const customer = await prisma.customer.delete({+ where: {+ projectId_externalId: {+ projectId: workspace.id,+ externalId,+ },},- },- });+ });++ return NextResponse.json({+ id: customer.externalId,+ });+ } catch (error) {+ if (error.code === "P2025") {+ throw new DubApiError({+ code: "not_found",+ message: "Customer not found",+ });+ }- return NextResponse.json({- id: customer.externalId,- });- } catch (error) {- if (error.code === "P2025") {throw new DubApiError({- code: "not_found",- message: "Customer not found",+ code: "unprocessable_entity",+ message: error.message,});}-- throw new DubApiError({- code: "unprocessable_entity",- message: error.message,- });- }-});+ },+ {+ requiredAddOn: "conversion",+ },+);diff --git a/apps/web/app/api/customers/route.ts b/apps/web/app/api/customers/route.tsindex 258e949520..ad40437392 100644--- a/apps/web/app/api/customers/route.ts+++ b/apps/web/app/api/customers/route.ts@@ -9,6 +9,27 @@ import {} from "@/lib/zod/schemas/customers";import { NextResponse } from "next/server";+// GET /api/customers – Get all customers+export const GET = withWorkspace(+ async ({ req, workspace }) => {+ const customers = await prisma.customer.findMany({+ where: {+ projectId: workspace.id,+ },+ });++ return NextResponse.json(+ customers.map((customer) => ({+ ...customer,+ id: customer.externalId,+ })),+ );+ },+ {+ requiredAddOn: "conversion",+ },+);+// POST /api/customers – Create a new customerexport const POST = withWorkspace(async ({ req, workspace }) => {@@ -49,6 +70,5 @@ export const POST = withWorkspace(},{requiredAddOn: "conversion",- requiredPermissions: ["conversions.write"],},);diff --git a/apps/web/lib/openapi/customers/get-customer.ts b/apps/web/lib/openapi/customers/get-customer.tsnew file mode 100644index 0000000000..c8105b0575--- /dev/null+++ b/apps/web/lib/openapi/customers/get-customer.ts@@ -0,0 +1,26 @@+import { openApiErrorResponses } from "@/lib/openapi/responses";+import { ZodOpenApiOperationObject } from "zod-openapi";+import { CustomerSchema } from "../../zod/schemas/customers";++export const getCustomer: ZodOpenApiOperationObject = {+ operationId: "getCustomer",+ "x-speakeasy-name-override": "get",+ summary: "Get a customer",+ description: "Get a customer by externalID for the authenticated workspace.",+ requestParams: {+ path: CustomerSchema.pick({ id: true }),+ },+ responses: {+ "200": {+ description: "The customer object.",+ content: {+ "application/json": {+ schema: CustomerSchema,+ },+ },+ },+ ...openApiErrorResponses,+ },+ tags: ["Customers"],+ security: [{ token: [] }],+};diff --git a/apps/web/lib/openapi/customers/get-customers.ts b/apps/web/lib/openapi/customers/get-customers.tsnew file mode 100644index 0000000000..a741dac066--- /dev/null+++ b/apps/web/lib/openapi/customers/get-customers.ts@@ -0,0 +1,24 @@+import { openApiErrorResponses } from "@/lib/openapi/responses";+import z from "@/lib/zod";+import { ZodOpenApiOperationObject } from "zod-openapi";+import { CustomerSchema } from "../../zod/schemas/customers";++export const getCustomers: ZodOpenApiOperationObject = {+ operationId: "getCustomers",+ "x-speakeasy-name-override": "list",+ summary: "Get a list of customers",+ description: "Get a list of customers for the authenticated workspace.",+ responses: {+ "200": {+ description: "The list of customers.",+ content: {+ "application/json": {+ schema: z.array(CustomerSchema),+ },+ },+ },+ ...openApiErrorResponses,+ },+ tags: ["Customers"],+ security: [{ token: [] }],+};diff --git a/apps/web/lib/openapi/customers/index.ts b/apps/web/lib/openapi/customers/index.tsindex ac995604e9..0cc5eb085f 100644--- a/apps/web/lib/openapi/customers/index.ts+++ b/apps/web/lib/openapi/customers/index.ts@@ -1,13 +1,17 @@import { ZodOpenApiPathsObject } from "zod-openapi";import { createCustomer } from "./create-customer";import { deleteCustomer } from "./delete-customer";+import { getCustomer } from "./get-customer";+import { getCustomers } from "./get-customers";import { updateCustomer } from "./update-customer";export const customersPaths: ZodOpenApiPathsObject = {"/customers": {+ get: getCustomers,post: createCustomer,},"/customers/{id}": {+ get: getCustomer,patch: updateCustomer,delete: deleteCustomer,},diff --git a/apps/web/lib/openapi/index.ts b/apps/web/lib/openapi/index.tsindex d9961f38b2..69e29c982a 100644--- a/apps/web/lib/openapi/index.ts+++ b/apps/web/lib/openapi/index.ts@@ -6,6 +6,7 @@ import { WorkspaceSchema } from "@/lib/zod/schemas/workspaces";import { createDocument } from "zod-openapi";import { webhookEventSchema } from "../webhook/schemas";import { analyticsPath } from "./analytics";+import { customersPaths } from "./customers";import { domainsPaths } from "./domains";import { eventsPath } from "./events";import { linksPaths } from "./links";@@ -40,13 +41,14 @@ export const document = createDocument({],paths: {...linksPaths,- ...qrCodePaths,...analyticsPath,...eventsPath,- ...workspacesPaths,...tagsPaths,...domainsPaths,...trackPaths,+ ...customersPaths,+ ...workspacesPaths,+ ...qrCodePaths,...metatagsPath,},components: {