edit repair
This commit is contained in:
parent
85fa912024
commit
f812298cb2
6 changed files with 166 additions and 13 deletions
|
@ -1,4 +1,4 @@
|
||||||
import { EntityManager, UpdateResult } from "typeorm";
|
import { EntityManager, In, UpdateResult } from "typeorm";
|
||||||
import { dataSource } from "../../data-source";
|
import { dataSource } from "../../data-source";
|
||||||
import { damageReport } from "../../entity/unit/damageReport";
|
import { damageReport } from "../../entity/unit/damageReport";
|
||||||
import DatabaseActionException from "../../exceptions/databaseActionException";
|
import DatabaseActionException from "../../exceptions/databaseActionException";
|
||||||
|
@ -8,6 +8,7 @@ import {
|
||||||
DeleteDamageReportCommand,
|
DeleteDamageReportCommand,
|
||||||
UpdateDamageReportRelatedRepairCommand,
|
UpdateDamageReportRelatedRepairCommand,
|
||||||
} from "./damageReportCommand";
|
} from "./damageReportCommand";
|
||||||
|
import DamageReportService from "../../service/unit/damageReportService";
|
||||||
|
|
||||||
export default abstract class DamageReportCommandHandler {
|
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
|
* @description update damageReport related maintenance in transaction
|
||||||
* @param {UpdateDamageReportCommand} updateDamageReport
|
* @param {UpdateDamageReportCommand} updateDamageReport
|
||||||
|
|
|
@ -9,11 +9,15 @@ export interface CreateRepairCommand {
|
||||||
|
|
||||||
export interface UpdateRepairCommand {
|
export interface UpdateRepairCommand {
|
||||||
id: string;
|
id: string;
|
||||||
status: string;
|
|
||||||
title: string;
|
title: string;
|
||||||
description: string;
|
description: string;
|
||||||
responsible: string;
|
responsible: string;
|
||||||
reports: string[];
|
}
|
||||||
|
|
||||||
|
export interface UpdateRepairStatusCommand {
|
||||||
|
id: string;
|
||||||
|
status: string;
|
||||||
|
done: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DeleteRepairCommand {
|
export interface DeleteRepairCommand {
|
||||||
|
|
|
@ -2,7 +2,12 @@ import { dataSource } from "../../data-source";
|
||||||
import { repair } from "../../entity/unit/repair";
|
import { repair } from "../../entity/unit/repair";
|
||||||
import DatabaseActionException from "../../exceptions/databaseActionException";
|
import DatabaseActionException from "../../exceptions/databaseActionException";
|
||||||
import DamageReportCommandHandler from "./damageReportCommandHandler";
|
import DamageReportCommandHandler from "./damageReportCommandHandler";
|
||||||
import { CreateRepairCommand, UpdateRepairCommand, DeleteRepairCommand } from "./repairCommand";
|
import {
|
||||||
|
CreateRepairCommand,
|
||||||
|
UpdateRepairCommand,
|
||||||
|
DeleteRepairCommand,
|
||||||
|
UpdateRepairStatusCommand,
|
||||||
|
} from "./repairCommand";
|
||||||
|
|
||||||
export default abstract class RepairCommandHandler {
|
export default abstract class RepairCommandHandler {
|
||||||
/**
|
/**
|
||||||
|
@ -57,10 +62,8 @@ export default abstract class RepairCommandHandler {
|
||||||
.createQueryBuilder()
|
.createQueryBuilder()
|
||||||
.update(repair)
|
.update(repair)
|
||||||
.set({
|
.set({
|
||||||
status: updateRepair.status,
|
|
||||||
title: updateRepair.title,
|
title: updateRepair.title,
|
||||||
description: updateRepair.description,
|
description: updateRepair.description,
|
||||||
reports: updateRepair.reports.map((r) => ({ id: r })),
|
|
||||||
responsible: updateRepair.responsible,
|
responsible: updateRepair.responsible,
|
||||||
})
|
})
|
||||||
.where("id = :id", { id: updateRepair.id })
|
.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
|
* @description delete repair
|
||||||
* @param {DeleteRepairCommand} deleteRepair
|
* @param {DeleteRepairCommand} deleteRepair
|
||||||
|
|
|
@ -1,10 +1,16 @@
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import RepairService from "../../../service/unit/repairService";
|
import RepairService from "../../../service/unit/repairService";
|
||||||
import RepairFactory from "../../../factory/admin/unit/repair";
|
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 RepairCommandHandler from "../../../command/unit/repairCommandHandler";
|
||||||
import BadRequestException from "../../../exceptions/badRequestException";
|
import BadRequestException from "../../../exceptions/badRequestException";
|
||||||
import { FileSystemHelper } from "../../../helpers/fileSystemHelper";
|
import { FileSystemHelper } from "../../../helpers/fileSystemHelper";
|
||||||
|
import { UpdateDamageReportRelatedRepairCommand } from "../../../command/unit/damageReportCommand";
|
||||||
|
import DamageReportCommandHandler from "../../../command/unit/damageReportCommandHandler";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description get all repairs by status
|
* @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> {
|
export async function updateRepairById(req: Request, res: Response): Promise<any> {
|
||||||
const repairId = req.params.id;
|
const repairId = req.params.id;
|
||||||
const status = req.body.status;
|
|
||||||
const title = req.body.title;
|
const title = req.body.title;
|
||||||
const description = req.body.description;
|
const description = req.body.description;
|
||||||
const responsible = req.body.responsible;
|
const responsible = req.body.responsible;
|
||||||
const reports = req.body.reports;
|
|
||||||
|
|
||||||
let updateRepair: UpdateRepairCommand = {
|
let updateRepair: UpdateRepairCommand = {
|
||||||
id: repairId,
|
id: repairId,
|
||||||
status,
|
|
||||||
title,
|
title,
|
||||||
description,
|
description,
|
||||||
responsible,
|
responsible,
|
||||||
reports,
|
|
||||||
};
|
};
|
||||||
await RepairCommandHandler.update(updateRepair);
|
await RepairCommandHandler.update(updateRepair);
|
||||||
|
|
||||||
res.sendStatus(204);
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -6,6 +6,8 @@ import {
|
||||||
getAllRepairsForRelated,
|
getAllRepairsForRelated,
|
||||||
getRepairById,
|
getRepairById,
|
||||||
updateRepairById,
|
updateRepairById,
|
||||||
|
updateRepairReportsById,
|
||||||
|
updateRepairStatusById,
|
||||||
} from "../../../controller/admin/unit/repairController";
|
} from "../../../controller/admin/unit/repairController";
|
||||||
|
|
||||||
var router = express.Router({ mergeParams: true });
|
var router = express.Router({ mergeParams: true });
|
||||||
|
@ -35,7 +37,7 @@ router.get("/:id", async (req: Request, res: Response) => {
|
||||||
|
|
||||||
router.post(
|
router.post(
|
||||||
"/",
|
"/",
|
||||||
PermissionHelper.passCheckMiddleware("update", "unit", "inspection"),
|
PermissionHelper.passCheckMiddleware("update", "unit", "repair"),
|
||||||
async (req: Request, res: Response) => {
|
async (req: Request, res: Response) => {
|
||||||
await createRepair(req, res);
|
await createRepair(req, res);
|
||||||
}
|
}
|
||||||
|
@ -43,10 +45,26 @@ router.post(
|
||||||
|
|
||||||
router.patch(
|
router.patch(
|
||||||
"/:id",
|
"/:id",
|
||||||
PermissionHelper.passCheckMiddleware("update", "unit", "inspection"),
|
PermissionHelper.passCheckMiddleware("update", "unit", "repair"),
|
||||||
async (req: Request, res: Response) => {
|
async (req: Request, res: Response) => {
|
||||||
await updateRepairById(req, res);
|
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;
|
export default router;
|
||||||
|
|
|
@ -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
|
* @description get damageReport by id
|
||||||
* @returns {Promise<damageReport>}
|
* @returns {Promise<damageReport>}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue