Skip to content

fix(settings): enforce country-scoped regional preference invariants #390

Closed

Description

@khaira777

Refs: #388

Context

FloCafe supports country-specific locale display preferences (currency_display, number_digits, calendar). These are declared per country in main/countries.ts via Country.localeOptions. Currently, Iran (IR) is the primary profile declaring these options (currencyDisplay: ['rial', 'toman', 'toman_short'], digits: ['locale', 'latin'], calendar: ['locale', 'persian', 'gregorian']).

Verified Current Behavior

  • Static Profiles (main/countries.ts lines 14-18, 50-97): Only IR declares localeOptions. Countries like US, CA, GB, IN, etc. have localeOptions: undefined.
  • Backend Validation (main/routes/settings.ts lines 92-102, 183-187): LOCALE_PREFERENCE_VALUES validates incoming preference values against a global union set of all possible options across all countries. Consequently, saving country: 'US' with calendar: 'persian' or currency_display: 'toman' passes validation!
  • Default Returns (main/routes/settings.ts lines 143-145): businessShape() falls back to global defaults (currency_display: s.currency_display || 'rial'), causing non-Iran stores to carry phantom display settings.
  • Frontend Panel (frontend/src/components/settings/LocalePreferencesPanel.tsx): Renders only when the selected country's profile has localeOptions, but the backend does not enforce country-scoped containment on write.

Problem

A client can persist invalid regional preferences for countries that do not support them (e.g. US store persisting Persian calendar or Toman currency display). Furthermore, country transitions (e.g. switching from IR to US, or US to IR) risk leaving stale regional display flags active on the tenant record.

Architectural Decision

  • A stored regional preference must be valid according to the active country's Country.localeOptions.
  • If a country does not declare localeOptions (or does not support a specific option group), the active preference must remain neutral/default.
  • Backward Compatibility Contract:
    • Explicit client submission of an unsupported non-default preference (e.g. PUT /settings/business with country: 'US' and calendar: 'persian') -> Return HTTP 400 with a stable error message (Invalid {field} for country {countryCode}).
    • Stale legacy preferences on existing databases not being explicitly changed -> Safely normalize/default during save without rejecting the request, preventing disruption for older installs.
  • Canonical Storage Invariant:
    • Monetary values in the database remain strictly in canonical currency units (e.g. IRR for Iran). Preferences only affect presentation and UI input adapters.

Exact Implementation Scope

  1. Backend Validation & Normalization (main/routes/settings.ts):
    • Scope validation of currency_display, number_digits, and calendar in PUT /api/settings/business against getCountryByCode(effectiveCountry)?.localeOptions.
    • Reject explicitly provided unsupported options with HTTP 400.
    • When switching countries (e.g. IR -> US), normalize/clear unsupported regional preference settings to their neutral defaults (currency_display reset, number_digits: 'locale', calendar: 'locale').
    • For non-Iran countries, ensure businessShape() does not expose misleading Iran-specific defaults.
  2. Frontend Settings State (frontend/src/app/(dashboard)/settings/page.tsx):
    • When the user selects a different country in Settings, reset/re-evaluate the locale preference state to match the newly selected country's localeOptions.
  3. Tests:
    • Add integration tests covering country transition matrices (IR -> US, US -> IR, fresh US, fresh IR, legacy non-IR databases).

Required Behavior

  • Attempting to set calendar: 'persian' or currency_display: 'toman' for a US or CA store returns HTTP 400.
  • Valid IR store preferences continue to save and format correctly.
  • Transitioning a store from IR to US or CA automatically neutralizes Toman and Persian calendar display preferences.
  • Existing legacy non-IR databases can update general business settings without validation errors.
  • Canonical stored amounts and historical orders/bills remain untouched.

Backward Compatibility / Upgrade Behavior

  • Old databases containing legacy default strings continue to function normally.
  • Normalization occurs seamlessly during updates without requiring database migration scripts.

Offline-First Requirements

  • Purely in-memory and local SQLite execution; 100% offline.

Non-Goals / What NOT to Do

  • Do NOT create a complicated generic preference plugin system.
  • Do NOT alter canonical database currency storage (no Toman in database).
  • Do NOT break existing non-Iran installations when updating business details.

Acceptance Criteria

  • Submitting unsupported locale preferences for a country returns HTTP 400.
  • Valid locale preferences for IR continue to save and format as expected.
  • Changing country from IR to another country cleanses unsupported regional display state.
  • Existing non-IR tenants can update business settings without validation crashes.
  • Canonical monetary storage remains unchanged.

Verification

npm run lint
npm run test:currency
npm run test:rtl-setup-auth-settings
npm test

Dependencies

Relationship to Epic

Activity

  1. added
    area:frontend
    area:backend
    on Aug 18, 2026
  2. khaira777 commented on Aug 20, 2026

    @khaira777
    ContributorAuthor

    Status update — 2026-08-20

    ⚠️ Implementation complete; validation blocked. Commit 282948b1 contains the implementation, but the no-mistakes pipeline failed during the review step before a PR was opened. Next stage: address or rerun validation, then allow the gate to create the PR.

  3. khaira777 commented on Aug 20, 2026

    @khaira777
    ContributorAuthor

    Status update — 2026-08-20

    🔄 Implementation complete; validation active but currently parked. Commit 282948b1 remains intact. The restarted no-mistakes run 01M0GRWKXPW6DQW22YGCG29CH8 rebased the branch to pipeline head 53e7c6b9 and is waiting at review.

    The gate surfaced one real ask-user finding: a timezone-preservation regression in the country-change path, unrelated to the intended locale-preference invariant change. There is still no PR; the next step is to resolve that finding, then continue through tests, lint, push, and PR creation.

  4. khaira777 commented on Aug 21, 2026

    @khaira777
    ContributorAuthor

    Completed — PR #436 merged

    Issue #390 is complete. PR #436 merged into main at 079edbab.

    • Country-scoped currency, digit, and calendar preference invariants are enforced.
    • Explicit timezone overrides are preserved during country changes.
    • Backend normalization and frontend behavior are covered by the issue regression suite.
    • CI and Greptile checks passed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:backendarea:frontend

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions