Skip to content

Commit

Permalink
SIMSBIOHUB-459: Amended taxon insert logic
Browse files Browse the repository at this point in the history
  • Loading branch information
curtisupshall committed Feb 12, 2024
1 parent 5df8cbd commit 687c01c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
14 changes: 12 additions & 2 deletions api/src/repositories/taxonomy-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { z } from 'zod';
import { getKnex } from '../database/db';
import { getLogger } from '../utils/logger';

Check warning on line 4 in api/src/repositories/taxonomy-repository.ts

View workflow job for this annotation

GitHub Actions / Running Linter and Formatter

Insert `ApiExecuteSQLError·}·from·'../errors/api-error';⏎import·{·`
import { BaseRepository } from './base-repository';

Check warning on line 5 in api/src/repositories/taxonomy-repository.ts

View workflow job for this annotation

GitHub Actions / Running Linter and Formatter

Delete `';⏎import·{·ApiExecuteSQLError·}·from·'../errors/api-error`
import { ApiExecuteSQLError } from '../errors/api-error';

const defaultLog = getLogger('repositories/taxonomy-repository');

Expand Down Expand Up @@ -64,7 +65,7 @@ export class TaxonomyRepository extends BaseRepository {
commonName: string | null,
itisData: Record<string, unknown>,
itisUpdateDate: string
): Promise<void> {
): Promise<TaxonRecord> {
defaultLog.debug({ label: 'addItisTaxonRecord', itisTsn });

const sqlStatement = SQL`
Expand Down Expand Up @@ -98,7 +99,16 @@ export class TaxonomyRepository extends BaseRepository {
taxon.record_end_date IS null;
`;

await this.connection.sql(sqlStatement, TaxonRecord);
const response = await this.connection.sql(sqlStatement, TaxonRecord);

if (response.rowCount !== 1) {
throw new ApiExecuteSQLError('Failed to insert new taxon record', [
'TaxonomyRepository->addItisTaxonRecord',
'rowCount was null or undefined, expected rowCount = 1'
]);
}

return response.rows[0];
}

/**
Expand Down
8 changes: 5 additions & 3 deletions api/src/services/taxonomy-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,19 @@ export class TaxonomyService {

const missingTsnIds = tsnIds.filter((tsnId) => !existingTsnIds.includes(tsnId));

let patchedTaxonRecords: TaxonRecord[] = [];

if (missingTsnIds.length) {
// If the local database does not contain a record for all of the requested ids, search ITIS for the missing
// taxon records, patching the missing records in the local database in the process
const itisService = new ItisService();
const itisResponse = await itisService.searchItisByTSN(missingTsnIds);

await Promise.all(itisResponse.map(async (item) => this.addItisTaxonRecord(item)));
patchedTaxonRecords = await Promise.all(itisResponse.map(async (item) => this.addItisTaxonRecord(item)));
}

// Missing ids patched, return taxon records for all requested ids
return this._sanitizeTaxonRecordsData(await this.taxonRepository.getTaxonByTsnIds(tsnIds));
return this._sanitizeTaxonRecordsData(existingTaxonRecords.concat(patchedTaxonRecords));
}

_sanitizeTaxonRecordsData(taxonRecords: TaxonRecord[]): TaxonSearchResult[] {
Expand All @@ -70,7 +72,7 @@ export class TaxonomyService {
* @return {*} {Promise<TaxonRecord>}
* @memberof TaxonomyService
*/
async addItisTaxonRecord(itisSolrResponse: ItisSolrSearchResponse): Promise<void> {
async addItisTaxonRecord(itisSolrResponse: ItisSolrSearchResponse): Promise<TaxonRecord> {
let commonName = null;
if (itisSolrResponse.commonNames) {
commonName = itisSolrResponse.commonNames[0].split('$')[1];
Expand Down

0 comments on commit 687c01c

Please sign in to comment.