diff --git a/src/command/unit/inspection/inspectionCommand.ts b/src/command/unit/inspection/inspectionCommand.ts index 1c020fb..e3a7b9d 100644 --- a/src/command/unit/inspection/inspectionCommand.ts +++ b/src/command/unit/inspection/inspectionCommand.ts @@ -3,7 +3,7 @@ export interface CreateInspectionCommand { nextInspection?: Date; inspectionPlanId: string; relatedId: string; - assigned: "vehicle" | "equipment"; + assigned: "vehicle" | "equipment" | "wearable"; } export interface UpdateInspectionCommand { diff --git a/src/command/unit/inspection/inspectionCommandHandler.ts b/src/command/unit/inspection/inspectionCommandHandler.ts index 34b7bf5..f287625 100644 --- a/src/command/unit/inspection/inspectionCommandHandler.ts +++ b/src/command/unit/inspection/inspectionCommandHandler.ts @@ -1,4 +1,4 @@ -import { Not } from "typeorm"; +import { IsNull, Not } from "typeorm"; import { dataSource } from "../../../data-source"; import { inspection } from "../../../entity/unit/inspection/inspection"; import DatabaseActionException from "../../../exceptions/databaseActionException"; @@ -25,6 +25,12 @@ export default abstract class InspectionCommandHandler { .set({ hasNewer: true, }) + .where({ + inspectionPlanId: createInspection.inspectionPlanId, + equipmentId: createInspection.assigned == "equipment" ? createInspection.relatedId : IsNull(), + vehicleId: createInspection.assigned == "vehicle" ? createInspection.relatedId : IsNull(), + wearableId: createInspection.assigned == "wearable" ? createInspection.relatedId : IsNull(), + }) .execute(); await manager @@ -38,6 +44,7 @@ export default abstract class InspectionCommandHandler { inspectionVersionedPlanId: latestVersionedPlan.id, equipmentId: createInspection.assigned == "equipment" ? createInspection.relatedId : null, vehicleId: createInspection.assigned == "vehicle" ? createInspection.relatedId : null, + wearableId: createInspection.assigned == "wearable" ? createInspection.relatedId : null, }) .execute() .then((result) => { diff --git a/src/controller/admin/unit/inspectionController.ts b/src/controller/admin/unit/inspectionController.ts index f98f4c5..fc126cd 100644 --- a/src/controller/admin/unit/inspectionController.ts +++ b/src/controller/admin/unit/inspectionController.ts @@ -24,7 +24,7 @@ export async function getAllInspectionsSortedNotHavingNewer(req: Request, res: R let [inspections, total] = await InspectionService.getAllSortedNotHavingNewer({ offset, count, noLimit }); res.json({ - inspections: InspectionFactory.mapToBase(inspections), + inspections: InspectionFactory.mapToBaseNext(inspections), total: total, offset: offset, count: count, @@ -45,7 +45,7 @@ export async function getAllInspectionsRunning(req: Request, res: Response): Pro let [inspections, total] = await InspectionService.getAllRunning({ offset, count, noLimit }); res.json({ - inspections: InspectionFactory.mapToBase(inspections), + inspections: InspectionFactory.mapToBaseMinified(inspections), total: total, offset: offset, count: count, @@ -112,6 +112,13 @@ export async function createInspection(req: Request, res: Response): Promise): Array { return records.map((r) => this.mapToSingle(r)); } + + /** + * @description map record to minified inspection + * @param {inspection} record + * @returns {MinifiedInspectionViewModel} + */ + public static mapToSingleMinified(record: inspection): MinifiedInspectionViewModel { + let related = this.mapRelated(record); + + return { + id: record.id, + inspectionPlanId: record.inspectionPlanId, + inspectionPlan: InspectionPlanFactory.mapToSingle(record.inspectionPlan), + context: record.context, + created: record.createdAt, + finished: record?.finishedAt, + isOpen: record?.finishedAt == undefined, + nextInspection: record?.nextInspection, + ...related, + }; + } + + /** + * @description map records to minified inspection + * @param {Array} records + * @returns {Array} + */ + public static mapToBaseMinified(records: Array): Array { + return records.map((r) => this.mapToSingleMinified(r)); + } + + /** + * @description map record to next inspection + * @param {inspection} record + * @returns {InspectionNextViewModel} + */ + public static mapToSingleNext(record: inspection): InspectionNextViewModel { + let related = this.mapRelated(record); + + return { + id: record.id, + inspectionPlanId: record.inspectionPlanId, + inspectionPlan: InspectionPlanFactory.mapToSingle(record.inspectionPlan), + dueDate: record?.nextInspection, + ...related, + }; + } + + /** + * @description map records to next inspection + * @param {Array} records + * @returns {Array} + */ + public static mapToBaseNext(records: Array): Array { + return records.map((r) => this.mapToSingleNext(r)); + } } diff --git a/src/service/unit/inspection/inspectionService.ts b/src/service/unit/inspection/inspectionService.ts index fbbe081..6e4e79b 100644 --- a/src/service/unit/inspection/inspectionService.ts +++ b/src/service/unit/inspection/inspectionService.ts @@ -1,4 +1,4 @@ -import { IsNull } from "typeorm"; +import { IsNull, Not } from "typeorm"; import { dataSource } from "../../../data-source"; import { inspection } from "../../../entity/unit/inspection/inspection"; import DatabaseActionException from "../../../exceptions/databaseActionException"; @@ -129,6 +129,36 @@ export default abstract class InspectionService { }); } + /** + * @description uses versionedPlan + * @returns {Promise} + */ + static async existsUnfinishedInspectionToPlan( + inspectionPlanId: string, + relation: "vehicle" | "equipment" | "wearable", + relationId: string + ): Promise { + let where: { equipmentId: string } | { vehicleId: string } | { wearableId: string }; + if (relation == "equipment") { + where = { equipmentId: relationId }; + } else if (relation == "vehicle") { + where = { vehicleId: relationId }; + } else { + where = { wearableId: relationId }; + } + return await dataSource + .getRepository(inspection) + .createQueryBuilder("inspection") + .where({ inspectionPlanId, finishedAt: IsNull(), ...where }) + .getExists() + .then((res) => { + return res; + }) + .catch((err) => { + throw new DatabaseActionException("SELECT", "used inspection", err); + }); + } + /** * @description uses versionedPlan * @returns {Promise} diff --git a/src/viewmodel/admin/unit/inspection/inspection.models.ts b/src/viewmodel/admin/unit/inspection/inspection.models.ts index 7efdede..87af590 100644 --- a/src/viewmodel/admin/unit/inspection/inspection.models.ts +++ b/src/viewmodel/admin/unit/inspection/inspection.models.ts @@ -38,6 +38,26 @@ export type InspectionViewModel = { checks: Array; } & InspectionRelated; +export type MinifiedInspectionViewModel = { + id: string; + inspectionPlanId: string; + inspectionPlan: InspectionPlanViewModel; + context: string; + created: Date; + finished?: Date; + isOpen: boolean; + nextInspection?: Date; + relatedId: string; +} & InspectionRelated; + +export type InspectionNextViewModel = { + id: string; + inspectionPlanId: string; + inspectionPlan: InspectionPlanViewModel; + dueDate: Date; + relatedId: string; +} & InspectionRelated; + export interface InspectionPointResultViewModel { inspectionId: string; inspectionPointId: string;