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

@@ -1,7 +1,7 @@
import { describe, expect, it, vi } from "vitest";
import { describe, expect, it, test, vi } from "vitest";
import type { Session } from "@homarr/auth";
import { schema } from "@homarr/db";
import { createId, eq, schema } from "@homarr/db";
import { createDb } from "@homarr/db/test";
import { userRouter } from "../user";
@@ -91,4 +91,140 @@ describe("initUser should initialize the first user", () => {
await expect(act()).rejects.toThrow("too_small");
});
test("editProfile should update users and not update emailVerified when email not dirty", async () => {
// arrange
const db = createDb();
const caller = userRouter.createCaller({
db,
session: null,
});
const id = createId();
const emailVerified = new Date(2024, 0, 5);
await db.insert(schema.users).values({
id,
name: "TEST 1",
email: "abc@gmail.com",
emailVerified,
});
// act
await caller.editProfile({
userId: id,
form: {
name: "ABC",
email: "",
},
});
// assert
const user = await db
.select()
.from(schema.users)
.where(eq(schema.users.id, id));
expect(user).toHaveLength(1);
expect(user[0]).toStrictEqual({
id,
name: "ABC",
email: "abc@gmail.com",
emailVerified,
salt: null,
password: null,
image: null,
});
});
test("editProfile should update users and update emailVerified when email dirty", async () => {
// arrange
const db = createDb();
const caller = userRouter.createCaller({
db,
session: null,
});
const id = createId();
await db.insert(schema.users).values({
id,
name: "TEST 1",
email: "abc@gmail.com",
emailVerified: new Date(2024, 0, 5),
});
// act
await caller.editProfile({
userId: id,
form: {
name: "ABC",
email: "myNewEmail@gmail.com",
},
});
// assert
const user = await db
.select()
.from(schema.users)
.where(eq(schema.users.id, id));
expect(user).toHaveLength(1);
expect(user[0]).toStrictEqual({
id,
name: "ABC",
email: "myNewEmail@gmail.com",
emailVerified: null,
salt: null,
password: null,
image: null,
});
});
test("delete should delete user", async () => {
const db = createDb();
const caller = userRouter.createCaller({
db,
session: null,
});
const userToDelete = createId();
const initialUsers = [
{
id: createId(),
name: "User 1",
email: null,
emailVerified: null,
image: null,
password: null,
salt: null,
},
{
id: userToDelete,
name: "User 2",
email: null,
emailVerified: null,
image: null,
password: null,
salt: null,
},
{
id: createId(),
name: "User 3",
email: null,
emailVerified: null,
image: null,
password: null,
salt: null,
},
];
await db.insert(schema.users).values(initialUsers);
await caller.delete(userToDelete);
const usersInDb = await db.select().from(schema.users);
expect(usersInDb).toStrictEqual([initialUsers[0], initialUsers[2]]);
});
});

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 (