From 3dca8fc27c82bd96dbab423b111e4de57f3b4bd8 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sun, 20 Oct 2024 22:27:31 +0800 Subject: [PATCH] feat: add workspace settings manage --- src/client/store/user.ts | 17 +++++++++++++ src/server/model/_schema/index.ts | 1 + src/server/model/user.ts | 1 + src/server/trpc/routers/workspace.ts | 36 ++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+) diff --git a/src/client/store/user.ts b/src/client/store/user.ts index eca54d7..38eb0be 100644 --- a/src/client/store/user.ts +++ b/src/client/store/user.ts @@ -10,6 +10,7 @@ export type UserLoginInfo = NonNullable; interface UserState { info: UserLoginInfo | null; updateCurrentWorkspaceName: (name: string) => void; + updateCurrentWorkspaceSettings: (settings: Record) => void; } export const useUserStore = createWithEqualityFn()( @@ -27,6 +28,21 @@ export const useUserStore = createWithEqualityFn()( } }); }, + updateCurrentWorkspaceSettings: (settings) => { + set((state) => { + const currentUserInfo = useUserStore.getState().info; + if (!currentUserInfo) { + return; + } + + for (const workspace of state.info?.workspaces ?? []) { + workspace.workspace.settings = { + ...workspace.workspace.settings, + ...settings, + }; + } + }); + }, })), shallow ); @@ -88,6 +104,7 @@ export function useCurrentWorkspaceSafe() { id: currentWorkspace.workspace.id, name: currentWorkspace.workspace.name, role: currentWorkspace.role, + settings: currentWorkspace.workspace.settings, }; }); diff --git a/src/server/model/_schema/index.ts b/src/server/model/_schema/index.ts index c170a06..879a4ac 100644 --- a/src/server/model/_schema/index.ts +++ b/src/server/model/_schema/index.ts @@ -19,6 +19,7 @@ export const workspaceDashboardLayoutSchema = z.object({ export const workspaceSchema = z.object({ id: z.string(), name: z.string(), + settings: z.record(z.string(), z.any()), }); export const userInfoSchema = z.object({ diff --git a/src/server/model/user.ts b/src/server/model/user.ts index d200a8a..fd8395d 100644 --- a/src/server/model/user.ts +++ b/src/server/model/user.ts @@ -41,6 +41,7 @@ export const createUserSelect = { select: { id: true, name: true, + settings: true, }, }, }, diff --git a/src/server/trpc/routers/workspace.ts b/src/server/trpc/routers/workspace.ts index f63876e..ab1ac3e 100644 --- a/src/server/trpc/routers/workspace.ts +++ b/src/server/trpc/routers/workspace.ts @@ -26,6 +26,7 @@ import { } from '../../model/user.js'; import { WorkspacesOnUsersModelSchema } from '../../prisma/zod/workspacesonusers.js'; import { monitorManager } from '../../model/monitor/index.js'; +import { get, merge } from 'lodash-es'; export const workspaceRouter = router({ create: protectProedure @@ -236,6 +237,41 @@ export const workspaceRouter = router({ return list; }), + updateSettings: workspaceAdminProcedure + .meta( + buildWorkspaceOpenapi({ + method: 'POST', + path: '/{workspaceId}/updateSettings', + }) + ) + .input( + z.object({ + settings: z.object({}).passthrough(), + }) + ) + .output(workspaceSchema) + .mutation(async ({ input }) => { + const { workspaceId, settings } = input; + + const prev = await prisma.workspace.findUniqueOrThrow({ + where: { + id: workspaceId, + }, + select: { + settings: true, + }, + }); + + const res = await prisma.workspace.update({ + where: { + id: workspaceId, + }, + data: { + settings: merge({}, prev.settings, settings), + }, + }); + return res; + }), invite: workspaceAdminProcedure .meta( buildWorkspaceOpenapi({