refactor: modify the user type and create method to make it correct
This commit is contained in:
parent
7b519b152f
commit
89612eff5f
@ -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[]
|
||||
}
|
||||
|
@ -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(
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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,
|
||||
},
|
||||
});
|
||||
}),
|
||||
|
Loading…
Reference in New Issue
Block a user