@@ -3,21 +3,22 @@ import {
33 HttpException ,
44 HttpStatus ,
55 Injectable ,
6+ NotFoundException ,
67 UnprocessableEntityException ,
78} from '@nestjs/common' ;
89import { ModuleRef } from '@nestjs/core' ;
9- import { Types } from 'mongoose' ;
10+ import { Document , ModifyResult , Query , Types } from 'mongoose' ;
1011import { AbstractQueueProcessor } from '~/_common/abstracts/abstract.queue.processor' ;
1112import { IdentityState } from '~/management/identities/_enums/states.enum' ;
1213import { Identities } from '~/management/identities/_schemas/identities.schema' ;
1314import { IdentitiesService } from '~/management/identities/identities.service' ;
1415import { JobState } from '../jobs/_enums/state.enum' ;
1516import { Jobs } from '../jobs/_schemas/jobs.schema' ;
1617import { JobsService } from '../jobs/jobs.service' ;
18+ import { Tasks } from '../tasks/_schemas/tasks.schema' ;
1719import { TasksService } from '../tasks/tasks.service' ;
1820import { ActionType } from './_enum/action-type.enum' ;
1921import { ExecuteJobOptions } from './_interfaces/execute-job-options.interface' ;
20- import { Tasks } from '../tasks/_schemas/tasks.schema' ;
2122
2223const DEFAULT_SYNC_TIMEOUT = 30_000 ;
2324
@@ -115,7 +116,7 @@ export class BackendsService extends AbstractQueueProcessor {
115116 } ,
116117 { upsert : true , new : true } ,
117118 ) ;
118- console . log ( 'completedJob' , completedJob ) ;
119+
119120 await this . identitiesService . model . findByIdAndUpdate ( completedJob ?. concernedTo ?. id , {
120121 $set : {
121122 state : IdentityState . SYNCED ,
@@ -205,7 +206,7 @@ export class BackendsService extends AbstractQueueProcessor {
205206
206207 public async executeJob (
207208 actionType : ActionType ,
208- concernedTo : Types . ObjectId ,
209+ concernedTo ? : Types . ObjectId ,
209210 // eslint-disable-next-line @typescript-eslint/no-unused-vars
210211 payload ?: Record < string | number , any > ,
211212 options ?: ExecuteJobOptions ,
@@ -223,63 +224,82 @@ export class BackendsService extends AbstractQueueProcessor {
223224 optionals [ 'processedAt' ] = new Date ( ) ;
224225 optionals [ 'state' ] = JobState . IN_PROGRESS ;
225226 }
226- const identity = await this . identitiesService . findById < Identities > ( concernedTo ) ;
227- const jobStore = await this . jobsService . create < Jobs > ( {
228- jobId : job . id ,
229- action : actionType ,
230- params : payload ,
231- concernedTo : {
232- $ref : 'identities' ,
233- id : concernedTo ,
234- name : [ identity . inetOrgPerson ?. cn , identity . inetOrgPerson ?. givenName ] . join ( ' ' ) ,
235- } ,
236- comment : options ?. comment ,
237- task : options ?. task ,
238- state : JobState . CREATED ,
239- ...optionals ,
240- } ) ;
241- await this . identitiesService . model . findByIdAndUpdate ( concernedTo , {
242- $set : {
243- state : IdentityState . PROCESSING ,
244- } ,
245- } ) ;
227+
228+ let jobStore : Document < Jobs > = null ;
229+ if ( ! options ?. disableLogs ) {
230+ const identity = concernedTo ? await this . identitiesService . findById < Identities > ( concernedTo ) : null ;
231+ jobStore = await this . jobsService . create < Jobs > ( {
232+ jobId : job . id ,
233+ action : actionType ,
234+ params : payload ,
235+ concernedTo : identity ? {
236+ $ref : 'identities' ,
237+ id : concernedTo ,
238+ name : [ identity ?. inetOrgPerson ?. cn , identity ?. inetOrgPerson ?. givenName ] . join ( ' ' ) ,
239+ } : null ,
240+ comment : options ?. comment ,
241+ task : options ?. task ,
242+ state : JobState . CREATED ,
243+ ...optionals ,
244+ } ) ;
245+ }
246+
247+ if ( concernedTo ) {
248+ await this . identitiesService . model . findByIdAndUpdate ( concernedTo , {
249+ $set : {
250+ state : IdentityState . PROCESSING ,
251+ } ,
252+ } ) ;
253+ }
254+
246255 if ( ! options ?. async ) {
247256 let error : Error ;
248257 try {
249258 const response = await job . waitUntilFinished ( this . queueEvents , options . syncTimeout || DEFAULT_SYNC_TIMEOUT ) ;
250- const jobStoreUpdated = await this . jobsService . update < Jobs > ( jobStore . _id , {
259+ let jobStoreUpdated : ModifyResult < Query < Jobs , Jobs > > = null ;
260+ if ( ! options ?. disableLogs ) {
261+ jobStoreUpdated = await this . jobsService . update < Jobs > ( jobStore . _id , {
262+ $set : {
263+ state : JobState . COMPLETED ,
264+ processedAt : new Date ( ) ,
265+ finishedAt : new Date ( ) ,
266+ result : response ,
267+ } ,
268+ } ) ;
269+ }
270+ if ( concernedTo ) {
271+ await this . identitiesService . model . findByIdAndUpdate ( concernedTo , {
272+ $set : {
273+ state : IdentityState . SYNCED ,
274+ } ,
275+ } ) ;
276+ }
277+ return [ jobStoreUpdated as unknown as Jobs , response ] ;
278+ } catch ( err ) {
279+ error = err ;
280+ }
281+
282+ let jobFailed : ModifyResult < Query < Jobs , Jobs > > = null ;
283+ if ( ! options ?. disableLogs ) {
284+ jobFailed = await this . jobsService . update < Jobs > ( jobStore . _id , {
251285 $set : {
252- state : JobState . COMPLETED ,
253- processedAt : new Date ( ) ,
286+ state : JobState . FAILED ,
254287 finishedAt : new Date ( ) ,
255- result : response ,
288+ result : {
289+ error : {
290+ message : error . message ,
291+ } ,
292+ } ,
256293 } ,
257294 } ) ;
295+ }
296+ if ( concernedTo ) {
258297 await this . identitiesService . model . findByIdAndUpdate ( concernedTo , {
259298 $set : {
260- state : IdentityState . SYNCED ,
299+ state : IdentityState . ON_ERROR ,
261300 } ,
262301 } ) ;
263- return [ jobStoreUpdated as unknown as Jobs , response ] ;
264- } catch ( err ) {
265- error = err ;
266302 }
267- const jobFailed = await this . jobsService . update < Jobs > ( jobStore . _id , {
268- $set : {
269- state : JobState . FAILED ,
270- finishedAt : new Date ( ) ,
271- result : {
272- error : {
273- message : error . message ,
274- } ,
275- } ,
276- } ,
277- } ) ;
278- await this . identitiesService . model . findByIdAndUpdate ( concernedTo , {
279- $set : {
280- state : IdentityState . ON_ERROR ,
281- } ,
282- } ) ;
283303 if ( options ?. timeoutDiscard !== false ) {
284304 job . discard ( ) ;
285305 throw new BadRequestException ( {
@@ -296,6 +316,6 @@ export class BackendsService extends AbstractQueueProcessor {
296316 job : jobFailed as unknown as Jobs ,
297317 } ) ;
298318 }
299- return [ jobStore . toObject ( ) , null ] ;
319+ return [ jobStore ? .toObject ( ) || null , null ] ;
300320 }
301321}
0 commit comments