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-1531] prevent error on metadata null #35

Open
wants to merge 2 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
65 changes: 65 additions & 0 deletions apps/job-service/src/jobs/delayed-jobs.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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({
Expand Down
4 changes: 2 additions & 2 deletions apps/job-service/src/jobs/delayed-jobs.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
})
);
Expand Down Expand Up @@ -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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should not be relying on any particular structure to the metadata. I think this would be cleaner:

const entityName = job.metadata?.entity_name ?? "";

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't use it that way because I had this error:
Property 'entity_name' does not exist on type 'object'.ts(2339)

Because the definition is like this:

 @AllowNull
  @Column(JSON)
  metadata: object | null;

Some options to fix it:

const entityName = (job.metadata as any)?.entity_name ?? "";

or:

@AllowNull
@Column(JSON)
metadata: any | null;


return { ...job.toJSON(), entityName };
});
Expand Down
Loading