Skip to content

Commit

Permalink
Add some more place details to conservation status responses.
Browse files Browse the repository at this point in the history
  • Loading branch information
kueda committed Oct 12, 2016
1 parent 2e392c5 commit b4645d1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/controllers/v1/taxa_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ TaxaController.searchQuery = function( req, options, callback ) {
"names.place_taxon_names", "rank", "default_photo", "ancestor_ids",
"colors", "is_active", "observations_count", "iconic_taxon_id", "parent_id",
"rank_level", "listed_taxa.place_id", "listed_taxa.establishment_means",
"statuses.place_id", "statuses.iucn" ];
"statuses.*" ];
// we don't want all photos for ancestors or children
if( options.details ) {
defaultSource.push( "taxon_photos" );
Expand Down
39 changes: 32 additions & 7 deletions lib/models/taxon.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ var Taxon = class Taxon extends Model {
var cs, em;
if(( cs = this.conservationStatus( localeOpts.place || localeOpts.preferredPlace ))) {
this.conservation_status = cs;
this.conservation_status.status = Taxon.IUCN_STATUSES[ cs.iucn ];
delete this.conservation_status.iucn;
this.preferred_conservation_status = Taxon.IUCN_STATUSES[ cs.iucn ];
}
if(( em = this.establishmentMeans( localeOpts.place || localeOpts.preferredPlace ))) {
this.establishment_means = em;
Expand Down Expand Up @@ -212,7 +209,22 @@ var Taxon = class Taxon extends Model {
statusByTaxonID[ r.taxon_id ] = statusByTaxonID[ r.taxon_id ] || [ ];
statusByTaxonID[ r.taxon_id ].push( r );
});
_.each( taxa, t => t.conservation_statuses = statusByTaxonID[ t.id ] );
_.each( taxa, t => {
t.conservation_statuses = statusByTaxonID[ t.id ]
if ( t.conservation_status && t.conservation_statuses && t.conservation_statuses.length > 0) {
const csCandidate = _.detect( t.conservation_statuses, status => {
return (
( status.place && status.place.id || null ) ===
( t.conservation_status.place && t.conservation_status.place.id || null ) &&
status.authority.toLowerCase( ) === t.conservation_status.authority.toLowerCase( ) &&
status.status.toLowerCase( ) === t.conservation_status.status.toLowerCase( )
);
} );
if ( csCandidate ) {
t.conservation_status = csCandidate;
}
}
} );
callback( null, taxa );
}
);
Expand All @@ -223,8 +235,9 @@ var Taxon = class Taxon extends Model {
if( !options.details ) { return callback( null, taxa ); }
var ids = _.map( taxa, "id" );
var query = squel.select( ).
field( "lt.taxon_id, lt.establishment_means, lt.list_id, l.title list_title, " +
"lt.place_id, p.name place_name" ).
field( "lt.id, lt.taxon_id, lt.establishment_means, lt.list_id, l.title list_title, " +
"lt.place_id, p.name place_name, p.display_name place_display_name, " +
"p.ancestry place_ancestry, p.admin_level place_admin_level" ).
from( "listed_taxa lt" ).
left_join( "places p", null, "lt.place_id = p.id" ).
left_join( "lists l", null, "lt.list_id = l.id" ).
Expand All @@ -235,9 +248,21 @@ var Taxon = class Taxon extends Model {
if( err ) { return callback( err ); }
var byTaxonID = { };
_.each( result.rows, r => {
r.place = r.place_id ? { id: r.place_id, name: r.place_name } : null;
r.place = r.place_id ? {
id: r.place_id,
name: r.place_name,
display_name: r.place_display_name,
admin_level: r.place_admin_level,
ancestor_place_ids: _.flatten( [
_.compact( ( r.place_ancestry || "" ).split( "/" ) ).map( i => parseInt( i ) ),
r.place_id
] )
} : null;
delete r.place_id;
delete r.place_name;
delete r.place_display_name;
delete r.place_ancestry;
delete r.place_admin_level;
r.list = r.list_id ? { id: r.list_id, title: r.list_title } : null;
delete r.list_id;
delete r.list_title;
Expand Down
2 changes: 1 addition & 1 deletion schema/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@
"names": [{ "name_autocomplete": "Los", "exact": "Los" }],
"observations_count": 50,
"is_active": true,
"statuses": [ { "place_id": 432, "iucn": 30 } ],
"statuses": [ { "place_id": 432, "iucn": 30, "authority": "IUCN Red List", "status": "VU" } ],
"listed_taxa": [ { "place_id": 432, "establishment_means": "endemic" } ]
},
{
Expand Down

0 comments on commit b4645d1

Please sign in to comment.