5.2 KiB
Live customer segmentation (Phase 4H)
Staff can inspect three fixed, live segments based on current commerce records. Accounts must be active and belong to the caller's organization. Membership contains only account IDs, not names, contacts, addresses, purchase evidence, or cart/wishlist contents. Accounts with staff roles are included if they have the same qualifying commerce activity; the current identity model has no separate customer-only account type.
| Segment key | Definition |
|---|---|
WISHLIST_INTEREST |
At least one saved wishlist variant is currently active and its product published. Multiple saved items count the account once. |
RECOVERABLE_CART |
A nonempty cart has a known activity timestamp at least 24 hours old, using the same cutoff as private cart recovery. Empty/undated/recently edited carts are excluded. Catalog/stock eligibility remains authoritative at recovery and checkout. |
VERIFIED_BUYER |
At least one unrevoked external purchase verification exists under the verified-review evidence workflow. Pending-payment orders and cancelled orders do not establish completed purchase evidence. |
GET /api/v1/customer-segments requires segments.read and returns asOf plus a segments array of { key, description, total }. The groups can overlap; their counts should not be summed to infer unique customers. No account without the qualifying evidence is included merely because it is active.
GET /api/v1/customer-segments/:segment/members requires segments.read and returns { segment, asOf, total, members: [{ userId }] }. Members are distinct and ordered by UUID ascending. limit defaults to 25 with a maximum of 100, and offset defaults to zero with a maximum of 10,000. total is the complete filtered count, not the size of this page. Unknown segment keys, invalid pagination, scope overrides, and extra query fields return 400. Missing sessions return 401; missing permission returns 403.
Both routes optionally accept channel=EMAIL&purpose=PROMOTIONS (also WHATSAPP and CART_RECOVERY). Supply both fields together. This intersects the segment with the latest explicit grant for that channel and purpose; absent consent and later withdrawal exclude the account. A grant for another channel/purpose does not qualify, and replaying an old grant after withdrawal does not restore membership. Consent-filtered reads additionally require the caller's live messaging.read permission, preventing consent disclosure through a segment-only role.
Inactive accounts, unavailable wishlist merchandise, cart edits/clearing, and purchase-evidence revocation affect the next read immediately. Reads recheck the staff session and permissions inside the existing organization-locked transaction. One server time is used across each response's segment checks; total and page use the same query predicate. Source API writes share the lock. Different requests/pages are live evaluations and can change between reads; asOf is an evaluation timestamp, not a persistent audience snapshot or paging token.
All SQL values are bound parameters. Fixed predicate fragments use scoped joins and EXISTS, so multiple qualifying source records do not duplicate accounts. Queries use the existing user, wishlist, cart, verification, and consent indexes. No materialized audience, copied contact list, custom rule DSL, saved segment, scheduler, CSV export, or provider delivery is introduced. Keep live reads until measured dataset size and query latency justify cached/materialized audiences.
These are staff analysis and planning APIs, not permission to deliver messages. Future adapters must recheck live consent, recipient status/contact ownership, channel policy, campaign/cart eligibility and suppressions at dispatch and retries. A filtered preview can become stale after withdrawal. Existing transactional notifications and customer preferences are unchanged.
Deploy the additive permission migration before the application. Existing system Owners gain segments.read; custom roles remain explicit. There are no new audience tables or schema changes. Existing migrations and source evidence remain unchanged. Older code ignores the new permission; rollback preserves all commerce records and consent history. Existing native PostgreSQL CI requirements continue to apply.
Verification covers deduplication, live catalog/account changes, shared recovery timing and empty/undated carts, revoked purchase evidence, unpaid-order exclusion, newest consent and stale-grant replay, channel/purpose isolation, separate permissions, organization isolation, strict inputs, pagination, private projections, migration drift, and prior recovery regressions.