update or create inspection versioned plan

This commit is contained in:
Julian Krauser 2025-07-09 16:01:44 +02:00
parent 95b6cec66d
commit db3004fa04
13 changed files with 229 additions and 11 deletions

View file

@ -1,6 +1,7 @@
import { dataSource } from "../../../data-source";
import { inspectionPoint } from "../../../entity/unit/inspection/inspectionPoint";
import DatabaseActionException from "../../../exceptions/databaseActionException";
import InspectionPointService from "../../../service/unit/inspection/inspectionPointService";
import { CreateInspectionPointCommand } from "./inspectionPointCommand";
export default abstract class InspectionPointCommandHandler {
@ -31,4 +32,42 @@ export default abstract class InspectionPointCommandHandler {
throw new DatabaseActionException("CREATE", "inspectionPoint", err);
});
}
/**
* @description sync points
* @param {string} versionedPlanId
* @param {Array<CreateInspectionPointCommand>} sync
* @returns {Promise<void>}
*/
static async sync(versionedPlanId: string, sync: Array<CreateInspectionPointCommand>): Promise<void> {
let points = await InspectionPointService.getAllForVersionedPlan(versionedPlanId);
await dataSource
.transaction(async (manager) => {
let remove = points.filter((r) => !sync.some((cp) => cp.id == r.id));
await manager
.createQueryBuilder()
.insert()
.into(inspectionPoint)
.values(
sync.map((s) => ({
...s,
versionedPlanId,
}))
)
.orUpdate(["title", "description", "min", "max", "others", "sort"], ["id"])
.execute();
if (remove.length != 0)
await manager
.createQueryBuilder()
.delete()
.from(inspectionPoint)
.where("id IN (:...ids)", { ids: remove.map((r) => r.id) })
.andWhere({ versionedPlanId })
.execute();
})
.catch((err) => {
throw new DatabaseActionException("SYNC", "inspectionPoint", err);
});
}
}