From 9788d5749d900d80b3a806213a7685f90e77b140 Mon Sep 17 00:00:00 2001 From: Kjartan Date: Fri, 2 Feb 2024 12:10:14 -0800 Subject: [PATCH] pr edits --- api/src/paths/taxonomy/taxon/tsn/index.ts | 3 ++- api/src/services/itis-service.ts | 4 +--- api/src/services/taxonomy-service.ts | 9 +++------ 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/api/src/paths/taxonomy/taxon/tsn/index.ts b/api/src/paths/taxonomy/taxon/tsn/index.ts index 9bd6d9ae..7ac992da 100644 --- a/api/src/paths/taxonomy/taxon/tsn/index.ts +++ b/api/src/paths/taxonomy/taxon/tsn/index.ts @@ -53,7 +53,8 @@ GET.apiDoc = { type: 'integer' }, commonName: { - type: 'string' + type: 'string', + nullable: true }, scientificName: { type: 'string' diff --git a/api/src/services/itis-service.ts b/api/src/services/itis-service.ts index d1e2361e..306c0801 100644 --- a/api/src/services/itis-service.ts +++ b/api/src/services/itis-service.ts @@ -74,7 +74,7 @@ export class ItisService { */ _sanitizeItisData = (data: ItisSolrSearchResponse[]): TaxonSearchResult[] => { return data.map((item: ItisSolrSearchResponse) => { - const commonName = (item.commonNames && item.commonNames[0].split('$')[1]) || null; + const commonName = item.commonNames ? item.commonNames[0].split('$')[1] : null; return { tsn: Number(item.tsn), @@ -99,8 +99,6 @@ export class ItisService { throw new Error('Failed to build ITIS query.'); } - // const itisSearchSpecies = this._getItisSolrSearchSpeciesQuery(searchParams); - return `${itisUrl}?${this._getItisSolrTypeParam()}&${this._getItisSolrSortParam( 'nameWOInd', 'asc', diff --git a/api/src/services/taxonomy-service.ts b/api/src/services/taxonomy-service.ts index d277dd98..3d49ad8c 100644 --- a/api/src/services/taxonomy-service.ts +++ b/api/src/services/taxonomy-service.ts @@ -31,6 +31,7 @@ export class TaxonomyService { async getTaxonByTsnIds(tsnIds: number[]): Promise { // Search for taxon records in the database const existingTaxonRecords = await this.taxonRepository.getTaxonByTsnIds(tsnIds); + let patchedTaxonRecords: TaxonRecord[] = []; const missingTsnIds = tsnIds.filter((tsnId) => !existingTaxonRecords.find((item) => item.itis_tsn === tsnId)); @@ -40,15 +41,11 @@ export class TaxonomyService { const itisService = new ItisService(); const itisResponse = await itisService.searchItisByTSN(missingTsnIds); - for (const itisRecord of itisResponse) { - // Add the taxon record to the database - const newTaxonRecord = await this.addItisTaxonRecord(itisRecord); - existingTaxonRecords.push(newTaxonRecord); - } + patchedTaxonRecords = await Promise.all(itisResponse.map(async (item) => this.addItisTaxonRecord(item))); } // Missing ids patched, return taxon records for all requested ids - return this._sanitizeTaxonRecordsData(existingTaxonRecords); + return this._sanitizeTaxonRecordsData(existingTaxonRecords.concat(patchedTaxonRecords)); } _sanitizeTaxonRecordsData(taxonRecords: TaxonRecord[]): TaxonSearchResult[] {