From 06da566512350451d753027a2e4f3866eea1e704 Mon Sep 17 00:00:00 2001 From: JORGE Date: Thu, 26 Dec 2024 16:23:17 -0400 Subject: [PATCH 1/4] [TM-1531] prevent error on metadata null --- apps/job-service/src/jobs/delayed-jobs.controller.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/job-service/src/jobs/delayed-jobs.controller.ts b/apps/job-service/src/jobs/delayed-jobs.controller.ts index d1378480..f1702182 100644 --- a/apps/job-service/src/jobs/delayed-jobs.controller.ts +++ b/apps/job-service/src/jobs/delayed-jobs.controller.ts @@ -41,7 +41,7 @@ export class DelayedJobsController { const jobsWithEntityNames = await Promise.all( runningJobs.map(async job => { - const entityName = (job.metadata as { entity_name?: string }).entity_name; + const entityName = job.metadata ? (job.metadata as { entity_name?: string }).entity_name : null; return { ...job.toJSON(), entityName }; }) ); @@ -111,7 +111,7 @@ export class DelayedJobsController { job.isAcknowledged = attributes.isAcknowledged; await job.save(); - const entityName = (job.metadata as { entity_name?: string }).entity_name; + const entityName = job.metadata ? (job.metadata as { entity_name?: string }).entity_name : null; return { ...job.toJSON(), entityName }; }); From 82a0d35df7b916560ef53020d06d5375732b9be2 Mon Sep 17 00:00:00 2001 From: JORGE Date: Thu, 26 Dec 2024 16:50:20 -0400 Subject: [PATCH 2/4] [TM-1531] add test --- .../src/jobs/delayed-jobs.controller.spec.ts | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) 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 69287d19..6dd73b0d 100644 --- a/apps/job-service/src/jobs/delayed-jobs.controller.spec.ts +++ b/apps/job-service/src/jobs/delayed-jobs.controller.spec.ts @@ -26,6 +26,26 @@ describe("DelayedJobsController", () => { jest.restoreAllMocks(); }); describe("getRunningJobs", () => { + it("should return a job with null entity_name if metadata is null", async () => { + const authenticatedUserId = 130999; + + const job = await DelayedJob.create({ + uuid: uuidv4(), + createdBy: authenticatedUserId, + isAcknowledged: false, + status: "completed", + metadata: null + }); + + const request = { authenticatedUserId }; + const result = await controller.getRunningJobs(request); + + const data = Array.isArray(result.data) ? result.data : [result.data]; + + expect(data).toHaveLength(1); + expect(data[0].id).toBe(job.uuid); + expect(data[0].attributes.entityName).toBeNull(); + }); it("should return a job with entity_name if metadata exists", async () => { const authenticatedUserId = 130999; @@ -96,6 +116,51 @@ describe("DelayedJobsController", () => { }); describe("bulkUdpateJobs", () => { + it("should successfully update jobs with null metadata", async () => { + const authenticatedUserId = 130999; + const job = await DelayedJob.create({ + uuid: uuidv4(), + createdBy: authenticatedUserId, + isAcknowledged: false, + status: "completed", + metadata: null + }); + + const job1 = await DelayedJob.create({ + uuid: uuidv4(), + createdBy: authenticatedUserId, + isAcknowledged: false, + status: "completed", + metadata: { entity_name: "TestEntity1" } + }); + + const payload: DelayedJobBulkUpdateBodyDto = { + data: [ + { + type: "delayedJobs", + uuid: job.uuid, + attributes: { isAcknowledged: true } + }, + { + type: "delayedJobs", + uuid: job1.uuid, + attributes: { isAcknowledged: true } + } + ] + }; + + const request = { authenticatedUserId }; + + const result = await controller.bulkUpdateJobs(payload, request); + expect(result.data).toHaveLength(2); + expect(result.data[0].id).toBe(job.uuid); + expect(result.data[0].attributes.entityName).toBeNull(); + expect(result.data[1].id).toBe(job1.uuid); + expect(result.data[1].attributes.entityName).toBe("TestEntity1"); + + const updatedJob = await DelayedJob.findOne({ where: { uuid: job.uuid } }); + expect(updatedJob.isAcknowledged).toBe(true); + }); it("should successfully bulk update jobs to acknowledged with entity_name", async () => { const authenticatedUserId = 130999; const job1 = await DelayedJob.create({ From 917d27fd408fb385351d8f8594bd0f1e13f84212 Mon Sep 17 00:00:00 2001 From: JORGE Date: Mon, 6 Jan 2025 14:59:27 -0400 Subject: [PATCH 3/4] [TM-1531] fix metadata type --- apps/job-service/src/jobs/delayed-jobs.controller.ts | 4 ++-- libs/database/src/lib/entities/delayed-job.entity.ts | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/apps/job-service/src/jobs/delayed-jobs.controller.ts b/apps/job-service/src/jobs/delayed-jobs.controller.ts index f1702182..701d70c0 100644 --- a/apps/job-service/src/jobs/delayed-jobs.controller.ts +++ b/apps/job-service/src/jobs/delayed-jobs.controller.ts @@ -41,7 +41,7 @@ export class DelayedJobsController { const jobsWithEntityNames = await Promise.all( runningJobs.map(async job => { - const entityName = job.metadata ? (job.metadata as { entity_name?: string }).entity_name : null; + const entityName = job.metadata?.entity_name; return { ...job.toJSON(), entityName }; }) ); @@ -111,7 +111,7 @@ export class DelayedJobsController { job.isAcknowledged = attributes.isAcknowledged; await job.save(); - const entityName = job.metadata ? (job.metadata as { entity_name?: string }).entity_name : null; + const entityName = job.metadata?.entity_name; return { ...job.toJSON(), entityName }; }); diff --git a/libs/database/src/lib/entities/delayed-job.entity.ts b/libs/database/src/lib/entities/delayed-job.entity.ts index 772288ed..bdfad61b 100644 --- a/libs/database/src/lib/entities/delayed-job.entity.ts +++ b/libs/database/src/lib/entities/delayed-job.entity.ts @@ -12,6 +12,11 @@ import { import { BIGINT, BOOLEAN, INTEGER, JSON, STRING, UUID } from "sequelize"; import { User } from "./user.entity"; +// holds the definition for members that may exist in a job metadata that this codebase explicitly +// references. +interface Metadata { + entity_name?: string; +} @Table({ tableName: "delayed_jobs", underscored: true }) export class DelayedJob extends Model { @PrimaryKey @@ -61,5 +66,5 @@ export class DelayedJob extends Model { @AllowNull @Column(JSON) - metadata: object | null; + metadata: Metadata | null; } From db621a23cd197875e47615103509c29840875d9e Mon Sep 17 00:00:00 2001 From: JORGE Date: Mon, 6 Jan 2025 15:14:12 -0400 Subject: [PATCH 4/4] [TM-1531] fix for undefined metadata --- apps/job-service/src/jobs/delayed-jobs.controller.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 6dd73b0d..0630d618 100644 --- a/apps/job-service/src/jobs/delayed-jobs.controller.spec.ts +++ b/apps/job-service/src/jobs/delayed-jobs.controller.spec.ts @@ -44,7 +44,7 @@ describe("DelayedJobsController", () => { expect(data).toHaveLength(1); expect(data[0].id).toBe(job.uuid); - expect(data[0].attributes.entityName).toBeNull(); + expect(data[0].attributes.entityName).toBeUndefined(); }); it("should return a job with entity_name if metadata exists", async () => { const authenticatedUserId = 130999; @@ -154,7 +154,7 @@ describe("DelayedJobsController", () => { const result = await controller.bulkUpdateJobs(payload, request); expect(result.data).toHaveLength(2); expect(result.data[0].id).toBe(job.uuid); - expect(result.data[0].attributes.entityName).toBeNull(); + expect(result.data[0].attributes.entityName).toBeUndefined(); expect(result.data[1].id).toBe(job1.uuid); expect(result.data[1].attributes.entityName).toBe("TestEntity1");