Every id in the schema is a uuid and stays one. What was wrong was putting one in front of a person: RecordVisit named every new customer 'Visitor ' || left(id::text, 8), so the arrivals feed, the shop PC and the mobile app all read "Visitor 3446ec35" - the string a shop assistant reads to a colleague and types into a search box. label is a stored column staff can overwrite and SearchVisitors matches on, so formatting around it in a front end would have left the data wrong on three surfaces. Migration 012 adds a per-client visitors.number, taken from a counter on clients with UPDATE ... RETURNING inside the visit transaction. Per client rather than global: a global sequence would tell any customer who signs up how many people the whole platform has ever seen, from their own first visitor number. The backfill numbers existing rows by first_seen_at and relabels only the eight-hex pattern the old statement produced, so a human-typed name is never overwritten. Three of the four things anyone addresses by URL already had a human name and the API simply refused it - a site has a slug, a camera has the id the engine knows it by. refs.go accepts either form anywhere an id is taken; a uuid resolves with no lookup, so every URL a client already stored keeps working. - An ambiguous camera name resolves to nothing, never to a guess: two shops may each have an "Office1" and acting on the first row would edit the wrong shop's camera. - 404 on a path, 400 on a query filter. /api/visits answered fine and it was the filter that was wrong. - site and site_id are both accepted everywhere now. They differed per endpoint, and an unknown query parameter is silently ignored, so getting it the wrong way round returned the whole estate. - The search matches V-13, which is what the product now shows. Two bugs found by running it rather than testing it: - 'Visitor ' || $2::text beside number = $2 makes Postgres deduce two types for one parameter and refuse the insert. It compiled and passed every in-memory test; the first real database rejected it, along with the existing face tests that share the path. - The fallback avatar said "V1" for Visitor 13, Visitor 10 and Visitor 15 alike, and read as the V-1 reference for a fourth person. It shows the number now. The prop is customerRef, not ref - React reserves that name and it would never have arrived. Verified on the live database and through the running API: 13 hex labels became Visitor 1-13 in first-seen order, two typed names left alone, and the same customer reachable by uuid, V-13 and 13. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HViLj9gYNRtSr7YVZmW5sn
67 lines
2.2 KiB
Go
67 lines
2.2 KiB
Go
package api
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
// Issuing the code that claims a shop PC.
|
|
//
|
|
// This existed only as a provisioning command, which made every replacement PC
|
|
// a support ticket and an SSH session - and a shop PC is exactly the kind of
|
|
// machine that gets replaced, reimaged and swapped between branches. The
|
|
// command remains the bootstrap, because a brand new customer has nobody to
|
|
// sign in as yet; this is for every time after that.
|
|
//
|
|
// Manager and above, never staff: the code is redeemed for the site's broker
|
|
// password, so it is a credential in its own right, not a convenience.
|
|
func (s *Server) handleIssueEnrolmentCode(w http.ResponseWriter, r *http.Request) {
|
|
p := PrincipalFrom(r.Context())
|
|
if !p.CanManageSites() {
|
|
writeErr(w, http.StatusForbidden, "forbidden",
|
|
"Your account cannot set up shop computers. Ask a manager or the owner.")
|
|
return
|
|
}
|
|
site, ok := s.resolveSite(w, r, r.PathValue("site"))
|
|
if !ok {
|
|
return
|
|
}
|
|
var in NewEnrolmentCodeInput
|
|
if err := decodeOptional(w, r, &in); err != nil {
|
|
badRequest(w, err.Error())
|
|
return
|
|
}
|
|
// A week by default, and a month at the very most. The code is read aloud,
|
|
// photographed and pasted into chat on its way to a shop; a long-lived one
|
|
// is a broker credential lying about in a WhatsApp thread.
|
|
days := in.Days
|
|
if days <= 0 {
|
|
days = 7
|
|
}
|
|
if days > 30 {
|
|
days = 30
|
|
}
|
|
out, err := s.Store.IssueEnrolmentCode(r.Context(), p.ClientID, site,
|
|
p.UserID, clip(trim(in.Label), 120), time.Duration(days)*24*time.Hour)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
writeErr(w, http.StatusNotFound, "not_found", "No such shop.")
|
|
return
|
|
}
|
|
s.serverError(w, "issue enrolment code", err)
|
|
return
|
|
}
|
|
// A code hands out a site's broker password, so who minted one and when is
|
|
// worth a row - the same reason every read of a face image writes one.
|
|
s.Store.Audit(r.Context(), AuditEntry{
|
|
ClientID: p.ClientID, ActorID: p.UserID, ActorKind: "user",
|
|
Action: "enrolment_code.issued", Entity: "site", EntityID: site,
|
|
Detail: map[string]any{"label": out.Label, "expires_at": out.ExpiresAt},
|
|
})
|
|
// 201: a credential was created. The body is the only time it is readable.
|
|
writeJSON(w, http.StatusCreated, out)
|
|
}
|