status updated
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
@@ -19,6 +21,8 @@ type TenantService interface {
|
||||
CreateTenantCustomer(req models.CreateTenantCustomerRequest) (*models.Tenantcustomer, error)
|
||||
AssignPartner(tenantID, partnerID int) error
|
||||
GetCustomerTenants(customerID int, categoryID int, tenantFlag int) (*models.CustomerTenantResponse, error)
|
||||
SetStoreOpen(tenantID, locationID int, isOpen bool, closedUntil string) error
|
||||
StoreClosedReason(tenantID, locationID int) string
|
||||
GetTenantPricing(tid, aid int) (models.Tenantpricing, error)
|
||||
UpdateLocation(input models.Tenantlocations) error
|
||||
CreateLocation(data models.Tenantlocations) error
|
||||
@@ -98,6 +102,27 @@ func (s *tenantService) GetCustomerTenants(customerID int, categoryID int, tenan
|
||||
return nil, err
|
||||
}
|
||||
|
||||
/*
|
||||
Whether each branch is actually taking orders.
|
||||
|
||||
Worked out here rather than in SQL so there is ONE implementation of the
|
||||
rule — order creation calls the same function before accepting an order,
|
||||
and a shop the app showed as open while the server refused it would be a
|
||||
shopper staring at an error nothing on screen explains.
|
||||
|
||||
`now` is read once for the whole list: a request that straddled midnight
|
||||
could otherwise return two branches on different days.
|
||||
*/
|
||||
now := time.Now()
|
||||
for i := range details {
|
||||
details[i].Isaccepting = models.StoreIsOpen(
|
||||
details[i].Locationstatus, details[i].Isopen, details[i].Closeduntil, now,
|
||||
)
|
||||
details[i].Closedreason = models.StoreClosedReason(
|
||||
details[i].Locationstatus, details[i].Isopen, details[i].Closeduntil, now,
|
||||
)
|
||||
}
|
||||
|
||||
return &models.CustomerTenantResponse{
|
||||
Details: details,
|
||||
}, nil
|
||||
@@ -467,3 +492,53 @@ func (s *tenantService) resendTo(target repositories.InviteTarget, tenantID int)
|
||||
sent, reason := s.invites.Invite(target.Userid, tenantID, target.Email, target.Tenantname)
|
||||
return InviteOutcome{Sent: sent, Reason: reason}, nil
|
||||
}
|
||||
|
||||
/*
|
||||
Open or close a branch for the day.
|
||||
|
||||
Refuses a reopen date in the past outright. "Closed until yesterday" is a shop
|
||||
that is open, so storing it would mean the console showing closed while the
|
||||
app showed open — the two disagreeing about the same branch is worse than the
|
||||
edit failing.
|
||||
*/
|
||||
func (s *tenantService) SetStoreOpen(tenantID, locationID int, isOpen bool, closedUntil string) error {
|
||||
if tenantID <= 0 || locationID <= 0 {
|
||||
return fmt.Errorf("tenantid and locationid are both required")
|
||||
}
|
||||
|
||||
trimmed := strings.TrimSpace(closedUntil)
|
||||
if !isOpen && trimmed != "" {
|
||||
reopen, err := time.Parse("2006-01-02", trimmed)
|
||||
if err != nil {
|
||||
return fmt.Errorf("the reopening date must be written as YYYY-MM-DD")
|
||||
}
|
||||
today := time.Now().Truncate(24 * time.Hour)
|
||||
if reopen.Truncate(24 * time.Hour).Before(today) {
|
||||
return fmt.Errorf("that reopening date has already passed")
|
||||
}
|
||||
}
|
||||
|
||||
return s.repo.SetStoreOpen(tenantID, locationID, isOpen, trimmed)
|
||||
}
|
||||
|
||||
/*
|
||||
Why this branch cannot take an order, in words fit to show a shopper.
|
||||
|
||||
Empty when it can. Called by order creation, and it leans on the same
|
||||
models.StoreIsOpen the customer-facing list uses — so a shop the app showed as
|
||||
open is never refused here, and one it showed as closed is never quietly
|
||||
accepted.
|
||||
|
||||
Any failure reading the branch returns "" — open. A database hiccup must not
|
||||
stop a shop trading.
|
||||
*/
|
||||
func (s *tenantService) StoreClosedReason(tenantID, locationID int) string {
|
||||
if tenantID <= 0 || locationID <= 0 {
|
||||
return ""
|
||||
}
|
||||
status, isOpen, closedUntil, err := s.repo.StoreOpenState(tenantID, locationID)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return models.StoreClosedReason(status, isOpen, closedUntil, time.Now())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user