diff --git a/backend/checkCitizen.js b/backend/checkCitizen.js index bbb9f6b..2c32cee 100644 --- a/backend/checkCitizen.js +++ b/backend/checkCitizen.js @@ -16,6 +16,11 @@ async function checkCitizen() { process.exit(1); } + if (!users[0]) { + console.log('❌ Citizen user not found!'); + process.exit(1); + } + const user = users[0]; console.log('✅ Found user:', { id: user.id, diff --git a/backend/config/database.js b/backend/config/database.js index 78454c4..f9af1de 100644 --- a/backend/config/database.js +++ b/backend/config/database.js @@ -1,5 +1,4 @@ const mysql = require('mysql2'); -require('dotenv').config(); const pool = mysql.createPool({ host: process.env.DB_HOST, @@ -24,4 +23,4 @@ pool.getConnection((err, connection) => { const promisePool = pool.promise(); -module.exports = promisePool; \ No newline at end of file +module.exports = promisePool; diff --git a/backend/controller/dashboardController.js b/backend/controller/dashboardController.js index a50a2dc..29842b0 100644 --- a/backend/controller/dashboardController.js +++ b/backend/controller/dashboardController.js @@ -20,9 +20,9 @@ exports.getDashboardStats = async (req, res) => { SELECT COUNT(*) as total_schedules, SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as pending_schedules, - SUM(CASE WHEN status = 'completed' AND DATE(completed_at) = CURDATE() THEN 1 ELSE 0 END) as completed_today + SUM(CASE WHEN status = 'completed' THEN 1 ELSE 0 END) as completed_today FROM collection_schedules - WHERE scheduled_date >= CURDATE() + WHERE scheduled_date = CURDATE() `); // Report statistics diff --git a/backend/controller/reportController.js b/backend/controller/reportController.js index 5c80787..d413dad 100644 --- a/backend/controller/reportController.js +++ b/backend/controller/reportController.js @@ -9,7 +9,7 @@ exports.getAllReports = async (req, res) => { let query = ` SELECT r.*, - b.id, b.location as bin_location, + b.location as bin_location, u.name as reporter_name FROM reports r LEFT JOIN bins b ON r.bin_id = b.id @@ -64,7 +64,7 @@ exports.getReport = async (req, res) => { try { const [reports] = await db.query( `SELECT r.*, - b.id, b.location as bin_location, + b.location as bin_location, u.name as reporter_name, u.email as reporter_email, u.phone as reporter_phone FROM reports r LEFT JOIN bins b ON r.bin_id = b.id @@ -129,7 +129,7 @@ exports.createReport = async (req, res) => { ); const [newReport] = await db.query( - `SELECT r.*, b.id, b.location as bin_location, u.name as reporter_name + `SELECT r.*, b.location as bin_location, u.name as reporter_name FROM reports r LEFT JOIN bins b ON r.bin_id = b.id LEFT JOIN users u ON r.user_id = u.id @@ -250,7 +250,7 @@ exports.updateReport = async (req, res) => { } const [updatedReport] = await db.query( - `SELECT r.*, b.id, b.location as bin_location, u.name as reporter_name + `SELECT r.*, b.location as bin_location, u.name as reporter_name FROM reports r LEFT JOIN bins b ON r.bin_id = b.id LEFT JOIN users u ON r.user_id = u.id diff --git a/backend/controller/scheduleController.js b/backend/controller/scheduleController.js index 32de4de..0c036b1 100644 --- a/backend/controller/scheduleController.js +++ b/backend/controller/scheduleController.js @@ -9,7 +9,7 @@ exports.getAllSchedules = async (req, res) => { let query = ` SELECT s.*, - b.id as bin_id_ref, b.location as bin_location, b.status as bin_status, b.fill_level, + b.location as bin_location, b.status as bin_status, b.fill_level, u.name as collector_name FROM schedules s LEFT JOIN bins b ON s.bin_id = b.id @@ -64,7 +64,7 @@ exports.getSchedule = async (req, res) => { try { const [schedules] = await db.query( `SELECT s.*, - b.id as bin_id_ref, b.location as bin_location, b.latitude, b.longitude, b.status as bin_status, b.fill_level, + b.location as bin_location, b.latitude, b.longitude, b.status as bin_status, b.fill_level, u.name as collector_name, u.phone as collector_phone FROM schedules s LEFT JOIN bins b ON s.bin_id = b.id @@ -100,14 +100,21 @@ exports.createSchedule = async (req, res) => { try { const { bin_id, collector_id, scheduled_date, scheduled_time, route, status } = req.body; + if (!collector_id) { + return res.status(400).json({ + success: false, + message: 'Collector ID is required' + }); + } + const [result] = await db.query( `INSERT INTO schedules (bin_id, collector_id, scheduled_date, scheduled_time, route, status) VALUES (?, ?, ?, ?, ?, ?)`, - [bin_id, collector_id || null, scheduled_date, scheduled_time || '09:00:00', route || null, status || 'pending'] + [bin_id, collector_id, scheduled_date, scheduled_time || '09:00:00', route || null, status || 'pending'] ); const [newSchedule] = await db.query( - `SELECT s.*, b.id as bin_id_ref, b.location as bin_location, u.name as collector_name + `SELECT s.*, b.location as bin_location, u.name as collector_name FROM schedules s LEFT JOIN bins b ON s.bin_id = b.id LEFT JOIN users u ON s.collector_id = u.id @@ -146,7 +153,7 @@ exports.updateSchedule = async (req, res) => { } // If status is being set to completed, update completed_at - const completedAt = status === 'completed' ? 'NOW()' : 'completed_at'; + // status === 'completed' ? 'NOW()' : 'completed_at'; await db.query( `UPDATE schedules @@ -161,7 +168,7 @@ exports.updateSchedule = async (req, res) => { ); const [updatedSchedule] = await db.query( - `SELECT s.*, b.id as bin_id_ref, b.location as bin_location, u.name as collector_name + `SELECT s.*, b.location as bin_location, u.name as collector_name FROM schedules s LEFT JOIN bins b ON s.bin_id = b.id LEFT JOIN users u ON s.collector_id = u.id diff --git a/backend/controller/userController.js b/backend/controller/userController.js index c6b33dc..47f2928 100644 --- a/backend/controller/userController.js +++ b/backend/controller/userController.js @@ -95,11 +95,11 @@ exports.updateUser = async (req, res) => { updates.push('email = ?'); values.push(email); } - if (phone !== undefined) { + if (phone != null && phone !== undefined) { updates.push('phone = ?'); values.push(phone); } - if (address !== undefined) { + if (address != null && address !== undefined) { updates.push('address = ?'); values.push(address); } @@ -116,8 +116,8 @@ exports.updateUser = async (req, res) => { }); } - updates.push('updated_at = CURRENT_TIMESTAMP'); values.push(userId); + updates.push('updated_at = CURRENT_TIMESTAMP'); await db.query( `UPDATE users SET ${updates.join(', ')} WHERE id = ?`, diff --git a/backend/createCitizen.js b/backend/createCitizen.js index 9b37210..ebfac6e 100644 --- a/backend/createCitizen.js +++ b/backend/createCitizen.js @@ -11,7 +11,7 @@ async function createCitizen() { ['citizen@smartwaste.com'] ); - if (existing.length > 0) { + if (existing[0].length > 0) { console.log('✅ Citizen user already exists!'); process.exit(0); } @@ -29,7 +29,6 @@ async function createCitizen() { console.log('✅ Citizen user created successfully!'); console.log('\nLogin credentials:'); console.log('Email: citizen@smartwaste.com'); - console.log('Password: citizen123'); console.log('Role: citizen'); process.exit(0); diff --git a/backend/middleware/authmiddleware.js b/backend/middleware/authmiddleware.js index 55c9990..bb110c4 100644 --- a/backend/middleware/authmiddleware.js +++ b/backend/middleware/authmiddleware.js @@ -24,14 +24,14 @@ exports.protect = async (req, res, next) => { [decoded.id] ); - if (users.length === 0) { + if (users.length === 0 || users[0].length === 0) { return res.status(401).json({ success: false, message: 'User not found' }); } - req.user = users[0]; + req.user = users[0][0]; next(); } catch (error) { return res.status(401).json({ @@ -57,4 +57,4 @@ exports.authorize = (...roles) => { } next(); }; -}; \ No newline at end of file +}; diff --git a/package-lock.json b/package-lock.json index 8292baa..40a284e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "SmartWasteManagementSystem", + "name": "7713bbca-9acb-43fd-bc79-5803283af34e", "lockfileVersion": 3, "requires": true, "packages": {}