Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions backend/checkCitizen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions backend/config/database.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const mysql = require('mysql2');
require('dotenv').config();

const pool = mysql.createPool({
host: process.env.DB_HOST,
Expand All @@ -24,4 +23,4 @@ pool.getConnection((err, connection) => {

const promisePool = pool.promise();

module.exports = promisePool;
module.exports = promisePool;
4 changes: 2 additions & 2 deletions backend/controller/dashboardController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions backend/controller/reportController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
19 changes: 13 additions & 6 deletions backend/controller/scheduleController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions backend/controller/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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 = ?`,
Expand Down
3 changes: 1 addition & 2 deletions backend/createCitizen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions backend/middleware/authmiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -57,4 +57,4 @@ exports.authorize = (...roles) => {
}
next();
};
};
};
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.