Feature: Add territory-to-country mapping for 50+ dependent territories

This commit is contained in:
2026-02-14 15:13:26 -05:00
parent ca4c76d1a0
commit 7798179376

View File

@@ -18,21 +18,81 @@ export interface CountryData {
export class WorldBankService {
private apiUrl = 'https://api.worldbank.org/v2/country';
// Map territories to their parent countries for World Bank API
private territoryMapping: { [key: string]: string } = {
'AX': 'FI', // Åland Islands → Finland
'AW': 'NL', // Aruba → Netherlands
'BL': 'FR', // Saint Barthélemy → France
'BQ': 'NL', // Caribbean Netherlands → Netherlands
'CW': 'NL', // Curaçao → Netherlands
'GF': 'FR', // French Guiana → France
'GL': 'DK', // Greenland → Denmark
'GP': 'FR', // Guadeloupe → France
'GG': 'GB', // Guernsey → United Kingdom
'IM': 'GB', // Isle of Man → United Kingdom
'JE': 'GB', // Jersey → United Kingdom
'MF': 'FR', // Saint Martin → France
'MQ': 'FR', // Martinique → France
'NC': 'FR', // New Caledonia → France
'PF': 'FR', // French Polynesia → France
'PM': 'FR', // Saint Pierre and Miquelon → France
'PR': 'US', // Puerto Rico → United States
'RE': 'FR', // Réunion → France
'SJ': 'NO', // Svalbard and Jan Mayen → Norway
'SX': 'NL', // Sint Maarten → Netherlands
'TF': 'FR', // French Southern Territories → France
'VI': 'US', // U.S. Virgin Islands → United States
'WF': 'FR', // Wallis and Futuna → France
'YT': 'FR', // Mayotte → France
'EH': 'MA', // Western Sahara → Morocco (disputed, using Morocco)
'FK': 'GB', // Falkland Islands → United Kingdom
'FO': 'DK', // Faroe Islands → Denmark
'GI': 'GB', // Gibraltar → United Kingdom
'GS': 'GB', // South Georgia → United Kingdom
'GU': 'US', // Guam → United States
'HM': 'AU', // Heard Island → Australia
'AS': 'US', // American Samoa → United States
'BM': 'GB', // Bermuda → United Kingdom
'BV': 'NO', // Bouvet Island → Norway
'IO': 'GB', // British Indian Ocean Territory → United Kingdom
'KY': 'GB', // Cayman Islands → United Kingdom
'CX': 'AU', // Christmas Island → Australia
'CC': 'AU', // Cocos Islands → Australia
'CK': 'NZ', // Cook Islands → New Zealand
'MS': 'GB', // Montserrat → United Kingdom
'NF': 'AU', // Norfolk Island → Australia
'NU': 'NZ', // Niue → New Zealand
'PN': 'GB', // Pitcairn Islands → United Kingdom
'SH': 'GB', // Saint Helena → United Kingdom
'TC': 'GB', // Turks and Caicos → United Kingdom
'TK': 'NZ', // Tokelau → New Zealand
'UM': 'US', // U.S. Minor Outlying Islands → United States
'VG': 'GB', // British Virgin Islands → United Kingdom
};
constructor(private http: HttpClient) { }
getCountryData(countryCode: string): Observable<CountryData> {
const url = `${this.apiUrl}/${countryCode}?format=json`;
// Check if this is a territory - if so, use parent country
const actualCode = this.territoryMapping[countryCode] || countryCode;
const url = `${this.apiUrl}/${actualCode}?format=json`;
return this.http.get<any>(url).pipe(
map(response => {
// Check if API returned an error message
if (response[0]?.message) {
throw new Error(`World Bank API: ${response[0].message[0].value}`);
}
const country = response[1][0];
// If this was a territory lookup, customize the name
let displayName = country.name || 'N/A';
if (countryCode !== actualCode) {
displayName = `${this.getTerritoryName(countryCode)} (data from ${country.name})`;
}
return {
name: country.name || 'N/A',
name: displayName,
capital: country.capitalCity || 'N/A',
region: country.region?.value || 'N/A',
incomeLevel: country.incomeLevel?.value || 'N/A',
@@ -42,11 +102,10 @@ export class WorldBankService {
}),
catchError(error => {
console.error(`Failed to fetch data for ${countryCode}:`, error);
// Return placeholder data instead of failing completely
return of({
name: `Country Code: ${countryCode}`,
name: `Territory: ${countryCode}`,
capital: 'Data not available',
region: 'Not available in World Bank API',
region: 'Not in World Bank database',
incomeLevel: 'N/A',
longitude: 'N/A',
latitude: 'N/A'
@@ -54,4 +113,26 @@ export class WorldBankService {
})
);
}
private getTerritoryName(code: string): string {
const names: { [key: string]: string } = {
'AX': 'Åland Islands', 'AW': 'Aruba', 'BL': 'Saint Barthélemy',
'BQ': 'Caribbean Netherlands', 'CW': 'Curaçao', 'GF': 'French Guiana',
'GL': 'Greenland', 'GP': 'Guadeloupe', 'GG': 'Guernsey',
'IM': 'Isle of Man', 'JE': 'Jersey', 'MF': 'Saint Martin',
'MQ': 'Martinique', 'NC': 'New Caledonia', 'PF': 'French Polynesia',
'PM': 'Saint Pierre and Miquelon', 'PR': 'Puerto Rico', 'RE': 'Réunion',
'SJ': 'Svalbard and Jan Mayen', 'SX': 'Sint Maarten', 'TF': 'French Southern Territories',
'VI': 'U.S. Virgin Islands', 'WF': 'Wallis and Futuna', 'YT': 'Mayotte',
'EH': 'Western Sahara', 'FK': 'Falkland Islands', 'FO': 'Faroe Islands',
'GI': 'Gibraltar', 'GS': 'South Georgia', 'GU': 'Guam',
'HM': 'Heard Island', 'AS': 'American Samoa', 'BM': 'Bermuda',
'BV': 'Bouvet Island', 'IO': 'British Indian Ocean Territory', 'KY': 'Cayman Islands',
'CX': 'Christmas Island', 'CC': 'Cocos Islands', 'CK': 'Cook Islands',
'MS': 'Montserrat', 'NF': 'Norfolk Island', 'NU': 'Niue',
'PN': 'Pitcairn Islands', 'SH': 'Saint Helena', 'TC': 'Turks and Caicos',
'TK': 'Tokelau', 'UM': 'U.S. Minor Outlying Islands', 'VG': 'British Virgin Islands'
};
return names[code] || code;
}
}