62 lines
2.2 KiB
TypeScript
62 lines
2.2 KiB
TypeScript
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<string>}
|
|
*/
|
|
static async create(
|
|
createInspectionVersionedPlan: CreateInspectionVersionedPlanCommand,
|
|
inspectionPoints: Array<CreateInspectionPointCommand>
|
|
): Promise<string> {
|
|
let count = await InspectionVersionedPlanService.countForPlanId(createInspectionVersionedPlan.inspectionPlanId);
|
|
let returnId = "";
|
|
|
|
return await dataSource
|
|
.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) => ({
|
|
title: ip.title,
|
|
description: ip.description,
|
|
type: ip.type,
|
|
min: ip.min,
|
|
max: ip.max,
|
|
others: ip.others,
|
|
sort: ip.sort,
|
|
versionedPlanId: returnId,
|
|
}))
|
|
)
|
|
.execute();
|
|
})
|
|
.then(() => {
|
|
return returnId;
|
|
})
|
|
.catch((err) => {
|
|
throw new DatabaseActionException("CREATE", "inspectionVersionedPlan", err);
|
|
});
|
|
}
|
|
}
|