Skip to content

Commit 4490778

Browse files
committed
Ajouter la méthode syncAllIdentities dans le contrôleur BackendsController
1 parent 0c9cfd8 commit 4490778

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/core/backends/backends.controller.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import { ExecuteJobDto } from './_dto/execute-job.dto';
2020
import { BackendsService } from './backends.service';
2121
import { SyncIdentitiesDto } from './_dto/sync-identities.dto';
2222
import { Types } from 'mongoose';
23+
import { IdentityState } from '~/management/identities/_enums/states.enum';
24+
import { Identities } from '~/management/identities/_schemas/identities.schema';
2325

2426
function fireMessage(observer: Subscriber<MessageEvent>, channel: string, message: any, loggername: string) {
2527
try {
@@ -56,6 +58,19 @@ export class BackendsController {
5658
return res.status(HttpStatus.ACCEPTED).json({ async, data });
5759
}
5860

61+
@Post('syncall')
62+
public async syncAllIdentities(
63+
@Res() res: Response,
64+
@Body() body: SyncIdentitiesDto,
65+
@Query('async') asyncQuery: string,
66+
) {
67+
const async = /true|on|yes|1/i.test(asyncQuery);
68+
const data = await this.backendsService.syncAllIdentities({
69+
async,
70+
});
71+
return res.status(HttpStatus.ACCEPTED).json({ async, data });
72+
}
73+
5974
@Post('execute')
6075
public async executeJob(
6176
@Res() res: Response,

src/core/backends/backends.service.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,45 @@ export class BackendsService extends AbstractQueueProcessor {
108108
});
109109
}
110110

111+
public async syncAllIdentities(options?: ExecuteJobOptions): Promise<any> {
112+
const syncAllIdentities = (await this.identitiesService.find<Identities>({
113+
state: IdentityState.TO_SYNC,
114+
})) as unknown as Identities[];
115+
const identities = syncAllIdentities.map((identity: Identities) => {
116+
return {
117+
action: ActionType.IDENTITY_UPDATE,
118+
identity,
119+
};
120+
});
121+
122+
const task: Tasks = await this.tasksService.create<Tasks>({
123+
jobs: identities.map((identity) => identity.identity._id),
124+
});
125+
126+
const result = {};
127+
for (const identity of identities) {
128+
const [executedJob] = await this.executeJob(
129+
identity.action,
130+
identity.identity._id,
131+
{ identity },
132+
{
133+
...options,
134+
task: task._id,
135+
},
136+
);
137+
result[identity.identity._id] = executedJob;
138+
}
139+
return result;
140+
}
141+
111142
public async syncIdentities(payload: string[], options?: ExecuteJobOptions): Promise<any> {
112143
const identities: {
113144
action: ActionType;
114145
identity: Identities;
115146
}[] = [];
116147

148+
if (!payload.length) throw new BadRequestException('No identities to sync');
149+
117150
for (const key of payload) {
118151
const identity = await this.identitiesService.findById<Identities>(key);
119152
if (identity.state !== IdentityState.TO_SYNC) {

0 commit comments

Comments
 (0)