mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-27 19:34:38 +00:00
feat(audit): add audit log support for APIs and WebUI dashboard
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { prisma, AuditAction } from '@shumai/db'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { auditLogService } from './auditLog'
|
||||
|
||||
describe('AuditLogService', () => {
|
||||
setupTestDbHooks()
|
||||
|
||||
it('creates and lists audit logs with pagination and filters', async () => {
|
||||
const user = await prisma.user.create({
|
||||
data: { name: 'Audit Test User', email: 'audit@example.com', password: 'password' },
|
||||
})
|
||||
|
||||
const team = await prisma.team.create({
|
||||
data: { name: 'Audit Test Team' },
|
||||
})
|
||||
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Audit Test Project', teamId: team.id },
|
||||
})
|
||||
|
||||
// Create action logs
|
||||
const log1 = await auditLogService.logAction({
|
||||
action: AuditAction.project_create,
|
||||
teamId: team.id,
|
||||
userId: user.id,
|
||||
projectId: project.id,
|
||||
itemId: project.id,
|
||||
})
|
||||
|
||||
const log2 = await auditLogService.logAction({
|
||||
action: AuditAction.project_update,
|
||||
teamId: team.id,
|
||||
userId: user.id,
|
||||
projectId: project.id,
|
||||
itemId: project.id,
|
||||
})
|
||||
|
||||
expect(log1).toBeDefined()
|
||||
expect(log2).toBeDefined()
|
||||
|
||||
// Test list all audit logs for team
|
||||
const resultAll = await auditLogService.listAuditLogs({
|
||||
teamId: team.id,
|
||||
})
|
||||
|
||||
expect(resultAll.total).toBe(2)
|
||||
expect(resultAll.nodes).toHaveLength(2)
|
||||
expect(resultAll.nodes[0].action).toBe(AuditAction.project_update)
|
||||
expect(resultAll.nodes[1].action).toBe(AuditAction.project_create)
|
||||
|
||||
// Test filtering by action
|
||||
const resultAction = await auditLogService.listAuditLogs({
|
||||
teamId: team.id,
|
||||
actions: [AuditAction.project_create],
|
||||
})
|
||||
|
||||
expect(resultAction.total).toBe(1)
|
||||
expect(resultAction.nodes[0].action).toBe(AuditAction.project_create)
|
||||
|
||||
// Test filtering by user ID
|
||||
const resultUser = await auditLogService.listAuditLogs({
|
||||
teamId: team.id,
|
||||
userIds: [user.id],
|
||||
})
|
||||
|
||||
expect(resultUser.total).toBe(2)
|
||||
|
||||
// Test filtering by item ID
|
||||
const resultItem = await auditLogService.listAuditLogs({
|
||||
teamId: team.id,
|
||||
itemId: project.id,
|
||||
})
|
||||
|
||||
expect(resultItem.total).toBe(2)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,83 @@
|
||||
import { prisma } from '@shumai/db'
|
||||
import type { Prisma, AuditAction } from '@shumai/db'
|
||||
import { paginateQuery } from '@shumai/core/src/pagination'
|
||||
|
||||
export interface LogActionParams {
|
||||
action: AuditAction
|
||||
teamId: string
|
||||
userId?: string
|
||||
projectId?: string
|
||||
itemId?: string
|
||||
}
|
||||
|
||||
export interface ListAuditLogsParams {
|
||||
teamId: string
|
||||
actions?: AuditAction[]
|
||||
userIds?: string[]
|
||||
itemId?: string
|
||||
first?: number
|
||||
after?: string
|
||||
}
|
||||
|
||||
export class AuditLogService {
|
||||
async logAction(params: LogActionParams) {
|
||||
return await prisma.auditLog.create({
|
||||
data: {
|
||||
action: params.action,
|
||||
teamId: params.teamId,
|
||||
userId: params.userId,
|
||||
projectId: params.projectId,
|
||||
itemId: params.itemId,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
async listAuditLogs(params: ListAuditLogsParams) {
|
||||
const where: Prisma.AuditLogWhereInput = {
|
||||
teamId: params.teamId,
|
||||
}
|
||||
|
||||
if (params.actions && params.actions.length > 0) {
|
||||
where.action = { in: params.actions }
|
||||
}
|
||||
|
||||
if (params.userIds && params.userIds.length > 0) {
|
||||
where.userId = { in: params.userIds }
|
||||
}
|
||||
|
||||
if (params.itemId && params.itemId.trim() !== '') {
|
||||
where.itemId = { contains: params.itemId.trim(), mode: 'insensitive' }
|
||||
}
|
||||
|
||||
const result = await paginateQuery(
|
||||
(skip, take) =>
|
||||
prisma.auditLog.findMany({
|
||||
where,
|
||||
orderBy: { id: 'desc' },
|
||||
skip,
|
||||
take,
|
||||
}),
|
||||
() => prisma.auditLog.count({ where }),
|
||||
{ first: params.first, after: params.after },
|
||||
)
|
||||
|
||||
return {
|
||||
nodes: result.data.map((item) => ({
|
||||
id: item.id,
|
||||
action: item.action,
|
||||
teamId: item.teamId,
|
||||
userId: item.userId,
|
||||
projectId: item.projectId,
|
||||
itemId: item.itemId,
|
||||
createdAt: item.createdAt.toISOString(),
|
||||
})),
|
||||
pageInfo: {
|
||||
hasNextPage: !!result.pageInfo.cursor,
|
||||
endCursor: result.pageInfo.cursor || null,
|
||||
},
|
||||
total: result.pageInfo.total ?? 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const auditLogService = new AuditLogService()
|
||||
Reference in New Issue
Block a user