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,11 +1,13 @@
import { InspectionPointEnum } from "../../../enums/inspectionEnum";
export interface CreateInspectionPointCommand {
id?: string;
title: string;
description: string;
type: InspectionPointEnum;
min?: number;
max?: number;
others?: string;
sort: number;
versionedPointId: string;
versionedPointId?: string;
}

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);
});
}
}

View file

@ -1,25 +1,53 @@
import { dataSource } from "../../../data-source";
import { inspectionPoint } from "../../../entity/unit/inspection/inspectionPoint";
import { inspectionVersionedPlan } from "../../../entity/unit/inspection/inspectionVersionedPlan";
import DatabaseActionException from "../../../exceptions/databaseActionException";
import InspectionVersionedPlanService from "../../../service/unit/inspection/inspectionVersionedPlanService";
import { CreateInspectionPointCommand } from "./inspectionPointCommand";
import { CreateInspectionVersionedPlanCommand } from "./inspectionVersionedPlanCommand";
export default abstract class InspectionVersionedPlanCommandHandler {
/**
* @description create inspectionVersionedPlan
* @param {CreateInspectionVersionedPlanCommand} createInspectionVersionedPlan
* @returns {Promise<number>}
* @returns {Promise<string>}
*/
static async create(createInspectionVersionedPlan: CreateInspectionVersionedPlanCommand): Promise<number> {
static async create(
createInspectionVersionedPlan: CreateInspectionVersionedPlanCommand,
inspectionPoints: Array<CreateInspectionPointCommand>
): Promise<string> {
let count = await InspectionVersionedPlanService.countForPlanId(createInspectionVersionedPlan.inspectionPlanId);
let returnId = "";
return await dataSource
.createQueryBuilder()
.insert()
.into(inspectionVersionedPlan)
.values({
inspectionPlanId: createInspectionVersionedPlan.inspectionPlanId,
.transaction(async (manager) => {
await manager
.createQueryBuilder()
.insert()
.into(inspectionVersionedPlan)
.values({
inspectionPlanId: createInspectionVersionedPlan.inspectionPlanId,
version: count,
})
.execute()
.then((result) => {
returnId = result.identifiers[0].id;
});
await manager
.createQueryBuilder()
.insert()
.into(inspectionPoint)
.values(
inspectionPoints.map((ip) => ({
...ip,
versionedPlanId: returnId,
}))
)
.execute();
})
.execute()
.then((result) => {
return result.identifiers[0].id;
.then(() => {
return returnId;
})
.catch((err) => {
throw new DatabaseActionException("CREATE", "inspectionVersionedPlan", err);