CRUD Modulo requisiciones LGP y actualización modulos de aprobaciones
This commit is contained in:
@ -2,8 +2,13 @@ import ApprovalModel from "./approval.model";
|
||||
|
||||
import FuelRequestModel from "../fuel-requests/fuelRequest.model";
|
||||
|
||||
import externalInvoiceModel from "../external-invoices/externalInvoice.model";
|
||||
|
||||
import lpgModel from "../lpg/lpg.model";
|
||||
|
||||
import { IApproval } from "./approval.interface";
|
||||
|
||||
|
||||
export class ApprovalService {
|
||||
|
||||
//Obtener todas las aprobaciones
|
||||
@ -103,6 +108,52 @@ export class ApprovalService {
|
||||
|
||||
}
|
||||
|
||||
if (
|
||||
approval.referenceType ===
|
||||
"ExternalInvoice"
|
||||
) {
|
||||
|
||||
await externalInvoiceModel
|
||||
.findByIdAndUpdate(
|
||||
|
||||
approval.referenceId,
|
||||
|
||||
{
|
||||
status:
|
||||
"APROBADO"
|
||||
},
|
||||
|
||||
{
|
||||
new: true
|
||||
}
|
||||
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
if (
|
||||
approval.referenceType ===
|
||||
"LPG"
|
||||
) {
|
||||
|
||||
await lpgModel
|
||||
.findByIdAndUpdate(
|
||||
|
||||
approval.referenceId,
|
||||
|
||||
{
|
||||
status:
|
||||
"APROBADO"
|
||||
},
|
||||
|
||||
{
|
||||
new: true
|
||||
}
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return approval;
|
||||
|
||||
}
|
||||
@ -167,6 +218,51 @@ export class ApprovalService {
|
||||
|
||||
}
|
||||
|
||||
if (
|
||||
approval.referenceType ===
|
||||
"ExternalInvoice"
|
||||
) {
|
||||
|
||||
await externalInvoiceModel
|
||||
.findByIdAndUpdate(
|
||||
|
||||
approval.referenceId,
|
||||
|
||||
{
|
||||
status:
|
||||
"RECHAZADO"
|
||||
},
|
||||
|
||||
{
|
||||
new: true
|
||||
}
|
||||
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
if (
|
||||
approval.referenceType ===
|
||||
"LPG"
|
||||
) {
|
||||
|
||||
await lpgModel
|
||||
.findByIdAndUpdate(
|
||||
|
||||
approval.referenceId,
|
||||
|
||||
{
|
||||
status:
|
||||
"RECHAZADO"
|
||||
},
|
||||
|
||||
{
|
||||
new: true
|
||||
}
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
return approval;
|
||||
|
||||
}
|
||||
|
||||
@ -143,89 +143,6 @@ export class ExternalInvoiceController {
|
||||
|
||||
}
|
||||
|
||||
//Aprobar factura
|
||||
|
||||
async approve(
|
||||
req: Request,
|
||||
res: Response
|
||||
) {
|
||||
|
||||
try {
|
||||
|
||||
const invoice =
|
||||
await service.approve(
|
||||
|
||||
req.params.id as string,
|
||||
req.body.approvedBy
|
||||
|
||||
);
|
||||
|
||||
if (!invoice) {
|
||||
|
||||
return res.status(404).json({
|
||||
message:
|
||||
"Factura no encontrada"
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
res.json(invoice);
|
||||
|
||||
}
|
||||
catch (error) {
|
||||
|
||||
res.status(500).json({
|
||||
message:
|
||||
"Error al aprobar factura",
|
||||
error
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//Rechazar factura
|
||||
|
||||
async reject(
|
||||
req: Request,
|
||||
res: Response
|
||||
) {
|
||||
|
||||
try {
|
||||
|
||||
const invoice =
|
||||
await service.reject(
|
||||
|
||||
req.params.id as string,
|
||||
req.body.approvedBy,
|
||||
req.body.comments
|
||||
|
||||
);
|
||||
|
||||
if (!invoice) {
|
||||
|
||||
return res.status(404).json({
|
||||
message:
|
||||
"Factura no encontrada"
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
res.json(invoice);
|
||||
|
||||
}
|
||||
catch (error) {
|
||||
|
||||
res.status(500).json({
|
||||
message:
|
||||
"Error al rechazar factura",
|
||||
error
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//Buscar por estado
|
||||
|
||||
async getByStatus(
|
||||
|
||||
@ -24,14 +24,6 @@ invoicerouter.post("/", authMiddleware, controller.create);
|
||||
|
||||
invoicerouter.put("/:id", authMiddleware, controller.update);
|
||||
|
||||
//Aprobar factura
|
||||
|
||||
invoicerouter.put("/:id/approve", authMiddleware, controller.approve);
|
||||
|
||||
//Rechazar factura
|
||||
|
||||
invoicerouter.put("/:id/reject", authMiddleware, controller.reject);
|
||||
|
||||
//Buscar por estado
|
||||
|
||||
invoicerouter.get("/status/:status", authMiddleware, controller.getByStatus);
|
||||
|
||||
@ -80,58 +80,6 @@ export class ExternalInvoiceService {
|
||||
|
||||
}
|
||||
|
||||
//Aprobar factura
|
||||
|
||||
async approve(
|
||||
id: string,
|
||||
approvedBy: string
|
||||
) {
|
||||
|
||||
return await ExternalInvoiceModel
|
||||
.findByIdAndUpdate(
|
||||
|
||||
id,
|
||||
|
||||
{
|
||||
status: "APROBADO",
|
||||
approvedBy
|
||||
},
|
||||
|
||||
{
|
||||
new: true
|
||||
}
|
||||
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
//Rechazar factura
|
||||
|
||||
async reject(
|
||||
id: string,
|
||||
approvedBy: string,
|
||||
comments?: string
|
||||
) {
|
||||
|
||||
return await ExternalInvoiceModel
|
||||
.findByIdAndUpdate(
|
||||
|
||||
id,
|
||||
|
||||
{
|
||||
status: "RECHAZADO",
|
||||
approvedBy,
|
||||
comments
|
||||
},
|
||||
|
||||
{
|
||||
new: true
|
||||
}
|
||||
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
// Buscar por estado
|
||||
|
||||
async getByStatus(
|
||||
|
||||
@ -8,41 +8,21 @@ const controller = new FuelRequestController();
|
||||
|
||||
|
||||
//Obtener todas las requisiciones
|
||||
fuelRequestRouter.get(
|
||||
"/",
|
||||
authMiddleware,
|
||||
controller.getAll
|
||||
);
|
||||
fuelRequestRouter.get("/", authMiddleware, controller.getAll);
|
||||
|
||||
|
||||
//Obtener requisición por ID
|
||||
fuelRequestRouter.get(
|
||||
"/:id",
|
||||
authMiddleware,
|
||||
controller.getById
|
||||
);
|
||||
fuelRequestRouter.get("/:id", authMiddleware, controller.getById);
|
||||
|
||||
|
||||
//Crear requisición
|
||||
fuelRequestRouter.post(
|
||||
"/",
|
||||
authMiddleware,
|
||||
controller.create
|
||||
);
|
||||
fuelRequestRouter.post( "/", authMiddleware, controller.create);
|
||||
|
||||
|
||||
//Actualizar requisición
|
||||
fuelRequestRouter.put(
|
||||
"/:id",
|
||||
authMiddleware,
|
||||
controller.update
|
||||
);
|
||||
fuelRequestRouter.put("/:id", authMiddleware, controller.update);
|
||||
|
||||
//Eliminar requisició
|
||||
fuelRequestRouter.delete(
|
||||
"/:id",
|
||||
authMiddleware,
|
||||
controller.delete
|
||||
);
|
||||
fuelRequestRouter.delete("/:id", authMiddleware, controller.delete);
|
||||
|
||||
export default fuelRequestRouter;
|
||||
137
backend/src/modules/lpg/lpg.controller.ts
Normal file
137
backend/src/modules/lpg/lpg.controller.ts
Normal file
@ -0,0 +1,137 @@
|
||||
import { Request, Response} from "express";
|
||||
import { LPGService } from "./lpg.service";
|
||||
|
||||
|
||||
const service = new LPGService();
|
||||
|
||||
export class LPGController{
|
||||
|
||||
async getAll(
|
||||
req:Request,
|
||||
res:Response
|
||||
){
|
||||
|
||||
try {
|
||||
const requests = await service.getAll();
|
||||
res.json(requests);
|
||||
} catch(error) {
|
||||
|
||||
res.status(500).json({
|
||||
message:"Error al obtener requisiciones",
|
||||
error
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async getById(
|
||||
req:Request,
|
||||
res:Response
|
||||
){
|
||||
try {
|
||||
const request =
|
||||
await service.getById(
|
||||
req.params.id as string
|
||||
);
|
||||
|
||||
if(!request) {
|
||||
return res.status(404).json({
|
||||
message:
|
||||
"Requisición no encontrada"
|
||||
});
|
||||
}
|
||||
|
||||
res.json(request);
|
||||
}
|
||||
|
||||
catch (error) {
|
||||
res.status(500).json({
|
||||
message:"Error al obtener requisicion",
|
||||
error
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
//Crear requisición
|
||||
|
||||
async create(
|
||||
req:Request,
|
||||
res:Response
|
||||
){
|
||||
try {
|
||||
const request =
|
||||
await service.create(
|
||||
req.body
|
||||
);
|
||||
|
||||
res.status(201).json(
|
||||
request
|
||||
);
|
||||
|
||||
}
|
||||
catch (error) {
|
||||
res.status(500).json({
|
||||
message:"Error al crear requisición",
|
||||
error
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Actualizar requisición
|
||||
async update(
|
||||
req:Request,
|
||||
res:Response
|
||||
){
|
||||
try {
|
||||
|
||||
const request = await service.update(
|
||||
req.params.id as string,
|
||||
req.body
|
||||
);
|
||||
|
||||
if (!request) {
|
||||
return res.status(404).json({
|
||||
message:
|
||||
"Requisición no encontrada"
|
||||
});
|
||||
}
|
||||
|
||||
res.json(request);
|
||||
}catch(error) {
|
||||
res.status(500).json({
|
||||
message:"Error al actualizar requisición",
|
||||
error
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
//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"
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
res.status(500).json({
|
||||
message: "Error al eliminar requisición",
|
||||
error
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
16
backend/src/modules/lpg/lpg.interface.ts
Normal file
16
backend/src/modules/lpg/lpg.interface.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { ObjectId } from "mongoose";
|
||||
|
||||
export interface ILpg{
|
||||
|
||||
requestNumber:string;
|
||||
plant:string,
|
||||
equipment:string;
|
||||
requestedBy:ObjectId;
|
||||
approvedBy?:ObjectId;
|
||||
requestDate:Date;
|
||||
gallons:number;
|
||||
pricePerGallon:number;
|
||||
totalAmount:number,
|
||||
comments?:string;
|
||||
status:string;
|
||||
}
|
||||
81
backend/src/modules/lpg/lpg.model.ts
Normal file
81
backend/src/modules/lpg/lpg.model.ts
Normal file
@ -0,0 +1,81 @@
|
||||
import mongoose from "mongoose";
|
||||
import { timeStamp } from "node:console";
|
||||
|
||||
const lpgSchema = new mongoose.Schema({
|
||||
|
||||
requestNumber:{
|
||||
type:String,
|
||||
required:true,
|
||||
unique:true,
|
||||
},
|
||||
|
||||
plant:{
|
||||
type:String,
|
||||
requiered:true,
|
||||
enum:[
|
||||
"Planta 1",
|
||||
"Planta 2"
|
||||
]
|
||||
},
|
||||
|
||||
equipment:{
|
||||
type:String,
|
||||
required:true
|
||||
},
|
||||
|
||||
requestedBy:{
|
||||
type:mongoose.Schema.Types.ObjectId,
|
||||
ref:"User",
|
||||
requeride:true
|
||||
},
|
||||
|
||||
approvedBy:{
|
||||
type:mongoose.Schema.Types.ObjectId,
|
||||
ref:"User"
|
||||
},
|
||||
|
||||
requestDate:{
|
||||
type:Date,
|
||||
default:Date.now,
|
||||
requiered:true
|
||||
},
|
||||
|
||||
gallons:{
|
||||
type:Number,
|
||||
required:true,
|
||||
},
|
||||
|
||||
pricePerGallon:{
|
||||
type:Number,
|
||||
default:0
|
||||
},
|
||||
|
||||
totalAmount:{
|
||||
type:Number,
|
||||
default:0
|
||||
},
|
||||
|
||||
comments:{
|
||||
type:String
|
||||
},
|
||||
|
||||
status:{
|
||||
type:String,
|
||||
enum:[
|
||||
"PENDIENTE",
|
||||
"APROBADO",
|
||||
"RECHAZADO"
|
||||
],
|
||||
|
||||
default:"PENDIENTE"
|
||||
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
timestamps:true,
|
||||
versionKey:false
|
||||
}
|
||||
);
|
||||
|
||||
export default mongoose.model("LPG", lpgSchema);
|
||||
27
backend/src/modules/lpg/lpg.routes.ts
Normal file
27
backend/src/modules/lpg/lpg.routes.ts
Normal file
@ -0,0 +1,27 @@
|
||||
import { Router } from "express";
|
||||
import { LPGController } from "./lpg.controller";
|
||||
import { authMiddleware } from "../auth/auth.middleware";
|
||||
|
||||
const lpgRouter = Router();
|
||||
|
||||
const controller = new LPGController();
|
||||
|
||||
|
||||
//Obtener todas las requisiciones
|
||||
lpgRouter.get("/", authMiddleware, controller.getAll);
|
||||
|
||||
//Obtener requisición por ID
|
||||
lpgRouter.get("/:id", authMiddleware, controller.getById);
|
||||
|
||||
|
||||
//Crear requisición
|
||||
lpgRouter.post( "/", authMiddleware, controller.create);
|
||||
|
||||
|
||||
//Actualizar requisición
|
||||
lpgRouter.put("/:id", authMiddleware, controller.update);
|
||||
|
||||
//Eliminar requisició
|
||||
lpgRouter.delete("/:id", authMiddleware, controller.delete);
|
||||
|
||||
export default lpgRouter;
|
||||
87
backend/src/modules/lpg/lpg.service.ts
Normal file
87
backend/src/modules/lpg/lpg.service.ts
Normal file
@ -0,0 +1,87 @@
|
||||
import lpgModel from "./lpg.model";
|
||||
import { ILpg } from "./lpg.interface";
|
||||
import { idText } from "typescript";
|
||||
|
||||
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
|
||||
async getAll() {
|
||||
|
||||
return await lpgModel
|
||||
.find()
|
||||
.populate("requestedBy")
|
||||
.populate("approveBy")
|
||||
}
|
||||
|
||||
//Obtener requisición por Id
|
||||
async getById(
|
||||
id:string
|
||||
){
|
||||
return await lpgModel
|
||||
.findById(id)
|
||||
.populate("requestedBy")
|
||||
.populate("approveBy")
|
||||
}
|
||||
|
||||
async update(
|
||||
id:string,
|
||||
data:Partial<ILpg>
|
||||
){
|
||||
if (data.gallons &&
|
||||
data.pricePerGallon
|
||||
){
|
||||
data.totalAmount =
|
||||
data.gallons *
|
||||
data.pricePerGallon
|
||||
}
|
||||
|
||||
return await lpgModel
|
||||
.findByIdAndUpdate(
|
||||
id,
|
||||
data,
|
||||
{
|
||||
new:true
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
async getByStatus(
|
||||
status:string
|
||||
){
|
||||
return await lpgModel
|
||||
.find({
|
||||
status
|
||||
} as any)
|
||||
}
|
||||
|
||||
async getByPlant(
|
||||
plant:string
|
||||
){
|
||||
return await lpgModel
|
||||
.find({
|
||||
plant
|
||||
}as any)
|
||||
}
|
||||
|
||||
async delete(
|
||||
id:string
|
||||
){
|
||||
return await lpgModel
|
||||
.findByIdAndDelete(id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user