28 lines
895 B
JavaScript
28 lines
895 B
JavaScript
const Report = require('../models/Report');
|
|
const User = require('../models/User');
|
|
|
|
exports.getDashboardUsers = async (req, res) => {
|
|
try {
|
|
// Fetch all users
|
|
const users = await User.find().sort({ createdAt: -1 });
|
|
|
|
// We also need the latest report link for each user
|
|
// In a real scenario, this could be optimized with an aggregation pipeline
|
|
const dashboardData = await Promise.all(users.map(async (user) => {
|
|
const latestReport = await Report.findOne({ phone: user.phone }).sort({ createdAt: -1 });
|
|
|
|
return {
|
|
id: user._id,
|
|
name: user.name,
|
|
phone: user.phone,
|
|
lastLink: latestReport ? latestReport.link : null,
|
|
};
|
|
}));
|
|
|
|
res.json(dashboardData);
|
|
} catch (error) {
|
|
console.error('Error fetching dashboard users:', error);
|
|
res.status(500).json({ success: false, message: 'Server error' });
|
|
}
|
|
};
|