Skip to content

Commit

Permalink
Merge pull request #80 from atlp-rwanda/feat-admin-stat
Browse files Browse the repository at this point in the history
feat(admin):admin can view statistics
  • Loading branch information
leandreAlly authored Jul 25, 2024
2 parents b856aa5 + c3c8926 commit 1d98af8
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/controllers/adminStatistics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Request, Response } from "express";
import { sendResponse } from "../utils/http.exception";
import { User } from "../database/models/User";
import { Role } from "../database/models/role";
import { Sales } from "../database/models/sales";
import { Product } from "../database/models/product";
import { Order } from "../database/models/order";

export const adminStatistics = async (req: Request, res: Response) => {
try {
const SellerID = await Role.findOne({ where: { roleName: "SELLER" } });
const buyerID = await Role.findOne({ where: { roleName: "BUYER" } });
const allSellers = await User.findAndCountAll({
where: { role: SellerID?.dataValues.id },
});
const allBuyers = await User.findAndCountAll({
where: { role: buyerID?.dataValues.id },
});
const activeBuyers = allBuyers.rows.filter((item) => item.isActive);
const activeSellers = allSellers.rows.filter((item) => item.isActive);
const allOrders = await Order.findAndCountAll({
include: {
model: Sales,
as: "sales",
attributes: {
exclude: ["createdAt", "updatedAt", "orderId", "buyerId"],
},
include: [
{
model: Product,
as: "soldProducts",
attributes: ["name", "images", "price"],
},
],
},
});
const completedOrder = allOrders.rows.filter(
(item) => item.status === "delivered",
);
const completedOrderPercent =
(completedOrder.length * 100) / allOrders.rows.length;
const data = {
Numbersofsellers: allSellers,
NumberofBuyer: allBuyers,
NumberofOrders: allOrders,
activeBuyers: activeBuyers.length,
activeSellers: activeSellers.length,
completedOrderPercent,
completedOrder,
};
return sendResponse(res, 200, "SUCCESS", "Here is your statistics", data);
} catch (error) {
return sendResponse(
res,
500,
"SERVER ERROR",
"Internal server error",
(error as Error).message,
);
}
};
3 changes: 3 additions & 0 deletions src/documention/stats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ export const stats = {
"/api/v1/stats": {
get: read_statistics,
},
"/api/v1/admin/stats": {
get: read_statistics,
},
};
13 changes: 13 additions & 0 deletions src/routes/adminStatsRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import express from "express";

import authenticate from "../middlewares/auth";
import { adminStatistics } from "../controllers/adminStatistics";

export const AdminstatisticsRouter = express.Router();

AdminstatisticsRouter.get(
"/admin/stats",
authenticate.authenticateUser,
authenticate.isAdmin,
adminStatistics,
);
2 changes: 2 additions & 0 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { statisticsRouter } from "./statisticsRoutes";

import reviewRouter from "./review.routes";
import notificationRouter from "./notificationsRoutes";
import { AdminstatisticsRouter } from "./adminStatsRoute";
const router = express.Router();

router.use("/users", userRoutes);
Expand All @@ -30,5 +31,6 @@ router.use("/reviews", reviewRouter);
router.use("/orders", orderRoutes);
router.use("/sales", salesRoutes);
router.use("/notifications", notificationRouter);
router.use("/", AdminstatisticsRouter);

export default router;

0 comments on commit 1d98af8

Please sign in to comment.