A shop can be renamed and, while empty, removed - from head office
The display name was always meant to be editable and the slug frozen;
until now neither had a way in. PATCH /api/sites/{site} takes a name
and a timezone (manager and above), DELETE removes an empty shop
(owner). The shop drawer in head office gets both, with the short name
shown read-only and the reason beside it.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KGcjxF1cNLcuwc3DAPcnfj
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
@@ -257,3 +258,57 @@ func (s *Server) handleDeleteSite(w http.ResponseWriter, r *http.Request) {
|
||||
// ErrSiteInUse is returned by DeleteEmptySite for a shop that has anything
|
||||
// under it.
|
||||
var ErrSiteInUse = errors.New("site has cameras or visits")
|
||||
|
||||
// PATCH /api/sites/{site} - rename a shop or change its timezone. Owner or
|
||||
// manager. The slug is not in the body and would be refused by the database
|
||||
// if it were: it is what the shop PC calls itself and a segment of the broker
|
||||
// topic, and renaming it would orphan both.
|
||||
func (s *Server) handleUpdateSite(w http.ResponseWriter, r *http.Request) {
|
||||
p := PrincipalFrom(r.Context())
|
||||
if !p.CanManageSites() || p.ClientID == "" {
|
||||
writeErr(w, http.StatusForbidden, "forbidden", "Only a manager or the owner can change a shop.")
|
||||
return
|
||||
}
|
||||
site, ok := s.resolveSite(w, r, r.PathValue("site"))
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var in SiteUpdate
|
||||
if err := decode(w, r, &in); err != nil {
|
||||
badRequest(w, err.Error())
|
||||
return
|
||||
}
|
||||
if in.Name != nil {
|
||||
n := clip(trim(*in.Name), 120)
|
||||
if n == "" {
|
||||
badRequest(w, "The shop needs a name.")
|
||||
return
|
||||
}
|
||||
in.Name = &n
|
||||
}
|
||||
if in.Timezone != nil {
|
||||
if _, err := time.LoadLocation(strings.TrimSpace(*in.Timezone)); err != nil {
|
||||
badRequest(w, "Unknown timezone. Use an IANA name such as Asia/Kolkata.")
|
||||
return
|
||||
}
|
||||
}
|
||||
if in.Name == nil && in.Timezone == nil {
|
||||
badRequest(w, "Nothing to change: give a name or a timezone.")
|
||||
return
|
||||
}
|
||||
out, err := s.Store.UpdateSite(r.Context(), p.ClientID, site, in)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
writeErr(w, http.StatusNotFound, "not_found", "No such shop.")
|
||||
return
|
||||
}
|
||||
s.serverError(w, "update site", err)
|
||||
return
|
||||
}
|
||||
s.Store.Audit(r.Context(), AuditEntry{
|
||||
ClientID: p.ClientID, ActorID: p.UserID, ActorKind: "user",
|
||||
Action: "site.updated", Entity: "site", EntityID: site,
|
||||
Detail: map[string]any{"name": out.Name, "timezone": out.Timezone},
|
||||
})
|
||||
writeJSON(w, http.StatusOK, out)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user