@@ -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 }
0 commit comments