-
Notifications
You must be signed in to change notification settings - Fork 86
[enhancement] fetch cities by country code and state code #210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -772,15 +772,21 @@ 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 || !iso3) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validation uses OR instead of AND operatorsHigh Severity The validation checks use Additional Locations (1) |
||
| 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()); | ||
| 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); | ||
| } | ||
|
|
@@ -789,7 +795,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()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if (!stateData) { | ||
| return Respond.error(res, 'state not found', 404); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Undefined
iso3causes validation to always failHigh Severity
The variable
iso3is referenced in the validation check and later lookup logic, but it's never destructured fromreq.queryon line 775 (onlycountry,state,iso2, andstate_codeare extracted). Sinceiso3is alwaysundefined, the condition!iso3is alwaystrue, causing the function to return a 400 error for every request regardless of the parameters provided.Additional Locations (1)
controllers/countryController.js#L786-L788