diff --git a/backend/checkCitizen.js b/backend/checkCitizen.js index bbb9f6b..5d4f234 100644 --- a/backend/checkCitizen.js +++ b/backend/checkCitizen.js @@ -11,7 +11,7 @@ async function checkCitizen() { ['citizen@smartwaste.com'] ); - if (users.length === 0) { + if (!users || users.length === 0) { console.log('❌ Citizen user not found!'); process.exit(1); } @@ -43,9 +43,6 @@ async function checkCitizen() { } console.log('\n✅ Citizen account is ready!'); - console.log('Email: citizen@smartwaste.com'); - console.log('Password: citizen123'); - process.exit(0); } catch (error) { console.error('❌ Error:', error); diff --git a/backend/config/database.js b/backend/config/database.js index 78454c4..29b3097 100644 --- a/backend/config/database.js +++ b/backend/config/database.js @@ -16,7 +16,7 @@ const pool = mysql.createPool({ pool.getConnection((err, connection) => { if (err) { console.error('❌ Database connection failed:', err.message); - return; + throw err; } console.log('✅ Database connected successfully'); connection.release(); @@ -24,4 +24,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/binController.js b/backend/controller/binController.js index 90df823..c2c5fbb 100644 --- a/backend/controller/binController.js +++ b/backend/controller/binController.js @@ -84,7 +84,7 @@ exports.createBin = async (req, res) => { const [newBin] = await db.query( 'SELECT * FROM bins WHERE id = ?', - [result.insertId] + [req.body.id] ); res.status(201).json({ diff --git a/backend/controller/dashboardController.js b/backend/controller/dashboardController.js index a50a2dc..5fa1c14 100644 --- a/backend/controller/dashboardController.js +++ b/backend/controller/dashboardController.js @@ -22,7 +22,7 @@ exports.getDashboardStats = async (req, res) => { 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 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..3c109f1 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.id AS bin_id, 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.id AS bin_id, 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.id AS bin_id, 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 @@ -200,23 +200,20 @@ exports.updateReport = async (req, res) => { const updateFields = []; const params = []; - // Citizens can edit basic fields if it's their report - if (bin_id && (req.user.role === 'citizen' && reports[0].user_id === req.user.id)) { - updateFields.push('bin_id = ?'); - params.push(bin_id); - } + const isCitizenUpdating = req.user.role === 'citizen' && reports[0].user_id === req.user.id; - if (issue_type && (req.user.role === 'citizen' && reports[0].user_id === req.user.id)) { + // Citizens can edit basic fields if it's their report + if (issue_type && isCitizenUpdating) { updateFields.push('issue_type = ?'); params.push(issue_type); } - if (description && (req.user.role === 'citizen' && reports[0].user_id === req.user.id)) { + if (description && isCitizenUpdating) { updateFields.push('description = ?'); params.push(description); } - if (priority && (req.user.role === 'citizen' && reports[0].user_id === req.user.id)) { + if (priority && isCitizenUpdating) { updateFields.push('priority = ?'); params.push(priority); } @@ -250,7 +247,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.id AS bin_id, 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..7099328 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.id AS bin_id, 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.id AS bin_id, 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 @@ -107,7 +107,7 @@ exports.createSchedule = async (req, res) => { ); 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.id AS bin_id, 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 +146,7 @@ exports.updateSchedule = async (req, res) => { } // If status is being set to completed, update completed_at - const completedAt = status === 'completed' ? 'NOW()' : 'completed_at'; + // const completed_at = status === 'completed' ? 'NOW()' : 'completed_at'; await db.query( `UPDATE schedules @@ -161,7 +161,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.id AS bin_id, 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..c06440e 100644 --- a/backend/controller/userController.js +++ b/backend/controller/userController.js @@ -117,6 +117,8 @@ exports.updateUser = async (req, res) => { } updates.push('updated_at = CURRENT_TIMESTAMP'); + values.push(new Date().toISOString().slice(0, 19).replace('T', ' ')); + values.push(userId); await db.query( diff --git a/backend/createCitizen.js b/backend/createCitizen.js index 9b37210..41d7cfc 100644 --- a/backend/createCitizen.js +++ b/backend/createCitizen.js @@ -3,7 +3,7 @@ const db = require('./config/database'); async function createCitizen() { try { - console.log('🔧 Creating citizen user...\n'); + // console.log('🔧 Creating citizen user...\n'); // Check if citizen already exists const [existing] = await db.query( @@ -11,8 +11,8 @@ async function createCitizen() { ['citizen@smartwaste.com'] ); - if (existing.length > 0) { - console.log('✅ Citizen user already exists!'); + if (existing) { + // console.log('✅ Citizen user already exists!'); process.exit(0); } @@ -26,15 +26,14 @@ async function createCitizen() { ['Jane Citizen', 'citizen@smartwaste.com', password, '+0987654321', '123 Main Street', 'citizen'] ); - console.log('✅ Citizen user created successfully!'); - console.log('\nLogin credentials:'); - console.log('Email: citizen@smartwaste.com'); - console.log('Password: citizen123'); - console.log('Role: citizen'); + // console.log('✅ Citizen user created successfully!'); + // console.log('\nLogin credentials:'); + // console.log('Email: citizen@smartwaste.com'); + // console.log('Role: citizen'); process.exit(0); } catch (error) { - console.error('❌ Error:', error); + // console.error('❌ Error:', error); process.exit(1); } } diff --git a/backend/listUsers.js b/backend/listUsers.js index d757ce9..73ae931 100644 --- a/backend/listUsers.js +++ b/backend/listUsers.js @@ -2,24 +2,24 @@ const db = require('./config/database'); async function listUsers() { try { - console.log('📋 Current users in database:\n'); + // console.log('📋 Current users in database:\n'); const [users] = await db.query( 'SELECT id, name, email, role FROM users ORDER BY id' ); if (users.length === 0) { - console.log('❌ No users found!'); + // console.log('❌ No users found!'); } else { users.forEach(user => { - console.log(`${user.id}. ${user.name} (${user.email}) - ${user.role}`); + // console.log(`${user.id}. ${user.name} (${user.email}) - ${user.role}`); }); } - console.log(`\nTotal users: ${users.length}`); + // console.log(`\nTotal users: ${users.length}`); process.exit(0); } catch (error) { - console.error('❌ Error:', error); + // console.error('❌ Error:', error); process.exit(1); } } diff --git a/package-lock.json b/package-lock.json index 8292baa..194da0a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "SmartWasteManagementSystem", + "name": "8a0cc19a-af17-42c5-b86d-7e300319768f", "lockfileVersion": 3, "requires": true, "packages": {}