mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-30 12:54:55 +00:00
feat(core): wrap audit log creation in try catch to prevent blocking APIs
This commit is contained in:
@@ -75,4 +75,13 @@ describe('AuditLogService', () => {
|
|||||||
|
|
||||||
expect(resultItem.total).toBe(2)
|
expect(resultItem.total).toBe(2)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('handles database errors gracefully and logs error without throwing', async () => {
|
||||||
|
const result = await auditLogService.logAction({
|
||||||
|
action: AuditAction.project_create,
|
||||||
|
teamId: 'invalid-non-existent-team-id',
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(result).toBeNull()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { prisma } from '@shumai/db'
|
|||||||
import type { Prisma } from '@shumai/db'
|
import type { Prisma } from '@shumai/db'
|
||||||
import type { AuditAction } from '@shumai/dtos'
|
import type { AuditAction } from '@shumai/dtos'
|
||||||
import { paginateQuery } from '@shumai/core/src/pagination'
|
import { paginateQuery } from '@shumai/core/src/pagination'
|
||||||
|
import { logger } from '@shumai/core/src/logger'
|
||||||
|
|
||||||
export interface LogActionParams {
|
export interface LogActionParams {
|
||||||
action: AuditAction
|
action: AuditAction
|
||||||
@@ -22,15 +23,20 @@ export interface ListAuditLogsParams {
|
|||||||
|
|
||||||
export class AuditLogService {
|
export class AuditLogService {
|
||||||
async logAction(params: LogActionParams) {
|
async logAction(params: LogActionParams) {
|
||||||
return await prisma.auditLog.create({
|
try {
|
||||||
data: {
|
return await prisma.auditLog.create({
|
||||||
action: params.action,
|
data: {
|
||||||
teamId: params.teamId,
|
action: params.action,
|
||||||
userId: params.userId,
|
teamId: params.teamId,
|
||||||
projectId: params.projectId,
|
userId: params.userId,
|
||||||
itemId: params.itemId,
|
projectId: params.projectId,
|
||||||
},
|
itemId: params.itemId,
|
||||||
})
|
},
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
logger.error({ err, params }, 'Failed to create audit log')
|
||||||
|
return null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async listAuditLogs(params: ListAuditLogsParams) {
|
async listAuditLogs(params: ListAuditLogsParams) {
|
||||||
|
|||||||
Reference in New Issue
Block a user