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.

Tags

Kiran Kebbff7fa9058
diff --git a/apps/web/app/api/tags/[id]/route.ts b/apps/web/app/api/tags/[id]/route.ts
index 504ef791d9..1b8203876e 100644
--- a/apps/web/app/api/tags/[id]/route.ts
+++ b/apps/web/app/api/tags/[id]/route.ts
@@ -1,5 +1,5 @@
import { withAuth } from "@/lib/auth";
-import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { DubApiError } from "@/lib/api/errors";
import prisma from "@/lib/prisma";
import { updateTagBodySchema } from "@/lib/zod/schemas/tags";
import { NextResponse } from "next/server";
@@ -7,34 +7,41 @@ import { NextResponse } from "next/server";
// PUT /api/projects/[slug]/tags/[id] – update a tag for a project
export const PUT = withAuth(async ({ req, params }) => {
const { id } = params;
- try {
- const { name, color } = updateTagBodySchema.parse(await req.json());
+ const { name, color } = updateTagBodySchema.parse(await req.json());
- const tag = await prisma.tag.findUnique({
- where: {
- id,
- },
- });
+ const tag = await prisma.tag.findUnique({
+ where: {
+ id,
+ },
+ });
- if (!tag) {
- throw new DubApiError({
- code: "not_found",
- message: "Tag not found.",
- });
- }
+ if (!tag) {
+ throw new DubApiError({
+ code: "not_found",
+ message: "Tag not found.",
+ });
+ }
+ try {
const response = await prisma.tag.update({
where: {
id,
},
data: {
name,
- color: color as any,
+ color,
},
});
return NextResponse.json(response);
} catch (error) {
- return handleAndReturnErrorResponse(error);
+ if (error.code === "P2002") {
+ throw new DubApiError({
+ code: "conflict",
+ message: "A tag with that name already exists.",
+ });
+ }
+
+ throw error;
}
});
@@ -49,6 +56,13 @@ export const DELETE = withAuth(async ({ params }) => {
});
return NextResponse.json(response);
} catch (error) {
- return handleAndReturnErrorResponse(error);
+ if (error.code === "P2025") {
+ throw new DubApiError({
+ code: "not_found",
+ message: "Tag not found.",
+ });
+ }
+
+ throw error;
}
});
diff --git a/apps/web/app/api/tags/route.ts b/apps/web/app/api/tags/route.ts
index 6f56ba1304..7d21cdd1c6 100644
--- a/apps/web/app/api/tags/route.ts
+++ b/apps/web/app/api/tags/route.ts
@@ -1,6 +1,6 @@
import { exceededLimitError } from "@/lib/api/errors";
import { withAuth } from "@/lib/auth";
-import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { DubApiError } from "@/lib/api/errors";
import prisma from "@/lib/prisma";
import { createTagBodySchema } from "@/lib/zod/schemas/tags";
import { COLORS_LIST, randomBadgeColor } from "@/ui/links/tag-badge";
@@ -26,58 +26,49 @@ export const GET = withAuth(async ({ project, headers }) => {
// POST /api/projects/[slug]/tags - create a tag for a project
export const POST = withAuth(async ({ req, project, headers }) => {
- try {
- const tagsCount = await prisma.tag.count({
- where: {
- projectId: project.id,
- },
- });
-
- if (tagsCount >= project.tagsLimit) {
- throw new DubApiError({
- code: "exceeded_limit",
- message: exceededLimitError({
- plan: project.plan,
- limit: project.tagsLimit,
- type: "tags",
- }),
- });
- }
-
- const { tag, color } = createTagBodySchema.parse(await req.json());
+ const tagsCount = await prisma.tag.count({
+ where: {
+ projectId: project.id,
+ },
+ });
- const existingTag = await prisma.tag.findFirst({
- where: {
- projectId: project.id,
- name: tag,
- },
+ if (tagsCount >= project.tagsLimit) {
+ throw new DubApiError({
+ code: "exceeded_limit",
+ message: exceededLimitError({
+ plan: project.plan,
+ limit: project.tagsLimit,
+ type: "tags",
+ }),
});
+ }
+
+ const { tag, color } = createTagBodySchema.parse(await req.json());
- if (existingTag) {
- throw new DubApiError({
- code: "conflict",
- message: "A tag with that name already exists.",
- });
- }
+ const existingTag = await prisma.tag.findFirst({
+ where: {
+ projectId: project.id,
+ name: tag,
+ },
+ });
- const response = await prisma.tag.create({
- data: {
- name: tag,
- color:
- color && COLORS_LIST.map(({ color }) => color).includes(color)
- ? color
- : randomBadgeColor(),
- projectId: project.id,
- },
+ if (existingTag) {
+ throw new DubApiError({
+ code: "conflict",
+ message: "A tag with that name already exists.",
});
+ }
- return NextResponse.json(response, { headers, status: 201 });
- } catch (error) {
- if (error.code === "P2002") {
- error.code = "conflict";
- error.message = "A tag with that name already exists.";
- }
+ const response = await prisma.tag.create({
+ data: {
+ name: tag,
+ color:
+ color && COLORS_LIST.map(({ color }) => color).includes(color)
+ ? color
+ : randomBadgeColor(),
+ projectId: project.id,
+ },
+ });
- return handleAndReturnErrorResponse(error);
- }
+ return NextResponse.json(response, { headers, status: 201 });
});
diff --git a/apps/web/ui/modals/add-edit-tag-modal.tsx b/apps/web/ui/modals/add-edit-tag-modal.tsx
index 947cc04e1b..23d4c82ec3 100644
--- a/apps/web/ui/modals/add-edit-tag-modal.tsx
+++ b/apps/web/ui/modals/add-edit-tag-modal.tsx
@@ -115,7 +115,7 @@ function AddEditTagModal({
color: data.color,
}),
}).then(async (res) => {
- if (res.status === 200) {
+ if (res.status === 200 || res.status === 201) {
va.track(props ? "Edited Tag" : "Created Tag");
await Promise.all([
mutate(`/api/tags?projectSlug=${projectSlug}`),
@@ -131,7 +131,8 @@ function AddEditTagModal({
toast.success(endpoint.successMessage);
setShowAddEditTagModal(false);
} else {
- toast.error(await res.text());
+ const { error } = await res.json();
+ toast.error(error.message);
}
setSaving(false);
});