enhance: filter inspections by related decommission date

This commit is contained in:
Julian Krauser 2025-07-29 10:26:33 +02:00
parent a26bbe6e45
commit 69169aa925

View file

@ -1,4 +1,4 @@
import { IsNull, Not } from "typeorm";
import { Brackets, IsNull, Not } from "typeorm";
import { dataSource } from "../../../data-source";
import { inspection } from "../../../entity/unit/inspection/inspection";
import DatabaseActionException from "../../../exceptions/databaseActionException";
@ -46,7 +46,45 @@ export default abstract class InspectionService {
count?: number;
noLimit?: boolean;
}): Promise<[Array<inspection>, number]> {
let query = this.query().where({ hasNewer: false });
let query = this.query()
.where({ hasNewer: false })
.andWhere(
new Brackets((qb) => {
["equipment", "vehicle", "wearable"].forEach((type) => {
qb.orWhere(
new Brackets((subQb) => {
subQb.where(`${type}.id IS NOT NULL`).andWhere(
new Brackets((innerQb) => {
// 1) {type}.decommissioned IS NULL -> ausgeben
innerQb.where(`${type}.decommissioned IS NULL`);
// 2) {type}.decommissioned IS NOT NULL
innerQb.orWhere(
new Brackets((decomQb) => {
decomQb.where(`${type}.decommissioned IS NOT NULL`).andWhere(
new Brackets((dateQb) => {
// 2.1) inspection.nextInspection < {type}.decommissioned -> ausgeben
dateQb.where(
`inspection.nextInspection IS NOT NULL AND inspection.nextInspection < ${type}.decommissioned`
);
// 2.2) inspection.nextInspection IS NULL -> ausgeben, wenn {type}.decommissioned < [todayDate]
dateQb.orWhere(
new Brackets((nullQb) => {
nullQb
.where("inspection.nextInspection IS NULL")
.andWhere(`${type}.decommissioned > :today`, { today: new Date() });
})
);
})
);
})
);
})
);
})
);
});
})
);
if (!noLimit) {
query = query.offset(offset).limit(count);