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
16 changes: 8 additions & 8 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 @@ -201,22 +201,22 @@ exports.updateReport = async (req, res) => {
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)) {
if (bin_id) {
updateFields.push('bin_id = ?');
params.push(bin_id);
}

if (issue_type && (req.user.role === 'citizen' && reports[0].user_id === req.user.id)) {
if (issue_type) {
updateFields.push('issue_type = ?');
params.push(issue_type);
}

if (description && (req.user.role === 'citizen' && reports[0].user_id === req.user.id)) {
if (description) {
updateFields.push('description = ?');
params.push(description);
}

if (priority && (req.user.role === 'citizen' && reports[0].user_id === req.user.id)) {
if (priority) {
updateFields.push('priority = ?');
params.push(priority);
}
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
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 !== undefined && phone !== null && phone !== '') {
updates.push('phone = ?');
values.push(phone);
}
if (address !== undefined) {
if (address !== undefined && address !== null && address !== '') {
updates.push('address = ?');
values.push(address);
}
Expand All @@ -117,7 +117,7 @@ exports.updateUser = async (req, res) => {
}

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

await db.query(
`UPDATE users SET ${updates.join(', ')} WHERE id = ?`,
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,15 +24,15 @@ exports.protect = async (req, res, next) => {
[decoded.id]
);

if (users.length === 0) {
if (users && users.length > 0) {
req.user = users[0];
next();
} else {
return res.status(401).json({
success: false,
message: 'User not found'
});
}

req.user = users[0];
next();
} catch (error) {
return res.status(401).json({
success: false,
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.