refactor: modify the user type and create method to make it correct

This commit is contained in:
moonrailgun 2023-11-29 01:22:33 +08:00
parent 7b519b152f
commit 89612eff5f
4 changed files with 58 additions and 68 deletions

View File

@ -15,9 +15,9 @@ model User {
createdAt DateTime @default(now()) @db.Timestamptz(6) createdAt DateTime @default(now()) @db.Timestamptz(6)
updatedAt DateTime @updatedAt @db.Timestamptz(6) updatedAt DateTime @updatedAt @db.Timestamptz(6)
deletedAt DateTime? @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[] workspaces WorkspacesOnUsers[]
} }

View File

@ -1,4 +1,5 @@
import { z } from 'zod'; import { z } from 'zod';
import { schemas } from '../../../types';
// Match prisma `JsonValue` // Match prisma `JsonValue`
export const jsonFieldSchema = z.union([ export const jsonFieldSchema = z.union([
@ -10,12 +11,10 @@ export const jsonFieldSchema = z.union([
z.number(), z.number(),
]); ]);
export const workspaceDashboardLayoutSchema = z export const workspaceDashboardLayoutSchema = z.object({
.object({
layouts: z.record(z.string(), z.array(z.any())), layouts: z.record(z.string(), z.array(z.any())),
items: z.array(z.any()), items: z.array(z.any()),
}) });
.nullable();
export const workspaceSchema = z.object({ export const workspaceSchema = z.object({
id: z.string(), id: z.string(),
@ -30,9 +29,9 @@ export const userInfoSchema = z.object({
updatedAt: z.date(), updatedAt: z.date(),
deletedAt: z.date().nullable(), deletedAt: z.date().nullable(),
currentWorkspace: z.intersection( currentWorkspace: z.intersection(
workspaceSchema.nullable(), workspaceSchema,
z.object({ z.object({
dashboardLayout: workspaceDashboardLayoutSchema, dashboardLayout: workspaceDashboardLayoutSchema.nullable(),
}) })
), ),
workspaces: z.array( workspaces: z.array(

View File

@ -59,7 +59,14 @@ export async function createAdminUser(username: string, password: string) {
); );
} }
let user = await prisma.user.create({ const user = await prisma.$transaction(async (p) => {
const newWorkspace = await p.workspace.create({
data: {
name: username,
},
});
const user = await p.user.create({
data: { data: {
username, username,
password: await hashPassword(password), password: await hashPassword(password),
@ -68,29 +75,17 @@ export async function createAdminUser(username: string, password: string) {
create: [ create: [
{ {
role: ROLES.owner, role: ROLES.owner,
workspace: { workspaceId: newWorkspace.id,
create: {
name: username,
},
},
}, },
], ],
}, },
currentWorkspaceId: newWorkspace.id,
}, },
select: createUserSelect, select: createUserSelect,
}); });
if (user.workspaces[0]) { return user;
user = await prisma.user.update({
where: {
id: user.id,
},
data: {
currentWorkspaceId: user.workspaces[0].workspace.id,
},
select: createUserSelect,
}); });
}
return user; return user;
} }
@ -106,7 +101,14 @@ export async function createUser(username: string, password: string) {
throw new Error('User already exists'); throw new Error('User already exists');
} }
let user = await prisma.user.create({ const user = await prisma.$transaction(async (p) => {
const newWorkspace = await p.workspace.create({
data: {
name: username,
},
});
const user = await p.user.create({
data: { data: {
username, username,
password: await hashPassword(password), password: await hashPassword(password),
@ -115,29 +117,17 @@ export async function createUser(username: string, password: string) {
create: [ create: [
{ {
role: ROLES.owner, role: ROLES.owner,
workspace: { workspaceId: newWorkspace.id,
create: {
name: username,
},
},
}, },
], ],
}, },
currentWorkspaceId: newWorkspace.id,
}, },
select: createUserSelect, select: createUserSelect,
}); });
if (user.workspaces[0]) { return user;
user = await prisma.user.update({
where: {
id: user.id,
},
data: {
currentWorkspaceId: user.workspaces[0].workspace.id,
},
select: createUserSelect,
}); });
}
return user; return user;
} }

View File

@ -2,6 +2,7 @@ import { router, workspaceOwnerProcedure } from '../trpc';
import { z } from 'zod'; import { z } from 'zod';
import { prisma } from '../../model/_client'; import { prisma } from '../../model/_client';
import { workspaceDashboardLayoutSchema } from '../../model/_schema'; import { workspaceDashboardLayoutSchema } from '../../model/_schema';
import { Prisma } from '@prisma/client';
export const workspaceRouter = router({ export const workspaceRouter = router({
updateDashboardOrder: workspaceOwnerProcedure updateDashboardOrder: workspaceOwnerProcedure
@ -25,7 +26,7 @@ export const workspaceRouter = router({
saveDashboardLayout: workspaceOwnerProcedure saveDashboardLayout: workspaceOwnerProcedure
.input( .input(
z.object({ z.object({
dashboardLayout: workspaceDashboardLayoutSchema, dashboardLayout: workspaceDashboardLayoutSchema.nullable(),
}) })
) )
.mutation(async ({ input }) => { .mutation(async ({ input }) => {
@ -36,7 +37,7 @@ export const workspaceRouter = router({
id: workspaceId, id: workspaceId,
}, },
data: { data: {
dashboardLayout, dashboardLayout: dashboardLayout ?? Prisma.DbNull,
}, },
}); });
}), }),