manicanldes-backend/docs/messaging-consent-api.md

5.4 KiB

Consent-aware messaging (Phase 4F)

Customers explicitly choose optional promotions and cart recovery reminders for email and WhatsApp independently. All four preferences default to off. Preferences are self-service; staff can check eligibility but cannot grant consent on a customer's behalf. The existing order outbox and account-recovery mail retain their separate transactional contracts.

Method Route Access and result
GET /api/v1/messaging/preferences Own active session; current notice texts and four current channel/purpose preferences.
POST /api/v1/messaging/preferences Own active session; append a versioned grant or withdrawal and return its receipt plus the current preference.
GET /api/v1/messaging/preferences/history Own active session; immutable receipts, newest first, with existing limit/offset pagination.
GET /api/v1/messaging/eligibility/:userId?channel=EMAIL&purpose=PROMOTIONS messaging.read; scoped recipient's current preference and eligibility.

To grant, send { "channel": "EMAIL", "purpose": "PROMOTIONS", "granted": true, "version": 0, "noticeVersion": "2026-10-07", "idempotencyKey": "<uuid>" }. Supported channels are EMAIL and WHATSAPP; supported purposes are PROMOTIONS and CART_RECOVERY. The UI must show the returned notice for the selected purpose and channel before submitting a customer's affirmative choice. Never preselect opt-in or infer consent from an order, login, wishlist, cart activity, review, or business verification.

To withdraw, send the same channel/purpose with granted: false, the current version, and a fresh idempotencyKey, omitting noticeVersion. Withdrawal requires no notice agreement. Bodies are strict: no target user, contact data, arbitrary purpose, or client timestamp is accepted. All successful decisions, including repeated choices with new keys, append the next revision and a messaging.consent.recorded audit event atomically. Timestamps come from the server/database; notice versions and decision receipts are retained as evidence.

Each channel/purpose has its own version starting at zero. Stale writes return CONSENT_CHANGED (409). Reusing a key with changed input returns IDEMPOTENCY_CONFLICT (409). Exact retries return the original immutable decision and the live current preference, so replaying an old grant after withdrawal cannot revive consent or imply current eligibility. Receipt/history responses omit user profile, contact details, internal organization/user scope, hashes, and idempotency keys.

Staff eligibility returns channel, purpose, granted, version, and eligible. Eligibility requires a current grant and an active recipient in the staff member's organization. Missing/foreign recipients return MESSAGING_RECIPIENT_NOT_FOUND (404), unauthorized staff return 403, and inactive customer sessions cannot change preferences. The additive migration grants messaging.read to existing system Owners; custom roles remain explicit.

The shared messagingEligibility function reads the latest decision and live recipient status. A staff preview is not a delivery authorization: future adapters must recheck this gate inside the existing serialized dispatch transaction, including retries, and also validate recipient contact ownership, provider/channel policy, suppression and content eligibility. Consent withdrawal must suppress unsent optional messages. Recovery messages additionally need a currently recoverable cart; promotions need current campaign eligibility. Consent alone does not prove a verified email or WhatsApp destination.

This milestone stores preferences and exposes the reusable eligibility gate. It sends no messages and adds no provider credentials, contact directory, campaign audience export, guest unsubscribe tokens, templates, scheduler, or promotional outbox. Those belong with enabled provider delivery; existing transactional notifications are not converted into promotional messages.

Deploy the new table and permission migration before the application. Existing accounts remain opted out with no inferred decisions. Composite user/organization foreign keys enforce scope; native checks validate terms, a serialized insert guard enforces consecutive revisions, and update/delete triggers protect immutable evidence. No historical migration is changed. Older application code ignores the table; preserve decisions and audit evidence on rollback, and keep optional delivery disabled until it uses the consent gate.

Verification covers default-off and purpose/channel isolation, explicit notice requirements, strict schemas, withdrawal without agreement, stale/conflicting retries, old-grant replay, scoped permission checks, inactive recipients, competing revisions, privacy-preserving history, immutable database records, and migration drift. Multi-connection PostgreSQL verification remains dependent on the project's existing native CI setup.