diff --git a/repositories/deliveriesRepository.go b/repositories/deliveriesRepository.go index 69a0b57..af20403 100644 --- a/repositories/deliveriesRepository.go +++ b/repositories/deliveriesRepository.go @@ -61,8 +61,12 @@ 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, - ROUND(SUM(c.kms), 2) AS kms, - ROUND(SUM(c.actualkms), 2) AS actualkms, + -- Cast before summing: both are TEXT columns. + -- 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, SUM(c.deliveryamt) AS charges FROM tenants b @@ -77,8 +81,19 @@ const ( SUM(CASE WHEN b.orderstatus = 'arrived' THEN 1 ELSE 0 END) AS arrived, SUM(CASE WHEN b.orderstatus = 'picked' THEN 1 ELSE 0 END) AS picked, SUM(CASE WHEN b.orderstatus = 'delivered' THEN 1 ELSE 0 END) AS delivered, - SUM(CASE WHEN b.orderstatus = 'delivered' THEN b.actualkms ELSE 0 END) AS actualkms, - SUM(CASE WHEN b.orderstatus = 'delivered' THEN b.kms ELSE 0 END) AS kms, + -- kms and actualkms are TEXT columns, not numbers. + -- + -- Summed raw they produce + -- "CASE types integer and text cannot be matched", because the ELSE arm is + -- the integer 0 — so this endpoint answered 500 on every call. Verified on + -- production 2026-09-04. Every other SUM here is over a real numeric + -- (deliveryamt) or a literal 1, which is why only these two broke it. + -- + -- 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.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 @@ -460,40 +475,40 @@ func (r *deliveriesRepository) GetReportSummary(tid, pid, uid, aid int, fdate, t case tid != 0: if fdate != "" && tdate != "" { q1 = reports + ` WHERE a.tenantid=` + strconv.Itoa(tid) + - ` AND a.orderdate::date BETWEEN '` + fdate + `' AND '` + tdate + `' GROUP BY a.tenantid, b.tenantname` + ` AND a.orderdate::date BETWEEN '` + fdate + `' AND '` + tdate + `' GROUP BY b.tenantid, b.tenantname` } else { - q1 = reports + ` WHERE a.tenantid=` + strconv.Itoa(tid) + ` GROUP BY a.tenantid, b.tenantname` + q1 = reports + ` WHERE a.tenantid=` + strconv.Itoa(tid) + ` GROUP BY b.tenantid, b.tenantname` } case pid != 0: if fdate != "" && tdate != "" { q1 = reports + ` WHERE a.partnerid=` + strconv.Itoa(pid) + - ` AND a.orderdate::date BETWEEN '` + fdate + `' AND '` + tdate + `' GROUP BY a.tenantid, b.tenantname` + ` AND a.orderdate::date BETWEEN '` + fdate + `' AND '` + tdate + `' GROUP BY b.tenantid, b.tenantname` } else { - q1 = reports + ` WHERE a.partnerid=` + strconv.Itoa(pid) + ` GROUP BY a.tenantid, b.tenantname` + q1 = reports + ` WHERE a.partnerid=` + strconv.Itoa(pid) + ` GROUP BY b.tenantid, b.tenantname` } case uid != 0: if fdate != "" && tdate != "" { q1 = reports + ` WHERE c.userid=` + strconv.Itoa(uid) + - ` AND a.orderdate::date BETWEEN '` + fdate + `' AND '` + tdate + `' GROUP BY a.tenantid, b.tenantname` + ` AND a.orderdate::date BETWEEN '` + fdate + `' AND '` + tdate + `' GROUP BY b.tenantid, b.tenantname` } else { - q1 = reports + ` WHERE c.userid=` + strconv.Itoa(uid) + ` GROUP BY a.tenantid, b.tenantname` + q1 = reports + ` WHERE c.userid=` + strconv.Itoa(uid) + ` GROUP BY b.tenantid, b.tenantname` } case aid != 0: if fdate != "" && tdate != "" { q1 = reports + ` WHERE c.applocationid=` + strconv.Itoa(aid) + - ` AND a.orderdate::date BETWEEN '` + fdate + `' AND '` + tdate + `' GROUP BY a.tenantid, b.tenantname` + ` AND a.orderdate::date BETWEEN '` + fdate + `' AND '` + tdate + `' GROUP BY b.tenantid, b.tenantname` } else { - q1 = reports + ` WHERE c.applocationid=` + strconv.Itoa(aid) + ` GROUP BY a.tenantid, b.tenantname` + q1 = reports + ` WHERE c.applocationid=` + strconv.Itoa(aid) + ` GROUP BY b.tenantid, b.tenantname` } default: if fdate != "" && tdate != "" { - q1 = reports + ` WHERE a.orderdate::date BETWEEN '` + fdate + `' AND '` + tdate + `' GROUP BY a.tenantid, b.tenantname` + q1 = reports + ` WHERE a.orderdate::date BETWEEN '` + fdate + `' AND '` + tdate + `' GROUP BY b.tenantid, b.tenantname` } else { - q1 = reports + ` GROUP BY a.tenantid, b.tenantname` + q1 = reports + ` GROUP BY b.tenantid, b.tenantname` } } diff --git a/repositories/partnerRepository.go b/repositories/partnerRepository.go index 80e1bf2..a118f33 100644 --- a/repositories/partnerRepository.go +++ b/repositories/partnerRepository.go @@ -168,7 +168,20 @@ func (r *partnerRepository) GetRiderLogs(pid, aid int, fdate, tdate string) ([]m baseQuery += " AND a.logdate::date = CURRENT_DATE" } - baseQuery += " GROUP BY a.userid ORDER BY logid ASC" + // GROUP BY the two PRIMARY KEYS, not by a.userid. + // + // The select is "a.*, b.*" — every column of riderlogs and app_users — and + // grouping by a.userid leaves all of them unaggregated: userid is not + // riderlogs' key (logid is), so it does not functionally determine a.logid + // or anything else. Postgres refuses the whole statement, so this endpoint + // answered 500 "column a.logid must appear in the GROUP BY clause" on every + // call it has ever received. Verified on production 2026-09-04. + // + // Grouping by both primary keys is what the query means: one row per LOG + // (the model is RiderlogDetails and the sort is by logid), carrying that + // log's break hours summed. Postgres treats a PK in GROUP BY as determining + // the rest of its table's columns, so "a.*, b.*" is then legal. + baseQuery += " GROUP BY a.logid, b.userid ORDER BY a.logid ASC" if err := r.db.Raw(baseQuery, args...).Find(&data).Error; err != nil { return nil, err