command Handlers and schema update
This commit is contained in:
parent
0f6401953f
commit
7883bb7d7f
42 changed files with 1076 additions and 159 deletions
76
src/command/unit/inspection/inspectionCommandHandler.ts
Normal file
76
src/command/unit/inspection/inspectionCommandHandler.ts
Normal file
|
@ -0,0 +1,76 @@
|
|||
import { dataSource } from "../../../data-source";
|
||||
import { inspection } from "../../../entity/unit/inspection/inspection";
|
||||
import DatabaseActionException from "../../../exceptions/databaseActionException";
|
||||
import InspectionVersionedPlanService from "../../../service/unit/inspection/inspectionVersionedPlanService";
|
||||
import { CreateInspectionCommand, UpdateInspectionCommand, DeleteInspectionCommand } from "./inspectionCommand";
|
||||
|
||||
export default abstract class InspectionCommandHandler {
|
||||
/**
|
||||
* @description create inspection
|
||||
* @param {CreateInspectionCommand} createInspection
|
||||
* @returns {Promise<number>}
|
||||
*/
|
||||
static async create(createInspection: CreateInspectionCommand): Promise<number> {
|
||||
let latestVersionedPlan = await InspectionVersionedPlanService.getLatestForInspectionPlan(
|
||||
createInspection.inspectionPlanId
|
||||
);
|
||||
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,
|
||||
})
|
||||
.execute()
|
||||
.then((result) => {
|
||||
return result.identifiers[0].id;
|
||||
})
|
||||
.catch((err) => {
|
||||
throw new DatabaseActionException("CREATE", "inspection", err);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description update inspection
|
||||
* @param {UpdateInspectionCommand} updateInspection
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
static async update(updateInspection: UpdateInspectionCommand): Promise<void> {
|
||||
return await dataSource
|
||||
.createQueryBuilder()
|
||||
.update(inspection)
|
||||
.set({
|
||||
context: updateInspection.context,
|
||||
nextInspection: updateInspection.nextInspection,
|
||||
})
|
||||
.where("id = :id", { id: updateInspection.id })
|
||||
.execute()
|
||||
.then(() => {})
|
||||
.catch((err) => {
|
||||
throw new DatabaseActionException("UPDATE", "inspection", err);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description delete inspection
|
||||
* @param {DeleteInspectionCommand} deleteInspection
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
static async delete(deleteInspection: DeleteInspectionCommand): Promise<void> {
|
||||
return await dataSource
|
||||
.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