diff --git a/apps/job-service/src/jobs/delayed-jobs.controller.spec.ts b/apps/job-service/src/jobs/delayed-jobs.controller.spec.ts index 62c0d85..d23408a 100644 --- a/apps/job-service/src/jobs/delayed-jobs.controller.spec.ts +++ b/apps/job-service/src/jobs/delayed-jobs.controller.spec.ts @@ -223,30 +223,6 @@ describe("DelayedJobsController", () => { await expect(controller.bulkUpdateJobs(payload, request)).rejects.toThrow(NotFoundException); }); - - it('should not update jobs with status "pending"', async () => { - const authenticatedUserId = 130999; - const pendingJob = await DelayedJob.create({ - uuid: uuidv4(), - createdBy: authenticatedUserId, - isAcknowledged: false, - status: "pending", - metadata: { entity_name: "TestEntityPending" } // Adding entity_name - }); - - const payload: DelayedJobBulkUpdateBodyDto = { - data: [ - { - type: "delayedJobs", - uuid: pendingJob.uuid, - attributes: { isAcknowledged: true } - } - ] - }; - const request = { authenticatedUserId }; - - await expect(controller.bulkUpdateJobs(payload, request)).rejects.toThrow(NotFoundException); - }); }); describe("DelayedJobAttributes", () => { diff --git a/apps/job-service/src/jobs/delayed-jobs.controller.ts b/apps/job-service/src/jobs/delayed-jobs.controller.ts index dc30c66..404b93f 100644 --- a/apps/job-service/src/jobs/delayed-jobs.controller.ts +++ b/apps/job-service/src/jobs/delayed-jobs.controller.ts @@ -10,7 +10,7 @@ import { UnauthorizedException } from "@nestjs/common"; import { ApiOperation } from "@nestjs/swagger"; -import { Op } from "sequelize"; +import { Op, WhereOptions } from "sequelize"; import { ExceptionResponse, JsonApiResponse } from "@terramatch-microservices/common/decorators"; import { buildJsonApi, JsonApiDocument } from "@terramatch-microservices/common/util"; import { DelayedJobDto } from "./dto/delayed-job.dto"; @@ -83,19 +83,28 @@ export class DelayedJobsController { @Request() { authenticatedUserId } ): Promise { const jobUpdates = bulkUpdateJobsDto.data; + const whereCondition: WhereOptions = { + uuid: { [Op.in]: jobUpdates.map(({ uuid }) => uuid) }, + createdBy: authenticatedUserId + }; + + if (jobUpdates.length > 1) { + whereCondition.status = { [Op.ne]: "pending" }; + } + const jobs = await DelayedJob.findAll({ - where: { - uuid: { [Op.in]: jobUpdates.map(({ uuid }) => uuid) }, - createdBy: authenticatedUserId, - status: { [Op.ne]: "pending" } - }, + where: whereCondition, order: [["createdAt", "DESC"]] }); - if (jobs.length !== jobUpdates.length) { + if (jobs.length !== jobUpdates.length && jobUpdates.length > 1) { throw new NotFoundException("Some jobs in the request could not be updated"); } + if (jobUpdates.length === 1 && jobs.length === 0) { + throw new NotFoundException("The job in the request could not be found"); + } + const updatePromises = jobUpdates .filter(({ uuid }) => jobs.some(job => job.uuid === uuid)) .map(async ({ uuid, attributes }) => {