Skip to content

Commit

Permalink
FEAT/router
Browse files Browse the repository at this point in the history
  • Loading branch information
AugustinSorel committed Jun 30, 2024
1 parent afbc2e2 commit c05e392
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/server/api/routers/team.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit c05e392

Please sign in to comment.