28 lines
824 B
TypeScript
28 lines
824 B
TypeScript
import * as countryRepository from '../repositories/countryRepository';
|
|
|
|
const UNKNOWN_COUNTRY_CODE = 'XX';
|
|
|
|
let countryMapCache: Map<string, string> | null = null;
|
|
|
|
export async function getCountryNameMap(): Promise<Map<string, string>> {
|
|
if (!countryMapCache) {
|
|
const countries = await countryRepository.listAll();
|
|
countryMapCache = new Map(countries.map((c) => [c.code, c.name]));
|
|
}
|
|
return countryMapCache;
|
|
}
|
|
|
|
/** Clears cached country names (e.g. after admin sync). */
|
|
export function clearCountryNameCache(): void {
|
|
countryMapCache = null;
|
|
}
|
|
|
|
export function formatCountryCode(
|
|
code: string | null | undefined,
|
|
map: Map<string, string>
|
|
): string {
|
|
if (!code) return '';
|
|
if (code === UNKNOWN_COUNTRY_CODE) return 'Non renseigné';
|
|
return map.get(code) ?? map.get(code.toUpperCase()) ?? code;
|
|
}
|