fix: Handle existing user when editing profile

This commit is contained in:
Thomas Camlong
2024-05-10 15:24:40 +02:00
parent f5d71aebb4
commit 6e2bd0ce11
2 changed files with 39 additions and 5 deletions

View File

@@ -90,7 +90,16 @@ export const userRouter = createTRPCRouter({
.from(users)
.where(eq(users.id, input.userId))
.limit(1);
const existingUser = await ctx.db.query.users.findFirst({
where: eq(users.name, input.form.name),
});
if (existingUser !== undefined) {
throw new TRPCError({
code: "FORBIDDEN",
message: `User ${input.form.name} already exists`,
});
}
const emailDirty =
input.form.email && user[0]?.email !== input.form.email;
await ctx.db