diff --git a/lib/controllers/v1/computervision_controller.js b/lib/controllers/v1/computervision_controller.js index 3faa95f8..131f9178 100644 --- a/lib/controllers/v1/computervision_controller.js +++ b/lib/controllers/v1/computervision_controller.js @@ -313,7 +313,7 @@ const ComputervisionController = class ComputervisionController { static async delegatedScoresProcessing( req, visionApiResponse ) { const localeOpts = util.localeOpts( req ); const prepareTaxon = t => { - t.prepareForResponse( localeOpts ); + t.prepareForV1Response( localeOpts ); }; const taxonOpts = { modifier: prepareTaxon, diff --git a/lib/controllers/v1/identifications_controller.js b/lib/controllers/v1/identifications_controller.js index 42473c48..8af9235e 100644 --- a/lib/controllers/v1/identifications_controller.js +++ b/lib/controllers/v1/identifications_controller.js @@ -409,7 +409,7 @@ const IdentificationsController = class IdentificationsController { }; const localeOpts = util.localeOpts( req ); const prepareTaxon = t => { - t.prepareForResponse( localeOpts ); + t.prepareForV1Response( localeOpts ); }; const taxonOpts = { modifier: prepareTaxon, diff --git a/lib/controllers/v1/observations_controller.js b/lib/controllers/v1/observations_controller.js index 77069926..3be21522 100644 --- a/lib/controllers/v1/observations_controller.js +++ b/lib/controllers/v1/observations_controller.js @@ -980,7 +980,7 @@ ObservationsController.iconicTaxaCounts = async req => { ) ); const localeOpts = util.localeOpts( req ); const prepareTaxon = t => { - t.prepareForResponse( localeOpts ); + t.prepareForV1Response( localeOpts ); }; const taxonOpts = { modifier: prepareTaxon, @@ -1036,7 +1036,7 @@ ObservationsController.iconicTaxaSpeciesCounts = async req => { const localeOpts = util.localeOpts( req ); const prepareTaxon = t => { - t.prepareForResponse( localeOpts ); + t.prepareForV1Response( localeOpts ); }; const taxonOpts = { modifier: prepareTaxon, @@ -1739,7 +1739,7 @@ ObservationsController.similarSpecies = async req => { } ) ); const localeOpts = util.localeOpts( req ); const prepareTaxon = t => { - t.prepareForResponse( localeOpts ); + t.prepareForV1Response( localeOpts ); }; const taxonOpts = { modifier: prepareTaxon, diff --git a/lib/controllers/v1/search_controller.js b/lib/controllers/v1/search_controller.js index a6b636e9..a15debc4 100644 --- a/lib/controllers/v1/search_controller.js +++ b/lib/controllers/v1/search_controller.js @@ -249,7 +249,7 @@ SearchController.search = async req => { // returned in _index, so use a string match instead of equality here if ( _.includes( h._index, "taxa" ) ) { const t = new Taxon( h._source ); - t.prepareForResponse( localeOpts ); + t.prepareForV1Response( localeOpts ); return { score, type: "Taxon", diff --git a/lib/controllers/v1/taxa_controller.js b/lib/controllers/v1/taxa_controller.js index bf2261ae..a845bea7 100644 --- a/lib/controllers/v1/taxa_controller.js +++ b/lib/controllers/v1/taxa_controller.js @@ -740,7 +740,7 @@ TaxaController.searchQuery = async ( req, opts = { } ) => { h._source.matched_term = highlighted[0].replace( /<\/?em>/g, "" ); } const t = new Taxon( h._source ); - t.prepareForResponse( localeOpts, mainTaxonOptions ); + t.prepareForV1Response( localeOpts, mainTaxonOptions ); return t; } ); const wikiOpts = { ...options, ...localeOpts }; @@ -1231,7 +1231,7 @@ TaxaController.suggest = async req => { response.queryTaxon = await Taxon.findByID( req.suggestParams.taxon_id ); if ( response.queryTaxon ) { response.queryTaxon = new Taxon( response.queryTaxon ); - response.queryTaxon.prepareForResponse( localeOpts ); + response.queryTaxon.prepareForV1Response( localeOpts ); await TaxaController.assignPlaces( [response.queryTaxon] ); } } @@ -1395,7 +1395,7 @@ TaxaController.nearby = async req => { } const localeOpts = util.localeOpts( req ); const prepareTaxon = t => { - t.prepareForResponse( localeOpts ); + t.prepareForV1Response( localeOpts ); }; const taxonSourceExcludes = ["photos"]; if ( !req.query.all_photos ) { @@ -1478,7 +1478,7 @@ TaxaController.lifelistMetadata = async req => { const localeOpts = util.localeOpts( req ); const prepareTaxon = t => { - t.prepareForResponse( localeOpts ); + t.prepareForV1Response( localeOpts ); }; const taxonOpts = { modifier: prepareTaxon, diff --git a/lib/controllers/v2/search_controller.js b/lib/controllers/v2/search_controller.js index 9f3dbf80..53ec7338 100644 --- a/lib/controllers/v2/search_controller.js +++ b/lib/controllers/v2/search_controller.js @@ -1,10 +1,15 @@ const _ = require( "lodash" ); +const Place = require( "../../models/place" ); const ctrlv1 = require( "../v1/search_controller" ); const search = async req => { const v1Response = await ctrlv1.search( req ); _.each( v1Response.results, result => { const { type } = result; + if ( type === "Place" ) { + result.record = new Place( result.record ); + result.record.prepareForV2Response(); + } result[_.snakeCase( type )] = result.record; result.type = _.snakeCase( type ); delete result.record; diff --git a/lib/models/identification.js b/lib/models/identification.js index 901814fa..a6d56515 100644 --- a/lib/models/identification.js +++ b/lib/models/identification.js @@ -21,7 +21,7 @@ const Identification = class Identification extends Model { static async preloadInto( req, arr, localeOpts ) { const prepareTaxon = t => { - t.prepareForResponse( localeOpts ); + t.prepareForV1Response( localeOpts ); }; const taxonOpts = { modifier: prepareTaxon, diff --git a/lib/models/observation.js b/lib/models/observation.js index a3ddd6de..fce3d12e 100644 --- a/lib/models/observation.js +++ b/lib/models/observation.js @@ -223,7 +223,7 @@ const Observation = class Observation extends Model { const taxonOpts = { foreignKey: "community_taxon_id", attrName: "community_taxon", - modifier: t => t.prepareForResponse( localeOpts ), + modifier: t => t.prepareForV1Response( localeOpts ), source: Taxon.esReturnFields }; await ESModel.fetchBelongsTo( withProjects, Project, { source: Project.returnFields } ); @@ -245,7 +245,7 @@ const Observation = class Observation extends Model { static async preloadMinimal( req, obs, localeOpts ) { const prepareTaxon = t => { - t.prepareForResponse( localeOpts ); + t.prepareForV1Response( localeOpts ); }; const taxonOpts = { modifier: prepareTaxon, diff --git a/lib/models/place.js b/lib/models/place.js index d4f14ac4..fcef2deb 100644 --- a/lib/models/place.js +++ b/lib/models/place.js @@ -7,6 +7,12 @@ const esClient = require( "../es_client" ); const util = require( "../util" ); const Place = class Place extends Model { + prepareForV2Response() { + if ( _.isNull( this.observations_count ) ) { + this.observations_count = 0; + } + } + static async findByID( id, options = { } ) { if ( _.isEmpty( id ) && !_.isNumber( id ) ) { return null; } let lookupID = id; diff --git a/lib/models/project.js b/lib/models/project.js index 41a0a3d4..48e349cf 100644 --- a/lib/models/project.js +++ b/lib/models/project.js @@ -223,7 +223,7 @@ const Project = class Project extends Model { } } const taxonOpts = { - modifier: t => t.prepareForResponse( options.localeOpts ), + modifier: t => t.prepareForV1Response( options.localeOpts ), source: { excludes: ["taxon_photos"] }, foreignKey: "operand_id" }; diff --git a/lib/models/taxon.js b/lib/models/taxon.js index bd2a656f..d1ec6ea2 100644 --- a/lib/models/taxon.js +++ b/lib/models/taxon.js @@ -182,7 +182,7 @@ const Taxon = class Taxon extends Model { return localMeans || ancestorMeans; } - prepareForResponse( localeOptions = { }, opts = { } ) { + prepareForV1Response( localeOptions = { }, opts = { } ) { const localeOpts = { ...localeOptions }; const options = { ...opts }; const userPreferredCommonNames = this.preferredCommonNames( localeOpts ); @@ -519,7 +519,7 @@ const Taxon = class Taxon extends Model { static async preloadIntoTaxonPhotos( taxa, options ) { options = options || { }; - const prepareTaxon = t => t.prepareForResponse( options.localeOpts ); + const prepareTaxon = t => t.prepareForV1Response( options.localeOpts ); // const taxonPhotos = _.flatten( _.map( taxa, t => t.taxon_photos ) ); _.each( taxa, taxon => { _.each( taxon.taxon_photos, taxonPhoto => { @@ -623,7 +623,7 @@ const Taxon = class Taxon extends Model { static esQueryOptions( req ) { const localeOpts = util.localeOpts( req ); const prepareTaxon = t => { - t.prepareForResponse( localeOpts ); + t.prepareForV1Response( localeOpts ); }; return { modifier: prepareTaxon, diff --git a/test/models/taxon.js b/test/models/taxon.js index 0096b345..e5fdb386 100644 --- a/test/models/taxon.js +++ b/test/models/taxon.js @@ -158,7 +158,7 @@ describe( "Taxon", ( ) => { position: 1 }] }; - t.prepareForResponse( { userSession } ); + t.prepareForV1Response( { userSession } ); expect( t.preferred_common_name ).to.eq( "BestEnglish · BestSpanish" ); expect( t.preferred_common_names[0].name ).to.eq( "BestEnglish" ); expect( t.preferred_common_names[1].name ).to.eq( "BestSpanish" ); @@ -175,7 +175,7 @@ describe( "Taxon", ( ) => { position: 1 }] }; - t.prepareForResponse( { userSession } ); + t.prepareForV1Response( { userSession } ); expect( t.preferred_common_name ).to.be.undefined; expect( t.preferred_common_names ).to.be.undefined; } ); @@ -190,7 +190,7 @@ describe( "Taxon", ( ) => { position: 1 }] }; - t.prepareForResponse( { userSession } ); + t.prepareForV1Response( { userSession } ); expect( t.preferred_common_name ).to.eq( "BestSpanish · BestEnglish" ); expect( t.preferred_common_names[0].name ).to.eq( "BestSpanish" ); expect( t.preferred_common_names[1].name ).to.eq( "BestEnglish" ); @@ -203,7 +203,7 @@ describe( "Taxon", ( ) => { position: 0 }] }; - t.prepareForResponse( { userSession } ); + t.prepareForV1Response( { userSession } ); expect( t.preferred_common_name ).to.be.undefined; expect( t.preferred_common_names ).to.be.empty; } ); @@ -215,11 +215,11 @@ describe( "Taxon", ( ) => { position: 0 }] }; - t.prepareForResponse( { locale: "en", userSession } ); + t.prepareForV1Response( { locale: "en", userSession } ); expect( t.preferred_common_name ).to.eq( "BestEnglish" ); expect( t.preferred_common_names[0].name ).to.eq( "BestEnglish" ); t = _.cloneDeep( stubTaxon ); - t.prepareForResponse( { locale: "es", userSession } ); + t.prepareForV1Response( { locale: "es", userSession } ); expect( t.preferred_common_name ).to.eq( "BestSpanish" ); expect( t.preferred_common_names[0].name ).to.eq( "BestSpanish" ); } ); @@ -235,19 +235,19 @@ describe( "Taxon", ( ) => { }] }; // en - t.prepareForResponse( { locale: "en", userSession } ); + t.prepareForV1Response( { locale: "en", userSession } ); expect( t.preferred_common_name ).to.eq( "BestEnglish" ); expect( t.preferred_common_names[0].name ).to.eq( "BestEnglish" ); expect( t.preferred_common_names[1].name ).to.eq( "BestEnglish" ); // es t = _.cloneDeep( stubTaxon ); - t.prepareForResponse( { locale: "es", userSession } ); + t.prepareForV1Response( { locale: "es", userSession } ); expect( t.preferred_common_name ).to.eq( "BestEnglish · BestSpanish" ); expect( t.preferred_common_names[0].name ).to.eq( "BestEnglish" ); expect( t.preferred_common_names[1].name ).to.eq( "BestSpanish" ); // es-mx t = _.cloneDeep( stubTaxon ); - t.prepareForResponse( { locale: "es-mx", userSession } ); + t.prepareForV1Response( { locale: "es-mx", userSession } ); expect( t.preferred_common_name ).to.eq( "BestEnglish · BestSpanish" ); expect( t.preferred_common_names[0].name ).to.eq( "BestEnglish" ); expect( t.preferred_common_names[1].name ).to.eq( "BestSpanish" ); @@ -261,7 +261,7 @@ describe( "Taxon", ( ) => { place_id: 222 }] }; - t.prepareForResponse( { locale: "en", userSession } ); + t.prepareForV1Response( { locale: "en", userSession } ); expect( t.preferred_common_name ).to.eq( "BestInCalifornia" ); expect( t.preferred_common_names[0].name ).to.eq( "BestInCalifornia" ); } ); @@ -274,7 +274,7 @@ describe( "Taxon", ( ) => { place_id: 222 }] }; - t.prepareForResponse( { locale: "de", userSession } ); + t.prepareForV1Response( { locale: "de", userSession } ); expect( t.preferred_common_name ).to.be.undefined; expect( t.preferred_common_names ).to.be.empty; } ); @@ -288,7 +288,7 @@ describe( "Taxon", ( ) => { ancestor_place_ids: [111, 333] }] }; - t.prepareForResponse( { locale: "en", userSession } ); + t.prepareForV1Response( { locale: "en", userSession } ); expect( t.preferred_common_name ).to.eq( "BestInAmerica" ); expect( t.preferred_common_names[0].name ).to.eq( "BestInAmerica" ); } ); @@ -300,7 +300,7 @@ describe( "Taxon", ( ) => { position: 0 }] }; - t.prepareForResponse( { locale: "en", userSession } ); + t.prepareForV1Response( { locale: "en", userSession } ); expect( t.preferred_common_name ).to.be.undefined; expect( t.preferred_common_names ).to.be.empty; } ); @@ -360,14 +360,14 @@ describe( "Taxon", ( ) => { describe( "prepareForResponse", ( ) => { it( "skips listed taxa without means", ( ) => { const t2 = new Taxon( t ); - t2.prepareForResponse( ); + t2.prepareForV1Response( ); // expect( t2.preferred_common_name ).to.eq( "BestEnglish" ); // TODO I'm not sure how the above tested anything related to listed taxa } ); it( "defaults to strict locale check", ( ) => { const t2 = new Taxon( t ); - t2.prepareForResponse( { locale: "de" } ); + t2.prepareForV1Response( { locale: "de" } ); expect( t2.preferred_common_name ).to.be.undefined; } ); } );