From c05e39297b2975d47a1a76e07300c46e4176be81 Mon Sep 17 00:00:00 2001 From: AugustinSorel Date: Sun, 30 Jun 2024 18:12:03 +0100 Subject: [PATCH] FEAT/router --- src/server/api/routers/team.router.ts | 30 ++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/server/api/routers/team.router.ts b/src/server/api/routers/team.router.ts index af455ab8..4dc32b18 100644 --- a/src/server/api/routers/team.router.ts +++ b/src/server/api/routers/team.router.ts @@ -7,18 +7,34 @@ import { sendInviteToTeamEmail } from "@/lib/email"; import { and, eq } from "drizzle-orm"; import { teamInviteSchema } from "@/schemas/teamInvite.schema"; import { z } from "zod"; +import { isPgError } from "@/server/db/utils"; export const teamRouter = createTRPCRouter({ create: protectedProcedure .input(teamSchema.pick({ name: true })) .mutation(async ({ ctx, input }) => { - const [team] = await ctx.db - .insert(teams) - .values({ - name: input.name, - authorId: ctx.session.user.id, - }) - .returning(); + let team = null; + + try { + const res = await ctx.db + .insert(teams) + .values({ + name: input.name, + authorId: ctx.session.user.id, + }) + .returning(); + + team = res.at(0); + } catch (e) { + if (isPgError(e) && e.code === "23505") { + throw new TRPCError({ + code: "CONFLICT", + message: `${input.name} is already used`, + }); + } + + throw new TRPCError({ code: "INTERNAL_SERVER_ERROR" }); + } if (!team) { throw new TRPCError({