store based coustmers

This commit is contained in:
2026-08-03 15:12:13 +05:30
parent 481be667db
commit 5d2e1fca9b
2 changed files with 86 additions and 14 deletions

View File

@@ -188,10 +188,21 @@ func (r *customerRepository) GetTenantCustomers(tid, lid, pageno, pagesize int,
var args []interface{}
searchLike := "%" + keyword + "%"
// DISTINCT ON collapses to one row per customer BEFORE the LIMIT is applied.
// Without it the store-scoped branch below paginated the joined
// customerlocations rows — one per saved address — so `pagesize` bought a
// page of addresses, not of customers. Live example: locationid 1185 returned
// 12 rows that were only 2 people, 11 of them one customer's addresses. A
// store with a page size of 20 therefore listed roughly three customers and
// gave no hint that the rest existed.
//
// The ORDER BY must lead with the DISTINCT ON expression, so customerid sorts
// first; the trailing keys only decide WHICH address represents a customer,
// preferring the one flagged primary.
if lid != 0 {
q1 = `SELECT a.customerid,a.firstname,a.lastname,a.contactno,a.email,
q1 = `SELECT DISTINCT ON (a.customerid) a.customerid,a.firstname,a.lastname,a.contactno,a.email,
b.locationid as deliverylocationid,b.address,b.suburb,b.city,b.state,b.landmark,b.doorno,b.postcode,
b.latitude,b.longitude,a.applocationid,c.locationid as tenantlocationid,a.status
b.latitude,b.longitude,a.applocationid,c.locationid as tenantlocationid,a.status
FROM customers a
LEFT JOIN customerlocations b ON a.customerid=b.customerid
INNER JOIN tenantcustomers c ON a.customerid=c.customerid
@@ -204,13 +215,17 @@ func (r *customerRepository) GetTenantCustomers(tid, lid, pageno, pagesize int,
args = append(args, searchLike, searchLike, searchLike)
}
q1 += ` ORDER BY a.customerid DESC LIMIT ? OFFSET ?`
q1 += ` ORDER BY a.customerid DESC, b.primaryaddress DESC NULLS LAST, b.locationid ASC
LIMIT ? OFFSET ?`
args = append(args, pagesize, offset)
} else {
q1 = `SELECT a.customerid,a.firstname,a.lastname,a.contactno,a.email,
// A customer linked to several outlets of the same tenant has one
// tenantcustomers row per outlet, so this branch double-counted them
// against the LIMIT too.
q1 = `SELECT DISTINCT ON (a.customerid) a.customerid,a.firstname,a.lastname,a.contactno,a.email,
a.address,a.suburb,a.city,a.state,a.landmark,a.doorno,a.postcode,
a.latitude,a.longitude,a.applocationid,c.locationid as tenantlocationid,a.status
a.latitude,a.longitude,a.applocationid,c.locationid as tenantlocationid,a.status
FROM customers a
INNER JOIN tenantcustomers c ON a.customerid=c.customerid
WHERE c.tenantid = ?`
@@ -223,12 +238,10 @@ func (r *customerRepository) GetTenantCustomers(tid, lid, pageno, pagesize int,
args = append(args, searchLike, searchLike, searchLike)
}
q1 += ` ORDER BY a.customerid DESC LIMIT ? OFFSET ?`
q1 += ` ORDER BY a.customerid DESC, c.locationid ASC LIMIT ? OFFSET ?`
args = append(args, pagesize, offset)
}
print(q1)
r.db.Raw(q1, args...).Find(&data)
return data
}