Skip to content

Commit

Permalink
Look up the distinct country codes used in the database #2559
Browse files Browse the repository at this point in the history
  • Loading branch information
iamleeg committed Mar 14, 2022
1 parent cde90ea commit 97d1656
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
19 changes: 19 additions & 0 deletions verification/curator-service/api/openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,16 @@ paths:
$ref: '#/components/responses/400'
'500':
$ref: '#/components/responses/500'
/geocode/countryNames:
get:
tags: [Geocode]
summary: Get a list of all the ISO-3166-1 country codes in use, and the names we accept for those countries.
operationId: countryNames
responses:
'200':
$ref: '#/components/responses/200CountryNames'
'500':
$ref: '#/components/responses/500'
/users:
get:
tags: [User]
Expand Down Expand Up @@ -1928,6 +1938,9 @@ components:
type: array
items:
$ref: '#/components/schemas/Location'
CountryNames:
description: A dictionary where the keys are ISO-3166-1 country codes and the values names used in Global.health for those countries
type: object
BatchCaseUpdateResponse:
description: Response to batch update cases API requests
properties:
Expand Down Expand Up @@ -2080,6 +2093,12 @@ components:
application/json:
schema:
$ref: '#/components/schemas/Position'
'200CountryNames':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/CountryNames'
'201Case':
description: Created
content:
Expand Down
24 changes: 23 additions & 1 deletion verification/curator-service/api/src/controllers/geocode.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Request, Response } from 'express';

import axios, { AxiosError } from 'axios';
import mongoose from 'mongoose';

import { logger } from '../util/logger';

/** Dumb proxy to geocoder in data service */
/**
* Dumb proxy to geocoder in location service
* For the most part, anyway: see countryNames
*/
export default class GeocodeProxy {
constructor(private readonly locationServiceURL: string) {}

Expand Down Expand Up @@ -39,6 +44,23 @@ export default class GeocodeProxy {
}
}
}

/**
* This is the only "meaty" method on this controller. It does proxy the
* location service, but it also uses the database and two npm modules to
* find out what ISO-3166-1 country codes are in use in the database and
* what names are used for those countries.
* @param req Express request
* @param res Express response
*/
countryNames = async (req: Request, res: Response): Promise<void> => {
const mongoClient = mongoose.connection.getClient();
const locationCountryCodes = await mongoClient.db().collection('cases').distinct('location.country');
const travelHistoryCodes = await mongoClient.db().collection('cases').distinct('travelHistory.travel.location.country');
const allCodes = Array.from(new Set(locationCountryCodes.concat(travelHistoryCodes)).values());
res.status(200).json({'codes': allCodes});
}

seed = async (req: Request, res: Response): Promise<void> => {
const response = await axios.post(
this.locationServiceURL + req.url,
Expand Down
6 changes: 6 additions & 0 deletions verification/curator-service/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,12 @@ apiRouter.get(
mustHaveAnyRole(['curator']),
geocodeProxy.convertUTM,
);
apiRouter.get(
'/geocode/countryNames',
authenticateByAPIKey,
mustBeAuthenticated,
geocodeProxy.countryNames
);
apiRouter.post('/geocode/seed', geocodeProxy.seed);
apiRouter.post('/geocode/clear', geocodeProxy.clear);

Expand Down

0 comments on commit 97d1656

Please sign in to comment.