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({
|
||||
export const workspaceDashboardLayoutSchema = z.object({
|
||||
layouts: z.record(z.string(), z.array(z.any())),
|
||||
items: z.array(z.any()),
|
||||
})
|
||||
.nullable();
|
||||
});
|
||||
|
||||
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,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: {
|
||||
username,
|
||||
password: await hashPassword(password),
|
||||
@ -68,29 +75,17 @@ export async function createAdminUser(username: string, password: string) {
|
||||
create: [
|
||||
{
|
||||
role: ROLES.owner,
|
||||
workspace: {
|
||||
create: {
|
||||
name: username,
|
||||
},
|
||||
},
|
||||
workspaceId: newWorkspace.id,
|
||||
},
|
||||
],
|
||||
},
|
||||
currentWorkspaceId: newWorkspace.id,
|
||||
},
|
||||
select: createUserSelect,
|
||||
});
|
||||
|
||||
if (user.workspaces[0]) {
|
||||
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');
|
||||
}
|
||||
|
||||
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: {
|
||||
username,
|
||||
password: await hashPassword(password),
|
||||
@ -115,29 +117,17 @@ export async function createUser(username: string, password: string) {
|
||||
create: [
|
||||
{
|
||||
role: ROLES.owner,
|
||||
workspace: {
|
||||
create: {
|
||||
name: username,
|
||||
},
|
||||
},
|
||||
workspaceId: newWorkspace.id,
|
||||
},
|
||||
],
|
||||
},
|
||||
currentWorkspaceId: newWorkspace.id,
|
||||
},
|
||||
select: createUserSelect,
|
||||
});
|
||||
|
||||
if (user.workspaces[0]) {
|
||||
user = await prisma.user.update({
|
||||
where: {
|
||||
id: user.id,
|
||||
},
|
||||
data: {
|
||||
currentWorkspaceId: user.workspaces[0].workspace.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