Skip to content

Commit

Permalink
pr edits
Browse files Browse the repository at this point in the history
  • Loading branch information
KjartanE committed Feb 2, 2024
1 parent 88c5a44 commit 9788d57
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 10 deletions.
3 changes: 2 additions & 1 deletion api/src/paths/taxonomy/taxon/tsn/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ GET.apiDoc = {
type: 'integer'
},
commonName: {
type: 'string'
type: 'string',
nullable: true
},
scientificName: {
type: 'string'
Expand Down
4 changes: 1 addition & 3 deletions api/src/services/itis-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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',
Expand Down
9 changes: 3 additions & 6 deletions api/src/services/taxonomy-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class TaxonomyService {
async getTaxonByTsnIds(tsnIds: number[]): Promise<TaxonSearchResult[]> {
// 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));

Expand All @@ -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[] {
Expand Down

0 comments on commit 9788d57

Please sign in to comment.