From 6b1908484298c51283acbd6b2f96f1f3bd9bd09f Mon Sep 17 00:00:00 2001 From: Vinay Date: Tue, 11 Mar 2025 00:01:19 +0530 Subject: [PATCH 1/2] [enhancement] fetch cities by country code and state code --- controllers/countryController.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/controllers/countryController.js b/controllers/countryController.js index 150d8ee..e2e44d6 100644 --- a/controllers/countryController.js +++ b/controllers/countryController.js @@ -772,15 +772,15 @@ class CountryController { */ static async getStateCities(req, res, next) { try { - const { country, state } = req.query; - if (!country) { + const { country, state, iso2, state_code } = req.query; + if (!country || !iso2) { return Respond.error(res, 'missing param (country)', 400); } - if (!state) { + if (!state || !state_code) { return Respond.error(res, 'missing param (state)', 400); } - const countryData = Object.values(CountriesStateCityFormatted).find((x) => x.name.toLowerCase() === country.toLowerCase()); + const countryData = country ? Object.values(CountriesStateCityFormatted).find((x) => x.name.toLowerCase() === country.toLowerCase()) : Object.values(CountriesStateCityFormatted).find((x) => x.iso2.toLowerCase() === iso2.toLowerCase()); if (!countryData) { return Respond.error(res, 'country not found', 404); } @@ -789,7 +789,7 @@ class CountryController { name: x.name.trim().toLowerCase().endsWith('state') ? x.name.toLowerCase().trim() : x.name, cities: x.cities, })); - const stateData = Object.values(statesFormatted).find((x) => x.name.toLowerCase() === state.toLowerCase()); + const stateData = state ? Object.values(statesFormatted).find((x) => x.name.toLowerCase() === state.toLowerCase()) : Object.values(statesFormatted).find((x) => x.state_code.toLowerCase() === state_code.toLowerCase()); if (!stateData) { return Respond.error(res, 'state not found', 404); } From 2812a9231b1d8321fd648361632bec62da001804 Mon Sep 17 00:00:00 2001 From: Vinay Date: Tue, 11 Mar 2025 23:56:19 +0530 Subject: [PATCH 2/2] check iso3 in country code --- controllers/countryController.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/controllers/countryController.js b/controllers/countryController.js index e2e44d6..ebdff4b 100644 --- a/controllers/countryController.js +++ b/controllers/countryController.js @@ -773,14 +773,20 @@ class CountryController { static async getStateCities(req, res, next) { try { const { country, state, iso2, state_code } = req.query; - if (!country || !iso2) { + if (!country || !iso2 || !iso3) { return Respond.error(res, 'missing param (country)', 400); } if (!state || !state_code) { return Respond.error(res, 'missing param (state)', 400); } - - const countryData = country ? Object.values(CountriesStateCityFormatted).find((x) => x.name.toLowerCase() === country.toLowerCase()) : Object.values(CountriesStateCityFormatted).find((x) => x.iso2.toLowerCase() === iso2.toLowerCase()); + let countryData; + if(country){ + countryData = Object.values(CountriesStateCityFormatted).find((x) => x.name.toLowerCase() === country.toLowerCase()) + } else if(iso2){ + countryData = Object.values(CountriesStateCityFormatted).find((x) => x.iso2.toLowerCase() === iso2.toLowerCase()) + } else if(iso3){ + countryData = Object.values(CountriesStateCityFormatted).find((x) => x.iso3.toLowerCase() === iso3.toLowerCase()) + } if (!countryData) { return Respond.error(res, 'country not found', 404); }