diff --git a/apps/api/src/app/import-jobs/usecase/userjob-usecase/userjob-delete.usecase.ts b/apps/api/src/app/import-jobs/usecase/userjob-usecase/userjob-delete.usecase.ts index e4901b9ea..d870f3fb0 100644 --- a/apps/api/src/app/import-jobs/usecase/userjob-usecase/userjob-delete.usecase.ts +++ b/apps/api/src/app/import-jobs/usecase/userjob-usecase/userjob-delete.usecase.ts @@ -1,8 +1,8 @@ import { Injectable } from '@nestjs/common'; import { SchedulerRegistry } from '@nestjs/schedule'; -import { BadRequestException } from '@nestjs/common'; import { NameService } from '@impler/services'; import { UserJobEntity, UserJobRepository } from '@impler/dal'; +import { DocumentNotFoundException } from '@shared/exceptions/document-not-found.exception'; @Injectable() export class UserJobDelete { @@ -13,14 +13,22 @@ export class UserJobDelete { ) {} async execute({ externalUserId, _jobId }: { externalUserId: string; _jobId: string }): Promise { - const deletedUserJob = await this.userJobRepository.delete({ _id: _jobId, externalUserId }); + const userJobToDelete = await this.userJobRepository.findOne({ _id: _jobId, externalUserId }); - if (!deletedUserJob) { - throw new BadRequestException(`Unable to Delete UserJob with id ${_jobId}`); + if (!userJobToDelete) { + throw new DocumentNotFoundException( + 'Userjob', + _jobId, + `Userjob with JobId ${_jobId} and externalUserId ${externalUserId} not found` + ); } - this.schedulerRegistry.deleteCronJob(this.nameService.getCronName(_jobId)); + try { + this.schedulerRegistry.deleteCronJob(this.nameService.getCronName(_jobId)); + } catch (error) {} - return deletedUserJob; + await this.userJobRepository.delete({ _id: _jobId }); + + return userJobToDelete; } } diff --git a/apps/api/src/app/import-jobs/usecase/userjob-usecase/userjob-pause.usecase.ts b/apps/api/src/app/import-jobs/usecase/userjob-usecase/userjob-pause.usecase.ts index 1213dba84..a4b13a26c 100644 --- a/apps/api/src/app/import-jobs/usecase/userjob-usecase/userjob-pause.usecase.ts +++ b/apps/api/src/app/import-jobs/usecase/userjob-usecase/userjob-pause.usecase.ts @@ -14,12 +14,14 @@ export class UserJobPause { ) {} async execute(_jobId: string): Promise { - const userJob = this.schedulerRegistry.getCronJob(this.nameService.getCronName(_jobId)); - + const userJob = await this.userJobRepository.findById(_jobId); if (!userJob) { - throw new DocumentNotFoundException(`Userjob`, _jobId); + throw new DocumentNotFoundException('Userjob', _jobId); } - userJob.stop(); + + const userCronJob = this.schedulerRegistry.getCronJob(this.nameService.getCronName(_jobId)); + + userCronJob.stop(); const updatedUserJob = await this.userJobRepository.findOneAndUpdate( { _id: _jobId }, diff --git a/apps/api/src/app/import-jobs/usecase/userjob-usecase/userjob.resume.usecsae.ts b/apps/api/src/app/import-jobs/usecase/userjob-usecase/userjob.resume.usecsae.ts index f6bf1c96a..fdb57ad4f 100644 --- a/apps/api/src/app/import-jobs/usecase/userjob-usecase/userjob.resume.usecsae.ts +++ b/apps/api/src/app/import-jobs/usecase/userjob-usecase/userjob.resume.usecsae.ts @@ -18,14 +18,14 @@ export class UserJobResume { ) {} async execute(_jobId: string): Promise { - const userJob = await this.userJobRepository.findOne({ _id: _jobId }); + const userJob = await this.userJobRepository.findById(_jobId); if (!userJob) { throw new DocumentNotFoundException(`Userjob`, _jobId); } if (userJob.status !== UserJobImportStatusEnum.PAUSED) { - throw new BadRequestException(`Job ${_jobId} is not paused. Current status: ${userJob.status}`); + throw new BadRequestException(`Userjob with id ${_jobId} is not paused. Current status: ${userJob.status}`); } const cronExpression = userJob.cron; @@ -46,10 +46,6 @@ export class UserJobResume { { returnDocument: 'after' } ); - if (!updatedUserJob) { - throw new DocumentNotFoundException(`No User Job was found with the given _jobId ${_jobId}`, _jobId); - } - return updatedUserJob; } }