A customer's history now says whether they bought anything

"When was this customer last in" and "did they buy" are one question
staff ask in one breath, and answering it meant two calls and a join in
the client. Each visit row carries purchases, spend and currency.

LATERAL, not a join onto purchases. A plain join returns the visit TWICE
when it holds two sales, which would make a customer look like they came
more often than they did - a wrong number of exactly the kind this
product is otherwise careful about, arrived at by adding a feature.

Mixed currencies on one visit report the count and NO figure. Adding
rupees to dollars produces something that looks like money and is not,
and the sales still happened, so the count is the honest part to keep.

A purchase with no visit_id is deliberately absent: it belongs to the
customer rather than to a moment, and GET /api/sales?customer=V-42 lists
it. The two surfaces together cover every sale exactly once.

Both properties are asserted in the LIVE store tests, because both live
in the SQL. An in-memory fake asserting that a LATERAL does not duplicate
a row would only be checking the fake.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KGcjxF1cNLcuwc3DAPcnfj
This commit is contained in:
2026-09-29 14:49:23 +05:30
parent c7312d31b4
commit dae18d651b
3 changed files with 148 additions and 3 deletions

View File

@@ -198,6 +198,21 @@ type VisitRow struct {
Similarity float64 `json:"similarity,omitempty"`
Quality float64 `json:"quality,omitempty"`
Attributes map[string]any `json:"attributes,omitempty"`
// What they bought on that visit, if anything. Attached here because
// "when was this customer last in" and "did they buy" are one question
// staff ask in one breath, and answering it used to mean two calls and a
// join in the client.
//
// Spend and Currency are omitted when a single visit somehow holds more
// than one currency: adding rupees to dollars produces something that
// looks like money and is not, and Purchases still says a sale happened.
// A purchase with no visit_id is not here at all - it belongs to the
// customer rather than to a moment - and is listed by
// GET /api/sales?customer=V-42.
Purchases int `json:"purchases,omitempty"`
Spend float64 `json:"spend,omitempty"`
Currency string `json:"currency,omitempty"`
}
type Profile struct {