Skip to content

Commit 0ce3405

Browse files
committed
Refactor ExecuteJobOptions interface to include new properties
- Added switchToProcessing property to ExecuteJobOptions interface - Added targetState property to ExecuteJobOptions interface
1 parent 5198b0d commit 0ce3405

File tree

6 files changed

+80
-9
lines changed

6 files changed

+80
-9
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
import { IsArray } from 'class-validator';
3+
4+
export class DeleteIdentitiesDto {
5+
@IsArray()
6+
@ApiProperty({ type: Array })
7+
public payload: string[];
8+
}

src/core/backends/_interfaces/execute-job-options.interface.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export interface ExecuteJobOptions {
88
syncTimeout?: number;
99
timeoutDiscard?: boolean;
1010
updateStatus?: boolean;
11+
switchToProcessing?: boolean;
1112
comment?: string;
13+
targetState?: any;
1214
task?: Types.ObjectId;
1315
}

src/core/backends/backends.controller.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { BackendsService } from './backends.service';
2121
import { SyncIdentitiesDto } from './_dto/sync-identities.dto';
2222
import { Types } from 'mongoose';
2323
import { ActionType } from './_enum/action-type.enum';
24+
import { DeleteIdentitiesDto } from './_dto/delete-identities.dto';
2425

2526
function fireMessage(observer: Subscriber<MessageEvent>, channel: string, message: any, loggername: string) {
2627
try {
@@ -41,7 +42,22 @@ export class BackendsController {
4142
constructor(
4243
private backendsService: BackendsService,
4344
@InjectRedis() protected readonly redis: Redis,
44-
) {}
45+
) { }
46+
47+
@Post('delete')
48+
@ApiOperation({ summary: "Supprime une liste d'identitées" })
49+
public async deleteIdentities(
50+
@Res() res: Response,
51+
@Body() body: DeleteIdentitiesDto,
52+
@Query('async') asyncQuery: string,
53+
) {
54+
const async = /true|on|yes|1/i.test(asyncQuery);
55+
const data = await this.backendsService.deleteIdentities(body.payload, {
56+
async,
57+
});
58+
59+
return res.status(HttpStatus.ACCEPTED).json({ async, data });
60+
}
4561

4662
@Post('sync')
4763
@ApiOperation({ summary: "Synchronise une liste d'identitées" })

src/core/backends/backends.service.ts

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,48 @@ export class BackendsService extends AbstractQueueProcessor {
226226
return result;
227227
}
228228

229+
public async deleteIdentities(payload: string[], options?: ExecuteJobOptions): Promise<any> {
230+
const identities: {
231+
action: ActionType;
232+
identity: Identities;
233+
}[] = [];
234+
235+
if (!payload.length) throw new BadRequestException('No identities to disable');
236+
237+
for (const key of payload) {
238+
const identity = await this.identitiesService.findById<Identities>(key);
239+
if (!identity.lastSync) {
240+
throw new BadRequestException({
241+
status: HttpStatus.BAD_REQUEST,
242+
message: `Identity ${key} is not in state TO_DELETE`,
243+
identity,
244+
});
245+
}
246+
identities.push({
247+
action: ActionType.IDENTITY_DELETE,
248+
identity,
249+
});
250+
}
251+
252+
const task: Tasks = await this.tasksService.create<Tasks>({
253+
jobs: identities.map((identity) => identity.identity._id),
254+
});
255+
256+
const result = {};
257+
for (const identity of identities) {
258+
const [executedJob, res] = await this.executeJob(identity.action, identity.identity._id, identity.identity, {
259+
...options,
260+
updateStatus: true,
261+
switchToProcessing: false,
262+
targetState: IdentityState.DONT_SYNC,
263+
task: task._id,
264+
});
265+
result[identity.identity._id] = executedJob;
266+
console.log(res);
267+
}
268+
return result;
269+
}
270+
229271
public async executeJob(
230272
actionType: ActionType,
231273
concernedTo?: Types.ObjectId,
@@ -261,10 +303,10 @@ export class BackendsService extends AbstractQueueProcessor {
261303
params: payload,
262304
concernedTo: identity
263305
? {
264-
$ref: 'identities',
265-
id: concernedTo,
266-
name: identity?.inetOrgPerson?.cn,
267-
}
306+
$ref: 'identities',
307+
id: concernedTo,
308+
name: identity?.inetOrgPerson?.cn,
309+
}
268310
: null,
269311
comment: options?.comment,
270312
task: options?.task,
@@ -273,7 +315,7 @@ export class BackendsService extends AbstractQueueProcessor {
273315
});
274316
}
275317

276-
if (concernedTo && !!options?.updateStatus) {
318+
if (concernedTo && !!options?.switchToProcessing) {
277319
await this.identitiesService.model.findByIdAndUpdate(concernedTo, {
278320
$set: {
279321
state: IdentityState.PROCESSING,
@@ -310,7 +352,7 @@ export class BackendsService extends AbstractQueueProcessor {
310352
if (concernedTo && !!options?.updateStatus) {
311353
await this.identitiesService.model.findByIdAndUpdate(concernedTo, {
312354
$set: {
313-
state: IdentityState.SYNCED,
355+
state: options?.targetState || IdentityState.SYNCED,
314356
},
315357
});
316358
}

src/management/identities/identities.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ export class IdentitiesService extends AbstractServiceSchema {
442442
// retourne l'id de na nouvelle identité créée
443443
public async fusion(id1, id2) {
444444
let identity1: Identities = null;
445-
let identity2: Identities = null;
445+
let identity2: Identities & any = null;
446446
try {
447447
identity1 = await this.findById<Identities>(id1);
448448
} catch (error) {
@@ -467,7 +467,7 @@ export class IdentitiesService extends AbstractServiceSchema {
467467
const plainIdentity2 = toPlainAndCrush(identity2.toJSON(), {
468468
excludePrefixes: ['_id', 'fingerprint', 'metadata'],
469469
});
470-
const newObj: Partial<Identities> = construct({ ...plainIdentity2, ...plainIdentity1 });
470+
const newObj: Partial<Identities> & any = construct({ ...plainIdentity2, ...plainIdentity1 });
471471
//const newIdentity = await this.create(newObj);
472472
newObj.inetOrgPerson.employeeType = 'FUSION';
473473
newObj.inetOrgPerson.employeeNumber =

src/management/passwd/passwd.service.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ export class PasswdService extends AbstractService {
222222
async: false,
223223
timeoutDiscard: true,
224224
disableLogs: true,
225+
switchToProcessing: false,
225226
updateStatus: false,
226227
},
227228
);
@@ -355,6 +356,7 @@ export class PasswdService extends AbstractService {
355356
async: false,
356357
timeoutDiscard: true,
357358
disableLogs: false,
359+
switchToProcessing: false,
358360
updateStatus: false,
359361
},
360362
);
@@ -392,6 +394,7 @@ export class PasswdService extends AbstractService {
392394
async: false,
393395
timeoutDiscard: true,
394396
disableLogs: false,
397+
switchToProcessing: false,
395398
updateStatus: false,
396399
},
397400
);

0 commit comments

Comments
 (0)