Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Updated deletejob, pausejob and resumejob to show proper error message #849

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading