diff --git a/src/command/unit/damageReportCommand.ts b/src/command/unit/damageReportCommand.ts new file mode 100644 index 0000000..62a4773 --- /dev/null +++ b/src/command/unit/damageReportCommand.ts @@ -0,0 +1,22 @@ +export interface CreateDamageReportCommand { + description: string; + reportedBy: string; + imageCount: number; + affectedId: string; + affected: "equipment" | "vehicle" | "wearable"; +} + +export interface UpdateDamageReportCommand { + id: string; + status: string; + done: boolean; +} + +export interface UpdateDamageReportRelatedMaintenanceCommand { + id: string; + maintenanceId: string; +} + +export interface DeleteDamageReportCommand { + id: string; +} diff --git a/src/command/unit/damageReportCommandHandler.ts b/src/command/unit/damageReportCommandHandler.ts new file mode 100644 index 0000000..71a72fa --- /dev/null +++ b/src/command/unit/damageReportCommandHandler.ts @@ -0,0 +1,100 @@ +import { dataSource } from "../../data-source"; +import { damageReport } from "../../entity/unit/damageReport"; +import DatabaseActionException from "../../exceptions/databaseActionException"; +import { + CreateDamageReportCommand, + UpdateDamageReportCommand, + DeleteDamageReportCommand, + UpdateDamageReportRelatedMaintenanceCommand, +} from "./damageReportCommand"; + +export default abstract class DamageReportCommandHandler { + /** + * @description create damageReport + * @param {CreateDamageReportCommand} createDamageReport + * @returns {Promise} + */ + static async create(createDamageReport: CreateDamageReportCommand): Promise { + return await dataSource + .createQueryBuilder() + .insert() + .into(damageReport) + .values({ + status: "eingereicht", + description: createDamageReport.description, + reportedBy: createDamageReport.reportedBy, + imageCount: createDamageReport.imageCount, + equipmentId: createDamageReport.affected == "equipment" ? createDamageReport.affectedId : null, + vehicleId: createDamageReport.affected == "vehicle" ? createDamageReport.affectedId : null, + wearableId: createDamageReport.affected == "wearable" ? createDamageReport.affectedId : null, + }) + .execute() + .then((result) => { + return result.identifiers[0].id; + }) + .catch((err) => { + throw new DatabaseActionException("CREATE", "damageReport", err); + }); + } + + /** + * @description update damageReport + * @param {UpdateDamageReportCommand} updateDamageReport + * @returns {Promise} + */ + static async update(updateDamageReport: UpdateDamageReportCommand): Promise { + return await dataSource + .createQueryBuilder() + .update(damageReport) + .set({ + status: updateDamageReport.status, + done: updateDamageReport.done, + }) + .where("id = :id", { id: updateDamageReport.id }) + .execute() + .then(() => {}) + .catch((err) => { + throw new DatabaseActionException("UPDATE", "damageReport", err); + }); + } + + /** + * @description update damageReport related maintenance + * @param {UpdateDamageReportCommand} updateDamageReport + * @returns {Promise} + */ + static async updateRelatedMaintenance( + updateDamageReport: UpdateDamageReportRelatedMaintenanceCommand + ): Promise { + return await dataSource + .createQueryBuilder() + .update(damageReport) + .set({ + maintenanceId: updateDamageReport.maintenanceId, + }) + .where("id = :id", { id: updateDamageReport.id }) + .execute() + .then(() => {}) + .catch((err) => { + throw new DatabaseActionException("UPDATE", "damageReport->maintenance", err); + }); + } + + /** + * @description delete damageReport + * @param {DeleteDamageReportCommand} deleteDamageReport + * @returns {Promise} + */ + static async delete(deleteDamageReport: DeleteDamageReportCommand): Promise { + return await dataSource + .createQueryBuilder() + .delete() + .from(damageReport) + .where("id = :id", { id: deleteDamageReport.id }) + .execute() + .then(() => {}) + .catch((err) => { + throw new DatabaseActionException("DELETE", "damageReport", err); + }); + } +} diff --git a/src/command/unit/equipment/equipmentCommand.ts b/src/command/unit/equipment/equipmentCommand.ts new file mode 100644 index 0000000..500d4c2 --- /dev/null +++ b/src/command/unit/equipment/equipmentCommand.ts @@ -0,0 +1,20 @@ +export interface CreateEquipmentCommand { + code?: string; + name: string; + location: string; + commissioned: Date; + equipmentTypeId: string; +} + +export interface UpdateEquipmentCommand { + id: string; + code?: string; + name: string; + location: string; + commissioned: Date; + decommissioned?: Date; +} + +export interface DeleteEquipmentCommand { + id: string; +} diff --git a/src/command/unit/equipment/equipmentCommandHandler.ts b/src/command/unit/equipment/equipmentCommandHandler.ts new file mode 100644 index 0000000..0aef534 --- /dev/null +++ b/src/command/unit/equipment/equipmentCommandHandler.ts @@ -0,0 +1,74 @@ +import { dataSource } from "../../../data-source"; +import { equipment } from "../../../entity/unit/equipment/equipment"; +import DatabaseActionException from "../../../exceptions/databaseActionException"; +import { CreateEquipmentCommand, UpdateEquipmentCommand, DeleteEquipmentCommand } from "./equipmentCommand"; + +export default abstract class EquipmentCommandHandler { + /** + * @description create equipment + * @param {CreateEquipmentCommand} createEquipment + * @returns {Promise} + */ + static async create(createEquipment: CreateEquipmentCommand): Promise { + return await dataSource + .createQueryBuilder() + .insert() + .into(equipment) + .values({ + code: createEquipment.code, + name: createEquipment.name, + location: createEquipment.location, + commissioned: createEquipment.commissioned, + equipmentTypeId: createEquipment.equipmentTypeId, + }) + .execute() + .then((result) => { + return result.identifiers[0].id; + }) + .catch((err) => { + throw new DatabaseActionException("CREATE", "equipment", err); + }); + } + + /** + * @description update equipment + * @param {UpdateEquipmentCommand} updateEquipment + * @returns {Promise} + */ + static async update(updateEquipment: UpdateEquipmentCommand): Promise { + return await dataSource + .createQueryBuilder() + .update(equipment) + .set({ + code: updateEquipment.code, + name: updateEquipment.name, + location: updateEquipment.location, + commissioned: updateEquipment.commissioned, + decommissioned: updateEquipment.decommissioned, + }) + .where("id = :id", { id: updateEquipment.id }) + .execute() + .then(() => {}) + .catch((err) => { + throw new DatabaseActionException("UPDATE", "equipment", err); + }); + } + + /** + * @description delete equipment + * @param {DeleteEquipmentCommand} deleteEquipment + * @returns {Promise} + */ + static async delete(deleteEquipment: DeleteEquipmentCommand): Promise { + return await dataSource + .createQueryBuilder() + .delete() + .from(equipment) + .where("id = :id", { id: deleteEquipment.id }) + .execute() + .then(() => {}) + .catch((err) => { + throw new DatabaseActionException("DELETE", "equipment", err); + }); + } +} diff --git a/src/command/unit/equipment/equipmentTypeCommand.ts b/src/command/unit/equipment/equipmentTypeCommand.ts new file mode 100644 index 0000000..7d5c2f7 --- /dev/null +++ b/src/command/unit/equipment/equipmentTypeCommand.ts @@ -0,0 +1,14 @@ +export interface CreateEquipmentTypeCommand { + type: string; + description: string; +} + +export interface UpdateEquipmentTypeCommand { + id: string; + type: string; + description: string; +} + +export interface DeleteEquipmentTypeCommand { + id: string; +} diff --git a/src/command/unit/equipment/equipmentTypeCommandHandler.ts b/src/command/unit/equipment/equipmentTypeCommandHandler.ts new file mode 100644 index 0000000..47caec3 --- /dev/null +++ b/src/command/unit/equipment/equipmentTypeCommandHandler.ts @@ -0,0 +1,72 @@ +import { dataSource } from "../../../data-source"; +import { equipmentType } from "../../../entity/unit/equipment/equipmentType"; +import DatabaseActionException from "../../../exceptions/databaseActionException"; +import { + CreateEquipmentTypeCommand, + UpdateEquipmentTypeCommand, + DeleteEquipmentTypeCommand, +} from "./equipmentTypeCommand"; + +export default abstract class EquipmentTypeCommandHandler { + /** + * @description create equipmentType + * @param {CreateEquipmentTypeCommand} createEquipmentType + * @returns {Promise} + */ + static async create(createEquipmentType: CreateEquipmentTypeCommand): Promise { + return await dataSource + .createQueryBuilder() + .insert() + .into(equipmentType) + .values({ + type: createEquipmentType.type, + description: createEquipmentType.description, + }) + .execute() + .then((result) => { + return result.identifiers[0].id; + }) + .catch((err) => { + throw new DatabaseActionException("CREATE", "equipmentType", err); + }); + } + + /** + * @description update equipmentType + * @param {UpdateEquipmentTypeCommand} updateEquipmentType + * @returns {Promise} + */ + static async update(updateEquipmentType: UpdateEquipmentTypeCommand): Promise { + return await dataSource + .createQueryBuilder() + .update(equipmentType) + .set({ + type: updateEquipmentType.type, + description: updateEquipmentType.description, + }) + .where("id = :id", { id: updateEquipmentType.id }) + .execute() + .then(() => {}) + .catch((err) => { + throw new DatabaseActionException("UPDATE", "equipmentType", err); + }); + } + + /** + * @description delete equipmentType + * @param {DeleteEquipmentTypeCommand} deleteEquipmentType + * @returns {Promise} + */ + static async delete(deleteEquipmentType: DeleteEquipmentTypeCommand): Promise { + return await dataSource + .createQueryBuilder() + .delete() + .from(equipmentType) + .where("id = :id", { id: deleteEquipmentType.id }) + .execute() + .then(() => {}) + .catch((err) => { + throw new DatabaseActionException("DELETE", "equipmentType", err); + }); + } +} diff --git a/src/command/unit/inspection/inspectionCommand.ts b/src/command/unit/inspection/inspectionCommand.ts new file mode 100644 index 0000000..1c020fb --- /dev/null +++ b/src/command/unit/inspection/inspectionCommand.ts @@ -0,0 +1,17 @@ +export interface CreateInspectionCommand { + context: string; + nextInspection?: Date; + inspectionPlanId: string; + relatedId: string; + assigned: "vehicle" | "equipment"; +} + +export interface UpdateInspectionCommand { + id: string; + context: string; + nextInspection?: Date; +} + +export interface DeleteInspectionCommand { + id: string; +} diff --git a/src/command/unit/inspection/inspectionCommandHandler.ts b/src/command/unit/inspection/inspectionCommandHandler.ts new file mode 100644 index 0000000..5f4a61d --- /dev/null +++ b/src/command/unit/inspection/inspectionCommandHandler.ts @@ -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} + */ + static async create(createInspection: CreateInspectionCommand): Promise { + 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} + */ + static async update(updateInspection: UpdateInspectionCommand): Promise { + 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} + */ + static async delete(deleteInspection: DeleteInspectionCommand): Promise { + return await dataSource + .createQueryBuilder() + .delete() + .from(inspection) + .where("id = :id", { id: deleteInspection.id }) + .execute() + .then(() => {}) + .catch((err) => { + throw new DatabaseActionException("DELETE", "inspection", err); + }); + } +} diff --git a/src/command/unit/inspection/inspectionPlanCommand.ts b/src/command/unit/inspection/inspectionPlanCommand.ts new file mode 100644 index 0000000..0daac13 --- /dev/null +++ b/src/command/unit/inspection/inspectionPlanCommand.ts @@ -0,0 +1,20 @@ +import { PlanTimeDefinition } from "../../../viewmodel/admin/unit/inspection/inspectionPlan.models"; + +export interface CreateInspectionPlanCommand { + title: string; + inspectionInterval: PlanTimeDefinition; + remindTime: PlanTimeDefinition; + relatedId: string; + assigned: "vehicle" | "equipment"; +} + +export interface UpdateInspectionPlanCommand { + id: string; + title: string; + inspectionInterval: PlanTimeDefinition; + remindTime?: PlanTimeDefinition; +} + +export interface DeleteInspectionPlanCommand { + id: string; +} diff --git a/src/command/unit/inspection/inspectionPlanCommandHandler.ts b/src/command/unit/inspection/inspectionPlanCommandHandler.ts new file mode 100644 index 0000000..cf48ef7 --- /dev/null +++ b/src/command/unit/inspection/inspectionPlanCommandHandler.ts @@ -0,0 +1,76 @@ +import { dataSource } from "../../../data-source"; +import { inspectionPlan } from "../../../entity/unit/inspection/inspectionPlan"; +import DatabaseActionException from "../../../exceptions/databaseActionException"; +import { + CreateInspectionPlanCommand, + UpdateInspectionPlanCommand, + DeleteInspectionPlanCommand, +} from "./inspectionPlanCommand"; + +export default abstract class InspectionPlanCommandHandler { + /** + * @description create inspectionPlan + * @param {CreateInspectionPlanCommand} createInspectionPlan + * @returns {Promise} + */ + static async create(createInspectionPlan: CreateInspectionPlanCommand): Promise { + return await dataSource + .createQueryBuilder() + .insert() + .into(inspectionPlan) + .values({ + title: createInspectionPlan.title, + inspectionInterval: createInspectionPlan.inspectionInterval, + remindTime: createInspectionPlan.remindTime, + equipmentId: createInspectionPlan.assigned == "equipment" ? createInspectionPlan.relatedId : null, + vehicleId: createInspectionPlan.assigned == "vehicle" ? createInspectionPlan.relatedId : null, + }) + .execute() + .then((result) => { + return result.identifiers[0].id; + }) + .catch((err) => { + throw new DatabaseActionException("CREATE", "inspectionPlan", err); + }); + } + + /** + * @description update inspectionPlan + * @param {UpdateInspectionPlanCommand} updateInspectionPlan + * @returns {Promise} + */ + static async update(updateInspectionPlan: UpdateInspectionPlanCommand): Promise { + return await dataSource + .createQueryBuilder() + .update(inspectionPlan) + .set({ + title: updateInspectionPlan.title, + inspectionInterval: updateInspectionPlan.inspectionInterval, + remindTime: updateInspectionPlan.remindTime, + }) + .where("id = :id", { id: updateInspectionPlan.id }) + .execute() + .then(() => {}) + .catch((err) => { + throw new DatabaseActionException("UPDATE", "inspectionPlan", err); + }); + } + + /** + * @description delete inspectionPlan + * @param {DeleteInspectionPlanCommand} deleteInspectionPlan + * @returns {Promise} + */ + static async delete(deleteInspectionPlan: DeleteInspectionPlanCommand): Promise { + return await dataSource + .createQueryBuilder() + .delete() + .from(inspectionPlan) + .where("id = :id", { id: deleteInspectionPlan.id }) + .execute() + .then(() => {}) + .catch((err) => { + throw new DatabaseActionException("DELETE", "inspectionPlan", err); + }); + } +} diff --git a/src/command/unit/inspection/inspectionPointCommand.ts b/src/command/unit/inspection/inspectionPointCommand.ts new file mode 100644 index 0000000..5aa45a6 --- /dev/null +++ b/src/command/unit/inspection/inspectionPointCommand.ts @@ -0,0 +1,11 @@ +import { InspectionPointEnum } from "../../../enums/inspectionEnum"; + +export interface CreateInspectionPointCommand { + title: string; + description: string; + type: InspectionPointEnum; + min?: number; + max?: number; + sort: number; + versionedPointId: string; +} diff --git a/src/command/unit/inspection/inspectionPointCommandHandler.ts b/src/command/unit/inspection/inspectionPointCommandHandler.ts new file mode 100644 index 0000000..749f325 --- /dev/null +++ b/src/command/unit/inspection/inspectionPointCommandHandler.ts @@ -0,0 +1,34 @@ +import { dataSource } from "../../../data-source"; +import { inspectionPoint } from "../../../entity/unit/inspection/inspectionPoint"; +import DatabaseActionException from "../../../exceptions/databaseActionException"; +import { CreateInspectionPointCommand } from "./inspectionPointCommand"; + +export default abstract class InspectionPointCommandHandler { + /** + * @description create inspectionPoint + * @param {CreateInspectionPointCommand} createInspectionPoint + * @returns {Promise} + */ + static async create(createInspectionPoint: CreateInspectionPointCommand): Promise { + return await dataSource + .createQueryBuilder() + .insert() + .into(inspectionPoint) + .values({ + title: createInspectionPoint.title, + description: createInspectionPoint.description, + type: createInspectionPoint.type, + min: createInspectionPoint.min, + max: createInspectionPoint.max, + sort: createInspectionPoint.sort, + versionedPlanId: createInspectionPoint.versionedPointId, + }) + .execute() + .then((result) => { + return result.identifiers[0].id; + }) + .catch((err) => { + throw new DatabaseActionException("CREATE", "inspectionPoint", err); + }); + } +} diff --git a/src/command/unit/inspection/inspectionPointResultCommand.ts b/src/command/unit/inspection/inspectionPointResultCommand.ts new file mode 100644 index 0000000..9fb36d6 --- /dev/null +++ b/src/command/unit/inspection/inspectionPointResultCommand.ts @@ -0,0 +1,5 @@ +export interface CreateInspectionPointResultCommand { + inspectionId: string; + inspectionPointId: string; + value: string; +} diff --git a/src/command/unit/inspection/inspectionPointResultCommandHandler.ts b/src/command/unit/inspection/inspectionPointResultCommandHandler.ts new file mode 100644 index 0000000..23916dc --- /dev/null +++ b/src/command/unit/inspection/inspectionPointResultCommandHandler.ts @@ -0,0 +1,31 @@ +import { dataSource } from "../../../data-source"; +import { inspectionPointResult } from "../../../entity/unit/inspection/inspectionPointResult"; +import DatabaseActionException from "../../../exceptions/databaseActionException"; +import { CreateInspectionPointResultCommand } from "./inspectionPointResultCommand"; + +export default abstract class InspectionPointResultCommandHandler { + /** + * @description create inspectionPointResult + * @param {CreateInspectionPointResultCommand} createInspectionPointResult + * @returns {Promise} + */ + static async createOrUpdate(createInspectionPointResult: CreateInspectionPointResultCommand): Promise { + return await dataSource + .createQueryBuilder() + .insert() + .into(inspectionPointResult) + .values({ + inspectionId: createInspectionPointResult.inspectionId, + inspectionPointId: createInspectionPointResult.inspectionPointId, + value: createInspectionPointResult.value, + }) + .orUpdate(["value"], ["inspectionId", "inspectionPointId"]) + .execute() + .then((result) => { + return result.identifiers[0].id; + }) + .catch((err) => { + throw new DatabaseActionException("CREATE", "inspectionPointResult", err); + }); + } +} diff --git a/src/command/unit/inspection/inspectionVersionedPlanCommand.ts b/src/command/unit/inspection/inspectionVersionedPlanCommand.ts new file mode 100644 index 0000000..825de12 --- /dev/null +++ b/src/command/unit/inspection/inspectionVersionedPlanCommand.ts @@ -0,0 +1,3 @@ +export interface CreateInspectionVersionedPlanCommand { + inspectionPlanId: string; +} diff --git a/src/command/unit/inspection/inspectionVersionedPlanCommandHandler.ts b/src/command/unit/inspection/inspectionVersionedPlanCommandHandler.ts new file mode 100644 index 0000000..9c9710b --- /dev/null +++ b/src/command/unit/inspection/inspectionVersionedPlanCommandHandler.ts @@ -0,0 +1,28 @@ +import { dataSource } from "../../../data-source"; +import { inspectionVersionedPlan } from "../../../entity/unit/inspection/inspectionVersionedPlan"; +import DatabaseActionException from "../../../exceptions/databaseActionException"; +import { CreateInspectionVersionedPlanCommand } from "./inspectionVersionedPlanCommand"; + +export default abstract class InspectionVersionedPlanCommandHandler { + /** + * @description create inspectionVersionedPlan + * @param {CreateInspectionVersionedPlanCommand} createInspectionVersionedPlan + * @returns {Promise} + */ + static async create(createInspectionVersionedPlan: CreateInspectionVersionedPlanCommand): Promise { + return await dataSource + .createQueryBuilder() + .insert() + .into(inspectionVersionedPlan) + .values({ + inspectionPlanId: createInspectionVersionedPlan.inspectionPlanId, + }) + .execute() + .then((result) => { + return result.identifiers[0].id; + }) + .catch((err) => { + throw new DatabaseActionException("CREATE", "inspectionVersionedPlan", err); + }); + } +} diff --git a/src/command/unit/maintenanceCommand.ts b/src/command/unit/maintenanceCommand.ts new file mode 100644 index 0000000..637ed3b --- /dev/null +++ b/src/command/unit/maintenanceCommand.ts @@ -0,0 +1,16 @@ +export interface CreateMaintenanceCommand { + description: string; + affectedId: string; + affected: "equipment" | "vehicle" | "wearable"; +} + +export interface UpdateMaintenanceCommand { + id: string; + status: string; + done: boolean; + description: string; +} + +export interface DeleteMaintenanceCommand { + id: string; +} diff --git a/src/command/unit/maintenanceCommandHandler.ts b/src/command/unit/maintenanceCommandHandler.ts new file mode 100644 index 0000000..96819dd --- /dev/null +++ b/src/command/unit/maintenanceCommandHandler.ts @@ -0,0 +1,72 @@ +import { dataSource } from "../../data-source"; +import { maintenance } from "../../entity/unit/maintenance"; +import DatabaseActionException from "../../exceptions/databaseActionException"; +import { CreateMaintenanceCommand, UpdateMaintenanceCommand, DeleteMaintenanceCommand } from "./maintenanceCommand"; + +export default abstract class MaintenanceCommandHandler { + /** + * @description create maintenance + * @param {CreateMaintenanceCommand} createMaintenance + * @returns {Promise} + */ + static async create(createMaintenance: CreateMaintenanceCommand): Promise { + return await dataSource + .createQueryBuilder() + .insert() + .into(maintenance) + .values({ + status: "gestartet", + description: createMaintenance.description, + equipmentId: createMaintenance.affected == "equipment" ? createMaintenance.affectedId : null, + vehicleId: createMaintenance.affected == "vehicle" ? createMaintenance.affectedId : null, + wearableId: createMaintenance.affected == "wearable" ? createMaintenance.affectedId : null, + }) + .execute() + .then((result) => { + return result.identifiers[0].id; + }) + .catch((err) => { + throw new DatabaseActionException("CREATE", "maintenance", err); + }); + } + + /** + * @description update maintenance + * @param {UpdateMaintenanceCommand} updateMaintenance + * @returns {Promise} + */ + static async update(updateMaintenance: UpdateMaintenanceCommand): Promise { + return await dataSource + .createQueryBuilder() + .update(maintenance) + .set({ + status: updateMaintenance.status, + done: updateMaintenance.done, + description: updateMaintenance.description, + }) + .where("id = :id", { id: updateMaintenance.id }) + .execute() + .then(() => {}) + .catch((err) => { + throw new DatabaseActionException("UPDATE", "maintenance", err); + }); + } + + /** + * @description delete maintenance + * @param {DeleteMaintenanceCommand} deleteMaintenance + * @returns {Promise} + */ + static async delete(deleteMaintenance: DeleteMaintenanceCommand): Promise { + return await dataSource + .createQueryBuilder() + .delete() + .from(maintenance) + .where("id = :id", { id: deleteMaintenance.id }) + .execute() + .then(() => {}) + .catch((err) => { + throw new DatabaseActionException("DELETE", "maintenance", err); + }); + } +} diff --git a/src/command/unit/vehicle/vehicleCommand.ts b/src/command/unit/vehicle/vehicleCommand.ts new file mode 100644 index 0000000..1310c0b --- /dev/null +++ b/src/command/unit/vehicle/vehicleCommand.ts @@ -0,0 +1,20 @@ +export interface CreateVehicleCommand { + code?: string; + name: string; + location: string; + commissioned: Date; + vehicleTypeId: string; +} + +export interface UpdateVehicleCommand { + id: string; + code?: string; + name: string; + location: string; + commissioned: Date; + decommissioned?: Date; +} + +export interface DeleteVehicleCommand { + id: string; +} diff --git a/src/command/unit/vehicle/vehicleCommandHandler.ts b/src/command/unit/vehicle/vehicleCommandHandler.ts new file mode 100644 index 0000000..db493f6 --- /dev/null +++ b/src/command/unit/vehicle/vehicleCommandHandler.ts @@ -0,0 +1,73 @@ +import { dataSource } from "../../../data-source"; +import { vehicle } from "../../../entity/unit/vehicle/vehicle"; +import DatabaseActionException from "../../../exceptions/databaseActionException"; +import { CreateVehicleCommand, UpdateVehicleCommand, DeleteVehicleCommand } from "./vehicleCommand"; + +export default abstract class VehicleCommandHandler { + /** + * @description create vehicle + * @param {CreateVehicleCommand} createVehicle + * @returns {Promise} + */ + static async create(createVehicle: CreateVehicleCommand): Promise { + return await dataSource + .createQueryBuilder() + .insert() + .into(vehicle) + .values({ + code: createVehicle.code, + name: createVehicle.name, + location: createVehicle.location, + commissioned: createVehicle.commissioned, + vehicleTypeId: createVehicle.vehicleTypeId, + }) + .execute() + .then((result) => { + return result.identifiers[0].id; + }) + .catch((err) => { + throw new DatabaseActionException("CREATE", "vehicle", err); + }); + } + + /** + * @description update vehicle + * @param {UpdateVehicleCommand} updateVehicle + * @returns {Promise} + */ + static async update(updateVehicle: UpdateVehicleCommand): Promise { + return await dataSource + .createQueryBuilder() + .update(vehicle) + .set({ + code: updateVehicle.code, + name: updateVehicle.name, + location: updateVehicle.location, + commissioned: updateVehicle.commissioned, + }) + .where("id = :id", { id: updateVehicle.id }) + .execute() + .then(() => {}) + .catch((err) => { + throw new DatabaseActionException("UPDATE", "vehicle", err); + }); + } + + /** + * @description delete vehicle + * @param {DeleteVehicleCommand} deleteVehicle + * @returns {Promise} + */ + static async delete(deleteVehicle: DeleteVehicleCommand): Promise { + return await dataSource + .createQueryBuilder() + .delete() + .from(vehicle) + .where("id = :id", { id: deleteVehicle.id }) + .execute() + .then(() => {}) + .catch((err) => { + throw new DatabaseActionException("DELETE", "vehicle", err); + }); + } +} diff --git a/src/command/unit/vehicle/vehicleTypeCommand.ts b/src/command/unit/vehicle/vehicleTypeCommand.ts new file mode 100644 index 0000000..d155fe8 --- /dev/null +++ b/src/command/unit/vehicle/vehicleTypeCommand.ts @@ -0,0 +1,14 @@ +export interface CreateVehicleTypeCommand { + type: string; + description: string; +} + +export interface UpdateVehicleTypeCommand { + id: string; + type: string; + description: string; +} + +export interface DeleteVehicleTypeCommand { + id: string; +} diff --git a/src/command/unit/vehicle/vehicleTypeCommandHandler.ts b/src/command/unit/vehicle/vehicleTypeCommandHandler.ts new file mode 100644 index 0000000..de25fd1 --- /dev/null +++ b/src/command/unit/vehicle/vehicleTypeCommandHandler.ts @@ -0,0 +1,68 @@ +import { dataSource } from "../../../data-source"; +import { vehicleType } from "../../../entity/unit/vehicle/vehicleType"; +import DatabaseActionException from "../../../exceptions/databaseActionException"; +import { CreateVehicleTypeCommand, UpdateVehicleTypeCommand, DeleteVehicleTypeCommand } from "./vehicleTypeCommand"; + +export default abstract class VehicleTypeCommandHandler { + /** + * @description create vehicleType + * @param {CreateVehicleTypeCommand} createVehicleType + * @returns {Promise} + */ + static async create(createVehicleType: CreateVehicleTypeCommand): Promise { + return await dataSource + .createQueryBuilder() + .insert() + .into(vehicleType) + .values({ + type: createVehicleType.type, + description: createVehicleType.description, + }) + .execute() + .then((result) => { + return result.identifiers[0].id; + }) + .catch((err) => { + throw new DatabaseActionException("CREATE", "vehicleType", err); + }); + } + + /** + * @description update vehicleType + * @param {UpdateVehicleTypeCommand} updateVehicleType + * @returns {Promise} + */ + static async update(updateVehicleType: UpdateVehicleTypeCommand): Promise { + return await dataSource + .createQueryBuilder() + .update(vehicleType) + .set({ + type: updateVehicleType.type, + description: updateVehicleType.description, + }) + .where("id = :id", { id: updateVehicleType.id }) + .execute() + .then(() => {}) + .catch((err) => { + throw new DatabaseActionException("UPDATE", "vehicleType", err); + }); + } + + /** + * @description delete vehicleType + * @param {DeleteVehicleTypeCommand} deleteVehicleType + * @returns {Promise} + */ + static async delete(deleteVehicleType: DeleteVehicleTypeCommand): Promise { + return await dataSource + .createQueryBuilder() + .delete() + .from(vehicleType) + .where("id = :id", { id: deleteVehicleType.id }) + .execute() + .then(() => {}) + .catch((err) => { + throw new DatabaseActionException("DELETE", "vehicleType", err); + }); + } +} diff --git a/src/command/unit/wearable/wearableCommand.ts b/src/command/unit/wearable/wearableCommand.ts new file mode 100644 index 0000000..c58eeb7 --- /dev/null +++ b/src/command/unit/wearable/wearableCommand.ts @@ -0,0 +1,22 @@ +export interface CreateWearableCommand { + code?: string; + name: string; + location: string; + commissioned: Date; + wearableTypeId: string; + wearerId?: string; +} + +export interface UpdateWearableCommand { + id: string; + code?: string; + name: string; + location: string; + commissioned: Date; + decommissioned?: Date; + wearerId?: string; +} + +export interface DeleteWearableCommand { + id: string; +} diff --git a/src/command/unit/wearable/wearableCommandHandler.ts b/src/command/unit/wearable/wearableCommandHandler.ts new file mode 100644 index 0000000..a13b773 --- /dev/null +++ b/src/command/unit/wearable/wearableCommandHandler.ts @@ -0,0 +1,76 @@ +import { dataSource } from "../../../data-source"; +import { wearable } from "../../../entity/unit/wearable/wearable"; +import DatabaseActionException from "../../../exceptions/databaseActionException"; +import { CreateWearableCommand, UpdateWearableCommand, DeleteWearableCommand } from "./wearableCommand"; + +export default abstract class WearableCommandHandler { + /** + * @description create wearable + * @param {CreateWearableCommand} createWearable + * @returns {Promise} + */ + static async create(createWearable: CreateWearableCommand): Promise { + return await dataSource + .createQueryBuilder() + .insert() + .into(wearable) + .values({ + code: createWearable.code, + name: createWearable.name, + location: createWearable.location, + commissioned: createWearable.commissioned, + wearableTypeId: createWearable.wearableTypeId, + wearerId: createWearable.wearerId, + }) + .execute() + .then((result) => { + return result.identifiers[0].id; + }) + .catch((err) => { + throw new DatabaseActionException("CREATE", "wearable", err); + }); + } + + /** + * @description update wearable + * @param {UpdateWearableCommand} updateWearable + * @returns {Promise} + */ + static async update(updateWearable: UpdateWearableCommand): Promise { + return await dataSource + .createQueryBuilder() + .update(wearable) + .set({ + code: updateWearable.code, + name: updateWearable.name, + location: updateWearable.location, + commissioned: updateWearable.commissioned, + decommissioned: updateWearable.decommissioned, + wearerId: updateWearable.wearerId, + }) + .where("id = :id", { id: updateWearable.id }) + .execute() + .then(() => {}) + .catch((err) => { + throw new DatabaseActionException("UPDATE", "wearable", err); + }); + } + + /** + * @description delete wearable + * @param {DeleteWearableCommand} deleteWearable + * @returns {Promise} + */ + static async delete(deleteWearable: DeleteWearableCommand): Promise { + return await dataSource + .createQueryBuilder() + .delete() + .from(wearable) + .where("id = :id", { id: deleteWearable.id }) + .execute() + .then(() => {}) + .catch((err) => { + throw new DatabaseActionException("DELETE", "wearable", err); + }); + } +} diff --git a/src/command/unit/wearable/wearableTypeCommand.ts b/src/command/unit/wearable/wearableTypeCommand.ts new file mode 100644 index 0000000..45cbc41 --- /dev/null +++ b/src/command/unit/wearable/wearableTypeCommand.ts @@ -0,0 +1,14 @@ +export interface CreateWearableTypeCommand { + type: string; + description: string; +} + +export interface UpdateWearableTypeCommand { + id: string; + type: string; + description: string; +} + +export interface DeleteWearableTypeCommand { + id: string; +} diff --git a/src/command/unit/wearable/wearableTypeCommandHandler.ts b/src/command/unit/wearable/wearableTypeCommandHandler.ts new file mode 100644 index 0000000..b8f0f6f --- /dev/null +++ b/src/command/unit/wearable/wearableTypeCommandHandler.ts @@ -0,0 +1,68 @@ +import { dataSource } from "../../../data-source"; +import { wearableType } from "../../../entity/unit/wearable/wearableType"; +import DatabaseActionException from "../../../exceptions/databaseActionException"; +import { CreateWearableTypeCommand, UpdateWearableTypeCommand, DeleteWearableTypeCommand } from "./wearableTypeCommand"; + +export default abstract class WearableTypeCommandHandler { + /** + * @description create wearableType + * @param {CreateWearableTypeCommand} createWearableType + * @returns {Promise} + */ + static async create(createWearableType: CreateWearableTypeCommand): Promise { + return await dataSource + .createQueryBuilder() + .insert() + .into(wearableType) + .values({ + type: createWearableType.type, + description: createWearableType.description, + }) + .execute() + .then((result) => { + return result.identifiers[0].id; + }) + .catch((err) => { + throw new DatabaseActionException("CREATE", "wearableType", err); + }); + } + + /** + * @description update wearableType + * @param {UpdateWearableTypeCommand} updateWearableType + * @returns {Promise} + */ + static async update(updateWearableType: UpdateWearableTypeCommand): Promise { + return await dataSource + .createQueryBuilder() + .update(wearableType) + .set({ + type: updateWearableType.type, + description: updateWearableType.description, + }) + .where("id = :id", { id: updateWearableType.id }) + .execute() + .then(() => {}) + .catch((err) => { + throw new DatabaseActionException("UPDATE", "wearableType", err); + }); + } + + /** + * @description delete wearableType + * @param {DeleteWearableTypeCommand} deleteWearableType + * @returns {Promise} + */ + static async delete(deleteWearableType: DeleteWearableTypeCommand): Promise { + return await dataSource + .createQueryBuilder() + .delete() + .from(wearableType) + .where("id = :id", { id: deleteWearableType.id }) + .execute() + .then(() => {}) + .catch((err) => { + throw new DatabaseActionException("DELETE", "wearableType", err); + }); + } +} diff --git a/src/entity/unit/damageReport.ts b/src/entity/unit/damageReport.ts index 20a78c2..f080e38 100644 --- a/src/entity/unit/damageReport.ts +++ b/src/entity/unit/damageReport.ts @@ -31,7 +31,7 @@ export class damageReport { equipmentId?: string; @Column({ nullable: true, default: null }) - maintenanceId: string; + maintenanceId?: string; @Column({ nullable: true, default: null }) vehicleId?: string; diff --git a/src/entity/unit/inspection/inspectionPointResult.ts b/src/entity/unit/inspection/inspectionPointResult.ts index 5b3501d..4f98e74 100644 --- a/src/entity/unit/inspection/inspectionPointResult.ts +++ b/src/entity/unit/inspection/inspectionPointResult.ts @@ -1,21 +1,18 @@ -import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm"; +import { Column, Entity, ManyToOne, PrimaryColumn } from "typeorm"; import { inspection } from "./inspection"; import { inspectionPoint } from "./inspectionPoint"; @Entity() export class inspectionPointResult { - @PrimaryGeneratedColumn("uuid") - id: string; + @PrimaryColumn() + inspectionId: string; + + @PrimaryColumn() + inspectionPointId: string; @Column({ type: "text" }) value: string; - @Column() - inspectionId: string; - - @Column() - inspectionPointId: string; - @ManyToOne(() => inspection, { nullable: false, onDelete: "CASCADE", diff --git a/src/migrations/baseSchemaTables/inspection.ts b/src/migrations/baseSchemaTables/inspection.ts index 83e856f..b064cb6 100644 --- a/src/migrations/baseSchemaTables/inspection.ts +++ b/src/migrations/baseSchemaTables/inspection.ts @@ -126,10 +126,9 @@ export const inspection_table = new Table({ export const inspection_point_result_table = new Table({ name: "inspection_point_result", columns: [ - { name: "id", ...getTypeByORM("uuid"), ...isUUIDPrimary }, + { name: "inspectionId", ...getTypeByORM("uuid"), isPrimary: true }, + { name: "inspectionPointId", ...getTypeByORM("uuid"), isPrimary: true }, { name: "value", ...getTypeByORM("text") }, - { name: "inspectionId", ...getTypeByORM("uuid") }, - { name: "inspectionPointId", ...getTypeByORM("uuid") }, ], foreignKeys: [ new TableForeignKey({ diff --git a/src/service/unit/damageReport.ts b/src/service/unit/damageReportService.ts similarity index 100% rename from src/service/unit/damageReport.ts rename to src/service/unit/damageReportService.ts diff --git a/src/service/unit/inspection/inspectionPointResultService.ts b/src/service/unit/inspection/inspectionPointResultService.ts index d538a63..d294139 100644 --- a/src/service/unit/inspection/inspectionPointResultService.ts +++ b/src/service/unit/inspection/inspectionPointResultService.ts @@ -21,23 +21,4 @@ export default abstract class InspectionPointResultService { throw new DatabaseActionException("SELECT", "inspectionPointResult", err); }); } - - /** - * @description get inspectionPointResult by id - * @returns {Promise} - */ - static async getById(id: string): Promise { - return await dataSource - .getRepository(inspectionPointResult) - .createQueryBuilder("inspectionPointResult") - .leftJoinAndSelect("inspectionPointResult.inspectionPoint", "inspectionPoint") - .where({ id }) - .getOneOrFail() - .then((res) => { - return res; - }) - .catch((err) => { - throw new DatabaseActionException("SELECT", "inspectionPointResult", err); - }); - } } diff --git a/src/service/unit/inspection/inspectionVersionedPlanService.ts b/src/service/unit/inspection/inspectionVersionedPlanService.ts index 9313979..55eab31 100644 --- a/src/service/unit/inspection/inspectionVersionedPlanService.ts +++ b/src/service/unit/inspection/inspectionVersionedPlanService.ts @@ -23,6 +23,27 @@ export default abstract class InspectionVersionedPlanService { }); } + /** + * @description get latest inspectionVersionedPlan for plan + * @returns {Promise>} + */ + static async getLatestForInspectionPlan(inspectionPlanId: string): Promise { + return await dataSource + .getRepository(inspectionVersionedPlan) + .createQueryBuilder("inspectionVersionedPlan") + .leftJoinAndSelect("inspectionVersionedPlan.inspectionPoints", "inspectionPoints") + .where({ inspectionPlanId }) + .orderBy("inspectionVersionedPlan.version", "DESC") + .limit(1) + .getOneOrFail() + .then((res) => { + return res; + }) + .catch((err) => { + throw new DatabaseActionException("SELECT", "inspectionVersionedPlan", err); + }); + } + /** * @description get inspectionVersionedPlan by id * @returns {Promise} diff --git a/src/service/unit/maintenance.ts b/src/service/unit/maintenanceService.ts similarity index 100% rename from src/service/unit/maintenance.ts rename to src/service/unit/maintenanceService.ts diff --git a/src/viewmodel/admin/unit/damageReport.models.ts b/src/viewmodel/admin/unit/damageReport.models.ts index e675c9c..9427d8a 100644 --- a/src/viewmodel/admin/unit/damageReport.models.ts +++ b/src/viewmodel/admin/unit/damageReport.models.ts @@ -30,16 +30,3 @@ export type DamageReportViewModel = { reportedBy: string; maintenance?: MaintenanceViewModel; } & DamageReportAssigned; - -export interface CreateDamageReportViewModel { - description: string; - reportedBy: string; - affectedId: string; - affected: "equipment" | "vehicle" | "wearable"; -} - -export interface UpdateDamageReportViewModel { - id: string; - status: string; - done: boolean; -} diff --git a/src/viewmodel/admin/unit/equipment/equipment.models.ts b/src/viewmodel/admin/unit/equipment/equipment.models.ts index abbdee7..2990c1d 100644 --- a/src/viewmodel/admin/unit/equipment/equipment.models.ts +++ b/src/viewmodel/admin/unit/equipment/equipment.models.ts @@ -10,20 +10,3 @@ export interface EquipmentViewModel { equipmentTypeId: string; equipmentType: EquipmentTypeViewModel; } - -export interface CreateEquipmentViewModel { - code?: string; - name: string; - location: string; - commissioned: Date; - equipmentTypeId: string; -} - -export interface UpdateEquipmentViewModel { - id: string; - code?: string; - name: string; - location: string; - commissioned: Date; - decommissioned?: Date; -} diff --git a/src/viewmodel/admin/unit/equipment/equipmentType.models.ts b/src/viewmodel/admin/unit/equipment/equipmentType.models.ts index fcf47ee..05ad905 100644 --- a/src/viewmodel/admin/unit/equipment/equipmentType.models.ts +++ b/src/viewmodel/admin/unit/equipment/equipmentType.models.ts @@ -3,14 +3,3 @@ export interface EquipmentTypeViewModel { type: string; description: string; } - -export interface CreateEquipmentTypeViewModel { - type: string; - description: string; -} - -export interface UpdateEquipmentTypeViewModel { - id: string; - type: string; - description: string; -} diff --git a/src/viewmodel/admin/unit/inspection/inspectionPlan.models.ts b/src/viewmodel/admin/unit/inspection/inspectionPlan.models.ts index b2aecc0..cbbea41 100644 --- a/src/viewmodel/admin/unit/inspection/inspectionPlan.models.ts +++ b/src/viewmodel/admin/unit/inspection/inspectionPlan.models.ts @@ -40,18 +40,3 @@ export interface InspectionPointViewModel { max?: number; sort: number; } - -export interface CreateInspectionPlanViewModel { - title: string; - inspectionInterval: PlanTimeDefinition; - remindTime: PlanTimeDefinition; - relatedId: string; - assigned: "vehicle" | "equipment"; -} - -export interface UpdateInspectionPlanViewModel { - id: string; - title: string; - inspectionInterval: PlanTimeDefinition; - remindTime?: PlanTimeDefinition; -} diff --git a/src/viewmodel/admin/unit/maintenance.models.ts b/src/viewmodel/admin/unit/maintenance.models.ts index ce5c0b9..98f22f2 100644 --- a/src/viewmodel/admin/unit/maintenance.models.ts +++ b/src/viewmodel/admin/unit/maintenance.models.ts @@ -28,16 +28,3 @@ export type MaintenanceViewModel = { description: string; reports: DamageReportViewModel[]; } & MaintenanceAssigned; - -export interface CreateMaintenanceViewModel { - description: string; - reportedBy: string; - affectedId: string; - affected: "equipment" | "vehicle" | "wearable"; -} - -export interface UpdateMaintenanceViewModel { - id: string; - status: string; - done: boolean; -} diff --git a/src/viewmodel/admin/unit/vehicle/vehicle.models.ts b/src/viewmodel/admin/unit/vehicle/vehicle.models.ts index 4e273f6..b25ab0c 100644 --- a/src/viewmodel/admin/unit/vehicle/vehicle.models.ts +++ b/src/viewmodel/admin/unit/vehicle/vehicle.models.ts @@ -10,20 +10,3 @@ export interface VehicleViewModel { vehicleTypeId: string; vehicleType: VehicleTypeViewModel; } - -export interface CreateVehicleViewModel { - code?: string; - name: string; - location: string; - commissioned: Date; - vehicleTypeId: string; -} - -export interface UpdateVehicleViewModel { - id: string; - code?: string; - name: string; - location: string; - commissioned: Date; - decommissioned?: Date; -} diff --git a/src/viewmodel/admin/unit/vehicle/vehicleType.models.ts b/src/viewmodel/admin/unit/vehicle/vehicleType.models.ts index ea6c7cf..726311e 100644 --- a/src/viewmodel/admin/unit/vehicle/vehicleType.models.ts +++ b/src/viewmodel/admin/unit/vehicle/vehicleType.models.ts @@ -3,14 +3,3 @@ export interface VehicleTypeViewModel { type: string; description: string; } - -export interface CreateVehicleTypeViewModel { - type: string; - description: string; -} - -export interface UpdateVehicleTypeViewModel { - id: string; - type: string; - description: string; -} diff --git a/src/viewmodel/admin/unit/wearable/wearable.models.ts b/src/viewmodel/admin/unit/wearable/wearable.models.ts index e2c5bbc..e3bacac 100644 --- a/src/viewmodel/admin/unit/wearable/wearable.models.ts +++ b/src/viewmodel/admin/unit/wearable/wearable.models.ts @@ -13,22 +13,3 @@ export interface WearableViewModel { wearableTypeId: string; wearableType: WearableTypeViewModel; } - -export interface CreateWearableViewModel { - code?: string; - name: string; - wearerId?: string; - location?: string; - commissioned: Date; - wearableTypeId: string; -} - -export interface UpdateWearableViewModel { - id: string; - code?: string; - name: string; - location?: string; - commissioned: Date; - decommissioned?: Date; - wearerId?: string; -} diff --git a/src/viewmodel/admin/unit/wearable/wearableType.models.ts b/src/viewmodel/admin/unit/wearable/wearableType.models.ts index b3f49fc..73fd7c5 100644 --- a/src/viewmodel/admin/unit/wearable/wearableType.models.ts +++ b/src/viewmodel/admin/unit/wearable/wearableType.models.ts @@ -3,14 +3,3 @@ export interface WearableTypeViewModel { type: string; description: string; } - -export interface CreateWearableTypeViewModel { - type: string; - description: string; -} - -export interface UpdateWearableTypeViewModel { - id: string; - type: string; - description: string; -}