Skip to content

Commit 4483c5d

Browse files
authored
fix: restore LF agent actionBy on worker member merges (#4378)
Signed-off-by: Yeganathan S <63534555+skwowet@users.noreply.github.com>
1 parent b7a606e commit 4483c5d

2 files changed

Lines changed: 38 additions & 31 deletions

File tree

services/libs/audit-logs/src/svc.ts

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,39 @@ import { IS_PROD_ENV, SERVICE, generateUUIDv1 } from '../../common/src'
99

1010
import { 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

2847
async 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

services/libs/common_services/src/services/common.member.service.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import pick from 'lodash.pick'
33
import moment from 'moment'
44

55
import {
6+
buildAuditLogOptions,
67
captureApiChange,
78
memberEditOrganizationsAction,
89
memberMergeAction,
@@ -315,6 +316,8 @@ export class CommonMemberService extends LoggerBase {
315316
}
316317
}
317318

319+
const actorId = buildAuditLogOptions(options)?.actorId
320+
318321
const mergeActions = await queryMergeActions(this.qx, {
319322
fields: ['id', 'state'],
320323
filter: {
@@ -386,7 +389,7 @@ export class CommonMemberService extends LoggerBase {
386389
MergeActionStep.MERGE_STARTED,
387390
MergeActionState.IN_PROGRESS,
388391
backup,
389-
options?.currentUser?.id,
392+
actorId,
390393
)
391394

392395
await this.qx.tx(async (txQx) => {
@@ -469,13 +472,7 @@ export class CommonMemberService extends LoggerBase {
469472
retry: {
470473
maximumAttempts: 10,
471474
},
472-
args: [
473-
originalId,
474-
toMergeId,
475-
original.displayName,
476-
toMerge.displayName,
477-
options?.currentUser?.id,
478-
],
475+
args: [originalId, toMergeId, original.displayName, toMerge.displayName, actorId],
479476
searchAttributes: {
480477
TenantId: [DEFAULT_TENANT_ID],
481478
},

0 commit comments

Comments
 (0)