11import {
22 BadRequestException ,
3- HttpException ,
43 HttpStatus ,
4+ HttpException ,
55 Injectable ,
66 RequestTimeoutException ,
77 UnprocessableEntityException ,
@@ -11,24 +11,24 @@ import { Document, ModifyResult, Query, Types } from 'mongoose';
1111import { AbstractQueueProcessor } from '~/_common/abstracts/abstract.queue.processor' ;
1212import { IdentityState } from '~/management/identities/_enums/states.enum' ;
1313import { Identities } from '~/management/identities/_schemas/identities.schema' ;
14- import { IdentitiesService } from '~/management/identities/identities.service' ;
14+ import { IdentitiesCrudService } from '~/management/identities/identities-crud .service' ;
1515import { JobState } from '../jobs/_enums/state.enum' ;
1616import { Jobs } from '../jobs/_schemas/jobs.schema' ;
1717import { JobsService } from '../jobs/jobs.service' ;
1818import { Tasks } from '../tasks/_schemas/tasks.schema' ;
1919import { TasksService } from '../tasks/tasks.service' ;
2020import { ActionType } from './_enum/action-type.enum' ;
2121import { ExecuteJobOptions } from './_interfaces/execute-job-options.interface' ;
22- import { BackendResultInterface } from " ~/core/backends/_interfaces/backend -result.interface" ;
23- import { WorkerResultInterface } from "~/core/backends/_interfaces/worker-result.interface" ;
22+ import { WorkerResultInterface } from ' ~/core/backends/_interfaces/worker -result.interface' ;
23+ import { DataStatusEnum } from '~/management/identities/_enums/data-status' ;
2424
2525const DEFAULT_SYNC_TIMEOUT = 30_000 ;
2626
2727@Injectable ( )
2828export class BackendsService extends AbstractQueueProcessor {
2929 public constructor (
3030 protected moduleRef : ModuleRef ,
31- protected identitiesService : IdentitiesService ,
31+ protected identitiesService : IdentitiesCrudService ,
3232 protected jobsService : JobsService ,
3333 protected tasksService : TasksService ,
3434 ) {
@@ -265,6 +265,50 @@ export class BackendsService extends AbstractQueueProcessor {
265265 updateStatus : true ,
266266 switchToProcessing : false ,
267267 targetState : IdentityState . DONT_SYNC ,
268+ dataState : DataStatusEnum . DELETED ,
269+ task : task . _id ,
270+ } ) ;
271+ result [ identity . identity . _id ] = executedJob ;
272+ console . log ( res ) ;
273+ }
274+ return result ;
275+ }
276+
277+ public async disableIdentities ( payload : string [ ] , options ?: ExecuteJobOptions ) : Promise < any > {
278+ const identities : {
279+ action : ActionType ;
280+ identity : Identities ;
281+ } [ ] = [ ] ;
282+
283+ if ( ! payload . length ) throw new BadRequestException ( 'No identities to disable' ) ;
284+
285+ for ( const key of payload ) {
286+ const identity = await this . identitiesService . findById < Identities > ( key ) ;
287+ if ( ! identity . lastBackendSync ) {
288+ throw new BadRequestException ( {
289+ status : HttpStatus . BAD_REQUEST ,
290+ message : `Identity ${ key } has never been synched` ,
291+ identity,
292+ } ) ;
293+ }
294+ identities . push ( {
295+ action : ActionType . IDENTITY_DISABLE ,
296+ identity,
297+ } ) ;
298+ }
299+
300+ const task : Tasks = await this . tasksService . create < Tasks > ( {
301+ jobs : identities . map ( ( identity ) => identity . identity . _id ) ,
302+ } ) ;
303+
304+ const result = { } ;
305+ for ( const identity of identities ) {
306+ const [ executedJob , res ] = await this . executeJob ( identity . action , identity . identity . _id , identity . identity , {
307+ ...options ,
308+ updateStatus : true ,
309+ switchToProcessing : false ,
310+ targetState : IdentityState . SYNCED ,
311+ dataState : DataStatusEnum . INACTIVE ,
268312 task : task . _id ,
269313 } ) ;
270314 result [ identity . identity . _id ] = executedJob ;
@@ -273,6 +317,57 @@ export class BackendsService extends AbstractQueueProcessor {
273317 return result ;
274318 }
275319
320+ public async enableIdentities ( payload : string [ ] , options ?: ExecuteJobOptions ) : Promise < any > {
321+ const identities : {
322+ action : ActionType ;
323+ identity : Identities ;
324+ } [ ] = [ ] ;
325+
326+ if ( ! payload . length ) throw new BadRequestException ( 'No identities to disable' ) ;
327+
328+ for ( const key of payload ) {
329+ const identity = await this . identitiesService . findById < Identities > ( key ) ;
330+ if ( ! identity . lastBackendSync ) {
331+ throw new BadRequestException ( {
332+ status : HttpStatus . BAD_REQUEST ,
333+ message : `Identity ${ key } has never been synched` ,
334+ identity,
335+ } ) ;
336+ }
337+ identities . push ( {
338+ action : ActionType . IDENTITY_ENABLE ,
339+ identity,
340+ } ) ;
341+ }
342+
343+ const task : Tasks = await this . tasksService . create < Tasks > ( {
344+ jobs : identities . map ( ( identity ) => identity . identity . _id ) ,
345+ } ) ;
346+
347+ const result = { } ;
348+ for ( const identity of identities ) {
349+ const [ executedJob , res ] = await this . executeJob ( identity . action , identity . identity . _id , identity . identity , {
350+ ...options ,
351+ updateStatus : true ,
352+ switchToProcessing : false ,
353+ targetState : IdentityState . SYNCED ,
354+ dataState : DataStatusEnum . ACTIVE ,
355+ task : task . _id ,
356+ } ) ;
357+ result [ identity . identity . _id ] = executedJob ;
358+ console . log ( res ) ;
359+ }
360+ return result ;
361+ }
362+ public async activationIdentity ( payload : string , status : boolean , options ?: ExecuteJobOptions ) {
363+ let result = null ;
364+ if ( status === true ) {
365+ result = await this . enableIdentities ( [ payload ] , options ) ;
366+ } else {
367+ result = await this . disableIdentities ( [ payload ] , options ) ;
368+ }
369+ return result [ payload ] ;
370+ }
276371 public async executeJob (
277372 actionType : ActionType ,
278373 concernedTo ?: Types . ObjectId ,
@@ -289,7 +384,7 @@ export class BackendsService extends AbstractQueueProcessor {
289384 } ,
290385 {
291386 ...options ?. job ,
292- jobId : ( new Types . ObjectId ( ) ) . toHexString ( ) ,
387+ jobId : ( new Types . ObjectId ( ) ) . toHexString ( ) ,
293388 attempts : 1 ,
294389 } ,
295390 ) ;
0 commit comments