Fix availability checks
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
-- V18: Normalize past appointments.
|
||||
-- Any appointment that is still 'Booked' but the date/time has passed should be marked as 'Missed'.
|
||||
-- V18: Normalize past appointments and resolve initial employee double-bookings
|
||||
|
||||
-- Part 1: Normalize past appointments.
|
||||
-- Any appointment that is still 'Booked' but the date/time has passed should be marked as 'Missed'.
|
||||
UPDATE appointment
|
||||
SET appointmentStatus = 'Missed'
|
||||
WHERE LOWER(appointmentStatus) = 'booked'
|
||||
@@ -8,3 +9,37 @@ WHERE LOWER(appointmentStatus) = 'booked'
|
||||
appointmentDate < CURRENT_DATE
|
||||
OR (appointmentDate = CURRENT_DATE AND appointmentTime < CURRENT_TIME)
|
||||
);
|
||||
|
||||
-- Part 2: Resolve potential double-bookings caused by V15's simple backfill.
|
||||
-- We try to spread overlapping appointments among other active staff in the same store.
|
||||
-- This is a one-time cleanup for demo data integrity.
|
||||
|
||||
-- Temporary table to find conflicts (same employee, same date, overlapping time)
|
||||
-- For simplicity in SQL, we just check exact same time for the demo data cleanup.
|
||||
UPDATE appointment a1
|
||||
SET a1.employeeId = (
|
||||
SELECT es.employeeId
|
||||
FROM employeeStore es
|
||||
JOIN employee e ON e.employeeId = es.employeeId
|
||||
JOIN users u ON u.id = e.user_id
|
||||
WHERE es.storeId = a1.storeId
|
||||
AND e.isActive = TRUE
|
||||
AND u.role = 'STAFF'
|
||||
-- Find an employee who DOES NOT have an appointment at this exact time
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM appointment a2
|
||||
WHERE a2.employeeId = es.employeeId
|
||||
AND a2.appointmentDate = a1.appointmentDate
|
||||
AND a2.appointmentTime = a1.appointmentTime
|
||||
AND a2.appointmentId <> a1.appointmentId
|
||||
)
|
||||
ORDER BY es.employeeId ASC
|
||||
LIMIT 1
|
||||
)
|
||||
WHERE EXISTS (
|
||||
SELECT 1 FROM appointment a3
|
||||
WHERE a3.employeeId = a1.employeeId
|
||||
AND a3.appointmentDate = a1.appointmentDate
|
||||
AND a3.appointmentTime = a1.appointmentTime
|
||||
AND a3.appointmentId < a1.appointmentId
|
||||
) AND LOWER(a1.appointmentStatus) NOT IN ('cancelled', 'missed');
|
||||
|
||||
Reference in New Issue
Block a user