Files
Behavision/server/internal/store/api_refs.go
Suriyakumarvijayanayagam 9182f70442 A customer number people can say out loud
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
2026-09-07 11:52:32 +05:30

82 lines
2.5 KiB
Go

package store
import (
"context"
"errors"
"github.com/jackc/pgx/v5"
)
// Resolving a public reference to the uuid it names.
//
// Every id in this schema is a uuid and stays one. These three exist because a
// uuid is not something a person can say, type or recognise, and three of the
// four things anyone addresses by URL already HAD a human name that the API
// simply refused to accept: a site has a slug, a camera has the id the engine
// knows it by (and the one that lands in `visits.camera_id`), and a visitor now
// has a per-tenant number. See api/refs.go for the formats.
//
// All three answer "" with a nil error when nothing matches. A found id is
// never empty, so the two cases cannot be confused, and a mistyped reference is
// a 404 rather than an error the handler has to classify.
// SiteIDBySlug resolves a site slug within one tenant.
func (s *Store) SiteIDBySlug(ctx context.Context, clientID, slug string) (string, error) {
var id string
err := s.pool.QueryRow(ctx, `
SELECT id::text FROM sites
WHERE client_id = $1::uuid AND slug = $2`,
clientID, slug).Scan(&id)
if errors.Is(err, pgx.ErrNoRows) {
return "", nil
}
return id, err
}
// CameraIDByRef resolves the engine's own camera id - "Office1" - to the row
// uuid, within one tenant.
//
// A camera id is unique per SITE, not per tenant, so two shops may each have an
// "Office1". Ambiguity is resolved as no match rather than by picking one:
// silently acting on whichever row sorted first would edit or delete the wrong
// shop's camera. A caller in that position has the uuid, or can scope by site.
func (s *Store) CameraIDByRef(ctx context.Context, clientID, ref string) (string, error) {
rows, err := s.pool.Query(ctx, `
SELECT id::text FROM site_cameras
WHERE client_id = $1::uuid AND camera_id = $2 AND deleted_at IS NULL
LIMIT 2`, clientID, ref)
if err != nil {
return "", err
}
defer rows.Close()
var found []string
for rows.Next() {
var id string
if err := rows.Scan(&id); err != nil {
return "", err
}
found = append(found, id)
}
if err := rows.Err(); err != nil {
return "", err
}
if len(found) != 1 {
return "", nil
}
return found[0], nil
}
// VisitorIDByNumber resolves the number behind "V-42" within one tenant.
func (s *Store) VisitorIDByNumber(ctx context.Context, clientID string, number int64) (string, error) {
var id string
err := s.pool.QueryRow(ctx, `
SELECT id::text FROM visitors
WHERE client_id = $1::uuid AND number = $2`,
clientID, number).Scan(&id)
if errors.Is(err, pgx.ErrNoRows) {
return "", nil
}
return id, err
}