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)
|
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[]
|
||||||
}
|
}
|
||||||
|
@ -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(
|
||||||
|
@ -59,38 +59,33 @@ export async function createAdminUser(username: string, password: string) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let user = await prisma.user.create({
|
const user = await prisma.$transaction(async (p) => {
|
||||||
data: {
|
const newWorkspace = await p.workspace.create({
|
||||||
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,
|
|
||||||
},
|
|
||||||
data: {
|
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,
|
select: createUserSelect,
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
return user;
|
||||||
|
});
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
@ -106,38 +101,33 @@ 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) => {
|
||||||
data: {
|
const newWorkspace = await p.workspace.create({
|
||||||
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,
|
|
||||||
},
|
|
||||||
data: {
|
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,
|
select: createUserSelect,
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
return user;
|
||||||
|
});
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
@ -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,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}),
|
}),
|
||||||
|
Loading…
Reference in New Issue
Block a user