Skip to content
This repository was archived by the owner on Sep 20, 2025. It is now read-only.

Commit 95a13c7

Browse files
committed
Calculate percentage for shipments stat on dashboard screen
1 parent 1b17041 commit 95a13c7

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

grocery-store-management-system.postman_collection.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@
466466
"response": []
467467
},
468468
{
469-
"name": "Ongoing Shipments Count",
469+
"name": "Ongoing Shipments Percentage",
470470
"request": {
471471
"auth": {
472472
"type": "bearer",

src/models/v1/shipping.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ module.exports = (sequelize, DataTypes) => {
1616
/**
1717
* @returns {object|false}
1818
*/
19-
static async getAdminOngoingShipmentsCountStat() {
19+
static async getAdminOngoingShipmentsPercentageStat() {
2020
try {
21-
const result = await sequelize.query(
21+
const part = await sequelize.query(
2222
`SELECT count(id) as count
2323
FROM ${this.getTableName()}
2424
WHERE status IS NULL`,
@@ -27,7 +27,20 @@ module.exports = (sequelize, DataTypes) => {
2727
},
2828
);
2929

30-
return { count: result[0].count };
30+
const whole = await sequelize.query(
31+
`SELECT count(id) as count
32+
FROM ${this.getTableName()}`,
33+
{
34+
type: sequelize.QueryTypes.SELECT,
35+
},
36+
);
37+
38+
if (0 === whole[0].count) {
39+
return { percentage: 0, };
40+
}
41+
42+
const result = (part[0].count / whole[0].count) * 100;
43+
return { percentage: result };
3144
} catch(err) {
3245
if ("production" !== nodeEnv) {
3346
console.log(err);

src/routes/api/v1/web/stats/admin/shipments/ongoing/ongoing.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ const { message500 } = require("../../../../../../../../utils/httpResponses");
77
const router = express.Router();
88

99
router.get("/", adminAuthenticate, async (req, res) => {
10-
const result = await db.sequelize.models.shipping.getAdminOngoingShipmentsCountStat();
10+
const result = await db.sequelize.models.shipping.getAdminOngoingShipmentsPercentageStat();
1111
if (false === result) {
1212
res.status(status.UNAUTHORIZED);
1313
return res.json(message500);
1414
}
1515

1616
res.status(status.OK);
17-
return res.json({ data: result.count });
17+
return res.json({ data: result.percentage });
1818
});
1919

2020
module.exports = router;

0 commit comments

Comments
 (0)