From 89612eff5f325674c719700dbdcebcfe795bbca5 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Wed, 29 Nov 2023 01:22:33 +0800 Subject: [PATCH] refactor: modify the user type and create method to make it correct --- prisma/schema.prisma | 4 +- src/server/model/_schema/index.ts | 15 ++-- src/server/model/user.ts | 102 ++++++++++++--------------- src/server/trpc/routers/workspace.ts | 5 +- 4 files changed, 58 insertions(+), 68 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 7819e16..45721a0 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -15,9 +15,9 @@ model User { createdAt DateTime @default(now()) @db.Timestamptz(6) updatedAt DateTime @updatedAt @db.Timestamptz(6) deletedAt DateTime? @db.Timestamptz(6) - currentWorkspaceId String? @db.VarChar(30) + currentWorkspaceId String @db.VarChar(30) - currentWorkspace Workspace? @relation(fields: [currentWorkspaceId], references: [id]) + currentWorkspace Workspace @relation(fields: [currentWorkspaceId], references: [id]) workspaces WorkspacesOnUsers[] } diff --git a/src/server/model/_schema/index.ts b/src/server/model/_schema/index.ts index 5444a31..596b3cd 100644 --- a/src/server/model/_schema/index.ts +++ b/src/server/model/_schema/index.ts @@ -1,4 +1,5 @@ import { z } from 'zod'; +import { schemas } from '../../../types'; // Match prisma `JsonValue` export const jsonFieldSchema = z.union([ @@ -10,12 +11,10 @@ export const jsonFieldSchema = z.union([ z.number(), ]); -export const workspaceDashboardLayoutSchema = z - .object({ - layouts: z.record(z.string(), z.array(z.any())), - items: z.array(z.any()), - }) - .nullable(); +export const workspaceDashboardLayoutSchema = z.object({ + layouts: z.record(z.string(), z.array(z.any())), + items: z.array(z.any()), +}); export const workspaceSchema = z.object({ id: z.string(), @@ -30,9 +29,9 @@ export const userInfoSchema = z.object({ updatedAt: z.date(), deletedAt: z.date().nullable(), currentWorkspace: z.intersection( - workspaceSchema.nullable(), + workspaceSchema, z.object({ - dashboardLayout: workspaceDashboardLayoutSchema, + dashboardLayout: workspaceDashboardLayoutSchema.nullable(), }) ), workspaces: z.array( diff --git a/src/server/model/user.ts b/src/server/model/user.ts index 938a137..eef7cad 100644 --- a/src/server/model/user.ts +++ b/src/server/model/user.ts @@ -59,38 +59,33 @@ export async function createAdminUser(username: string, password: string) { ); } - let user = await prisma.user.create({ - data: { - username, - password: await hashPassword(password), - role: SYSTEM_ROLES.admin, - workspaces: { - create: [ - { - role: ROLES.owner, - workspace: { - create: { - name: username, - }, - }, - }, - ], - }, - }, - select: createUserSelect, - }); - - if (user.workspaces[0]) { - user = await prisma.user.update({ - where: { - id: user.id, - }, + const user = await prisma.$transaction(async (p) => { + const newWorkspace = await p.workspace.create({ data: { - currentWorkspaceId: user.workspaces[0].workspace.id, + name: username, + }, + }); + + const user = await p.user.create({ + data: { + username, + password: await hashPassword(password), + role: SYSTEM_ROLES.admin, + workspaces: { + create: [ + { + role: ROLES.owner, + workspaceId: newWorkspace.id, + }, + ], + }, + currentWorkspaceId: newWorkspace.id, }, select: createUserSelect, }); - } + + return user; + }); return user; } @@ -106,38 +101,33 @@ export async function createUser(username: string, password: string) { throw new Error('User already exists'); } - let user = await prisma.user.create({ - data: { - username, - password: await hashPassword(password), - role: SYSTEM_ROLES.user, - workspaces: { - create: [ - { - role: ROLES.owner, - workspace: { - create: { - name: username, - }, - }, - }, - ], - }, - }, - select: createUserSelect, - }); - - if (user.workspaces[0]) { - user = await prisma.user.update({ - where: { - id: user.id, - }, + const user = await prisma.$transaction(async (p) => { + const newWorkspace = await p.workspace.create({ data: { - currentWorkspaceId: user.workspaces[0].workspace.id, + name: username, + }, + }); + + const user = await p.user.create({ + data: { + username, + password: await hashPassword(password), + role: SYSTEM_ROLES.user, + workspaces: { + create: [ + { + role: ROLES.owner, + workspaceId: newWorkspace.id, + }, + ], + }, + currentWorkspaceId: newWorkspace.id, }, select: createUserSelect, }); - } + + return user; + }); return user; } diff --git a/src/server/trpc/routers/workspace.ts b/src/server/trpc/routers/workspace.ts index ad2740d..74cd96f 100644 --- a/src/server/trpc/routers/workspace.ts +++ b/src/server/trpc/routers/workspace.ts @@ -2,6 +2,7 @@ import { router, workspaceOwnerProcedure } from '../trpc'; import { z } from 'zod'; import { prisma } from '../../model/_client'; import { workspaceDashboardLayoutSchema } from '../../model/_schema'; +import { Prisma } from '@prisma/client'; export const workspaceRouter = router({ updateDashboardOrder: workspaceOwnerProcedure @@ -25,7 +26,7 @@ export const workspaceRouter = router({ saveDashboardLayout: workspaceOwnerProcedure .input( z.object({ - dashboardLayout: workspaceDashboardLayoutSchema, + dashboardLayout: workspaceDashboardLayoutSchema.nullable(), }) ) .mutation(async ({ input }) => { @@ -36,7 +37,7 @@ export const workspaceRouter = router({ id: workspaceId, }, data: { - dashboardLayout, + dashboardLayout: dashboardLayout ?? Prisma.DbNull, }, }); }),