diff --git a/repositories/deliveriesRepository.go b/repositories/deliveriesRepository.go index af20403..d89158f 100644 --- a/repositories/deliveriesRepository.go +++ b/repositories/deliveriesRepository.go @@ -61,12 +61,22 @@ const ( SUM(CASE WHEN c.orderstatus = 'cancelled' THEN 1 ELSE 0 END) AS deliveriescancelled, SUM(CASE WHEN c.paymenttype = 64 THEN c.deliveryamt ELSE 0 END) AS paylater, SUM(CASE WHEN c.paymenttype = 43 THEN c.deliveryamt ELSE 0 END) AS payondelivery, - -- Cast before summing: both are TEXT columns. + -- Guarded cast: both are TEXT columns holding whatever was written to them. + -- + -- Summed raw this is "function sum(text) does not exist". Casting them + -- unconditionally is not enough either: the rider summary then answered + -- "invalid input syntax for type numeric: null" on production 2026-09-05, + -- because some rows hold the four-character string "null" rather than an + -- empty one. NULLIF caught the empty case and not that one. + -- + -- So only a value that IS a number is cast; everything else scores 0. That + -- covers the empty string, the literal "null", and any future junk, without + -- one bad row aborting the whole statement. -- Raw, this is "function sum(text) does not exist" and the endpoint 500s on -- every call — verified on production 2026-09-04. Same defect as the rider -- summary above, which is the other place these two columns are added up. - ROUND(SUM(COALESCE(NULLIF(c.kms, '')::numeric, 0)), 2) AS kms, - ROUND(SUM(COALESCE(NULLIF(c.actualkms, '')::numeric, 0)), 2) AS actualkms, + ROUND(SUM(CASE WHEN c.kms ~ '^[0-9]+([.][0-9]+)?$' THEN c.kms::numeric ELSE 0 END), 2) AS kms, + ROUND(SUM(CASE WHEN c.actualkms ~ '^[0-9]+([.][0-9]+)?$' THEN c.actualkms::numeric ELSE 0 END), 2) AS actualkms, SUM(c.deliveryamt) AS charges FROM tenants b @@ -92,8 +102,8 @@ const ( -- NULLIF handles the empty string, which is what an unridden job carries -- and what ::numeric would choke on; a value that is not a number at all -- would still raise, but nothing writes one. - SUM(CASE WHEN b.orderstatus = 'delivered' THEN COALESCE(NULLIF(b.actualkms, '')::numeric, 0) ELSE 0 END) AS actualkms, - SUM(CASE WHEN b.orderstatus = 'delivered' THEN COALESCE(NULLIF(b.kms, '')::numeric, 0) ELSE 0 END) AS kms, + SUM(CASE WHEN b.orderstatus = 'delivered' THEN CASE WHEN b.actualkms ~ '^[0-9]+([.][0-9]+)?$' THEN b.actualkms::numeric ELSE 0 END ELSE 0 END) AS actualkms, + SUM(CASE WHEN b.orderstatus = 'delivered' THEN CASE WHEN b.kms ~ '^[0-9]+([.][0-9]+)?$' THEN b.kms::numeric ELSE 0 END ELSE 0 END) AS kms, SUM(CASE WHEN b.paymenttype = 64 THEN b.deliveryamt ELSE 0 END) AS paylater, SUM(CASE WHEN b.paymenttype = 43 THEN b.deliveryamt ELSE 0 END) AS payondelivery, SUM(CASE WHEN b.orderstatus = 'delivered' THEN b.deliveryamt ELSE 0 END) AS deliveryamt