feat(dashboard): agregar service, controller y routes
This commit is contained in:
@ -15,6 +15,7 @@ import invoicerouter from "./modules/external-invoices/externalInvoice.routes";
|
|||||||
import lpgRouter from "./modules/lpg/lpg.routes";
|
import lpgRouter from "./modules/lpg/lpg.routes";
|
||||||
import anomalyRouter from "./modules/anomalies/anomaly.routes";
|
import anomalyRouter from "./modules/anomalies/anomaly.routes";
|
||||||
import pumpReadingRouter from "./modules/pump-reading/pumpReading.routes";
|
import pumpReadingRouter from "./modules/pump-reading/pumpReading.routes";
|
||||||
|
import dashboardRouter from "./modules/dashboard/dashboard.routes";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -38,7 +39,7 @@ app.use("/api/exterinvoice", invoicerouter);
|
|||||||
app.use("/api/lpg",lpgRouter);
|
app.use("/api/lpg",lpgRouter);
|
||||||
app.use("/api/anomaly", anomalyRouter);
|
app.use("/api/anomaly", anomalyRouter);
|
||||||
app.use("/api/pumpReading", pumpReadingRouter);
|
app.use("/api/pumpReading", pumpReadingRouter);
|
||||||
|
app.use("/api/dashboard", dashboardRouter);
|
||||||
|
|
||||||
app.use(express.urlencoded({
|
app.use(express.urlencoded({
|
||||||
extended:true
|
extended:true
|
||||||
|
|||||||
25
backend/src/modules/dashboard/dashboard.controller.ts
Normal file
25
backend/src/modules/dashboard/dashboard.controller.ts
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import { Request, Response } from "express";
|
||||||
|
import { DashboardService } from "./dashboard.service";
|
||||||
|
|
||||||
|
const service = new DashboardService();
|
||||||
|
|
||||||
|
export class DashboardController {
|
||||||
|
|
||||||
|
async getDashboard(
|
||||||
|
req:Request,
|
||||||
|
res:Response
|
||||||
|
){
|
||||||
|
try{
|
||||||
|
|
||||||
|
const dashboard = await service.getDashboard();
|
||||||
|
res.status(200).json(dashboard);
|
||||||
|
|
||||||
|
}catch(error){
|
||||||
|
res.status(500).json({
|
||||||
|
message:"Error al obtener información del Dashboard",
|
||||||
|
error
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
11
backend/src/modules/dashboard/dashboard.routes.ts
Normal file
11
backend/src/modules/dashboard/dashboard.routes.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { Router } from "express";
|
||||||
|
import { DashboardController } from "./dashboard.controller";
|
||||||
|
import { authMiddleware } from "../auth/auth.middleware";
|
||||||
|
|
||||||
|
const dashboardRouter = Router();
|
||||||
|
const controller = new DashboardController();
|
||||||
|
|
||||||
|
|
||||||
|
dashboardRouter.get("/dashboard", authMiddleware, controller.getDashboard);
|
||||||
|
|
||||||
|
export default dashboardRouter;
|
||||||
97
backend/src/modules/dashboard/dashboard.service.ts
Normal file
97
backend/src/modules/dashboard/dashboard.service.ts
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
import fuelRequestModel from "../fuel-requests/fuelRequest.model";
|
||||||
|
import externalInvoiceModel from "../external-invoices/externalInvoice.model";
|
||||||
|
import lpgModel from "../lpg/lpg.model";
|
||||||
|
import approvalModel from "../approvals/approval.model";
|
||||||
|
import anomalyModel from "../anomalies/anomaly.model";
|
||||||
|
import pumpReadingModel from "../pump-reading/pumpReading.model";
|
||||||
|
|
||||||
|
export class DashboardService {
|
||||||
|
|
||||||
|
async getDashboard(){
|
||||||
|
|
||||||
|
//Total de requisiciones
|
||||||
|
const fuelRequests = await fuelRequestModel.countDocuments();
|
||||||
|
|
||||||
|
//Requisiciones pendientes
|
||||||
|
const pendingRequests = await fuelRequestModel.countDocuments({
|
||||||
|
|
||||||
|
status:"PENDIENTE"
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
const approvedRequests = await fuelRequestModel.countDocuments({
|
||||||
|
|
||||||
|
status:"APROBADO"
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
const rejectedRequests = await fuelRequestModel.countDocuments({
|
||||||
|
|
||||||
|
status:"RECHAZADO"
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
//Total de facturas externas
|
||||||
|
const externalInvoices = await externalInvoiceModel.countDocuments();
|
||||||
|
|
||||||
|
//Total de requisiciones LPG
|
||||||
|
const lpgRequests = await lpgModel.countDocuments();
|
||||||
|
|
||||||
|
//Total de galones solicitados
|
||||||
|
const pendingApprovals = await approvalModel.countDocuments({
|
||||||
|
status:"PENDIENTE"
|
||||||
|
});
|
||||||
|
|
||||||
|
//Total de anomalías abiertas
|
||||||
|
const openAnomalies = await anomalyModel.countDocuments({
|
||||||
|
status:"ABIERTA"
|
||||||
|
})
|
||||||
|
|
||||||
|
//Tootal de lecturas de bomba
|
||||||
|
const pumpReadings = await pumpReadingModel.countDocuments();
|
||||||
|
|
||||||
|
const gallons = await fuelRequestModel.aggregate([
|
||||||
|
{
|
||||||
|
$group:{
|
||||||
|
_id:null,
|
||||||
|
totalGallons:{
|
||||||
|
$sum: "$gallonQuantity"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
|
||||||
|
//Total invertido
|
||||||
|
const totalCost = await fuelRequestModel.aggregate([
|
||||||
|
{
|
||||||
|
$group:{
|
||||||
|
_id:null,
|
||||||
|
totalAmount:{
|
||||||
|
$sum: "$totalAmount"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
|
||||||
|
return{
|
||||||
|
fuelRequests,
|
||||||
|
externalInvoices,
|
||||||
|
lpgRequests,
|
||||||
|
pendingApprovals,
|
||||||
|
openAnomalies,
|
||||||
|
pumpReadings,
|
||||||
|
|
||||||
|
totalGallons:
|
||||||
|
gallons.length > 0
|
||||||
|
? gallons[0].totalGallons
|
||||||
|
:0,
|
||||||
|
|
||||||
|
|
||||||
|
totalCost:
|
||||||
|
totalCost.length > 0
|
||||||
|
? totalCost[0].totalAmount
|
||||||
|
:0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user