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
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
10 changes: 5 additions & 5 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 @@ -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.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 +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 completedAt = status === 'completed' ? 'NOW()' : 'completed_at';

await db.query(
`UPDATE schedules
Expand All @@ -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.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
7 changes: 3 additions & 4 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 !== undefined && phone !== null) {
updates.push('phone = ?');
values.push(phone);
}
if (address !== undefined) {
if (address !== undefined && address !== null) {
updates.push('address = ?');
values.push(address);
}
Expand All @@ -117,11 +117,10 @@ exports.updateUser = async (req, res) => {
}

updates.push('updated_at = CURRENT_TIMESTAMP');
values.push(userId);

await db.query(
`UPDATE users SET ${updates.join(', ')} WHERE id = ?`,
values
[...values, userId]
);

// Get updated user
Expand Down
2 changes: 1 addition & 1 deletion 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 && existing.length > 0) {
console.log('✅ Citizen user already exists!');
process.exit(0);
}
Expand Down
10 changes: 5 additions & 5 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 || 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 @@ -49,12 +49,12 @@ exports.protect = async (req, res, next) => {

exports.authorize = (...roles) => {
return (req, res, next) => {
if (!roles.includes(req.user.role)) {
if (!req.user || !roles.includes(req.user.role)) {
return res.status(403).json({
success: false,
message: `User role '${req.user.role}' is not authorized to access this route`
message: `User role '${req.user?.role}' is not authorized to access this route`
});
}
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.