Actualización CRUD módulo LPG, adición de validación y adición de la función para generar número de requisición
This commit is contained in:
@ -45,7 +45,7 @@ export class LPGController{
|
|||||||
|
|
||||||
catch (error) {
|
catch (error) {
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
message:"Error al obtener requisicion",
|
message:"Error al obtener requisición",
|
||||||
error
|
error
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -105,33 +105,71 @@ export class LPGController{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Eliminar requisición
|
async getByStatus(
|
||||||
|
req:Request,
|
||||||
|
res:Response,
|
||||||
|
){
|
||||||
|
|
||||||
async delete(
|
try{
|
||||||
req:Request,
|
const request = await service.getByStatus(
|
||||||
res:Response
|
req.params.status as string
|
||||||
){
|
);
|
||||||
try{
|
|
||||||
const request =
|
|
||||||
await service.delete(
|
|
||||||
req.params.id as string
|
|
||||||
);
|
|
||||||
|
|
||||||
if(!request){
|
res.json(request);
|
||||||
return res.status(404).json({
|
}catch(error){
|
||||||
message:
|
res.status(500).json({
|
||||||
"Requisición no encontrada"
|
message:"Error al obtener requisición "
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
res.json({
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async getByPlant(
|
||||||
|
req:Request,
|
||||||
|
res:Response,
|
||||||
|
){
|
||||||
|
|
||||||
|
try{
|
||||||
|
const request = await service.getByPlant(
|
||||||
|
req.params.plant as string
|
||||||
|
);
|
||||||
|
|
||||||
|
res.json(request);
|
||||||
|
}catch(error){
|
||||||
|
res.status(500).json({
|
||||||
|
message:"Error al obtener requisición "
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Eliminar requisición
|
||||||
|
|
||||||
|
async delete(
|
||||||
|
req:Request,
|
||||||
|
res:Response
|
||||||
|
){
|
||||||
|
try{
|
||||||
|
const request =
|
||||||
|
await service.delete(
|
||||||
|
req.params.id as string
|
||||||
|
);
|
||||||
|
|
||||||
|
if(!request){
|
||||||
|
return res.status(404).json({
|
||||||
|
message:"Requisición no encontrada"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
res.json({
|
||||||
message:"Requisición eliminada correctamente"
|
message:"Requisición eliminada correctamente"
|
||||||
});
|
});
|
||||||
}
|
|
||||||
catch (error) {
|
}catch(error) {
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
message: "Error al eliminar requisición",
|
message: "Error al eliminar requisición",
|
||||||
error
|
error
|
||||||
});
|
});
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -1,16 +1,29 @@
|
|||||||
import { ObjectId } from "mongoose";
|
import { Types } from "mongoose";
|
||||||
|
|
||||||
export interface ILpg{
|
export interface ILpg{
|
||||||
|
|
||||||
requestNumber:string;
|
requestNumber:string;
|
||||||
|
|
||||||
plant:string,
|
plant:string,
|
||||||
|
|
||||||
equipment:string;
|
equipment:string;
|
||||||
requestedBy:ObjectId;
|
|
||||||
approvedBy?:ObjectId;
|
requestedBy:Types.ObjectId;
|
||||||
|
|
||||||
|
approvedBy?:Types.ObjectId;
|
||||||
|
|
||||||
requestDate:Date;
|
requestDate:Date;
|
||||||
gallons:number;
|
|
||||||
|
gallonQuantity:number;
|
||||||
|
|
||||||
pricePerGallon:number;
|
pricePerGallon:number;
|
||||||
|
|
||||||
totalAmount:number,
|
totalAmount:number,
|
||||||
|
|
||||||
comments?:string;
|
comments?:string;
|
||||||
status:string;
|
|
||||||
|
status:
|
||||||
|
| "PENDIENTE"
|
||||||
|
| "APROBADO"
|
||||||
|
| "RECHAZADO";
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,7 +10,7 @@ const lpgSchema = new mongoose.Schema({
|
|||||||
|
|
||||||
plant:{
|
plant:{
|
||||||
type:String,
|
type:String,
|
||||||
requiered:true,
|
required:true,
|
||||||
enum:[
|
enum:[
|
||||||
"Planta 1",
|
"Planta 1",
|
||||||
"Planta 2"
|
"Planta 2"
|
||||||
@ -25,7 +25,7 @@ const lpgSchema = new mongoose.Schema({
|
|||||||
requestedBy:{
|
requestedBy:{
|
||||||
type:mongoose.Schema.Types.ObjectId,
|
type:mongoose.Schema.Types.ObjectId,
|
||||||
ref:"User",
|
ref:"User",
|
||||||
requeride:true
|
required:true
|
||||||
},
|
},
|
||||||
|
|
||||||
approvedBy:{
|
approvedBy:{
|
||||||
@ -36,10 +36,10 @@ const lpgSchema = new mongoose.Schema({
|
|||||||
requestDate:{
|
requestDate:{
|
||||||
type:Date,
|
type:Date,
|
||||||
default:Date.now,
|
default:Date.now,
|
||||||
requiered:true
|
required:true
|
||||||
},
|
},
|
||||||
|
|
||||||
gallons:{
|
gallonQuantity:{
|
||||||
type:Number,
|
type:Number,
|
||||||
required:true,
|
required:true,
|
||||||
},
|
},
|
||||||
|
|||||||
@ -10,14 +10,18 @@ const controller = new LPGController();
|
|||||||
//Obtener todas las requisiciones
|
//Obtener todas las requisiciones
|
||||||
lpgRouter.get("/", authMiddleware, controller.getAll);
|
lpgRouter.get("/", authMiddleware, controller.getAll);
|
||||||
|
|
||||||
|
//Obtener requisición por status
|
||||||
|
lpgRouter.get("/status/:status", authMiddleware, controller.getByStatus);
|
||||||
|
|
||||||
|
//Obtener requisición por planta
|
||||||
|
lpgRouter.get("/plant/:plant", authMiddleware, controller.getByPlant);
|
||||||
|
|
||||||
//Obtener requisición por ID
|
//Obtener requisición por ID
|
||||||
lpgRouter.get("/:id", authMiddleware, controller.getById);
|
lpgRouter.get("/:id", authMiddleware, controller.getById);
|
||||||
|
|
||||||
|
|
||||||
//Crear requisición
|
//Crear requisición
|
||||||
lpgRouter.post( "/", authMiddleware, controller.create);
|
lpgRouter.post( "/", authMiddleware, controller.create);
|
||||||
|
|
||||||
|
|
||||||
//Actualizar requisición
|
//Actualizar requisición
|
||||||
lpgRouter.put("/:id", authMiddleware, controller.update);
|
lpgRouter.put("/:id", authMiddleware, controller.update);
|
||||||
|
|
||||||
|
|||||||
@ -1,29 +1,16 @@
|
|||||||
import lpgModel from "./lpg.model";
|
import lpgModel from "./lpg.model";
|
||||||
import { ILpg } from "./lpg.interface";
|
import { ILpg } from "./lpg.interface";
|
||||||
|
import { generateRequestLPGNumber } from "../../utils/requestNumber";
|
||||||
|
|
||||||
export class LPGService{
|
export class LPGService{
|
||||||
|
|
||||||
|
|
||||||
//Crear solicitud gas LPG
|
|
||||||
async create (
|
|
||||||
data:ILpg
|
|
||||||
){
|
|
||||||
|
|
||||||
data.totalAmount = data.gallons *
|
|
||||||
data.pricePerGallon;
|
|
||||||
|
|
||||||
const lpg = new lpgModel(data);
|
|
||||||
|
|
||||||
return await lpg.save();
|
|
||||||
}
|
|
||||||
|
|
||||||
//Obtener todas las requisiciones
|
//Obtener todas las requisiciones
|
||||||
async getAll() {
|
async getAll() {
|
||||||
|
|
||||||
return await lpgModel
|
return await lpgModel
|
||||||
.find()
|
.find()
|
||||||
.populate("requestedBy")
|
.populate("requestedBy")
|
||||||
.populate("approveBy")
|
.populate("approvedBy")
|
||||||
}
|
}
|
||||||
|
|
||||||
//Obtener requisición por Id
|
//Obtener requisición por Id
|
||||||
@ -33,21 +20,49 @@ export class LPGService{
|
|||||||
return await lpgModel
|
return await lpgModel
|
||||||
.findById(id)
|
.findById(id)
|
||||||
.populate("requestedBy")
|
.populate("requestedBy")
|
||||||
.populate("approveBy")
|
.populate("approvedBy")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Crear solicitud gas LPG
|
||||||
|
async create (
|
||||||
|
data:ILpg
|
||||||
|
){
|
||||||
|
//Validar que los galones sean mayor a 0
|
||||||
|
if(data.gallonQuantity <= 0){
|
||||||
|
throw new Error ("La cantidad de galones debe ser mayor que 0");
|
||||||
|
}
|
||||||
|
|
||||||
|
//Generar número automático
|
||||||
|
data.requestNumber = generateRequestLPGNumber();
|
||||||
|
|
||||||
|
data.totalAmount = data.gallonQuantity * data.pricePerGallon;
|
||||||
|
|
||||||
|
//Estado inicial
|
||||||
|
data.status = "PENDIENTE";
|
||||||
|
|
||||||
|
const lpg = new lpgModel(data);
|
||||||
|
|
||||||
|
return await lpg.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(
|
async update(
|
||||||
id:string,
|
id:string,
|
||||||
data:Partial<ILpg>
|
data:Partial<ILpg>
|
||||||
){
|
){
|
||||||
if (data.gallons &&
|
//Buscar la requisición actual
|
||||||
data.pricePerGallon
|
const lpgRequest = await lpgModel.findById(id);
|
||||||
){
|
|
||||||
data.totalAmount =
|
if(!lpgRequest){
|
||||||
data.gallons *
|
return null;
|
||||||
data.pricePerGallon
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const gallonQuantity = data.gallonQuantity ?? lpgRequest.gallonQuantity;
|
||||||
|
|
||||||
|
const pricePerGallon = data.pricePerGallon ?? lpgRequest.pricePerGallon;
|
||||||
|
|
||||||
|
data.totalAmount = gallonQuantity * pricePerGallon
|
||||||
|
|
||||||
return await lpgModel
|
return await lpgModel
|
||||||
.findByIdAndUpdate(
|
.findByIdAndUpdate(
|
||||||
id,
|
id,
|
||||||
|
|||||||
@ -1,3 +1,7 @@
|
|||||||
export const generateRequestNumber = ():string =>{
|
export const generateRequestNumber = ():string =>{
|
||||||
return `REQ-${Date.now()}`;
|
return `REQ-${Date.now()}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const generateRequestLPGNumber =():string =>{
|
||||||
|
return `LPG-${Date.now()}`;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user