feat(audit): change audit log action to string at db level and split file/folder actions

This commit is contained in:
yiling
2026-08-01 14:49:08 +08:00
parent 6d80f06c22
commit 5ef7716c9c
19 changed files with 192 additions and 193 deletions
+25
View File
@@ -5,6 +5,7 @@ import { assetService } from '@shumai/core/src/asset/asset'
import { AssetType } from '@shumai/db'
import { authMiddleware } from './middleware/auth'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
vi.mock('./middleware/auth', () => ({
// any is used here because Hono's Context and Next types are complex to mock in vitest.mock
@@ -18,6 +19,23 @@ vi.mock('./middleware/auth', () => ({
vi.mock('@shumai/core/src/authz/authz')
vi.mock('@shumai/core/src/asset/asset')
vi.mock('@shumai/core/src/auditLog/auditLog', () => ({
auditLogService: {
logAction: vi.fn().mockResolvedValue({}),
},
}))
vi.mock('@shumai/db', async (importOriginal) => {
const actual = await importOriginal<typeof import('@shumai/db')>()
return {
...actual,
prisma: {
project: {
findUnique: vi.fn().mockResolvedValue({ teamId: 't1' }),
},
},
}
})
vi.mock('@shumai/core/src/s3/s3', () => ({
s3Service: {
@@ -65,5 +83,12 @@ describe('Attachment API', () => {
projectId: 'project1',
}),
)
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: 'file_create',
teamId: 't1',
userId: 'user1',
projectId: 'project1',
itemId: 'asset1',
})
})
})
+16 -1
View File
@@ -3,7 +3,8 @@ import { zValidator } from '@hono/zod-validator'
import { postAttachmentRequestSchema } from '@shumai/dtos'
import { assetService } from '@shumai/core/src/asset/asset'
import { s3Service } from '@shumai/core/src/s3/s3'
import { AssetType } from '@shumai/db'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
import { AssetType, AuditAction, prisma } from '@shumai/db'
import { ulid } from 'ulid'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import type { Prisma } from '@shumai/db'
@@ -37,6 +38,20 @@ const route = new Hono<{ Variables: { user: User } }>().post(
creatorId: user.id,
})
const proj = await prisma.project.findUnique({
where: { id: projectId },
select: { teamId: true },
})
if (proj) {
await auditLogService.logAction({
action: AuditAction.file_create,
teamId: proj.teamId,
userId: user.id,
projectId,
itemId: assetInfo.id,
})
}
const uploadUrl = await s3Service.presign(process.env.S3_BUCKET || 'shumai', key, 'PUT')
return c.json({
+2 -2
View File
@@ -138,7 +138,7 @@ describe('file api', () => {
id: 'test-id',
})
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: 'asset_update',
action: 'file_update',
teamId: 'test-team',
userId: 'user1',
projectId: undefined,
@@ -166,7 +166,7 @@ describe('file api', () => {
})
expect(assetService.deleteAssets).toHaveBeenCalledWith(['test-id'])
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: 'asset_delete',
action: 'file_delete',
teamId: 'test-team',
userId: 'user1',
projectId: undefined,
+2 -2
View File
@@ -78,7 +78,7 @@ const route = new Hono<{ Variables: { user: User } }>()
const context = await assetService.getAssetContext(fileId)
await auditLogService.logAction({
action: AuditAction.asset_update,
action: AuditAction.file_update,
teamId: context.teamId,
userId: user.id,
projectId: context.projectId,
@@ -107,7 +107,7 @@ const route = new Hono<{ Variables: { user: User } }>()
for (let i = 0; i < req.ids.length; i++) {
const ctx = contexts[i]
await auditLogService.logAction({
action: AuditAction.asset_delete,
action: AuditAction.file_delete,
teamId: ctx.teamId,
userId: user.id,
projectId: ctx.projectId,
+3 -3
View File
@@ -111,7 +111,7 @@ describe('folder api', () => {
id: 'test-id',
})
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: 'asset_update',
action: 'folder_update',
teamId: 'test-team',
userId: 'user1',
projectId: undefined,
@@ -149,7 +149,7 @@ describe('folder api', () => {
id: 'parent-id',
})
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: 'asset_create',
action: 'folder_create',
teamId: 'test-team',
userId: 'user1',
projectId: undefined,
@@ -210,7 +210,7 @@ describe('folder api', () => {
})
expect(assetService.deleteAssets).toHaveBeenCalledWith(['test-id'])
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: 'asset_delete',
action: 'folder_delete',
teamId: 'test-team',
userId: 'user1',
projectId: undefined,
+3 -3
View File
@@ -37,7 +37,7 @@ const route = new Hono<{ Variables: { user: User } }>()
const parentCtx = await assetService.getAssetContext(req.parentId)
await auditLogService.logAction({
action: AuditAction.asset_create,
action: AuditAction.folder_create,
teamId: parentCtx.teamId,
userId: user.id,
projectId: parentCtx.projectId,
@@ -84,7 +84,7 @@ const route = new Hono<{ Variables: { user: User } }>()
const ctx = await assetService.getAssetContext(folderId)
await auditLogService.logAction({
action: AuditAction.asset_update,
action: AuditAction.folder_update,
teamId: ctx.teamId,
userId: user.id,
projectId: ctx.projectId,
@@ -149,7 +149,7 @@ const route = new Hono<{ Variables: { user: User } }>()
for (let i = 0; i < req.ids.length; i++) {
const ctx = contexts[i]
await auditLogService.logAction({
action: AuditAction.asset_delete,
action: AuditAction.folder_delete,
teamId: ctx.teamId,
userId: user.id,
projectId: ctx.projectId,
+20
View File
@@ -7,6 +7,15 @@ import type { CreateUploadTaskResponse, TaskInfo } from '@shumai/dtos'
import type { PaginatedData } from '@shumai/core/src/pagination'
import { authzService, ResourceType, Permission } from '@shumai/core/src/authz/authz'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
import { assetService } from '@shumai/core/src/asset/asset'
vi.mock('@shumai/core/src/auditLog/auditLog', () => ({
auditLogService: {
logAction: vi.fn().mockResolvedValue({}),
},
}))
vi.mock('./middleware/auth', () => ({
// We use any here because mocking Hono context and middleware
// with full type safety is overly complex for these unit tests.
@@ -98,6 +107,10 @@ describe('Upload API', () => {
it('PATCH /teams/:teamId/upload/tasks/:taskId', async () => {
vi.spyOn(uploadService, 'confirmFileUpload').mockResolvedValue(undefined)
vi.spyOn(assetService, 'getAssetContext').mockResolvedValue({
teamId: 'team1',
projectId: 'p1',
})
const res = await app.request('/teams/team1/upload/tasks/task1', {
method: 'PATCH',
@@ -118,5 +131,12 @@ describe('Upload API', () => {
expect(uploadService.confirmFileUpload).toHaveBeenCalledWith('user1', 'task1', {
fileId: 'file1',
})
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: 'file_create',
teamId: 'team1',
userId: 'user1',
projectId: 'p1',
itemId: 'file1',
})
})
})
+12 -1
View File
@@ -9,7 +9,9 @@ import {
import { paginationParamsSchema } from '@shumai/dtos'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { notificationService } from '@shumai/core/src/notification/notification'
import { NotificationType } from '@shumai/db'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
import { assetService } from '@shumai/core/src/asset/asset'
import { NotificationType, AuditAction } from '@shumai/db'
import { s3Service, verifyLocalUrlSignature } from '@shumai/core/src/s3/s3'
import type { Prisma } from '@shumai/db'
@@ -108,6 +110,15 @@ const route = new Hono<{ Variables: { user: User } }>()
assetId: req.fileId,
taskId: taskId,
})
const context = await assetService.getAssetContext(req.fileId)
await auditLogService.logAction({
action: AuditAction.file_create,
teamId: teamId,
userId: user.id,
projectId: context.projectId,
itemId: req.fileId,
})
}
return c.json({ success: true })