@@ -9,20 +9,39 @@ import { IS_PROD_ENV, SERVICE, generateUUIDv1 } from '../../common/src'
99
1010import { BuildActionFn } from './baseActions'
1111
12- // eslint-disable-next-line @typescript-eslint/no-explicit-any
13- function convertRepositoryOptions ( options : any ) : AuditLogRequestOptions {
14- const actor = options . actor ?? {
15- id : options . currentUser ?. id ,
16- type : ActorType . USER ,
12+ export function buildAuditLogOptions (
13+ options ?: {
14+ actor ?: { id ?: string | null ; type ?: ActorType }
15+ currentUser ?: { id ?: string | null }
16+ requestId ?: string
17+ userData ?: { ip ?: string ; userAgent ?: string }
18+ } | null ,
19+ ) : AuditLogRequestOptions | null {
20+ const actorId = options ?. actor ?. id || options ?. currentUser ?. id
21+
22+ // If an actor ID is provided, use it.
23+ if ( actorId ) {
24+ return {
25+ actorId,
26+ actorType : options ?. actor ?. type ?? ActorType . USER ,
27+ requestId : options ?. requestId ?? generateUUIDv1 ( ) ,
28+ ipAddress : options ?. userData ?. ip ,
29+ userAgent : options ?. userData ?. userAgent ,
30+ }
1731 }
1832
19- return {
20- actorId : actor ?. id ,
21- actorType : actor ?. type ,
22- ipAddress : options . userData ?. ip ,
23- userAgent : options . userData ?. userAgent ,
24- requestId : options . requestId ,
33+ // This is the fallback for workers that don't have an actor ID.
34+ if ( process . env . CROWD_LF_AGENT_USER_ID ) {
35+ return {
36+ actorId : process . env . CROWD_LF_AGENT_USER_ID ,
37+ actorType : ActorType . SERVICE ,
38+ requestId : options ?. requestId ?? generateUUIDv1 ( ) ,
39+ ipAddress : options ?. userData ?. ip ?? '127.0.0.1' ,
40+ userAgent : options ?. userData ?. userAgent ?? SERVICE ,
41+ }
2542 }
43+
44+ return null
2645}
2746
2847async function captureChange (
@@ -59,18 +78,9 @@ export async function captureApiChange<T>(
5978) : Promise < T > {
6079 let skip = skipAuditLog
6180
62- let auditOptions : AuditLogRequestOptions
63- if ( options ) {
64- auditOptions = convertRepositoryOptions ( options )
65- } else if ( process . env . CROWD_LF_AGENT_USER_ID ) {
66- auditOptions = {
67- actorId : process . env . CROWD_LF_AGENT_USER_ID ,
68- actorType : ActorType . SERVICE ,
69- requestId : generateUUIDv1 ( ) ,
70- ipAddress : '127.0.0.1' ,
71- userAgent : SERVICE ,
72- }
73- } else if ( ! IS_PROD_ENV ) {
81+ const auditOptions = buildAuditLogOptions ( options )
82+
83+ if ( ! auditOptions && ! IS_PROD_ENV ) {
7484 skip = true
7585 }
7686
0 commit comments