Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(admin):admin can view statistics #80

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

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;