@@ -5,10 +5,11 @@ import { getAuditService } from "@/ee/features/audit/factory";
55import { ErrorCode } from "@/lib/errorCodes" ;
66import { ServiceError } from "@/lib/serviceError" ;
77import { prisma } from "@/prisma" ;
8- import { withAuthV2 } from "@/withAuthV2" ;
8+ import { withAuthV2 , withMinimumOrgRole } from "@/withAuthV2" ;
99import { createLogger } from "@sourcebot/shared" ;
1010import { StatusCodes } from "http-status-codes" ;
1111import { AuditEvent } from "./types" ;
12+ import { OrgRole } from "@sourcebot/db" ;
1213
1314const auditService = getAuditService ( ) ;
1415const logger = createLogger ( 'audit-utils' ) ;
@@ -25,38 +26,39 @@ export const createAuditAction = async (event: Omit<AuditEvent, 'sourcebotVersio
2526) ;
2627
2728export const fetchAuditRecords = async ( ) => sew ( ( ) =>
28- withAuthV2 ( async ( { user, org } ) => {
29- try {
30- const auditRecords = await prisma . audit . findMany ( {
31- where : {
32- orgId : org . id ,
33- } ,
34- orderBy : {
35- timestamp : 'desc'
36- }
37- } ) ;
29+ withAuthV2 ( async ( { user, org, role } ) =>
30+ withMinimumOrgRole ( role , OrgRole . OWNER , async ( ) => {
31+ try {
32+ const auditRecords = await prisma . audit . findMany ( {
33+ where : {
34+ orgId : org . id ,
35+ } ,
36+ orderBy : {
37+ timestamp : 'desc'
38+ }
39+ } ) ;
3840
39- await auditService . createAudit ( {
40- action : "audit.fetch" ,
41- actor : {
42- id : user . id ,
43- type : "user"
44- } ,
45- target : {
46- id : org . id . toString ( ) ,
47- type : "org"
48- } ,
49- orgId : org . id
50- } )
41+ await auditService . createAudit ( {
42+ action : "audit.fetch" ,
43+ actor : {
44+ id : user . id ,
45+ type : "user"
46+ } ,
47+ target : {
48+ id : org . id . toString ( ) ,
49+ type : "org"
50+ } ,
51+ orgId : org . id
52+ } )
5153
52- return auditRecords ;
53- } catch ( error ) {
54- logger . error ( 'Error fetching audit logs' , { error } ) ;
55- return {
56- statusCode : StatusCodes . INTERNAL_SERVER_ERROR ,
57- errorCode : ErrorCode . UNEXPECTED_ERROR ,
58- message : "Failed to fetch audit logs" ,
59- } satisfies ServiceError ;
60- }
61- } )
54+ return auditRecords ;
55+ } catch ( error ) {
56+ logger . error ( 'Error fetching audit logs' , { error } ) ;
57+ return {
58+ statusCode : StatusCodes . INTERNAL_SERVER_ERROR ,
59+ errorCode : ErrorCode . UNEXPECTED_ERROR ,
60+ message : "Failed to fetch audit logs" ,
61+ } satisfies ServiceError ;
62+ }
63+ } ) )
6264) ;
0 commit comments