Skip to content

Commit

Permalink
Merge pull request #48 from Code-the-Dream-School/reportController
Browse files Browse the repository at this point in the history
created controllers for topEmployee and outOfStock
  • Loading branch information
devandres-tech authored Jan 9, 2025
2 parents 815fe65 + 5570f1b commit 8ffb672
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 3 deletions.
98 changes: 98 additions & 0 deletions src/controllers/reportController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
const DispenseLog = require("../models/DispenseLog");
const { StatusCodes } = require("http-status-codes");
const User = require("../models/UserModel");

const topEmployee = async (req, res) => {
console.log("topEmployee controller running");
try {
// Aggregate logs to find the top 3 users with the most dispense entries
const logs = await DispenseLog.aggregate([
{
$group: {
_id: "$userId", // Group by userId
dispenseCount: { $sum: 1 }, // Count logs for each user
},
},
{
$sort: { dispenseCount: -1 }, // Sort by highest dispense count
},
{
$limit: 3, // Limit to top 3
},
]);

if (logs.length === 0) {
return res.status(StatusCodes.NOT_FOUND).json({
success: false,
message: "No dispense logs found.",
});
}

// Fetch user details for the top 3 employees
const topEmployees = await Promise.all(
logs.map(async (log) => {
const user = await User.findById(log._id).select("name store"); // Fetch the user using the userId (_id from the logs)
return {
user,
dispenseCount: log.dispenseCount,
};
}),
);

res.status(StatusCodes.OK).json({
success: true,
topEmployees,
});
} catch (error) {
res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({
success: false,
message: "Error fetching top employees",
});
}
};
const outOfStock = async (req, res) => {
console.log("outOfStock controller running");
try {
// Get the current date and calculate the date 30 days ago
const today = new Date();
const thirtyDaysAgo = new Date(today);
thirtyDaysAgo.setDate(today.getDate() - 30);

// Find all dispense logs in the last 30 days
const logs = await DispenseLog.find({
dispenseDate: { $gte: thirtyDaysAgo, $lte: today },
}).populate({
path: "medicationId",
select: "name quantity location", // Select medication fields
});

// Filter logs for medications with quantity 0 and track unique medicationId
const uniqueMedications = {};
const outOfStockMedications = [];

logs.forEach((log) => {
if (log.medicationId && log.medicationId.quantity === 0) {
const medicationId = log.medicationId._id.toString();
if (!uniqueMedications[medicationId]) {
uniqueMedications[medicationId] = true;
outOfStockMedications.push(log.medicationId);
}
}
});

res.status(StatusCodes.OK).json({
success: true,
medications: outOfStockMedications,
});
} catch (error) {
res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({
success: false,
message: "Error fetching out-of-stock medications",
});
}
};

module.exports = {
topEmployee,
outOfStock,
};
6 changes: 3 additions & 3 deletions src/routes/reportRoutes
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const express = require("express");
const authenticate = require("../middleware/authMiddleware");
const roleMiddleware = require("../middleware/roleMiddleware");

const { getDispenseLogs } = require("../controllers/dispenseController");
const { topEmployee, outOfStock } = require("../controllers/reportController");

const router = express.Router();

Expand All @@ -11,15 +11,15 @@ router.get(
"/topEmployee",
authenticate,
roleMiddleware(["admin", "clerk", "inventoryManager"]),
getDispenseLogs,
topEmployee,
);

// GET /reports/out-of-stock - Get medications with 0 quantity
router.get(
"/outOfStock",
authenticate,
roleMiddleware(["admin", "clerk", "inventoryManager"]),
getDispenseLogs,
outOfStock,
);

module.exports = router;

0 comments on commit 8ffb672

Please sign in to comment.