Skip to content

Commit

Permalink
feat: Updated deletejob, pausejob and resumejob to show proper erro…
Browse files Browse the repository at this point in the history
…r message (#849)
  • Loading branch information
chavda-bhavik authored Oct 17, 2024
2 parents 5f4315d + 8ee48d1 commit 066a29d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -13,14 +13,22 @@ export class UserJobDelete {
) {}

async execute({ externalUserId, _jobId }: { externalUserId: string; _jobId: string }): Promise<UserJobEntity> {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ export class UserJobPause {
) {}

async execute(_jobId: string): Promise<UserJobEntity> {
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 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ export class UserJobResume {
) {}

async execute(_jobId: string): Promise<UserJobEntity> {
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;
Expand All @@ -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;
}
}

0 comments on commit 066a29d

Please sign in to comment.