feat: add edit user page (#173)

This commit is contained in:
Manuel
2024-03-05 21:10:19 +01:00
committed by GitHub
parent 8c9adb31f3
commit 41b99f191c
7 changed files with 394 additions and 9 deletions

View File

@@ -4,7 +4,7 @@ import { TRPCError } from "@trpc/server";
import { createSalt, hashPassword } from "@homarr/auth";
import type { Database } from "@homarr/db";
import { createId, db, eq, schema } from "@homarr/db";
import { createId, eq, schema } from "@homarr/db";
import { users } from "@homarr/db/schema/sqlite";
import { validation, z } from "@homarr/validation";
@@ -34,8 +34,8 @@ export const userRouter = createTRPCRouter({
.mutation(async ({ ctx, input }) => {
await createUser(ctx.db, input);
}),
getAll: publicProcedure.query(async () => {
return db.query.users.findMany({
getAll: publicProcedure.query(async ({ ctx }) => {
return ctx.db.query.users.findMany({
columns: {
id: true,
name: true,
@@ -47,8 +47,8 @@ export const userRouter = createTRPCRouter({
}),
getById: publicProcedure
.input(z.object({ userId: z.string() }))
.query(async ({ input }) => {
return db.query.users.findFirst({
.query(async ({ input, ctx }) => {
return ctx.db.query.users.findFirst({
columns: {
id: true,
name: true,
@@ -59,6 +59,34 @@ export const userRouter = createTRPCRouter({
where: eq(users.id, input.userId),
});
}),
editProfile: publicProcedure
.input(
z.object({
form: validation.user.editProfile,
userId: z.string(),
}),
)
.mutation(async ({ input, ctx }) => {
const user = await ctx.db
.select()
.from(users)
.where(eq(users.id, input.userId))
.limit(1);
const emailDirty =
input.form.email && user[0]?.email !== input.form.email;
await ctx.db
.update(users)
.set({
name: input.form.name,
email: emailDirty === true ? input.form.email : undefined,
emailVerified: emailDirty === true ? null : undefined,
})
.where(eq(users.id, input.userId));
}),
delete: publicProcedure.input(z.string()).mutation(async ({ input, ctx }) => {
await ctx.db.delete(users).where(eq(users.id, input));
}),
});
const createUser = async (