Skip to content

Commit

Permalink
extract Place findByLocaleString method; use squel in place model #208
Browse files Browse the repository at this point in the history
  • Loading branch information
pleary committed Nov 24, 2020
1 parent 12f5f0f commit a61c588
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
39 changes: 9 additions & 30 deletions lib/inaturalist_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -668,39 +668,18 @@ InaturalistAPI.lookupPlaceMiddleware = ( req, res, next ) => {

InaturalistAPI.lookupPreferredPlaceMiddleware = ( req, res, next ) => {
if ( req.query.preferred_place_id ) {
return void InaturalistAPI.lookupInstanceMiddleware( req, res, next,
InaturalistAPI.lookupInstanceMiddleware( req, res, next,
"preferred_place_id", Place.findByID, "preferredPlace" );
return;
}
// lookup by locality code from locale
let locale = req.userSession ? req.userSession.locale : null;
if ( req.query.locale ) {
( { locale } = req.query );
}
if ( locale ) {
let localeObj;
try {
localeObj = new Intl.Locale( locale );
} catch ( err ) {
// continue if locale is invalid
return void next( );
}
const localeRegionCode = localeObj.region;
if ( !localeRegionCode ) {
return void next( );
}
// lookup the place by code and admin-level = country
return void Place.findByLocaleCode( localeRegionCode ).then( obj => {
// no error if none of the instances exist
// locale codes are not always same as country codes
if ( _.isEmpty( obj ) ) {
return void next( );
}
const { locale } = req.query;
Place.findByLocaleString( locale ).then( localePlace => {
if ( localePlace ) {
req.inat = req.inat || { };
req.inat.preferredPlace = obj;
return void next( );
} ).catch( next );
}
next( );
req.inat.preferredPlace = localePlace;
}
next( );
} ).catch( err => util.renderError( err, res ) );
};

InaturalistAPI.lookupUnobservedByUserMiddleware = ( req, res, next ) => {
Expand Down
33 changes: 28 additions & 5 deletions lib/models/place.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const _ = require( "lodash" );
const esClient = require( "../es_client" );
const pgClient = require( "../pg_client" );
const squel = require( "squel" ).useFlavour( "postgres" );
const Model = require( "./model" );

const Place = class Place extends Model {
Expand All @@ -17,9 +18,29 @@ const Place = class Place extends Model {
return response.hits.hits[0] ? response.hits.hits[0]._source : null;
}

static async findByLocaleString( locale ) {
let localeObj;
try {
localeObj = new Intl.Locale( locale );
} catch ( err ) {
// continue if locale is invalid
return null;
}
const localeRegionCode = localeObj.region;
if ( !localeRegionCode ) {
return null;
}
// lookup the place by code and admin-level = country
return Place.findByLocaleCode( localeRegionCode );
}

static async findByLocaleCode( code ) {
const { rows } = await pgClient.connection.query( "SELECT id, name, ancestry FROM "
+ "places WHERE UPPER(code) = $1 AND admin_level = 0", [code] );
const query = squel.select( )
.field( "id, name, ancestry" )
.from( "places" )
.where( "UPPER(code) = ?", code )
.where( "admin_level = 0" );
const { rows } = await pgClient.connection.query( query.toString( ) );
if ( !rows.length ) {
return null;
}
Expand All @@ -37,9 +58,11 @@ const Place = class Place extends Model {
static async assignToObject( object ) {
const ids = _.keys( object );
if ( ids.length === 0 ) { return; }
// TODO: turn this into squel?
const { rows } = await pgClient.connection.query( "SELECT id, name, display_name, ancestry FROM "
+ `places WHERE id IN (${ids.join( "," )})` );
const query = squel.select( )
.field( "id, name, display_name, ancestry" )
.from( "places" )
.where( "id IN ?", ids );
const { rows } = await pgClient.connection.query( query.toString( ) );
_.each( rows, r => {
object[r.id] = r;
} );
Expand Down

0 comments on commit a61c588

Please sign in to comment.