Skip to content

Commit b9573bf

Browse files
authored
Merge branch 'pradanvirudhunagar:main' into main
2 parents dd558f3 + 1c5fac0 commit b9573bf

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
const mysql = require("mysql2");
2+
const db = require("../configs/mysql_config");
3+
const asyncHandler = require("../middlewares/asyncHandler");
4+
5+
6+
const getTotalFormsStatusCount_sql = `SELECT s.status, COUNT(f.status) AS count
7+
FROM (
8+
SELECT 1 AS status
9+
UNION ALL
10+
SELECT 2
11+
UNION ALL
12+
SELECT 3
13+
UNION ALL
14+
SELECT 4
15+
UNION ALL
16+
SELECT 6
17+
UNION ALL
18+
SELECT 7
19+
UNION ALL
20+
SELECT 8
21+
UNION ALL
22+
SELECT 9
23+
) AS s
24+
LEFT JOIN forms f ON f.status = s.status AND f.user_id = ?
25+
GROUP BY s.status
26+
ORDER BY s.status`;
27+
28+
exports.getTotalFormsStatusCount = asyncHandler(async (req, res) => {
29+
const userId = req.params.userId;
30+
//console.log("userId (status count total):", userId);
31+
db.query(getTotalFormsStatusCount_sql, [userId], (err, results) => {
32+
if (err) {
33+
return res.status(500).json({ error: err.message });
34+
}
35+
//console.log("results:", results);
36+
if (results.length > 0) {
37+
return res.json(results);
38+
} else {
39+
return res.json(0);
40+
}
41+
});
42+
});
43+
44+
const getTodayFormsStatusCount_sql = `SELECT s.status, COUNT(f.status) AS count
45+
FROM (
46+
SELECT 1 AS status UNION ALL
47+
SELECT 2 UNION ALL
48+
SELECT 3 UNION ALL
49+
SELECT 4 UNION ALL
50+
SELECT 6 UNION ALL
51+
SELECT 7 UNION ALL
52+
SELECT 8 UNION ALL
53+
SELECT 9
54+
) AS s
55+
LEFT JOIN forms f ON f.status = s.status AND f.user_id = ? AND f.created_at = ?
56+
GROUP BY s.status
57+
ORDER BY s.status`;
58+
59+
60+
exports.getTodayFormsStatusCount = asyncHandler(async (req, res) => {
61+
const userId = req.params.userId;
62+
const date = new Date();
63+
const today = new Date(date.getFullYear(), date.getMonth(), date.getDate()).toLocaleDateString('en-CA');
64+
//const today = '17-04-2025'
65+
// console.log("today:", today);
66+
// console.log("userId (status count today):", userId);
67+
db.query(getTodayFormsStatusCount_sql, [userId,today], (err, results) => {
68+
if (err) {
69+
return res.status(500).json({ error: err.message });
70+
}
71+
//console.log("results (today):", results);
72+
if (results.length > 0) {
73+
return res.json(results);
74+
} else {
75+
return res.json(0);
76+
}
77+
});
78+
});

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ app.use(express.json());
1313
const userRoutes = require('./routes/users.route');
1414
app.use('/api/users', userRoutes);
1515

16+
const dashboardRoutes = require('./routes/dashboardData.route');
17+
app.use('/api/dashboard', dashboardRoutes);
18+
1619
app.get("/", (req, res) => {
1720
res.send("✅ Backend is running and ready!");
1821
});

readme

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ configs/
99
|__mysql_config.js
1010
routes/
1111
|__users.routes.js
12+
|__dashboardData.route.js
1213
controllers/
1314
|__users.controller.js
1415
|__userData.controller.js
16+
|__dashboardData.controller.js
1517
middlewares/
1618
|__asyncHandler.js
1719

routes/dashboardData.route.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const express = require("express");
2+
const router = express.Router();
3+
const { getTotalFormsStatusCount, getTodayFormsStatusCount } = require("../controllers/dashboardData.controller");
4+
5+
6+
router.route('/getTotalFormsStatusCount/:userId').get(getTotalFormsStatusCount);
7+
router.route('/getTodayFormsStatusCount/:userId').get(getTodayFormsStatusCount);
8+
9+
module.exports = router;

0 commit comments

Comments
 (0)