Skip to content

Commit e48dc22

Browse files
committed
2 parents e80d0f5 + c55787b commit e48dc22

File tree

5 files changed

+98
-6
lines changed

5 files changed

+98
-6
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;

sql_queries/insertion_in_forms.sql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ async function insertAllFormData() {
88

99
// 1. Insert into forms
1010
const [formResult] = await connection.execute(
11-
`INSERT INTO forms (
12-
form_type, farmer_name, age, mobile, district, block, panchayat, hamlet,
11+
`INSERT INTO forms (
12+
farmer_name, age, mobile, district, block, panchayat, hamlet,
1313
id_type, id_number, gender, spouse, type_of_households, h_members,
1414
hh_occupation, special_catog, caste, house_owner, type_of_house,
1515
drinking_water, potability, domestic_water, toilet_avail, toilet_cond,
16-
household_education, user_id, created_at, lat, lon, mcode, status
17-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
16+
household_education, user_id, created_at, lat, lon, status, form_type
17+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
1818
[2, 'FARMER_NAME', 'AGE', 9876543210, 'DISTRICT', 'BLOCK', 'PANCHAYAT', 'HAMLET',
1919
'ID_TYPE', 'ID_NUMBER', 'GENDER', 'SPOUSE', 'HOUSEHOLD_TYPE', 5,
2020
'OCCUPATION', 'SPECIAL_CAT', 'CASTE', 'OWNER', 'HOUSE_TYPE',
@@ -47,9 +47,9 @@ async function insertAllFormData() {
4747
`INSERT INTO form_lands (
4848
form_id, ownership, well_irrigation, area_irrigated, irrigated_lands,
4949
patta, total_area, taluk, firka, revenue, crop_season, livestocks,
50-
sf_number, soil_type, land_to_benefit, date_of_ins, area_benefited,
50+
sf_number, soil_type, land_to_benefit, area_benefited,
5151
type_of_work, any_other_works, p_contribution, f_contribution,
52-
total_est, field_insp, site_app, date_of_app, area_benefited_postfunding, verified_by
52+
total_est, field_insp, date_of_app
5353
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
5454
[formId, 'OWNERSHIP', 'WELL_IRR', 'AREA_IRR', 'IRRIG_LAND',
5555
'PATTA', 'TOT_AREA', 'TALUK', 'FIRKA', 'REVENUE', 'CROP_SEASON', 'LIVESTOCK',

0 commit comments

Comments
 (0)