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
15 changes: 14 additions & 1 deletion apps/job-service/src/jobs/delayed-jobs.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,20 @@ export class DelayedJobsController {
uuid: { [Op.in]: jobUpdates.map(({ uuid }) => uuid) },
createdBy: authenticatedUserId,
status: { [Op.ne]: "pending" }
}
},
include: [
{
association: "entityProject",
attributes: ["name"],
required: false
},
{
association: "entitySite",
attributes: ["name"],
required: false
}
],
logging: console.log
roguenet marked this conversation as resolved.
Show resolved Hide resolved
});
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");
Expand Down
26 changes: 5 additions & 21 deletions libs/database/src/lib/entities/delayed-job.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,8 @@ import { User } from "./user.entity";
import { Site } from "./site.entity";
import { Project } from "./project.entity";

interface EntityAssociations {
getEntityProject(): Promise<Project | null>;
getEntitySite(): Promise<Site | null>;
}

@Table({ tableName: "delayed_jobs", underscored: true })
export class DelayedJob extends Model<DelayedJob> implements EntityAssociations {
export class DelayedJob extends Model<DelayedJob> {
@PrimaryKey
@AutoIncrement
@Column(BIGINT.UNSIGNED)
Expand Down Expand Up @@ -78,37 +73,26 @@ export class DelayedJob extends Model<DelayedJob> implements EntityAssociations

@BelongsTo(() => Project, {
foreignKey: "entityId",
constraints: false,
scope: {
entityType: "App\\Models\\V2\\Projects\\Project"
},
as: "entityProject"
constraints: false
})
entityProject?: Project;

@BelongsTo(() => Site, {
foreignKey: "entityId",
constraints: false,
scope: {
entityType: "App\\Models\\V2\\Sites\\Site"
},
as: "entitySite"
constraints: false
roguenet marked this conversation as resolved.
Show resolved Hide resolved
})
entitySite?: Site;

declare getEntityProject: () => Promise<Project | null>;
declare getEntitySite: () => Promise<Site | null>;

async getRelatedEntity(): Promise<string | null> {
if (!this.entityId) return null;

if (this.entityType === "App\\Models\\V2\\Projects\\Project") {
const project = this.entityProject ?? (await this.getEntityProject());
const project = this.entityProject ?? (await this.$get("entityProject"));
return project?.name ?? null;
roguenet marked this conversation as resolved.
Show resolved Hide resolved
}

if (this.entityType === "App\\Models\\V2\\Sites\\Site") {
const site = this.entitySite ?? (await this.getEntitySite());
const site = this.entitySite ?? (await this.$get("entitySite"));
return site?.name ?? null;
}

Expand Down
Loading