From 7243b991ae4ed5978b365994896de26669863ab1 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Mon, 22 Jan 2024 13:07:46 +0800 Subject: [PATCH] feat: add workspace audit log db model --- src/server/model/auditLog.ts | 20 ++++++++++++++ .../migration.sql | 23 ++++++++++++++++ src/server/prisma/schema.prisma | 19 ++++++++++++++ src/server/prisma/zod/index.ts | 1 + src/server/prisma/zod/workspace.ts | 4 ++- src/server/prisma/zod/workspaceauditlog.ts | 26 +++++++++++++++++++ 6 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 src/server/model/auditLog.ts create mode 100644 src/server/prisma/migrations/20240122041756_add_workspace_audit_log_and_settings/migration.sql create mode 100644 src/server/prisma/zod/workspaceauditlog.ts diff --git a/src/server/model/auditLog.ts b/src/server/model/auditLog.ts new file mode 100644 index 0000000..4e2fb23 --- /dev/null +++ b/src/server/model/auditLog.ts @@ -0,0 +1,20 @@ +import { WorkspaceAuditLogType } from '@prisma/client'; +import { prisma } from './_client'; + +/** + * create audit log which can query by log + */ +export async function createAuditLog(info: { + workspaceId: string; + relatedId?: string; + relatedType?: WorkspaceAuditLogType; + content: string; +}) { + const log = await prisma.workspaceAuditLog.create({ + data: { + ...info, + }, + }); + + return log; +} diff --git a/src/server/prisma/migrations/20240122041756_add_workspace_audit_log_and_settings/migration.sql b/src/server/prisma/migrations/20240122041756_add_workspace_audit_log_and_settings/migration.sql new file mode 100644 index 0000000..4f1a01e --- /dev/null +++ b/src/server/prisma/migrations/20240122041756_add_workspace_audit_log_and_settings/migration.sql @@ -0,0 +1,23 @@ +-- CreateEnum +CREATE TYPE "WorkspaceAuditLogType" AS ENUM ('Monitor', 'Notification'); + +-- AlterTable +ALTER TABLE "Workspace" ADD COLUMN "settings" JSONB NOT NULL DEFAULT '{}'; + +-- CreateTable +CREATE TABLE "WorkspaceAuditLog" ( + "id" VARCHAR(30) NOT NULL, + "workspaceId" VARCHAR(30) NOT NULL, + "content" TEXT NOT NULL, + "relatedId" TEXT, + "relatedType" "WorkspaceAuditLogType", + "createdAt" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "WorkspaceAuditLog_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE INDEX "WorkspaceAuditLog_createdAt_idx" ON "WorkspaceAuditLog"("createdAt"); + +-- AddForeignKey +ALTER TABLE "WorkspaceAuditLog" ADD CONSTRAINT "WorkspaceAuditLog_workspaceId_fkey" FOREIGN KEY ("workspaceId") REFERENCES "Workspace"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/src/server/prisma/schema.prisma b/src/server/prisma/schema.prisma index 20b1a8e..cec56c4 100644 --- a/src/server/prisma/schema.prisma +++ b/src/server/prisma/schema.prisma @@ -52,6 +52,7 @@ model Workspace { // for user currentWorkspace selectedUsers User[] // user list who select this workspace, not use in most of case workspaceDailyUsage WorkspaceDailyUsage[] + workspaceAuditLog WorkspaceAuditLog[] } model WorkspacesOnUsers { @@ -342,3 +343,21 @@ model WorkspaceDailyUsage { @@unique([workspaceId, date]) } + +model WorkspaceAuditLog { + id String @id @default(cuid()) @db.VarChar(30) + workspaceId String @db.VarChar(30) + content String + relatedId String? + relatedType WorkspaceAuditLogType? + createdAt DateTime @default(now()) @db.Timestamptz(6) + + workspace Workspace @relation(fields: [workspaceId], references: [id], onUpdate: Cascade, onDelete: Cascade) + + @@index([createdAt]) +} + +enum WorkspaceAuditLogType { + Monitor + Notification +} diff --git a/src/server/prisma/zod/index.ts b/src/server/prisma/zod/index.ts index e33a8e9..e9c5a6f 100644 --- a/src/server/prisma/zod/index.ts +++ b/src/server/prisma/zod/index.ts @@ -15,3 +15,4 @@ export * from "./monitordata" export * from "./monitorstatus" export * from "./monitorstatuspage" export * from "./workspacedailyusage" +export * from "./workspaceauditlog" diff --git a/src/server/prisma/zod/workspace.ts b/src/server/prisma/zod/workspace.ts index 5e166a8..150755e 100644 --- a/src/server/prisma/zod/workspace.ts +++ b/src/server/prisma/zod/workspace.ts @@ -1,6 +1,6 @@ import * as z from "zod" import * as imports from "./schemas" -import { CompleteWorkspacesOnUsers, RelatedWorkspacesOnUsersModelSchema, CompleteWebsite, RelatedWebsiteModelSchema, CompleteNotification, RelatedNotificationModelSchema, CompleteMonitor, RelatedMonitorModelSchema, CompleteMonitorStatusPage, RelatedMonitorStatusPageModelSchema, CompleteUser, RelatedUserModelSchema, CompleteWorkspaceDailyUsage, RelatedWorkspaceDailyUsageModelSchema } from "./index" +import { CompleteWorkspacesOnUsers, RelatedWorkspacesOnUsersModelSchema, CompleteWebsite, RelatedWebsiteModelSchema, CompleteNotification, RelatedNotificationModelSchema, CompleteMonitor, RelatedMonitorModelSchema, CompleteMonitorStatusPage, RelatedMonitorStatusPageModelSchema, CompleteUser, RelatedUserModelSchema, CompleteWorkspaceDailyUsage, RelatedWorkspaceDailyUsageModelSchema, CompleteWorkspaceAuditLog, RelatedWorkspaceAuditLogModelSchema } from "./index" // Helper schema for JSON fields type Literal = boolean | number | string @@ -32,6 +32,7 @@ export interface CompleteWorkspace extends z.infer monitorStatusPages: CompleteMonitorStatusPage[] selectedUsers: CompleteUser[] workspaceDailyUsage: CompleteWorkspaceDailyUsage[] + workspaceAuditLog: CompleteWorkspaceAuditLog[] } /** @@ -47,4 +48,5 @@ export const RelatedWorkspaceModelSchema: z.ZodSchema = z.laz monitorStatusPages: RelatedMonitorStatusPageModelSchema.array(), selectedUsers: RelatedUserModelSchema.array(), workspaceDailyUsage: RelatedWorkspaceDailyUsageModelSchema.array(), + workspaceAuditLog: RelatedWorkspaceAuditLogModelSchema.array(), })) diff --git a/src/server/prisma/zod/workspaceauditlog.ts b/src/server/prisma/zod/workspaceauditlog.ts new file mode 100644 index 0000000..c2613f3 --- /dev/null +++ b/src/server/prisma/zod/workspaceauditlog.ts @@ -0,0 +1,26 @@ +import * as z from "zod" +import * as imports from "./schemas" +import { WorkspaceAuditLogType } from "@prisma/client" +import { CompleteWorkspace, RelatedWorkspaceModelSchema } from "./index" + +export const WorkspaceAuditLogModelSchema = z.object({ + id: z.string(), + workspaceId: z.string(), + content: z.string(), + relatedId: z.string().nullish(), + relatedType: z.nativeEnum(WorkspaceAuditLogType).nullish(), + createdAt: z.date(), +}) + +export interface CompleteWorkspaceAuditLog extends z.infer { + workspace: CompleteWorkspace +} + +/** + * RelatedWorkspaceAuditLogModelSchema contains all relations on your model in addition to the scalars + * + * NOTE: Lazy required in case of potential circular dependencies within schema + */ +export const RelatedWorkspaceAuditLogModelSchema: z.ZodSchema = z.lazy(() => WorkspaceAuditLogModelSchema.extend({ + workspace: RelatedWorkspaceModelSchema, +}))