Skip to content

Commit 318d453

Browse files
committed
fix: enhance lifecycleChangedIdentities method to handle identity changes
#81 - Updated the lifecycleChangedIdentities method to accept a more complex payload structure, allowing for both before and after states of identities. - Adjusted the logic to ensure proper handling of identity IDs and added checks for missing IDs. - Modified the lifecycle hooks service to pass the new identity structure when calling lifecycleChangedIdentities, improving the clarity of identity updates.
1 parent 113b925 commit 318d453

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

apps/api/src/core/backends/backends.service.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,16 +248,23 @@ export class BackendsService extends AbstractQueueProcessor {
248248
return result;
249249
}
250250

251-
public async lifecycleChangedIdentities(payload: string[], options?: ExecuteJobOptions): Promise<any> {
251+
public async lifecycleChangedIdentities(
252+
payload: (string | { id?: string; before?: Identities; after?: Identities })[],
253+
options?: ExecuteJobOptions,
254+
): Promise<any> {
252255
const identities: {
253256
action: ActionType;
257+
before?: Identities;
254258
identity: Identities;
255259
}[] = [];
256260

257261
if (!payload.length) throw new BadRequestException('No identities to sync');
258262

259-
for (const key of payload) {
260-
const identity = await this.identitiesService.findById<any>(key);
263+
for (const item of payload) {
264+
const before = typeof item === 'string' ? undefined : item.before;
265+
const identityId = typeof item === 'string' ? item : (item.after?._id?.toString() || item.id);
266+
if (!identityId) throw new BadRequestException('Missing identity id for lifecycle change');
267+
const identity = (typeof item === 'string' || !item.after) ? await this.identitiesService.findById<any>(identityId) : item.after;
261268
// cas des fusion l employeeNumber doit etre celui de l identite primaire
262269
if (identity.primaryEmployeeNumber !== null && identity.primaryEmployeeNumber !== '') {
263270
identity.inetOrgPerson.employeeNumber = identity.primaryEmployeeNumber;
@@ -267,6 +274,7 @@ export class BackendsService extends AbstractQueueProcessor {
267274
}
268275
identities.push({
269276
action: ActionType.IDENTITY_LIFECYCLE_CHANGED,
277+
before,
270278
identity,
271279
});
272280
}
@@ -277,7 +285,7 @@ export class BackendsService extends AbstractQueueProcessor {
277285

278286
const result = {};
279287
for (const identity of identities) {
280-
const [executedJob] = await this.executeJob(identity.action, identity.identity._id, identity.identity, {
288+
const [executedJob] = await this.executeJob(identity.action, identity.identity._id, { before: identity.before, after: identity.identity }, {
281289
...options,
282290
updateStatus: true,
283291
task: task._id as unknown as Types.ObjectId,

apps/api/src/management/lifecycle/lifecycle-hooks.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ export class LifecycleHooksService extends AbstractLifecycleService {
486486
date: new Date(),
487487
})
488488

489-
const identities = res._id ? [res._id] : []
489+
const identities = res._id ? [{ id: res._id.toString(), before: after, after: res }] : []
490490
await this.backendsService.lifecycleChangedIdentities(identities)
491491

492492
this.logger.log(`Identity <${res._id}> updated to lifecycle <${lcs.target}> based on rules from source <${after.lifecycle}>`)
@@ -495,7 +495,7 @@ export class LifecycleHooksService extends AbstractLifecycleService {
495495
}
496496

497497
if (lifecycleChanged) {
498-
const identities = after._id ? [after._id] : []
498+
const identities = after._id ? [{ id: after._id.toString(), before, after }] : []
499499
await this.backendsService.lifecycleChangedIdentities(identities)
500500
}
501501
}

0 commit comments

Comments
 (0)