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: 1 addition & 4 deletions backend/checkCitizen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions backend/config/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ 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();
});

const promisePool = pool.promise();

module.exports = promisePool;
module.exports = promisePool;
2 changes: 1 addition & 1 deletion backend/controller/binController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion backend/controller/dashboardController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 9 additions & 12 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.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
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.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
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.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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
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.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
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.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
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.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
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 completed_at = 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.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
Expand Down
2 changes: 2 additions & 0 deletions backend/controller/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
17 changes: 8 additions & 9 deletions backend/createCitizen.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ 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(
'SELECT id FROM users WHERE email = ?',
['citizen@smartwaste.com']
);

if (existing.length > 0) {
console.log('βœ… Citizen user already exists!');
if (existing) {
// console.log('βœ… Citizen user already exists!');
process.exit(0);
}

Expand All @@ -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);
}
}
Expand Down
10 changes: 5 additions & 5 deletions backend/listUsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
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.