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

[TM-1711] update bulkUpdateJobs to allow updating a single job that i… #60

Open
wants to merge 9 commits into
base: staging
Choose a base branch
from
Open
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
24 changes: 0 additions & 24 deletions apps/job-service/src/jobs/delayed-jobs.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
23 changes: 16 additions & 7 deletions apps/job-service/src/jobs/delayed-jobs.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -83,19 +83,28 @@ export class DelayedJobsController {
@Request() { authenticatedUserId }
): Promise<JsonApiDocument> {
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 }) => {
Expand Down