From 5c40dd84a1a2b8f0f7180ee296d3c9789a03f473 Mon Sep 17 00:00:00 2001 From: Kjartan Date: Wed, 31 Jan 2024 16:07:34 -0800 Subject: [PATCH] clean up request flow --- api/src/paths/taxonomy/taxon/index.ts | 4 +- api/src/paths/taxonomy/taxon/tsn/index.ts | 4 +- api/src/repositories/taxonomy-repository.ts | 3 -- api/src/services/itis-service.ts | 5 +- api/src/services/taxonomy-service.ts | 53 ++++++++------------- 5 files changed, 25 insertions(+), 44 deletions(-) diff --git a/api/src/paths/taxonomy/taxon/index.ts b/api/src/paths/taxonomy/taxon/index.ts index ea78558d..74a4ddc5 100644 --- a/api/src/paths/taxonomy/taxon/index.ts +++ b/api/src/paths/taxonomy/taxon/index.ts @@ -44,7 +44,7 @@ GET.apiDoc = { required: ['tsn', 'label'], properties: { tsn: { - type: 'string' + type: 'number' }, label: { type: 'string' @@ -85,13 +85,11 @@ export function findTaxonBySearchTerms(): RequestHandler { defaultLog.debug({ label: 'findTaxonBySearchTerms', message: 'query params', query: req.query }); const searchTerms = req.query.terms as string[]; - console.log('searchTerms', searchTerms); try { const itisService = new ItisService(); const response = await itisService.searchItisByTerm(searchTerms); - console.log('response', response); // Overwrite default cache-control header, allow caching up to 7 days res.setHeader('Cache-Control', 'max-age=604800'); diff --git a/api/src/paths/taxonomy/taxon/tsn/index.ts b/api/src/paths/taxonomy/taxon/tsn/index.ts index 53079336..98af3cfb 100644 --- a/api/src/paths/taxonomy/taxon/tsn/index.ts +++ b/api/src/paths/taxonomy/taxon/tsn/index.ts @@ -51,7 +51,7 @@ GET.apiDoc = { required: ['tsn', 'label'], properties: { tsn: { - type: 'string' + type: 'number' }, label: { type: 'string' @@ -101,7 +101,7 @@ export function getTaxonByTSN(): RequestHandler { const connection = getServiceAccountDBConnection(serviceClientSystemUser); - const tsnIds: number[] = (req.query.tsn as string[]).map(Number); + const tsnIds: number[] = (req.query.tsn as (string | number)[]).map(Number); try { await connection.open(); diff --git a/api/src/repositories/taxonomy-repository.ts b/api/src/repositories/taxonomy-repository.ts index 1e5c1e7f..ca1cada1 100644 --- a/api/src/repositories/taxonomy-repository.ts +++ b/api/src/repositories/taxonomy-repository.ts @@ -59,7 +59,6 @@ export class TaxonomyRepository extends BaseRepository { */ async addItisTaxonRecord( itisTsn: number, - bcTaxonCode: string | null = null, itisScientificName: string, commonName: string, itisData: Record, @@ -70,7 +69,6 @@ export class TaxonomyRepository extends BaseRepository { taxon ( itis_tsn, - bc_taxon_code, itis_scientific_name, common_name, itis_data, @@ -78,7 +76,6 @@ export class TaxonomyRepository extends BaseRepository { ) VALUES ( ${itisTsn}, - ${bcTaxonCode}, ${itisScientificName}, ${commonName}, ${itisData}, diff --git a/api/src/services/itis-service.ts b/api/src/services/itis-service.ts index 6532a731..a77a3738 100644 --- a/api/src/services/itis-service.ts +++ b/api/src/services/itis-service.ts @@ -30,7 +30,7 @@ export interface IItisSearchResponse { } export interface IItisSearchResult { - tsn: string; + tsn: number; label: string; scientificName: string; } @@ -57,7 +57,6 @@ export class ItisService { defaultLog.debug({ label: 'searchItisByTerm', message: 'url', url }); const response = await axios.get(url); - console.log('response', response); if (!response.data || !response.data.response || !response.data.response.docs) { return []; @@ -98,7 +97,7 @@ export class ItisService { const commonName = (item.commonNames && item.commonNames[0].split('$')[1]) || item.scientificName; return { - tsn: item.tsn, + tsn: Number(item.tsn), label: commonName, scientificName: item.scientificName }; diff --git a/api/src/services/taxonomy-service.ts b/api/src/services/taxonomy-service.ts index 367331c4..0f68bd47 100644 --- a/api/src/services/taxonomy-service.ts +++ b/api/src/services/taxonomy-service.ts @@ -30,7 +30,7 @@ export interface IItisSearchResponse { } export interface IItisSearchResult { - tsn: string; + tsn: number; label: string; scientificName: string; } @@ -58,67 +58,54 @@ export class TaxonomyService extends ESService { * @memberof TaxonomyService */ async getTaxonByTsnIds(tsnIds: number[]): Promise { + // Search for taxon records in the database const taxon = await this.taxonRepository.getTaxonByTsnIds(tsnIds); + // If taxon records are found, return them if (taxon.length > 0) { - return this._sanitizeItisData(taxon); + return this._sanitizeTaxonRecordsData(taxon); } + // If no taxon records are found, search ITIS for the taxon records const itisService = new ItisService(); const itisResponse = await itisService.searchItisByTSN(tsnIds); const taxonRecords: ItisTaxonRecord[] = []; for (const itisRecord of itisResponse) { - const taxonRecord = await this.addItisTaxonRecord(itisRecord, itisRecord.tsn); + // Add the taxon record to the database + const taxonRecord = await this.addItisTaxonRecord(itisRecord); taxonRecords.push(taxonRecord); } - return this._sanitizeItisData(taxonRecords); + // Return the taxon records + return this._sanitizeTaxonRecordsData(taxonRecords); } - _sanitizeItisData(itisData: ItisTaxonRecord[]): IItisSearchResult[] { - const sanitizedData: IItisSearchResult[] = []; - - for (const itisRecord of itisData) { - const label = itisRecord.common_name || itisRecord.itis_scientific_name; - const scientificName = itisRecord.itis_scientific_name; - const tsn = itisRecord.itis_tsn.toString(); - - sanitizedData.push({ tsn, label, scientificName }); - } - - return sanitizedData; + _sanitizeTaxonRecordsData(itisData: ItisTaxonRecord[]): IItisSearchResult[] { + return itisData.map((item: ItisTaxonRecord) => { + return { + tsn: item.itis_tsn, + label: item.common_name || item.itis_scientific_name, + scientificName: item.itis_scientific_name + } as IItisSearchResult; + }); } /** * Adds a new taxon record. * * @param {IItisSearchResponse} itisResponse - * @param {(string | null)} [bcTaxonCode=null] * @return {*} {Promise} * @memberof TaxonomyService */ - async addItisTaxonRecord(itisResponse: IItisSearchResponse, bcTaxonCode: string): Promise { - // TODO include optional param for aliases - - let commonName = ''; + async addItisTaxonRecord(itisResponse: IItisSearchResponse): Promise { + let commonName = itisResponse.scientificName; if (itisResponse.commonNames) { - switch (itisResponse.commonNames.length) { - case 0: - commonName = itisResponse.scientificName; - break; - case 1: - commonName = itisResponse.commonNames[0]; - break; - default: - commonName = itisResponse.commonNames.join(', '); - break; - } + commonName = itisResponse.commonNames && itisResponse.commonNames[0].split('$')[1]; } return this.taxonRepository.addItisTaxonRecord( Number(itisResponse.tsn), - bcTaxonCode, itisResponse.scientificName, commonName, itisResponse,