inspection fetch
This commit is contained in:
parent
a0a8edf7af
commit
95b6cec66d
6 changed files with 189 additions and 21 deletions
|
@ -1,6 +1,8 @@
|
|||
import { Not } from "typeorm";
|
||||
import { dataSource } from "../../../data-source";
|
||||
import { inspection } from "../../../entity/unit/inspection/inspection";
|
||||
import DatabaseActionException from "../../../exceptions/databaseActionException";
|
||||
import InspectionService from "../../../service/unit/inspection/inspectionService";
|
||||
import InspectionVersionedPlanService from "../../../service/unit/inspection/inspectionVersionedPlanService";
|
||||
import { CreateInspectionCommand, UpdateInspectionCommand, DeleteInspectionCommand } from "./inspectionCommand";
|
||||
|
||||
|
@ -8,27 +10,42 @@ export default abstract class InspectionCommandHandler {
|
|||
/**
|
||||
* @description create inspection
|
||||
* @param {CreateInspectionCommand} createInspection
|
||||
* @returns {Promise<number>}
|
||||
* @returns {Promise<string>}
|
||||
*/
|
||||
static async create(createInspection: CreateInspectionCommand): Promise<number> {
|
||||
static async create(createInspection: CreateInspectionCommand): Promise<string> {
|
||||
let latestVersionedPlan = await InspectionVersionedPlanService.getLatestForInspectionPlan(
|
||||
createInspection.inspectionPlanId
|
||||
);
|
||||
let insertId = "";
|
||||
return await dataSource
|
||||
.createQueryBuilder()
|
||||
.insert()
|
||||
.into(inspection)
|
||||
.values({
|
||||
context: createInspection.context,
|
||||
nextInspection: createInspection.nextInspection,
|
||||
inspectionPlanId: createInspection.inspectionPlanId,
|
||||
inspectionVersionedPlanId: latestVersionedPlan.id,
|
||||
equipmentId: createInspection.assigned == "equipment" ? createInspection.relatedId : null,
|
||||
vehicleId: createInspection.assigned == "vehicle" ? createInspection.relatedId : null,
|
||||
.transaction(async (manager) => {
|
||||
await manager
|
||||
.createQueryBuilder()
|
||||
.update(inspection)
|
||||
.set({
|
||||
hasNewer: true,
|
||||
})
|
||||
.execute();
|
||||
|
||||
await manager
|
||||
.createQueryBuilder()
|
||||
.insert()
|
||||
.into(inspection)
|
||||
.values({
|
||||
context: createInspection.context,
|
||||
nextInspection: createInspection.nextInspection,
|
||||
inspectionPlanId: createInspection.inspectionPlanId,
|
||||
inspectionVersionedPlanId: latestVersionedPlan.id,
|
||||
equipmentId: createInspection.assigned == "equipment" ? createInspection.relatedId : null,
|
||||
vehicleId: createInspection.assigned == "vehicle" ? createInspection.relatedId : null,
|
||||
})
|
||||
.execute()
|
||||
.then((result) => {
|
||||
insertId = result.identifiers[0].id;
|
||||
});
|
||||
})
|
||||
.execute()
|
||||
.then((result) => {
|
||||
return result.identifiers[0].id;
|
||||
.then(() => {
|
||||
return insertId;
|
||||
})
|
||||
.catch((err) => {
|
||||
throw new DatabaseActionException("CREATE", "inspection", err);
|
||||
|
@ -62,12 +79,42 @@ export default abstract class InspectionCommandHandler {
|
|||
* @returns {Promise<void>}
|
||||
*/
|
||||
static async delete(deleteInspection: DeleteInspectionCommand): Promise<void> {
|
||||
let deleteInspectionData = await InspectionService.getById(deleteInspection.id);
|
||||
return await dataSource
|
||||
.createQueryBuilder()
|
||||
.delete()
|
||||
.from(inspection)
|
||||
.where("id = :id", { id: deleteInspection.id })
|
||||
.execute()
|
||||
.transaction(async (manager) => {
|
||||
await manager
|
||||
.createQueryBuilder()
|
||||
.update(inspection)
|
||||
.set({
|
||||
hasNewer: false,
|
||||
})
|
||||
.where((qb) => {
|
||||
const subQuery = qb
|
||||
.createQueryBuilder()
|
||||
.select("id")
|
||||
.from(inspection, "sub")
|
||||
.where({
|
||||
inspectionPlanId: deleteInspectionData.inspectionPlanId,
|
||||
inspectionVersionedPlanId: deleteInspectionData.inspectionVersionedPlanId,
|
||||
equipmentId: deleteInspectionData.equipmentId,
|
||||
vehicleId: deleteInspectionData.vehicleId,
|
||||
wearableId: deleteInspectionData.wearableId,
|
||||
})
|
||||
.andWhere({ id: Not(deleteInspection.id) })
|
||||
.orderBy("sub.createdAt", "DESC")
|
||||
.limit(1)
|
||||
.getQuery();
|
||||
return "id = " + subQuery;
|
||||
})
|
||||
.execute();
|
||||
|
||||
await manager
|
||||
.createQueryBuilder()
|
||||
.delete()
|
||||
.from(inspection)
|
||||
.where("id = :id", { id: deleteInspection.id })
|
||||
.execute();
|
||||
})
|
||||
.then(() => {})
|
||||
.catch((err) => {
|
||||
throw new DatabaseActionException("DELETE", "inspection", err);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue