status updated
This commit is contained in:
@@ -18,6 +18,8 @@ type TenantRepository interface {
|
||||
GetTenantSlot() (models.Tenantslot, error)
|
||||
CreateTenantCustomer(customer models.Tenantcustomer) (*models.Tenantcustomer, error)
|
||||
GetCustomerTenants(customerID int, categoryID int, tenantFlag int) ([]models.TenantInfo, error)
|
||||
SetStoreOpen(tenantID, locationID int, isOpen bool, closedUntil string) error
|
||||
StoreOpenState(tenantID, locationID int) (status string, isOpen bool, closedUntil string, err error)
|
||||
GetTenantPricing(tid, aid int) (*models.Tenantpricing, error)
|
||||
UpdateLocation(input models.Tenantlocations) error
|
||||
CreateLocation(data models.Tenantlocations) error
|
||||
@@ -219,7 +221,14 @@ func (r *tenantRepository) GetCustomerTenants(customerID int, categoryID int, te
|
||||
b.primaryemail,b.primarycontact,b.applocationid,b.suburb,b.city,
|
||||
b.latitude,b.longitude,b.postcode,b.tenantimage,b.subcategoryid,
|
||||
b.categoryid,b.registrationno,d.userfcmtoken,c.locationname,
|
||||
COALESCE(o.orderscount, 0) AS orderscount
|
||||
COALESCE(o.orderscount, 0) AS orderscount,
|
||||
-- Whether the shop is trading today. Selected, never filtered on:
|
||||
-- a closed branch still comes back so the app can show it greyed
|
||||
-- rather than make a regular customer think it has gone for good.
|
||||
-- COALESCE because the column is new and older rows predate it.
|
||||
COALESCE(c.isopen, true) AS isopen,
|
||||
COALESCE(c.closeduntil::text, '') AS closeduntil,
|
||||
c.status AS locationstatus
|
||||
FROM tenantcustomers a
|
||||
LEFT JOIN tenants b ON a.tenantid = b.tenantid
|
||||
INNER JOIN tenantlocations c ON a.locationid = c.locationid
|
||||
@@ -1210,3 +1219,65 @@ func (r *tenantRepository) TenantNameByID(tenantID int) (string, error) {
|
||||
tenantID).Scan(&name).Error
|
||||
return name, err
|
||||
}
|
||||
|
||||
/*
|
||||
Open or close a branch for the day.
|
||||
|
||||
Scoped by tenant as well as branch: a locationid arrives from a console
|
||||
request, and looking it up by id alone would let one business close another
|
||||
business's shop.
|
||||
|
||||
`closeduntil` is written as NULL when empty rather than as an empty string, so
|
||||
the column holds a real date or nothing — a ” in a DATE column is an error in
|
||||
Postgres, and the shape of "closed with no end in sight" is the absence of a
|
||||
date, not a blank one.
|
||||
*/
|
||||
func (r *tenantRepository) SetStoreOpen(tenantID, locationID int, isOpen bool, closedUntil string) error {
|
||||
updates := map[string]interface{}{"isopen": isOpen}
|
||||
|
||||
if trimmed := strings.TrimSpace(closedUntil); trimmed != "" && !isOpen {
|
||||
updates["closeduntil"] = trimmed
|
||||
} else {
|
||||
// Reopening clears the date too. A branch switched back on must not
|
||||
// keep a stale "reopens on" that would read as still closed.
|
||||
updates["closeduntil"] = nil
|
||||
}
|
||||
|
||||
result := r.db.Table("tenantlocations").
|
||||
Where("locationid = ? AND tenantid = ?", locationID, tenantID).
|
||||
Updates(updates)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return fmt.Errorf("branch %d was not found for this business", locationID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// The three columns models.StoreIsOpen needs, for one branch.
|
||||
//
|
||||
// Returns an OPEN state when the branch is not found. Order creation calls
|
||||
// this, and a lookup that missed must not become a refusal to trade: the
|
||||
// endpoint already takes orders from callers that name no branch at all, and
|
||||
// turning a missing row into "we are closed" would stop sales on a data
|
||||
// problem rather than a business decision.
|
||||
func (r *tenantRepository) StoreOpenState(tenantID, locationID int) (string, bool, string, error) {
|
||||
var row struct {
|
||||
Status string
|
||||
Isopen bool
|
||||
Closeduntil string
|
||||
}
|
||||
|
||||
err := r.db.Table("tenantlocations").
|
||||
Select("status, COALESCE(isopen, true) AS isopen, COALESCE(closeduntil::text, '') AS closeduntil").
|
||||
Where("locationid = ? AND tenantid = ?", locationID, tenantID).
|
||||
Scan(&row).Error
|
||||
if err != nil {
|
||||
return "Active", true, "", err
|
||||
}
|
||||
if row.Status == "" {
|
||||
return "Active", true, "", nil
|
||||
}
|
||||
return row.Status, row.Isopen, row.Closeduntil, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user