feat: add workspace audit log db model

This commit is contained in:
moonrailgun 2024-01-22 13:07:46 +08:00
parent 0502185e7c
commit 7243b991ae
6 changed files with 92 additions and 1 deletions

View File

@ -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;
}

View File

@ -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;

View File

@ -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
}

View File

@ -15,3 +15,4 @@ export * from "./monitordata"
export * from "./monitorstatus"
export * from "./monitorstatuspage"
export * from "./workspacedailyusage"
export * from "./workspaceauditlog"

View File

@ -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<typeof WorkspaceModelSchema>
monitorStatusPages: CompleteMonitorStatusPage[]
selectedUsers: CompleteUser[]
workspaceDailyUsage: CompleteWorkspaceDailyUsage[]
workspaceAuditLog: CompleteWorkspaceAuditLog[]
}
/**
@ -47,4 +48,5 @@ export const RelatedWorkspaceModelSchema: z.ZodSchema<CompleteWorkspace> = z.laz
monitorStatusPages: RelatedMonitorStatusPageModelSchema.array(),
selectedUsers: RelatedUserModelSchema.array(),
workspaceDailyUsage: RelatedWorkspaceDailyUsageModelSchema.array(),
workspaceAuditLog: RelatedWorkspaceAuditLogModelSchema.array(),
}))

View File

@ -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<typeof WorkspaceAuditLogModelSchema> {
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<CompleteWorkspaceAuditLog> = z.lazy(() => WorkspaceAuditLogModelSchema.extend({
workspace: RelatedWorkspaceModelSchema,
}))