Skip to content

Commit

Permalink
clean up request flow
Browse files Browse the repository at this point in the history
  • Loading branch information
KjartanE committed Feb 1, 2024
1 parent 79293e4 commit 5c40dd8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 44 deletions.
4 changes: 1 addition & 3 deletions api/src/paths/taxonomy/taxon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ GET.apiDoc = {
required: ['tsn', 'label'],
properties: {
tsn: {
type: 'string'
type: 'number'
},
label: {
type: 'string'
Expand Down Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions api/src/paths/taxonomy/taxon/tsn/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ GET.apiDoc = {
required: ['tsn', 'label'],
properties: {
tsn: {
type: 'string'
type: 'number'
},
label: {
type: 'string'
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 0 additions & 3 deletions api/src/repositories/taxonomy-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export class TaxonomyRepository extends BaseRepository {
*/
async addItisTaxonRecord(
itisTsn: number,
bcTaxonCode: string | null = null,
itisScientificName: string,
commonName: string,
itisData: Record<any, any>,
Expand All @@ -70,15 +69,13 @@ export class TaxonomyRepository extends BaseRepository {
taxon
(
itis_tsn,
bc_taxon_code,
itis_scientific_name,
common_name,
itis_data,
itis_update_date
)
VALUES (
${itisTsn},
${bcTaxonCode},
${itisScientificName},
${commonName},
${itisData},
Expand Down
5 changes: 2 additions & 3 deletions api/src/services/itis-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface IItisSearchResponse {
}

export interface IItisSearchResult {
tsn: string;
tsn: number;
label: string;
scientificName: string;
}
Expand All @@ -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 [];
Expand Down Expand Up @@ -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
};
Expand Down
53 changes: 20 additions & 33 deletions api/src/services/taxonomy-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface IItisSearchResponse {
}

export interface IItisSearchResult {
tsn: string;
tsn: number;
label: string;
scientificName: string;
}
Expand Down Expand Up @@ -58,67 +58,54 @@ export class TaxonomyService extends ESService {
* @memberof TaxonomyService
*/
async getTaxonByTsnIds(tsnIds: number[]): Promise<IItisSearchResult[]> {
// 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<ItisTaxonRecord>}
* @memberof TaxonomyService
*/
async addItisTaxonRecord(itisResponse: IItisSearchResponse, bcTaxonCode: string): Promise<ItisTaxonRecord> {
// TODO include optional param for aliases

let commonName = '';
async addItisTaxonRecord(itisResponse: IItisSearchResponse): Promise<ItisTaxonRecord> {
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,
Expand Down

0 comments on commit 5c40dd8

Please sign in to comment.