refactor(today): replace inline duration queries with booking.end_time
Simplify auto-transition queries in GetCurrentAndNextHandler by using bookings.end_time computed column instead of inline start_time + duration calculations. Remove ::text casts from end_time columns. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -87,22 +87,7 @@ func GetCurrentAndNextHandler(w http.ResponseWriter, r *http.Request) {
|
||||
SET status = 'in_progress'
|
||||
WHERE status = 'confirmed'
|
||||
AND start_time <= $1
|
||||
AND (
|
||||
start_time + (
|
||||
COALESCE(
|
||||
(SELECT SUM(dur) FROM (
|
||||
SELECT COALESCE(bs.override_duration_minutes, s.duration_minutes) AS dur
|
||||
FROM booking_services bs JOIN services s ON bs.service_id = s.id
|
||||
WHERE bs.booking_id = bookings.id
|
||||
UNION ALL
|
||||
SELECT COALESCE(bcs.override_duration_minutes, cs.duration_minutes)
|
||||
FROM booking_custom_services bcs JOIN custom_services cs ON bcs.custom_service_id = cs.id
|
||||
WHERE bcs.booking_id = bookings.id
|
||||
) sub),
|
||||
0
|
||||
) || ' minutes'
|
||||
)::interval
|
||||
) > $1
|
||||
AND end_time > $1
|
||||
`, now)
|
||||
if err != nil {
|
||||
log.Printf("Failed to auto-transition bookings to in_progress: %v", err)
|
||||
@@ -113,22 +98,7 @@ func GetCurrentAndNextHandler(w http.ResponseWriter, r *http.Request) {
|
||||
UPDATE bookings
|
||||
SET status = 'completed'
|
||||
WHERE status = 'in_progress'
|
||||
AND (
|
||||
start_time + (
|
||||
COALESCE(
|
||||
(SELECT SUM(dur) FROM (
|
||||
SELECT COALESCE(bs.override_duration_minutes, s.duration_minutes) AS dur
|
||||
FROM booking_services bs JOIN services s ON bs.service_id = s.id
|
||||
WHERE bs.booking_id = bookings.id
|
||||
UNION ALL
|
||||
SELECT COALESCE(bcs.override_duration_minutes, cs.duration_minutes)
|
||||
FROM booking_custom_services bcs JOIN custom_services cs ON bcs.custom_service_id = cs.id
|
||||
WHERE bcs.booking_id = bookings.id
|
||||
) sub),
|
||||
0
|
||||
) || ' minutes'
|
||||
)::interval
|
||||
) <= $1
|
||||
AND end_time <= $1
|
||||
`, now)
|
||||
if err != nil {
|
||||
log.Printf("Failed to auto-transition bookings to completed: %v", err)
|
||||
@@ -419,7 +389,7 @@ func findNewBookingServices(r *http.Request, rangeStart time.Time) []ServiceBook
|
||||
COALESCE(eh.close_time, wh.close_time) AS close_time
|
||||
FROM days d
|
||||
LEFT JOIN LATERAL (
|
||||
SELECT (ewh.end_time)::text AS close_time
|
||||
SELECT ewh.end_time AS close_time
|
||||
FROM exceptional_working_hours ewh
|
||||
JOIN exceptional_working_hours_groups ewhg ON ewhg.id = ewh.group_id
|
||||
JOIN exceptional_group_applications ega ON ega.group_id = ewhg.id
|
||||
@@ -430,7 +400,7 @@ func findNewBookingServices(r *http.Request, rangeStart time.Time) []ServiceBook
|
||||
LIMIT 1
|
||||
) eh ON true
|
||||
LEFT JOIN LATERAL (
|
||||
SELECT wh.end_time::text AS close_time
|
||||
SELECT wh.end_time AS close_time
|
||||
FROM working_hours wh
|
||||
WHERE wh.weekday = d.weekday AND wh.is_open = true
|
||||
LIMIT 1
|
||||
@@ -574,7 +544,7 @@ func getClosingTime(r *http.Request, date time.Time) string {
|
||||
// Check exceptional hours first
|
||||
var exceptionalClose sql.NullString
|
||||
err := db.Conn.QueryRow(r.Context(), `
|
||||
SELECT ewh.end_time::text
|
||||
SELECT ewh.end_time
|
||||
FROM exceptional_working_hours ewh
|
||||
JOIN exceptional_working_hours_groups ewhg ON ewhg.id = ewh.group_id
|
||||
JOIN exceptional_group_applications ega ON ega.group_id = ewhg.id
|
||||
@@ -592,7 +562,7 @@ func getClosingTime(r *http.Request, date time.Time) string {
|
||||
// Fall back to default working hours
|
||||
var defaultClose sql.NullString
|
||||
err = db.Conn.QueryRow(r.Context(), `
|
||||
SELECT end_time::text FROM working_hours WHERE weekday = $1 AND is_open = true
|
||||
SELECT end_time FROM working_hours WHERE weekday = $1 AND is_open = true
|
||||
`, weekday).Scan(&defaultClose)
|
||||
|
||||
if err == nil && defaultClose.Valid {
|
||||
@@ -757,22 +727,7 @@ func GetTodayAppointmentsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
SET status = 'in_progress'
|
||||
WHERE status = 'confirmed'
|
||||
AND start_time <= $1
|
||||
AND (
|
||||
start_time + (
|
||||
COALESCE(
|
||||
(SELECT SUM(dur) FROM (
|
||||
SELECT COALESCE(bs.override_duration_minutes, s.duration_minutes) AS dur
|
||||
FROM booking_services bs JOIN services s ON bs.service_id = s.id
|
||||
WHERE bs.booking_id = bookings.id
|
||||
UNION ALL
|
||||
SELECT COALESCE(bcs.override_duration_minutes, cs.duration_minutes)
|
||||
FROM booking_custom_services bcs JOIN custom_services cs ON bcs.custom_service_id = cs.id
|
||||
WHERE bcs.booking_id = bookings.id
|
||||
) sub),
|
||||
0
|
||||
) || ' minutes'
|
||||
)::interval
|
||||
) > $1
|
||||
AND end_time > $1
|
||||
`, now)
|
||||
if err != nil {
|
||||
log.Printf("Failed to auto-transition bookings to in_progress: %v", err)
|
||||
@@ -783,22 +738,7 @@ func GetTodayAppointmentsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
UPDATE bookings
|
||||
SET status = 'completed'
|
||||
WHERE status = 'in_progress'
|
||||
AND (
|
||||
start_time + (
|
||||
COALESCE(
|
||||
(SELECT SUM(dur) FROM (
|
||||
SELECT COALESCE(bs.override_duration_minutes, s.duration_minutes) AS dur
|
||||
FROM booking_services bs JOIN services s ON bs.service_id = s.id
|
||||
WHERE bs.booking_id = bookings.id
|
||||
UNION ALL
|
||||
SELECT COALESCE(bcs.override_duration_minutes, cs.duration_minutes)
|
||||
FROM booking_custom_services bcs JOIN custom_services cs ON bcs.custom_service_id = cs.id
|
||||
WHERE bcs.booking_id = bookings.id
|
||||
) sub),
|
||||
0
|
||||
) || ' minutes'
|
||||
)::interval
|
||||
) <= $1
|
||||
AND end_time <= $1
|
||||
`, now)
|
||||
if err != nil {
|
||||
log.Printf("Failed to auto-transition bookings to completed: %v", err)
|
||||
|
||||
Reference in New Issue
Block a user