Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .changeset/28872-workspace-prompt-currency.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
'@objectstack/spec': patch
'@objectstack/plugin-auth': patch
'@object-ui/console': patch
'@object-ui/i18n': patch
---

feat(console): the workspace prompt also asks an administrator for a default currency that is still unset, offering the browser region's currency

Clause-②: yes (widening)
Metadata-protocol: none

`@objectstack/spec/shared` gains `REGION_CURRENCY` and `currencyForLocale`, the checked-in CLDR region → currency table and its lookup. They move there from `@objectstack/plugin-auth`, which gives a new organization its creator's currency through them and no longer keeps its own copy. The table and the mapping are unchanged. Neither name was exported from the plugin-auth package entry, so nothing an importer of that package used moves.

The console's one-time workspace prompt (Setup → Localization's keys, asked once per administrator per workspace) now covers `localization.currency` too. While that key has no value on any rung, code-less amounts show as plain numbers; an administrator who may write the namespace is asked once, with the field pre-filled with the currency of the browser's first preferred language (`zh-CN` → CNY, shown as "人民币(CNY)" in Chinese). Confirm writes it through the Settings page's own `PUT /api/settings/localization`; "Not now" writes nothing. When the timezone is still the default as well, it is one dialog with both fields and one write. A browser language with no country region (`es-419`) offers no currency, and the prompt does not ask about it. No settings key, server-side dismissed flag or gate is added: the prompt keeps its existing per-device record, so an administrator already asked about the timezone is not asked again.

`@object-ui/i18n` gains eight `console.workspaceTimezonePrompt.*` strings in every built-in locale.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

/**
* [#25495] The derivation and composition halves of a new organization's
* default currency, in isolation: the CLDR table, the locale → currency
* mapping, the host-locale reading, the never-overwrite rule and the wrapping
* of a host's `onOrganizationCreated`. The composed behaviour, through a real
* default currency, in isolation: the host-locale reading, the never-overwrite
* rule and the wrapping of a host's `onOrganizationCreated`. The CLDR table and
* the locale → currency mapping live in `@objectstack/spec/shared`, and
* `region-currency.test.ts` there pins them. The composed behaviour, through a real
* `POST /organization/create` and the real settings service, is
* `organization-created-default-currency.test.ts`.
*/

import { describe, expect, it, vi } from 'vitest';
import { isValueDomainMember } from '@objectstack/spec/shared';
import {
REGION_CURRENCY,
creatorLocaleForNewOrganization,
currencyForLocale,
hostLocaleFromEnv,
seedOrganizationCurrency,
withOrganizationDefaultCurrency,
Expand All @@ -36,47 +34,6 @@ function door(source: string) {

const silent = { info: () => {}, warn: () => {}, debug: () => {} };

describe('REGION_CURRENCY — the checked-in CLDR snapshot', () => {
it('holds only codes the settings key accepts (the iso_4217_currency value domain)', () => {
const outside = Object.entries(REGION_CURRENCY).filter(([, code]) => !isValueDomainMember('iso_4217_currency', code));
expect(outside).toEqual([]);
});

it('carries the regions the card names, and none of the CLDR regions with no current tender', () => {
expect(REGION_CURRENCY).toMatchObject({ CN: 'CNY', JP: 'JPY', GB: 'GBP', DE: 'EUR', FR: 'EUR', US: 'USD' });
for (const region of ['AQ', 'ZZ', 'SU', 'YU']) expect(REGION_CURRENCY).not.toHaveProperty(region);
expect(Object.keys(REGION_CURRENCY)).toHaveLength(255);
});
});

describe('currencyForLocale', () => {
it.each([
['zh-CN', 'CNY'],
['zh', 'CNY'],
['ja', 'JPY'],
['ja-JP', 'JPY'],
['en-GB', 'GBP'],
['de', 'EUR'],
['fr-FR', 'EUR'],
['en', 'USD'],
['zh-Hant', 'TWD'],
[' zh-CN ', 'CNY'],
])('%s → %s', (tag, currency) => {
expect(currencyForLocale(tag)).toBe(currency);
});

it.each([
['a region that is not a country', 'es-419'],
['another region that is not a country', 'en-150'],
['a malformed tag', 'not a tag!'],
['an empty string', ''],
['undefined', undefined],
['a non-string', 42],
])('%s maps to nothing', (_label, tag) => {
expect(currencyForLocale(tag)).toBeUndefined();
});
});

describe('hostLocaleFromEnv', () => {
it('reads LANG the POSIX way, dropping the codeset and modifier', () => {
expect(hostLocaleFromEnv({ LANG: 'zh_CN.UTF-8' })).toBe('zh-CN');
Expand Down
86 changes: 4 additions & 82 deletions packages/plugins/plugin-auth/src/organization-default-currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
* - **The mapping** ({@link currencyForLocale}): the tag's region, or, for a
* bare language, the region `Intl.Locale#maximize()` gives it (`zh` → `CN`,
* `ja` → `JP`, `en` → `US`), then that region's currency from
* {@link REGION_CURRENCY}, a checked-in CLDR snapshot. A tag with no country
* `REGION_CURRENCY`, a checked-in CLDR snapshot. Both live in
* `@objectstack/spec/shared` (`region-currency.ts`), which the console's
* workspace prompt reads too, so there is one table. A tag with no country
* region (`es-419`, `en-150`) or a region with no current currency maps to
* nothing.
* - **Nothing to derive from ⇒ nothing is written.** There is no fallback to
Expand All @@ -48,90 +50,10 @@

import { tryGetCurrentAuthEndpointContext } from '@better-auth/core/context';
import type { IDataEngine } from '@objectstack/spec/contracts';
import { currencyForLocale } from '@objectstack/spec/shared';
import { preferredLocaleFromHeader, SystemObjectName } from '@objectstack/spec/system';
import { acceptLanguageHeader } from './auth-manager.js';

/**
* Region (ISO 3166-1 alpha-2 and the CLDR territory codes beside it) → the
* region's current ISO 4217 currency.
*
* ## Provenance: CLDR `currencyData`, checked in, not hand-written
*
* Generated on 2026-10-24 from CLDR 48 `supplemental/currencyData.json`
* (`cldr-core@48.0.0` on npm, sha256 of that file
* `ab007c5bb654a11601d6328f73a9b5b504334c556f3edb760020d6e2cc408472`), the
* same CLDR release as `CURRENCY_FRACTION_DIGITS` in `@objectstack/spec`.
* For each two-letter region the value is the FIRST entry of its
* `currencyData.region` list that has no `_to` date and is not
* `_tender: "false"`. CLDR orders that list so the first current tender is the
* region's primary currency (`PS` → `ILS` ahead of `JOD`). Entries are kept
* only when the code is in the `iso_4217_currency` value domain, the set the
* settings key accepts. Eleven regions have no current tender and are absent:
* `AQ` and `ZZ` (`XXX`), and the retired `BU` `CP` `CS` `DD` `SU` `TP` `YD`
* `YU` `ZR`.
*
* `Intl` has no region → currency lookup, which is why this is a table. To
* refresh it for a later CLDR, regenerate it by the rule above and update this
* provenance block. Do not edit entries by hand.
*/
export const REGION_CURRENCY: Readonly<Record<string, string>> = {
AC: 'SHP', AD: 'EUR', AE: 'AED', AF: 'AFN', AG: 'XCD', AI: 'XCD', AL: 'ALL', AM: 'AMD',
AO: 'AOA', AR: 'ARS', AS: 'USD', AT: 'EUR', AU: 'AUD', AW: 'AWG', AX: 'EUR', AZ: 'AZN',
BA: 'BAM', BB: 'BBD', BD: 'BDT', BE: 'EUR', BF: 'XOF', BG: 'EUR', BH: 'BHD', BI: 'BIF',
BJ: 'XOF', BL: 'EUR', BM: 'BMD', BN: 'BND', BO: 'BOB', BQ: 'USD', BR: 'BRL', BS: 'BSD',
BT: 'BTN', BV: 'NOK', BW: 'BWP', BY: 'BYN', BZ: 'BZD', CA: 'CAD', CC: 'AUD', CD: 'CDF',
CF: 'XAF', CG: 'XAF', CH: 'CHF', CI: 'XOF', CK: 'NZD', CL: 'CLP', CM: 'XAF', CN: 'CNY',
CO: 'COP', CR: 'CRC', CU: 'CUP', CV: 'CVE', CW: 'XCG', CX: 'AUD', CY: 'EUR', CZ: 'CZK',
DE: 'EUR', DG: 'USD', DJ: 'DJF', DK: 'DKK', DM: 'XCD', DO: 'DOP', DZ: 'DZD', EA: 'EUR',
EC: 'USD', EE: 'EUR', EG: 'EGP', EH: 'MAD', ER: 'ERN', ES: 'EUR', ET: 'ETB', EU: 'EUR',
FI: 'EUR', FJ: 'FJD', FK: 'FKP', FM: 'USD', FO: 'DKK', FR: 'EUR', GA: 'XAF', GB: 'GBP',
GD: 'XCD', GE: 'GEL', GF: 'EUR', GG: 'GBP', GH: 'GHS', GI: 'GIP', GL: 'DKK', GM: 'GMD',
GN: 'GNF', GP: 'EUR', GQ: 'XAF', GR: 'EUR', GS: 'GBP', GT: 'GTQ', GU: 'USD', GW: 'XOF',
GY: 'GYD', HK: 'HKD', HM: 'AUD', HN: 'HNL', HR: 'EUR', HT: 'HTG', HU: 'HUF', IC: 'EUR',
ID: 'IDR', IE: 'EUR', IL: 'ILS', IM: 'GBP', IN: 'INR', IO: 'USD', IQ: 'IQD', IR: 'IRR',
IS: 'ISK', IT: 'EUR', JE: 'GBP', JM: 'JMD', JO: 'JOD', JP: 'JPY', KE: 'KES', KG: 'KGS',
KH: 'KHR', KI: 'AUD', KM: 'KMF', KN: 'XCD', KP: 'KPW', KR: 'KRW', KW: 'KWD', KY: 'KYD',
KZ: 'KZT', LA: 'LAK', LB: 'LBP', LC: 'XCD', LI: 'CHF', LK: 'LKR', LR: 'LRD', LS: 'ZAR',
LT: 'EUR', LU: 'EUR', LV: 'EUR', LY: 'LYD', MA: 'MAD', MC: 'EUR', MD: 'MDL', ME: 'EUR',
MF: 'EUR', MG: 'MGA', MH: 'USD', MK: 'MKD', ML: 'XOF', MM: 'MMK', MN: 'MNT', MO: 'MOP',
MP: 'USD', MQ: 'EUR', MR: 'MRU', MS: 'XCD', MT: 'EUR', MU: 'MUR', MV: 'MVR', MW: 'MWK',
MX: 'MXN', MY: 'MYR', MZ: 'MZN', NA: 'NAD', NC: 'XPF', NE: 'XOF', NF: 'AUD', NG: 'NGN',
NI: 'NIO', NL: 'EUR', NO: 'NOK', NP: 'NPR', NR: 'AUD', NU: 'NZD', NZ: 'NZD', OM: 'OMR',
PA: 'PAB', PE: 'PEN', PF: 'XPF', PG: 'PGK', PH: 'PHP', PK: 'PKR', PL: 'PLN', PM: 'EUR',
PN: 'NZD', PR: 'USD', PS: 'ILS', PT: 'EUR', PW: 'USD', PY: 'PYG', QA: 'QAR', RE: 'EUR',
RO: 'RON', RS: 'RSD', RU: 'RUB', RW: 'RWF', SA: 'SAR', SB: 'SBD', SC: 'SCR', SD: 'SDG',
SE: 'SEK', SG: 'SGD', SH: 'SHP', SI: 'EUR', SJ: 'NOK', SK: 'EUR', SL: 'SLE', SM: 'EUR',
SN: 'XOF', SO: 'SOS', SR: 'SRD', SS: 'SSP', ST: 'STN', SV: 'USD', SX: 'XCG', SY: 'SYP',
SZ: 'SZL', TA: 'GBP', TC: 'USD', TD: 'XAF', TF: 'EUR', TG: 'XOF', TH: 'THB', TJ: 'TJS',
TK: 'NZD', TL: 'USD', TM: 'TMT', TN: 'TND', TO: 'TOP', TR: 'TRY', TT: 'TTD', TV: 'AUD',
TW: 'TWD', TZ: 'TZS', UA: 'UAH', UG: 'UGX', UM: 'USD', US: 'USD', UY: 'UYU', UZ: 'UZS',
VA: 'EUR', VC: 'XCD', VE: 'VES', VG: 'USD', VI: 'USD', VN: 'VND', VU: 'VUV', WF: 'XPF',
WS: 'WST', XK: 'EUR', YE: 'YER', YT: 'EUR', ZA: 'ZAR', ZM: 'ZMW', ZW: 'ZWG',
};

/**
* The ISO 4217 currency of a BCP 47 tag's region, or `undefined`.
*
* The region is the tag's own (`zh-CN` → `CN`), or for a tag without one the
* likely region `Intl.Locale#maximize()` gives (`zh` → `CN`, `ja` → `JP`,
* `zh-Hant` → `TW`). A malformed tag, a tag whose region is not a country
* (`es-419`, `en-150`), and a region with no current currency all answer
* `undefined`: no guess is made.
*/
export function currencyForLocale(tag: unknown): string | undefined {
if (typeof tag !== 'string') return undefined;
const value = tag.trim();
if (!value) return undefined;
let region: string | undefined;
try {
region = new Intl.Locale(value).maximize().region;
} catch {
return undefined;
}
if (!region || !Object.prototype.hasOwnProperty.call(REGION_CURRENCY, region)) return undefined;
return REGION_CURRENCY[region];
}

/**
* The host's locale as a BCP 47 tag, read from the POSIX environment, or
* `undefined`.
Expand Down
2 changes: 2 additions & 0 deletions packages/spec/api-surface/shared.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"Protection (type)",
"ProtectionSchema (const)",
"QUALIFIED_ITEM_NAME_PATTERN (const)",
"REGION_CURRENCY (const)",
"RETIRED_KEY_UPGRADE_GUIDE_SENTENCE (const)",
"RateLimitConfig (type)",
"RateLimitConfigParsed (type)",
Expand Down Expand Up @@ -104,6 +105,7 @@
"canonicalMetaUrlType (function)",
"cel (function)",
"cron (function)",
"currencyForLocale (function)",
"expression (function)",
"findClosestMatches (function)",
"formatSuggestion (function)",
Expand Down
2 changes: 2 additions & 0 deletions packages/spec/export-origins/shared.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"Protection": "src/shared/protection.zod.ts#Protection (type)",
"ProtectionSchema": "src/shared/protection.zod.ts#ProtectionSchema (const)",
"QUALIFIED_ITEM_NAME_PATTERN": "src/shared/identifiers.zod.ts#QUALIFIED_ITEM_NAME_PATTERN (const)",
"REGION_CURRENCY": "src/shared/region-currency.ts#REGION_CURRENCY (const)",
"RETIRED_KEY_UPGRADE_GUIDE_SENTENCE": "src/shared/protocol-upgrade-guide.ts#RETIRED_KEY_UPGRADE_GUIDE_SENTENCE (const)",
"RateLimitConfig": "src/shared/http.zod.ts#RateLimitConfig (type)",
"RateLimitConfigParsed": "src/shared/http.zod.ts#RateLimitConfigParsed (type)",
Expand Down Expand Up @@ -99,6 +100,7 @@
"canonicalMetaUrlType": "src/meta-spelling/metadata-url-spelling.ts#canonicalMetaUrlType (function)",
"cel": "src/shared/expression.zod.ts#cel (function)",
"cron": "src/shared/expression.zod.ts#cron (function)",
"currencyForLocale": "src/shared/region-currency.ts#currencyForLocale (function)",
"expression": "src/shared/expression.zod.ts#expression (function)",
"findClosestMatches": "src/shared/suggestions.zod.ts#findClosestMatches (function)",
"formatSuggestion": "src/shared/suggestions.zod.ts#formatSuggestion (function)",
Expand Down
4 changes: 4 additions & 0 deletions packages/spec/src/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export * from './resilient-fetch';
// specifiers and object fields (maintainer ruling 2026-09-02). Declared here so
// `system/` and `data/` both reference one schema instead of carrying a copy.
export * from './value-domain.zod';
// The CLDR region → currency table and its lookup, read by plugin-auth (a new
// organization's default currency) and by the console's workspace prompt, so
// neither carries its own copy. Imports nothing.
export * from './region-currency';
// [#15676] The shared epoch-milliseconds INSTANT (`EpochMs`), and the first of
// the two structural exemptions ruling B of #14478 declares ON THE SCHEMA
// rather than in a gate ledger: a key whose value is this schema is an instant,
Expand Down
61 changes: 61 additions & 0 deletions packages/spec/src/shared/region-currency.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.

/**
* `shared/region-currency.ts`: the checked-in CLDR region → currency table and
* its lookup, read by plugin-auth (a new organization's currency) and by the
* console (the workspace prompt's offer). Both readers' own tests drive them
* through this module; the table and the mapping are pinned here, once.
*/

import { describe, expect, it } from 'vitest';
import { REGION_CURRENCY, currencyForLocale } from './region-currency';
import { isValueDomainMember } from './value-domain.zod';
import * as shared from './index';

describe('REGION_CURRENCY — the checked-in CLDR snapshot', () => {
it('holds only codes the settings key accepts (the iso_4217_currency value domain)', () => {
const outside = Object.entries(REGION_CURRENCY).filter(([, code]) => !isValueDomainMember('iso_4217_currency', code));
expect(outside).toEqual([]);
});

it('carries the regions the card names, and none of the CLDR regions with no current tender', () => {
expect(REGION_CURRENCY).toMatchObject({ CN: 'CNY', JP: 'JPY', GB: 'GBP', DE: 'EUR', FR: 'EUR', US: 'USD' });
for (const region of ['AQ', 'ZZ', 'SU', 'YU']) expect(REGION_CURRENCY).not.toHaveProperty(region);
expect(Object.keys(REGION_CURRENCY)).toHaveLength(255);
});
});

describe('currencyForLocale', () => {
it.each([
['zh-CN', 'CNY'],
['zh', 'CNY'],
['ja', 'JPY'],
['ja-JP', 'JPY'],
['en-GB', 'GBP'],
['de', 'EUR'],
['fr-FR', 'EUR'],
['en', 'USD'],
['zh-Hant', 'TWD'],
[' zh-CN ', 'CNY'],
])('%s → %s', (tag, currency) => {
expect(currencyForLocale(tag)).toBe(currency);
});

it.each([
['a region that is not a country', 'es-419'],
['another region that is not a country', 'en-150'],
['a malformed tag', 'not a tag!'],
['an empty string', ''],
['undefined', undefined],
['a non-string', 42],
])('%s maps to nothing', (_label, tag) => {
expect(currencyForLocale(tag)).toBeUndefined();
});
});

describe('the @objectstack/spec/shared entry', () => {
it('re-exports the one table and the one lookup, not copies', () => {
expect(shared.REGION_CURRENCY).toBe(REGION_CURRENCY);
expect(shared.currencyForLocale).toBe(currencyForLocale);
});
});
Loading
Loading