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] Add site name in entity and return entityname in delayed job #31

Merged
merged 12 commits into from
Dec 24, 2024
Merged
25 changes: 17 additions & 8 deletions apps/job-service/src/jobs/delayed-jobs.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,15 @@ export class DelayedJobsController {
order: [["createdAt", "DESC"]]
});
roguenet marked this conversation as resolved.
Show resolved Hide resolved

const jobsWithEntityNames = await Promise.all(
runningJobs.map(async job => {
const entityName = await job.getRelatedEntity();
return { ...job.toJSON(), entityName };
})
);

const document = buildJsonApi();
runningJobs.forEach(job => {
jobsWithEntityNames.forEach(job => {
document.addData(job.uuid, new DelayedJobDto(job));
});
return document.serialize();
Expand Down Expand Up @@ -92,16 +99,18 @@ export class DelayedJobsController {
status: { [Op.ne]: "pending" }
}
});
if (jobs.length !== jobUpdates.length) {
if (!jobs.length) {
roguenet marked this conversation as resolved.
Show resolved Hide resolved
throw new NotFoundException("Some jobs in the request could not be updated");
}

const updatePromises = jobUpdates.map(async ({ uuid, attributes }) => {
const job = jobs.find(job => job.uuid === uuid);
job.isAcknowledged = attributes.isAcknowledged;
await job.save();
return job;
});
const updatePromises = jobUpdates
.filter(({ uuid }) => jobs.some(job => job.uuid === uuid))
.map(async ({ uuid, attributes }) => {
const job = jobs.find(job => job.uuid === uuid);
job.isAcknowledged = attributes.isAcknowledged;
await job.save();
return job;
});

const updatedJobs = await Promise.all(updatePromises);

Expand Down
13 changes: 13 additions & 0 deletions apps/job-service/src/jobs/dto/delayed-job.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,17 @@ export class DelayedJobDto extends JsonApiAttributes<DelayedJobDto> {
nullable: true
})
isAcknowledged: boolean | null;

@ApiProperty({
description: "The name of the delayedJob",
nullable: true
})
name: string | null;

@ApiProperty({
description: "The name of the related entity (e.g., site, project).",
nullable: true,
required: false
})
entityName?: string | null;
roguenet marked this conversation as resolved.
Show resolved Hide resolved
}
47 changes: 44 additions & 3 deletions libs/database/src/lib/entities/delayed-job.entity.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import { AllowNull, AutoIncrement, Column, Default, ForeignKey, Index, Model, PrimaryKey, Table } from "sequelize-typescript";
import {
AllowNull,
AutoIncrement,
Column,
Default,
ForeignKey,
Index,
Model,
PrimaryKey,
Table
} from "sequelize-typescript";
import { BIGINT, BOOLEAN, INTEGER, JSON, STRING, UUID } from "sequelize";
import { User } from "./user.entity";
import { Site } from "./site.entity";
import { Project } from "./project.entity";
import { Logger } from "@nestjs/common";

const ENTITY_TYPE_MAPPING: Record<string, any> = {
"App\\Models\\V2\\Sites\\Site": Site,
roguenet marked this conversation as resolved.
Show resolved Hide resolved
"App\\Models\\V2\\Projects\\Project": Project
};
@Table({ tableName: "delayed_jobs", underscored: true })
export class DelayedJob extends Model<DelayedJob> {
@PrimaryKey
Expand Down Expand Up @@ -35,7 +52,7 @@ export class DelayedJob extends Model<DelayedJob> {

@AllowNull
@Column(STRING)
progressMessage: string | null
progressMessage: string | null;

@ForeignKey(() => User)
@AllowNull
Expand All @@ -44,5 +61,29 @@ export class DelayedJob extends Model<DelayedJob> {

@Column(BOOLEAN)
isAcknowledged: boolean;


@AllowNull
@Column(STRING)
name: string | null;

@AllowNull
@Column(STRING)
entityType: string | null;

@AllowNull
@Column(BIGINT.UNSIGNED)
entityId: number | null;

/**
* Fetch the related entity dynamically based on the entityType and entityId.
*/
async getRelatedEntity() {
const Model = ENTITY_TYPE_MAPPING[this.entityType || ""];
if (Model && this.entityId) {
const entity = await Model.findByPk(this.entityId);
return entity?.name;
}

return null;
}
}
roguenet marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions libs/database/src/lib/entities/site.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export class Site extends Model<Site> {
@Column(BIGINT.UNSIGNED)
override id: number;

@Column
name: string;

@Index
@Column(UUID)
uuid: string;
Expand Down
Loading