From 3c566e74fa3fe729920e0eb91eae456a61ed00d4 Mon Sep 17 00:00:00 2001 From: karol Date: Wed, 1 Jul 2026 16:49:38 +0000 Subject: [PATCH] feat(dashboard): agregar service, controller y routes --- backend/src/app.ts | 3 +- .../modules/dashboard/dashboard.controller.ts | 25 +++++ .../src/modules/dashboard/dashboard.routes.ts | 11 +++ .../modules/dashboard/dashboard.service.ts | 97 +++++++++++++++++++ 4 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 backend/src/modules/dashboard/dashboard.controller.ts create mode 100644 backend/src/modules/dashboard/dashboard.routes.ts create mode 100644 backend/src/modules/dashboard/dashboard.service.ts diff --git a/backend/src/app.ts b/backend/src/app.ts index 2cb5765..6868769 100644 --- a/backend/src/app.ts +++ b/backend/src/app.ts @@ -15,6 +15,7 @@ import invoicerouter from "./modules/external-invoices/externalInvoice.routes"; import lpgRouter from "./modules/lpg/lpg.routes"; import anomalyRouter from "./modules/anomalies/anomaly.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/anomaly", anomalyRouter); app.use("/api/pumpReading", pumpReadingRouter); - +app.use("/api/dashboard", dashboardRouter); app.use(express.urlencoded({ extended:true diff --git a/backend/src/modules/dashboard/dashboard.controller.ts b/backend/src/modules/dashboard/dashboard.controller.ts new file mode 100644 index 0000000..7ce17d7 --- /dev/null +++ b/backend/src/modules/dashboard/dashboard.controller.ts @@ -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 + }); + } + } + +} \ No newline at end of file diff --git a/backend/src/modules/dashboard/dashboard.routes.ts b/backend/src/modules/dashboard/dashboard.routes.ts new file mode 100644 index 0000000..c678316 --- /dev/null +++ b/backend/src/modules/dashboard/dashboard.routes.ts @@ -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; \ No newline at end of file diff --git a/backend/src/modules/dashboard/dashboard.service.ts b/backend/src/modules/dashboard/dashboard.service.ts new file mode 100644 index 0000000..7896a8a --- /dev/null +++ b/backend/src/modules/dashboard/dashboard.service.ts @@ -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 + } + } +} \ No newline at end of file