Actualización CRUD módulo lectura de bombas
This commit is contained in:
@ -117,44 +117,28 @@ export class PumpReadingController {
|
|||||||
async calculateDispensedGallons(
|
async calculateDispensedGallons(
|
||||||
|
|
||||||
req: Request,
|
req: Request,
|
||||||
|
|
||||||
res: Response
|
res: Response
|
||||||
|
|
||||||
) {
|
) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const gallons = await service.calculateDispensedGallons(
|
||||||
const gallons =
|
|
||||||
|
|
||||||
await service.calculateDispensedGallons(
|
|
||||||
|
|
||||||
req.params.id as string
|
req.params.id as string
|
||||||
|
|
||||||
);
|
);
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
|
|
||||||
gallonsDispensed: gallons
|
gallonsDispensed: gallons
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
|
|
||||||
message: "Error al calcular galones despachados.",
|
message: "Error al calcular galones despachados.",
|
||||||
|
|
||||||
error
|
error
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calcular galones registrados por el sistema
|
// Calcular galones registrados por el sistema
|
||||||
|
|
||||||
|
|
||||||
async calculateSystemDispensed(
|
async calculateSystemDispensed(
|
||||||
|
|
||||||
req: Request,
|
req: Request,
|
||||||
@ -163,68 +147,43 @@ export class PumpReadingController {
|
|||||||
) {
|
) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
const gallons = await service.calculateSystemDispensed(
|
const gallons = await service.calculateSystemDispensed(
|
||||||
|
|
||||||
req.params.id as string
|
req.params.id as string
|
||||||
|
|
||||||
);
|
);
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
|
|
||||||
systemDispensed: gallons
|
systemDispensed: gallons
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
|
|
||||||
message: "Error al calcular galones del sistema.",
|
message: "Error al calcular galones del sistema.",
|
||||||
error
|
error
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Calcular diferencia
|
// Calcular diferencia
|
||||||
|
|
||||||
async calculateDifference(
|
async calculateDifference(
|
||||||
|
|
||||||
req: Request,
|
req: Request,
|
||||||
res: Response
|
res: Response
|
||||||
|
|
||||||
) {
|
) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
const difference = await service.calculateDifference(
|
const difference = await service.calculateDifference(
|
||||||
|
|
||||||
req.params.id as string
|
req.params.id as string
|
||||||
|
|
||||||
);
|
);
|
||||||
|
|
||||||
res.json({
|
res.json({difference});
|
||||||
|
|
||||||
difference
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
|
|
||||||
message: "Error al calcular la diferencia.",
|
message: "Error al calcular la diferencia.",
|
||||||
|
|
||||||
error
|
error
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actualizar estado
|
// Actualizar estado
|
||||||
@ -235,25 +194,15 @@ export class PumpReadingController {
|
|||||||
res: Response
|
res: Response
|
||||||
|
|
||||||
) {
|
) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
const status = await service.updateStatus(
|
const status = await service.updateStatus(
|
||||||
|
|
||||||
req.params.id as string
|
req.params.id as string
|
||||||
|
|
||||||
);
|
);
|
||||||
|
|
||||||
res.json({
|
res.json({status});
|
||||||
|
|
||||||
status
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
|
|
||||||
message: "Error al actualizar el estado.",
|
message: "Error al actualizar el estado.",
|
||||||
error
|
error
|
||||||
|
|
||||||
@ -264,96 +213,62 @@ export class PumpReadingController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Actualizar Fuel Request relacionadas
|
//Actualizar Fuel Request relacionadas
|
||||||
|
|
||||||
|
|
||||||
async updateFuelRequests(
|
async updateFuelRequests(
|
||||||
|
|
||||||
req: Request,
|
req: Request,
|
||||||
res: Response
|
res: Response
|
||||||
|
|
||||||
) {
|
) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
const requests = await service.updateFuelRequests(
|
const requests = await service.updateFuelRequests(
|
||||||
|
|
||||||
req.params.id as string
|
req.params.id as string
|
||||||
|
|
||||||
);
|
);
|
||||||
|
|
||||||
res.json(requests);
|
res.json(requests);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
|
|
||||||
message: "Error al actualizar las Fuel Request.",
|
message: "Error al actualizar las Fuel Request.",
|
||||||
error
|
error
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Crear anomalía por diferencia
|
//Crear anomalía por diferencia
|
||||||
|
|
||||||
async createDifferenceAnomaly(
|
async createDifferenceAnomaly(
|
||||||
|
|
||||||
req: Request,
|
req: Request,
|
||||||
res: Response
|
res: Response
|
||||||
|
|
||||||
) {
|
) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
const anomaly = await service.creataDifferenceAnomaly(
|
const anomaly = await service.creataDifferenceAnomaly(
|
||||||
|
|
||||||
req.params.id as string
|
req.params.id as string
|
||||||
|
|
||||||
);
|
);
|
||||||
|
|
||||||
res.json(anomaly);
|
res.json(anomaly);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
|
|
||||||
message: "Error al crear la anomalía.",
|
message: "Error al crear la anomalía.",
|
||||||
|
|
||||||
error
|
error
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Cerrar lectura
|
//Cerrar lectura
|
||||||
|
|
||||||
async closeReading(
|
async closeReading(
|
||||||
|
|
||||||
req: Request,
|
req: Request,
|
||||||
res: Response
|
res: Response
|
||||||
|
|
||||||
) {
|
) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
const reading = await service.closeReading(
|
const reading = await service.closeReading(
|
||||||
|
|
||||||
req.params.id as string
|
req.params.id as string
|
||||||
|
|
||||||
);
|
);
|
||||||
|
|
||||||
res.json(reading);
|
res.json(reading);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
|
|
||||||
message: "Error al cerrar la lectura.",
|
message: "Error al cerrar la lectura.",
|
||||||
|
|
||||||
error
|
error
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
@ -79,11 +79,5 @@ const pumpReadingSchema = new mongoose.Schema({
|
|||||||
|
|
||||||
);
|
);
|
||||||
|
|
||||||
pumpReadingSchema.pre("save",
|
|
||||||
function(){
|
|
||||||
|
|
||||||
this.gallonsDispensed = this.finalReading-this.initialReading;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
export default mongoose.model("PumpReading",pumpReadingSchema);
|
export default mongoose.model("PumpReading",pumpReadingSchema);
|
||||||
@ -6,23 +6,41 @@ const pumpReadingRouter = Router();
|
|||||||
|
|
||||||
const controller = new PumpReadingController();
|
const controller = new PumpReadingController();
|
||||||
|
|
||||||
|
|
||||||
// Obtener todas las lecturas
|
// Obtener todas las lecturas
|
||||||
pumpReadingRouter.get("/", authMiddleware, controller.getAll);
|
pumpReadingRouter.get("/", authMiddleware, controller.getAll);
|
||||||
|
|
||||||
// Obtener una lectura por ID
|
// Obtener lectura por ID
|
||||||
pumpReadingRouter.get("/:id", authMiddleware, controller.getById);
|
pumpReadingRouter.get("/:id", authMiddleware, controller.getById);
|
||||||
|
|
||||||
// Crear una nueva lectura
|
// Crear lectura
|
||||||
pumpReadingRouter.post("/", authMiddleware, controller.create);
|
pumpReadingRouter.post("/",authMiddleware, controller.create
|
||||||
|
);
|
||||||
|
|
||||||
// Actualizar una lectura
|
// Actualizar lectura
|
||||||
pumpReadingRouter.put("/:id", authMiddleware, controller.update);
|
pumpReadingRouter.put("/:id", authMiddleware, controller.update);
|
||||||
|
|
||||||
// Eliminar una lectura
|
// Eliminar lectura
|
||||||
pumpReadingRouter.delete("/:id", authMiddleware, controller.delete);
|
pumpReadingRouter.delete("/:id", authMiddleware, controller.delete);
|
||||||
|
|
||||||
// Cerrar lectura y realizar el cuadre
|
// Calcular galones despachados
|
||||||
|
pumpReadingRouter.post("/:id/calculate-dispensed", authMiddleware, controller.calculateDispensedGallons);
|
||||||
|
|
||||||
|
// Calcular galones registrados en el sistema
|
||||||
|
pumpReadingRouter.post("/:id/calculate-system", authMiddleware, controller.calculateSystemDispensed);
|
||||||
|
|
||||||
|
// Calcular diferencia
|
||||||
|
pumpReadingRouter.post("/:id/calculate-difference", authMiddleware, controller.calculateDifference);
|
||||||
|
|
||||||
|
// Actualizar estado
|
||||||
|
pumpReadingRouter.post("/:id/update-status",authMiddleware,controller.updateStatus);
|
||||||
|
|
||||||
|
// Actualizar Fuel Requests relacionadas
|
||||||
|
pumpReadingRouter.post("/:id/update-requests", authMiddleware, controller.updateFuelRequests);
|
||||||
|
|
||||||
|
// Crear anomalía por diferencia
|
||||||
|
pumpReadingRouter.post("/:id/create-anomaly", authMiddleware, controller.createDifferenceAnomaly);
|
||||||
|
|
||||||
|
// Cerrar lectura (realiza todo el proceso)
|
||||||
pumpReadingRouter.post("/:id/close", authMiddleware, controller.closeReading);
|
pumpReadingRouter.post("/:id/close", authMiddleware, controller.closeReading);
|
||||||
|
|
||||||
export default pumpReadingRouter;
|
export default pumpReadingRouter;
|
||||||
@ -2,23 +2,11 @@ import pumpReadingModel from "./pumpReading.model";
|
|||||||
import { IPumpReading } from "./pumpReading.interface";
|
import { IPumpReading } from "./pumpReading.interface";
|
||||||
import fuelRequestModel from "../fuel-requests/fuelRequest.model";
|
import fuelRequestModel from "../fuel-requests/fuelRequest.model";
|
||||||
import anomalyModel from "../anomalies/anomaly.model";
|
import anomalyModel from "../anomalies/anomaly.model";
|
||||||
|
import { generatePumpReadingNumber } from "../../utils/requestNumber";
|
||||||
|
|
||||||
|
|
||||||
export class PumpReadingService{
|
export class PumpReadingService{
|
||||||
|
|
||||||
//Crear nueva lectura de bomba
|
|
||||||
|
|
||||||
async create(
|
|
||||||
data:IPumpReading
|
|
||||||
){
|
|
||||||
data.gallonsDispensed=
|
|
||||||
data.finalReading-
|
|
||||||
data.initialReading;
|
|
||||||
|
|
||||||
const pumpReading = new pumpReadingModel(data);
|
|
||||||
|
|
||||||
return await pumpReading.save();
|
|
||||||
}
|
|
||||||
|
|
||||||
//Obtener todas las lecturas
|
//Obtener todas las lecturas
|
||||||
|
|
||||||
@ -45,19 +33,81 @@ export class PumpReadingService{
|
|||||||
.populate("createdBy")
|
.populate("createdBy")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Crear nueva lectura de bomba
|
||||||
|
async create(
|
||||||
|
data: IPumpReading
|
||||||
|
){
|
||||||
|
// Validar lectura inicial
|
||||||
|
if (data.initialReading < 0) {
|
||||||
|
throw new Error("La lectura inicial no puede ser menor que 0.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validar lectura final
|
||||||
|
if (data.finalReading < data.initialReading) {
|
||||||
|
throw new Error("La lectura final no puede ser menor que la lectura inicial.");
|
||||||
|
}
|
||||||
|
|
||||||
|
data.readingNumber = generatePumpReadingNumber();
|
||||||
|
|
||||||
|
// Calcular galones despachados
|
||||||
|
data.gallonsDispensed = data.finalReading - data.initialReading;
|
||||||
|
|
||||||
|
// Inicializar valores
|
||||||
|
data.systemDispensed = 0;
|
||||||
|
|
||||||
|
data.difference = 0;
|
||||||
|
|
||||||
|
data.status = "ABIERTA";
|
||||||
|
|
||||||
|
// Guardar lectura
|
||||||
|
const pumpReading = new pumpReadingModel(data);
|
||||||
|
|
||||||
|
return await pumpReading.save();
|
||||||
|
}
|
||||||
|
|
||||||
async update(
|
async update(
|
||||||
id: string,
|
id: string,
|
||||||
data: Partial<IPumpReading>
|
data: Partial<IPumpReading>
|
||||||
){
|
){
|
||||||
if(
|
|
||||||
data.initialReading !== undefined&&
|
// Buscar la lectura actual
|
||||||
data.finalReading !== undefined
|
const reading = await pumpReadingModel.findById(id);
|
||||||
){
|
|
||||||
data.gallonsDispensed =
|
if (!reading) {
|
||||||
data.finalReading -
|
return null;
|
||||||
data.initialReading;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tomar los nuevos valores o conservar los existentes
|
||||||
|
const initialReading = data.initialReading ?? reading.initialReading;
|
||||||
|
|
||||||
|
const finalReading = data.finalReading ?? reading.finalReading;
|
||||||
|
|
||||||
|
// Validar lecturas
|
||||||
|
if (initialReading < 0) {
|
||||||
|
throw new Error("La lectura inicial no puede ser menor que 0.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (finalReading < initialReading) {
|
||||||
|
throw new Error("La lectura final no puede ser menor que la lectura inicial.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recalcular galones despachados
|
||||||
|
data.gallonsDispensed = finalReading - initialReading;
|
||||||
|
|
||||||
|
// Mantener actualizado el total del sistema
|
||||||
|
const systemDispensed = data.systemDispensed ?? reading.systemDispensed;
|
||||||
|
|
||||||
|
// Recalcular diferencia
|
||||||
|
data.difference = data.gallonsDispensed - systemDispensed;
|
||||||
|
|
||||||
|
// Actualizar estado automáticamente
|
||||||
|
if (data.difference === 0) {
|
||||||
|
data.status = "CUADRADA";
|
||||||
|
} else {
|
||||||
|
data.status = "CON_DIFERENCIA";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actualizar la lectura
|
||||||
return await pumpReadingModel.findByIdAndUpdate(
|
return await pumpReadingModel.findByIdAndUpdate(
|
||||||
id,
|
id,
|
||||||
data,
|
data,
|
||||||
@ -70,8 +120,7 @@ export class PumpReadingService{
|
|||||||
async delete (
|
async delete (
|
||||||
id:string
|
id:string
|
||||||
){
|
){
|
||||||
return await pumpReadingModel
|
return await pumpReadingModel.findByIdAndDelete(id);
|
||||||
.findByIdAndDelete
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Calcular galones despachados
|
//Calcular galones despachados
|
||||||
@ -83,14 +132,11 @@ export class PumpReadingService{
|
|||||||
await pumpReadingModel.findById(id);
|
await pumpReadingModel.findById(id);
|
||||||
|
|
||||||
if(!reading){
|
if(!reading){
|
||||||
throw new Error(
|
throw new Error("Lectura no encontrado");
|
||||||
"Lectura no encontrado"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
reading.gallonsDispensed =
|
reading.gallonsDispensed = reading.finalReading - reading.initialReading;
|
||||||
reading.finalReading -
|
|
||||||
reading.initialReading;
|
|
||||||
await reading.save();
|
await reading.save();
|
||||||
return reading.gallonsDispensed;
|
return reading.gallonsDispensed;
|
||||||
}
|
}
|
||||||
@ -142,9 +188,8 @@ export class PumpReadingService{
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
reading.difference =
|
reading.difference = reading.gallonsDispensed - reading.systemDispensed;
|
||||||
reading.gallonsDispensed -
|
|
||||||
reading.systemDispensed;
|
|
||||||
await reading.save();
|
await reading.save();
|
||||||
return reading.difference;
|
return reading.difference;
|
||||||
}
|
}
|
||||||
@ -210,7 +255,6 @@ export class PumpReadingService{
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Crear anomalía por diferencencia
|
//Crear anomalía por diferencencia
|
||||||
|
|
||||||
async creataDifferenceAnomaly(
|
async creataDifferenceAnomaly(
|
||||||
id:string
|
id:string
|
||||||
){
|
){
|
||||||
|
|||||||
Reference in New Issue
Block a user