refactor(payments): replace inline total_amount queries with booking field

Simplify payment handlers by using bookings.total_amount computed column instead of inline UNION sub-queries calculating price totals from booking_services and booking_custom_services.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
2026-06-21 21:47:16 +01:00
co-authored by Sisyphus
parent 7ea6fab3af
commit 14df08e129
3 changed files with 27 additions and 110 deletions
+6 -32
View File
@@ -177,17 +177,7 @@ func (s *PaymentService) GetBookingPaymentSummary(ctx context.Context, bookingID
var totalAmount float64
err := db.Conn.QueryRow(ctx, `
SELECT COALESCE(SUM(price_val), 0) FROM (
SELECT COALESCE(bs.override_price, s.price) AS price_val
FROM booking_services bs
JOIN services s ON bs.service_id = s.id
WHERE bs.booking_id = $1
UNION ALL
SELECT COALESCE(bcs.override_price, cs.price)
FROM booking_custom_services bcs
JOIN custom_services cs ON bcs.custom_service_id = cs.id
WHERE bcs.booking_id = $1
) price_sub
SELECT total_amount FROM bookings WHERE id = $1
`, bookingID).Scan(&totalAmount)
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
@@ -385,14 +375,8 @@ func (s *PaymentService) GetBookingPaymentInfo(ctx context.Context, bookingID st
COALESCE(pt.total_paid, 0)
FROM bookings b
LEFT JOIN (
SELECT booking_id, COALESCE(SUM(price_val), 0) AS total_amount FROM (
SELECT bs.booking_id, COALESCE(bs.override_price, s.price) AS price_val
FROM booking_services bs JOIN services s ON bs.service_id = s.id WHERE bs.booking_id = $1
UNION ALL
SELECT bcs.booking_id, COALESCE(bcs.override_price, cs.price)
FROM booking_custom_services bcs JOIN custom_services cs ON bcs.custom_service_id = cs.id WHERE bcs.booking_id = $1
) sub GROUP BY booking_id
) bt ON b.id = bt.booking_id
SELECT id, total_amount FROM bookings WHERE id = $1
) bt ON b.id = bt.id
LEFT JOIN (
SELECT booking_id, SUM(amount) AS total_paid
FROM payments WHERE booking_id = $1 AND status = 'completed' AND payment_method NOT IN ('discount', 'on_the_house') GROUP BY booking_id
@@ -417,19 +401,9 @@ func (s *PaymentService) GetBookingUserID(ctx context.Context, bookingID string)
func (s *PaymentService) GetBookingRemainingBalanceCents(ctx context.Context, bookingID string) (int64, error) {
var remainingCents int64
err := db.Conn.QueryRow(ctx, `
WITH booking_total AS (
SELECT COALESCE(SUM(price_val), 0) AS total_pounds FROM (
SELECT COALESCE(bs.override_price, s.price) AS price_val
FROM booking_services bs
LEFT JOIN services s ON bs.service_id = s.id
WHERE bs.booking_id = $1
UNION ALL
SELECT COALESCE(bcs.override_price, cs.price)
FROM booking_custom_services bcs
LEFT JOIN custom_services cs ON bcs.custom_service_id = cs.id
WHERE bcs.booking_id = $1
) price_sub
),
WITH booking_total AS (
SELECT total_amount AS total_pounds FROM bookings WHERE id = $1
),
paid_total AS (
SELECT COALESCE(SUM(amount), 0) AS paid_pounds
FROM payments