schema change and base operations
This commit is contained in:
parent
799a719012
commit
4d37571cb6
27 changed files with 660 additions and 52 deletions
126
src/controller/admin/unit/repairController.ts
Normal file
126
src/controller/admin/unit/repairController.ts
Normal file
|
@ -0,0 +1,126 @@
|
|||
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 RepairCommandHandler from "../../../command/unit/repairCommandHandler";
|
||||
import BadRequestException from "../../../exceptions/badRequestException";
|
||||
import { FileSystemHelper } from "../../../helpers/fileSystemHelper";
|
||||
|
||||
/**
|
||||
* @description get all repairs by status
|
||||
* @param req {Request} Express req object
|
||||
* @param res {Response} Express res object
|
||||
* @returns {Promise<*>}
|
||||
*/
|
||||
export async function getAllRepairsByStatus(req: Request, res: Response): Promise<any> {
|
||||
let done = req.query.done === "true";
|
||||
let offset = parseInt((req.query.offset as string) ?? "0");
|
||||
let count = parseInt((req.query.count as string) ?? "25");
|
||||
let noLimit = req.query.noLimit === "true";
|
||||
|
||||
let [repairs, total] = await RepairService.getAll(done, { offset, count, noLimit });
|
||||
|
||||
res.json({
|
||||
repairs: RepairFactory.mapToBase(repairs),
|
||||
total: total,
|
||||
offset: offset,
|
||||
count: count,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description get all repairs for related id
|
||||
* @param req {Request} Express req object
|
||||
* @param res {Response} Express res object
|
||||
* @returns {Promise<*>}
|
||||
*/
|
||||
export async function getAllRepairsForRelated(req: Request, res: Response): Promise<any> {
|
||||
let relation = req.params.related as "vehicle" | "equipment" | "wearable";
|
||||
let relationId = req.params.relatedId as string;
|
||||
let offset = parseInt((req.query.offset as string) ?? "0");
|
||||
let count = parseInt((req.query.count as string) ?? "25");
|
||||
let noLimit = req.query.noLimit === "true";
|
||||
|
||||
let where;
|
||||
if (relation == "equipment") {
|
||||
where = { equipmentId: relationId };
|
||||
} else if (relation == "vehicle") {
|
||||
where = { vehicleId: relationId };
|
||||
} else {
|
||||
where = { wearableId: relationId };
|
||||
}
|
||||
let [repairs, total] = await RepairService.getAllForRelated(where, { offset, count, noLimit });
|
||||
|
||||
res.json({
|
||||
repairs: RepairFactory.mapToBase(repairs),
|
||||
total: total,
|
||||
offset: offset,
|
||||
count: count,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description get repair by id
|
||||
* @param req {Request} Express req object
|
||||
* @param res {Response} Express res object
|
||||
* @returns {Promise<*>}
|
||||
*/
|
||||
export async function getRepairById(req: Request, res: Response): Promise<any> {
|
||||
const repairId = req.params.id;
|
||||
let repair = await RepairService.getById(repairId);
|
||||
|
||||
res.json(RepairFactory.mapToSingle(repair));
|
||||
}
|
||||
|
||||
/**
|
||||
* @description create repair
|
||||
* @param req {Request} Express req object
|
||||
* @param res {Response} Express res object
|
||||
* @returns {Promise<*>}
|
||||
*/
|
||||
export async function createRepair(req: Request, res: Response): Promise<any> {
|
||||
const description = req.body.description;
|
||||
const responsible = req.body.responsible;
|
||||
const reports = req.body.reports;
|
||||
const affectedId = req.body.affectedId;
|
||||
const affected = req.body.affected;
|
||||
|
||||
if (affected != "equipment" && affected != "vehicle" && affected != "wearable")
|
||||
throw new BadRequestException("set assigned to equipment or vehicle or wearable");
|
||||
|
||||
let createRepair: CreateRepairCommand = {
|
||||
description,
|
||||
affectedId,
|
||||
affected,
|
||||
responsible,
|
||||
reports,
|
||||
};
|
||||
let repairId = await RepairCommandHandler.create(createRepair);
|
||||
|
||||
res.status(200).send(repairId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description update repair by id
|
||||
* @param req {Request} Express req object
|
||||
* @param res {Response} Express res object
|
||||
* @returns {Promise<*>}
|
||||
*/
|
||||
export async function updateRepairById(req: Request, res: Response): Promise<any> {
|
||||
const repairId = req.params.id;
|
||||
const status = req.body.status;
|
||||
const description = req.body.description;
|
||||
const responsible = req.body.responsible;
|
||||
const reports = req.body.reports;
|
||||
|
||||
let updateRepair: UpdateRepairCommand = {
|
||||
id: repairId,
|
||||
status,
|
||||
description,
|
||||
responsible,
|
||||
reports,
|
||||
};
|
||||
await RepairCommandHandler.update(updateRepair);
|
||||
|
||||
res.sendStatus(204);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue