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.

Handle concurrent tag create conflicts via P2002

Kiran Kd533157bfeef
diff --git a/apps/web/app/api/tags/route.ts b/apps/web/app/api/tags/route.ts
index 37bbad565d..da0051c4d0 100644
--- a/apps/web/app/api/tags/route.ts
+++ b/apps/web/app/api/tags/route.ts
@@ -116,33 +116,30 @@ export const POST = withWorkspace(
const { tag, color, name } = createTagBodySchema.parse(await req.json());
- const existingTag = await prisma.tag.findFirst({
- where: {
- projectId: workspace.id,
- name: name || tag,
- },
- });
-
- if (existingTag) {
- throw new DubApiError({
- code: "conflict",
- message: "A tag with that name already exists.",
+ try {
+ const createdTag = await prisma.tag.create({
+ data: {
+ id: createId({ prefix: "tag_" }),
+ name: tag || name!,
+ color: color || randomBadgeColor(),
+ projectId: workspace.id,
+ },
});
- }
- const response = await prisma.tag.create({
- data: {
- id: createId({ prefix: "tag_" }),
- name: tag || name!,
- color: color || randomBadgeColor(),
- projectId: workspace.id,
- },
- });
+ return NextResponse.json(LinkTagSchema.parse(createdTag), {
+ headers,
+ status: 201,
+ });
+ } catch (error) {
+ if (error.code === "P2002") {
+ throw new DubApiError({
+ code: "conflict",
+ message: "A tag with that name already exists.",
+ });
+ }
- return NextResponse.json(LinkTagSchema.parse(response), {
- headers,
- status: 201,
- });
+ throw error;
+ }
},
{
requiredPermissions: ["tags.write"],