feat: add room dashboard with KPI bar and floor grid

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-15 01:33:08 +00:00
parent 74e2bed34d
commit 45a027694d
12 changed files with 505 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
const pool = require('../db/connection');
const getKPIs = async (req, res) => {
try {
const today = new Date().toISOString().split('T')[0];
const [totalRooms, availableRooms, checkIns, checkOuts] = await Promise.all([
pool.query('SELECT COUNT(*) as count FROM rooms'),
pool.query("SELECT COUNT(*) as count FROM rooms WHERE status = 'available'"),
pool.query("SELECT COUNT(*) as count FROM reservations WHERE check_in = $1 AND status IN ('confirmed', 'checked_in')", [today]),
pool.query("SELECT COUNT(*) as count FROM reservations WHERE check_out = $1 AND status = 'checked_in'", [today]),
]);
const total = parseInt(totalRooms.rows[0].count);
const available = parseInt(availableRooms.rows[0].count);
const occupancy = total > 0 ? Math.round(((total - available) / total) * 100) : 0;
res.json({
occupancy,
availableRooms: available,
totalRooms: total,
todayCheckIns: parseInt(checkIns.rows[0].count),
todayCheckOuts: parseInt(checkOuts.rows[0].count),
});
} catch (error) {
console.error(error);
res.status(500).json({ message: 'Error al obtener KPIs' });
}
};
const getWeeklyRevenue = async (req, res) => {
try {
const result = await pool.query(`
SELECT DATE(check_out) as day, COALESCE(SUM(total_amount), 0) as revenue
FROM reservations
WHERE check_out >= CURRENT_DATE - INTERVAL '7 days' AND status = 'checked_out'
GROUP BY DATE(check_out) ORDER BY day
`);
res.json({ weeklyRevenue: result.rows });
} catch (error) {
console.error(error);
res.status(500).json({ message: 'Error al obtener ingresos semanales' });
}
};
const getTodayArrivals = async (req, res) => {
try {
const today = new Date().toISOString().split('T')[0];
const result = await pool.query(`
SELECT r.id, r.check_in, r.check_out, r.room_id, r.status,
g.first_name, g.last_name, g.phone
FROM reservations r
JOIN guests g ON g.id = r.guest_id
WHERE (r.check_in = $1 OR r.check_out = $1)
AND r.status IN ('confirmed', 'checked_in')
ORDER BY r.check_in
`, [today]);
res.json({ arrivals: result.rows });
} catch (error) {
console.error(error);
res.status(500).json({ message: 'Error al obtener llegadas' });
}
};
module.exports = { getKPIs, getWeeklyRevenue, getTodayArrivals };