edit repair

This commit is contained in:
Julian Krauser 2025-07-22 13:10:13 +02:00
parent 85fa912024
commit f812298cb2
6 changed files with 166 additions and 13 deletions

View file

@ -1,4 +1,4 @@
import { EntityManager, UpdateResult } from "typeorm";
import { EntityManager, In, UpdateResult } from "typeorm";
import { dataSource } from "../../data-source";
import { damageReport } from "../../entity/unit/damageReport";
import DatabaseActionException from "../../exceptions/databaseActionException";
@ -8,6 +8,7 @@ import {
DeleteDamageReportCommand,
UpdateDamageReportRelatedRepairCommand,
} from "./damageReportCommand";
import DamageReportService from "../../service/unit/damageReportService";
export default abstract class DamageReportCommandHandler {
/**
@ -83,6 +84,41 @@ export default abstract class DamageReportCommandHandler {
});
}
/**
* @description update damageReport related maintenance
* @returns {Promise<void>}
*/
static async updateRelatedMaintenanceMulti(repairId: string, reports: Array<string>): Promise<void> {
let [related] = await DamageReportService.getAllForRepair(repairId, { noLimit: true });
return await dataSource
.transaction(async (manager) => {
let added = reports.filter((id) => !related.some((r) => r.id === id));
let removed = related.map((r) => r.id).filter((id) => !reports.includes(id));
await manager
.createQueryBuilder()
.update(damageReport)
.set({
repairId: repairId,
})
.where({ id: In(added) })
.execute();
await manager
.createQueryBuilder()
.update(damageReport)
.set({
repairId: null,
})
.where({ id: In(removed) })
.execute();
})
.then(() => {})
.catch((err) => {
throw new DatabaseActionException("UPDATE", "damageReport->maintenance", err);
});
}
/**
* @description update damageReport related maintenance in transaction
* @param {UpdateDamageReportCommand} updateDamageReport

View file

@ -9,11 +9,15 @@ export interface CreateRepairCommand {
export interface UpdateRepairCommand {
id: string;
status: string;
title: string;
description: string;
responsible: string;
reports: string[];
}
export interface UpdateRepairStatusCommand {
id: string;
status: string;
done: boolean;
}
export interface DeleteRepairCommand {

View file

@ -2,7 +2,12 @@ import { dataSource } from "../../data-source";
import { repair } from "../../entity/unit/repair";
import DatabaseActionException from "../../exceptions/databaseActionException";
import DamageReportCommandHandler from "./damageReportCommandHandler";
import { CreateRepairCommand, UpdateRepairCommand, DeleteRepairCommand } from "./repairCommand";
import {
CreateRepairCommand,
UpdateRepairCommand,
DeleteRepairCommand,
UpdateRepairStatusCommand,
} from "./repairCommand";
export default abstract class RepairCommandHandler {
/**
@ -57,10 +62,8 @@ export default abstract class RepairCommandHandler {
.createQueryBuilder()
.update(repair)
.set({
status: updateRepair.status,
title: updateRepair.title,
description: updateRepair.description,
reports: updateRepair.reports.map((r) => ({ id: r })),
responsible: updateRepair.responsible,
})
.where("id = :id", { id: updateRepair.id })
@ -71,6 +74,27 @@ export default abstract class RepairCommandHandler {
});
}
/**
* @description update repair
* @param {UpdateRepairStatusCommand} updateRepair
* @returns {Promise<void>}
*/
static async updateStatus(updateRepair: UpdateRepairStatusCommand): Promise<void> {
return await dataSource
.createQueryBuilder()
.update(repair)
.set({
status: updateRepair.status,
finishedAt: updateRepair.done ? new Date() : null,
})
.where("id = :id", { id: updateRepair.id })
.execute()
.then(() => {})
.catch((err) => {
throw new DatabaseActionException("UPDATE", "repair", err);
});
}
/**
* @description delete repair
* @param {DeleteRepairCommand} deleteRepair

View file

@ -1,10 +1,16 @@
import { Request, Response } from "express";
import RepairService from "../../../service/unit/repairService";
import RepairFactory from "../../../factory/admin/unit/repair";
import { CreateRepairCommand, UpdateRepairCommand } from "../../../command/unit/repairCommand";
import {
CreateRepairCommand,
UpdateRepairCommand,
UpdateRepairStatusCommand,
} from "../../../command/unit/repairCommand";
import RepairCommandHandler from "../../../command/unit/repairCommandHandler";
import BadRequestException from "../../../exceptions/badRequestException";
import { FileSystemHelper } from "../../../helpers/fileSystemHelper";
import { UpdateDamageReportRelatedRepairCommand } from "../../../command/unit/damageReportCommand";
import DamageReportCommandHandler from "../../../command/unit/damageReportCommandHandler";
/**
* @description get all repairs by status
@ -110,21 +116,53 @@ export async function createRepair(req: Request, res: Response): Promise<any> {
*/
export async function updateRepairById(req: Request, res: Response): Promise<any> {
const repairId = req.params.id;
const status = req.body.status;
const title = req.body.title;
const description = req.body.description;
const responsible = req.body.responsible;
const reports = req.body.reports;
let updateRepair: UpdateRepairCommand = {
id: repairId,
status,
title,
description,
responsible,
reports,
};
await RepairCommandHandler.update(updateRepair);
res.sendStatus(204);
}
/**
* @description update repair by id
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function updateRepairReportsById(req: Request, res: Response): Promise<any> {
const repairId = req.params.id;
const reports = req.body.reports as Array<string>;
await DamageReportCommandHandler.updateRelatedMaintenanceMulti(repairId, reports);
res.sendStatus(204);
}
/**
* @description update repair by id
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function updateRepairStatusById(req: Request, res: Response): Promise<any> {
const repairId = req.params.id;
const status = req.body.status;
const done = req.body.done;
let updateRepair: UpdateRepairStatusCommand = {
id: repairId,
status,
done,
};
await RepairCommandHandler.updateStatus(updateRepair);
res.sendStatus(204);
}

View file

@ -6,6 +6,8 @@ import {
getAllRepairsForRelated,
getRepairById,
updateRepairById,
updateRepairReportsById,
updateRepairStatusById,
} from "../../../controller/admin/unit/repairController";
var router = express.Router({ mergeParams: true });
@ -35,7 +37,7 @@ router.get("/:id", async (req: Request, res: Response) => {
router.post(
"/",
PermissionHelper.passCheckMiddleware("update", "unit", "inspection"),
PermissionHelper.passCheckMiddleware("update", "unit", "repair"),
async (req: Request, res: Response) => {
await createRepair(req, res);
}
@ -43,10 +45,26 @@ router.post(
router.patch(
"/:id",
PermissionHelper.passCheckMiddleware("update", "unit", "inspection"),
PermissionHelper.passCheckMiddleware("update", "unit", "repair"),
async (req: Request, res: Response) => {
await updateRepairById(req, res);
}
);
router.patch(
"/:id/reports",
PermissionHelper.passCheckMiddleware("update", "unit", "repair"),
async (req: Request, res: Response) => {
await updateRepairReportsById(req, res);
}
);
router.patch(
"/:id/status",
PermissionHelper.passCheckMiddleware("update", "unit", "repair"),
async (req: Request, res: Response) => {
await updateRepairStatusById(req, res);
}
);
export default router;

View file

@ -114,6 +114,39 @@ export default abstract class DamageReportService {
});
}
/**
* @description get all damageReport for repair
* @returns {Promise<[Array<damageReport>, number]>}
*/
static async getAllForRepair(
repairId: string,
{
offset = 0,
count = 25,
noLimit = false,
}: {
offset?: number;
count?: number;
noLimit?: boolean;
}
): Promise<[Array<damageReport>, number]> {
let query = this.query().where({ repairId });
if (!noLimit) {
query = query.offset(offset).limit(count);
}
return await query
.orderBy("damageReport.reportedAt", "ASC")
.getManyAndCount()
.then((res) => {
return res;
})
.catch((err) => {
throw new DatabaseActionException("SELECT", "damageReport", err);
});
}
/**
* @description get damageReport by id
* @returns {Promise<damageReport>}