Add country mapping support for imports and geographic map

- Add normalizeCountryToCode utility to convert country names to ISO-2 codes
- Support English, French and common alternate spellings
- Update Typeform import to support country field mapping
- Update Notion import to support country field mapping
- Allow project.update to set/update country with automatic normalization
- Fix geographic distribution map showing empty when country data exists

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-04 16:13:40 +01:00
parent 6d2537ec04
commit c0f318a867
4 changed files with 233 additions and 1 deletions

View File

@@ -221,3 +221,201 @@ export function getCountryCoordinates(code: string): [number, number] | null {
if (!country) return null
return [country.lat, country.lng]
}
/**
* Country name to ISO-2 code mappings.
* Includes English, French, and common alternate spellings.
*/
const COUNTRY_NAME_TO_CODE: Record<string, string> = {
// Build reverse lookup from COUNTRIES
...Object.fromEntries(
Object.entries(COUNTRIES).flatMap(([code, info]) => [
[info.name.toLowerCase(), code],
])
),
// French names and alternate spellings
'tunisie': 'TN',
'royaume-uni': 'GB',
'uk': 'GB',
'angleterre': 'GB',
'england': 'GB',
'espagne': 'ES',
'inde': 'IN',
'états-unis': 'US',
'etats-unis': 'US',
'usa': 'US',
'allemagne': 'DE',
'italie': 'IT',
'suisse': 'CH',
'belgique': 'BE',
'pays-bas': 'NL',
'australie': 'AU',
'japon': 'JP',
'chine': 'CN',
'brésil': 'BR',
'bresil': 'BR',
'mexique': 'MX',
'maroc': 'MA',
'egypte': 'EG',
'afrique du sud': 'ZA',
'sénégal': 'SN',
'senegal': 'SN',
"côte d'ivoire": 'CI',
'cote d\'ivoire': 'CI',
'indonésie': 'ID',
'indonesie': 'ID',
'thaïlande': 'TH',
'thailande': 'TH',
'malaisie': 'MY',
'singapour': 'SG',
'grèce': 'GR',
'grece': 'GR',
'turquie': 'TR',
'pologne': 'PL',
'norvège': 'NO',
'norvege': 'NO',
'suède': 'SE',
'suede': 'SE',
'danemark': 'DK',
'finlande': 'FI',
'irlande': 'IE',
'autriche': 'AT',
'nigéria': 'NG',
'nigeria': 'NG',
'tanzanie': 'TZ',
'ouganda': 'UG',
'zambie': 'ZM',
'somalie': 'SO',
'jordanie': 'JO',
'algérie': 'DZ',
'algerie': 'DZ',
'cameroun': 'CM',
'maurice': 'MU',
'malte': 'MT',
'croatie': 'HR',
'roumanie': 'RO',
'hongrie': 'HU',
'tchéquie': 'CZ',
'tcheque': 'CZ',
'slovaquie': 'SK',
'slovénie': 'SI',
'estonie': 'EE',
'lettonie': 'LV',
'lituanie': 'LT',
'chypre': 'CY',
'malawi': 'MW',
'mozambique': 'MZ',
'namibie': 'NA',
'botswana': 'BW',
'zimbabwe': 'ZW',
'éthiopie': 'ET',
'ethiopie': 'ET',
'soudan': 'SD',
'libye': 'LY',
'arabie saoudite': 'SA',
'émirats arabes unis': 'AE',
'emirats arabes unis': 'AE',
'uae': 'AE',
'qatar': 'QA',
'koweït': 'KW',
'koweit': 'KW',
'bahreïn': 'BH',
'bahrein': 'BH',
'oman': 'OM',
'yémen': 'YE',
'yemen': 'YE',
'irak': 'IQ',
'iran': 'IR',
'afghanistan': 'AF',
'pakistan': 'PK',
'bangladesh': 'BD',
'sri lanka': 'LK',
'népal': 'NP',
'nepal': 'NP',
'birmanie': 'MM',
'myanmar': 'MM',
'cambodge': 'KH',
'laos': 'LA',
'corée du sud': 'KR',
'coree du sud': 'KR',
'south korea': 'KR',
'corée du nord': 'KP',
'coree du nord': 'KP',
'north korea': 'KP',
'nouvelle-zélande': 'NZ',
'nouvelle zelande': 'NZ',
'fidji': 'FJ',
'fiji': 'FJ',
'papouasie-nouvelle-guinée': 'PG',
'argentine': 'AR',
'chili': 'CL',
'colombie': 'CO',
'pérou': 'PE',
'perou': 'PE',
'venezuela': 'VE',
'équateur': 'EC',
'equateur': 'EC',
'bolivie': 'BO',
'paraguay': 'PY',
'uruguay': 'UY',
'costa rica': 'CR',
'panama': 'PA',
'guatemala': 'GT',
'honduras': 'HN',
'salvador': 'SV',
'nicaragua': 'NI',
'cuba': 'CU',
'haïti': 'HT',
'haiti': 'HT',
'jamaïque': 'JM',
'jamaique': 'JM',
'trinidad': 'TT',
'trinité-et-tobago': 'TT',
'république dominicaine': 'DO',
'republique dominicaine': 'DO',
'dominican republic': 'DO',
'puerto rico': 'PR',
'porto rico': 'PR',
}
/**
* Convert a country name or code to ISO-2 code.
* Handles:
* - Already valid ISO-2 codes (returns as-is)
* - Full country names (English or French)
* - Common alternate spellings
*
* @param input Country name or code
* @returns ISO-2 code or null if not recognized
*/
export function normalizeCountryToCode(input: string | null | undefined): string | null {
if (!input) return null
const trimmed = input.trim()
if (!trimmed) return null
// If already a valid 2-letter ISO code
if (/^[A-Z]{2}$/.test(trimmed) && COUNTRIES[trimmed]) {
return trimmed
}
// Check uppercase version
const upper = trimmed.toUpperCase()
if (/^[A-Z]{2}$/.test(upper) && COUNTRIES[upper]) {
return upper
}
// Try to find in name mappings
const lower = trimmed.toLowerCase()
const code = COUNTRY_NAME_TO_CODE[lower]
if (code) return code
// Try partial matching for country names
for (const [name, countryCode] of Object.entries(COUNTRY_NAME_TO_CODE)) {
if (lower.includes(name) || name.includes(lower)) {
return countryCode
}
}
return null
}