Skip to content

Commit

Permalink
Techdebt: Import Service -> Strategy Rename (#1340)
Browse files Browse the repository at this point in the history
  • Loading branch information
MacQSL authored Aug 13, 2024
1 parent 98fa489 commit 941cd93
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { getDBConnection } from '../../../../../../../database/db';
import { HTTP400 } from '../../../../../../../errors/http-error';
import { csvFileSchema } from '../../../../../../../openapi/schemas/file';
import { authorizeRequestHandler } from '../../../../../../../request-handlers/security/authorization';
import { ImportCapturesService } from '../../../../../../../services/import-services/capture/import-captures-service';
import { importCSV } from '../../../../../../../services/import-services/csv-import-strategy';
import { ImportCapturesStrategy } from '../../../../../../../services/import-services/capture/import-captures-strategy';
import { importCSV } from '../../../../../../../services/import-services/import-csv';
import { scanFileForVirus } from '../../../../../../../utils/file-utils';
import { getLogger } from '../../../../../../../utils/logger';
import { parseMulterFile } from '../../../../../../../utils/media/media-utils';
Expand Down Expand Up @@ -142,7 +142,7 @@ export function importCsv(): RequestHandler {
throw new HTTP400('Malicious content detected, import cancelled.');
}

const importCsvCaptures = new ImportCapturesService(connection, surveyId);
const importCsvCaptures = new ImportCapturesStrategy(connection, surveyId);

// Pass CSV file and importer as dependencies
const capturesCreated = await importCSV(parseMulterFile(rawFile), importCsvCaptures);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from 'chai';
import sinon from 'sinon';
import * as db from '../../../../../../database/db';
import { HTTP400 } from '../../../../../../errors/http-error';
import * as strategy from '../../../../../../services/import-services/csv-import-strategy';
import * as strategy from '../../../../../../services/import-services/import-csv';
import * as fileUtils from '../../../../../../utils/file-utils';
import { getMockDBConnection, getRequestHandlerMocks } from '../../../../../../__mocks__/db';
import { importCsv } from './import';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { getDBConnection } from '../../../../../../database/db';
import { HTTP400 } from '../../../../../../errors/http-error';
import { csvFileSchema } from '../../../../../../openapi/schemas/file';
import { authorizeRequestHandler } from '../../../../../../request-handlers/security/authorization';
import { ImportCrittersService } from '../../../../../../services/import-services/critter/import-critters-service';
import { importCSV } from '../../../../../../services/import-services/csv-import-strategy';
import { ImportCrittersStrategy } from '../../../../../../services/import-services/critter/import-critters-strategy';
import { importCSV } from '../../../../../../services/import-services/import-csv';
import { scanFileForVirus } from '../../../../../../utils/file-utils';
import { getLogger } from '../../../../../../utils/logger';
import { parseMulterFile } from '../../../../../../utils/media/media-utils';
Expand Down Expand Up @@ -144,8 +144,8 @@ export function importCsv(): RequestHandler {
throw new HTTP400('Malicious content detected, import cancelled.');
}

// Critter CSV import service - child of CSVImportStrategy
const importCsvCritters = new ImportCrittersService(connection, surveyId);
// Critter CSV import strategy - child of CSVImportStrategy
const importCsvCritters = new ImportCrittersStrategy(connection, surveyId);

const surveyCritterIds = await importCSV(parseMulterFile(rawFile), importCsvCritters);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getDBConnection } from '../../../../../../../database/db';
import { HTTP400 } from '../../../../../../../errors/http-error';
import { csvFileSchema } from '../../../../../../../openapi/schemas/file';
import { authorizeRequestHandler } from '../../../../../../../request-handlers/security/authorization';
import { importCSV } from '../../../../../../../services/import-services/csv-import-strategy';
import { importCSV } from '../../../../../../../services/import-services/import-csv';
import { ImportMarkingsStrategy } from '../../../../../../../services/import-services/marking/import-markings-strategy';
import { scanFileForVirus } from '../../../../../../../utils/file-utils';
import { getLogger } from '../../../../../../../utils/logger';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { MediaFile } from '../../../utils/media/media-file';
import * as worksheetUtils from '../../../utils/xlsx-utils/worksheet-utils';
import { getMockDBConnection } from '../../../__mocks__/db';
import { IBulkCreateResponse, ICritterDetailed } from '../../critterbase-service';
import { importCSV } from '../csv-import-strategy';
import { ImportCapturesService } from './import-captures-service';
import { importCSV } from '../import-csv';
import { ImportCapturesStrategy } from './import-captures-strategy';

describe('import-captures-service', () => {
describe('importCSV capture worksheet', () => {
Expand Down Expand Up @@ -42,12 +42,12 @@ describe('import-captures-service', () => {

const mockDBConnection = getMockDBConnection();

const importCapturesService = new ImportCapturesService(mockDBConnection, 1);
const importCapturesStrategy = new ImportCapturesStrategy(mockDBConnection, 1);

const getDefaultWorksheetStub = sinon.stub(worksheetUtils, 'getDefaultWorksheet');
const aliasMapStub = sinon.stub(importCapturesService.surveyCritterService, 'getSurveyCritterAliasMap');
const aliasMapStub = sinon.stub(importCapturesStrategy.surveyCritterService, 'getSurveyCritterAliasMap');
const critterbaseInsertStub = sinon.stub(
importCapturesService.surveyCritterService.critterbaseService,
importCapturesStrategy.surveyCritterService.critterbaseService,
'bulkCreate'
);

Expand All @@ -72,15 +72,15 @@ describe('import-captures-service', () => {
);
critterbaseInsertStub.resolves({ created: { captures: 2 } } as IBulkCreateResponse);

const data = await importCSV(new MediaFile('test', 'test', 'test' as unknown as Buffer), importCapturesService);
const data = await importCSV(new MediaFile('test', 'test', 'test' as unknown as Buffer), importCapturesStrategy);

expect(data).to.deep.equal(2);
});
});
describe('validateRows', () => {
it('should format and validate the rows successfully', async () => {
const mockConnection = getMockDBConnection();
const importCaptures = new ImportCapturesService(mockConnection, 1);
const importCaptures = new ImportCapturesStrategy(mockConnection, 1);
const aliasMapStub = sinon.stub(importCaptures.surveyCritterService, 'getSurveyCritterAliasMap');

aliasMapStub.resolves(
Expand Down Expand Up @@ -134,7 +134,7 @@ describe('import-captures-service', () => {

it('should format and validate the rows with optional values successfully', async () => {
const mockConnection = getMockDBConnection();
const importCaptures = new ImportCapturesService(mockConnection, 1);
const importCaptures = new ImportCapturesStrategy(mockConnection, 1);
const aliasMapStub = sinon.stub(importCaptures.surveyCritterService, 'getSurveyCritterAliasMap');

aliasMapStub.resolves(
Expand Down Expand Up @@ -180,7 +180,7 @@ describe('import-captures-service', () => {

it('should return error if invalid', async () => {
const mockConnection = getMockDBConnection();
const importCaptures = new ImportCapturesService(mockConnection, 1);
const importCaptures = new ImportCapturesStrategy(mockConnection, 1);
const aliasMapStub = sinon.stub(importCaptures.surveyCritterService, 'getSurveyCritterAliasMap');

aliasMapStub.resolves(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import { IXLSXCSVValidator } from '../../../utils/xlsx-utils/worksheet-utils';
import { ICapture, ILocation } from '../../critterbase-service';
import { DBService } from '../../db-service';
import { SurveyCritterService } from '../../survey-critter-service';
import { CSVImportService, Row } from '../csv-import-strategy.interface';
import { CSVImportStrategy, Row } from '../import-csv.interface';
import { findCapturesFromDateTime, formatTimeString } from '../utils/datetime';
import { CsvCapture, CsvCaptureSchema } from './import-captures-service.interface';
import { CsvCapture, CsvCaptureSchema } from './import-captures-strategy.interface';

/**
*
* @class ImportCapturesService
* @class ImportCapturesStrategy
* @extends DBService
* @see CSVImportStrategy
*
*/
export class ImportCapturesService extends DBService implements CSVImportService {
export class ImportCapturesStrategy extends DBService implements CSVImportStrategy {
surveyCritterService: SurveyCritterService;
surveyId: number;

Expand All @@ -43,7 +43,7 @@ export class ImportCapturesService extends DBService implements CSVImportService
} satisfies IXLSXCSVValidator;

/**
* Construct an instance of ImportCapturesService.
* Construct an instance of ImportCapturesStrategy.
*
* @param {IDBConnection} connection - DB connection
* @param {string} surveyId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import sinonChai from 'sinon-chai';
import { WorkSheet } from 'xlsx';
import { getMockDBConnection } from '../../../__mocks__/db';
import { IBulkCreateResponse } from '../../critterbase-service';
import { ImportCrittersService } from './import-critters-service';
import { CsvCritter } from './import-critters-service.interface';
import { ImportCrittersStrategy } from './import-critters-strategy';
import { CsvCritter } from './import-critters-strategy.interface';

chai.use(sinonChai);

const mockConnection = getMockDBConnection();

describe('ImportCrittersService', () => {
describe('ImportCrittersStrategy', () => {
describe('_getRowsToValidate', () => {
it('it should correctly format rows', () => {
const rows = [
Expand All @@ -25,7 +25,7 @@ describe('ImportCrittersService', () => {
BAD_COLLECTION: 'Bad'
}
];
const service = new ImportCrittersService(mockConnection, 1);
const service = new ImportCrittersStrategy(mockConnection, 1);

const parsedRow = service._getRowsToValidate(rows, ['COLLECTION'])[0];

Expand All @@ -51,7 +51,7 @@ describe('ImportCrittersService', () => {
critter_comment: 'comment',
extra_property: 'test'
};
const service = new ImportCrittersService(mockConnection, 1);
const service = new ImportCrittersStrategy(mockConnection, 1);

const critter = service._getCritterFromRow(row);

Expand All @@ -78,7 +78,7 @@ describe('ImportCrittersService', () => {
COLLECTION: 'ID1',
HERD: 'ID2'
};
const service = new ImportCrittersService(mockConnection, 1);
const service = new ImportCrittersStrategy(mockConnection, 1);

const collectionUnits = service._getCollectionUnitsFromRow(row);

Expand All @@ -95,7 +95,7 @@ describe('ImportCrittersService', () => {
});

it('should return unique list of tsns', async () => {
const service = new ImportCrittersService(mockConnection, 1);
const service = new ImportCrittersStrategy(mockConnection, 1);

const getTaxonomyStub = sinon.stub(service.platformService, 'getTaxonomyByTsns').resolves([
{ tsn: '1', scientificName: 'a' },
Expand Down Expand Up @@ -152,7 +152,7 @@ describe('ImportCrittersService', () => {
];

it('should return collection unit mapping', async () => {
const service = new ImportCrittersService(mockConnection, 1);
const service = new ImportCrittersStrategy(mockConnection, 1);

const getColumnsStub = sinon.stub(service, '_getNonStandardColumns');
const mockWorksheet = {} as unknown as WorkSheet;
Expand All @@ -173,7 +173,7 @@ describe('ImportCrittersService', () => {
});

it('should return empty map when no collection unit columns', async () => {
const service = new ImportCrittersService(mockConnection, 1);
const service = new ImportCrittersStrategy(mockConnection, 1);

const getColumnsStub = sinon.stub(service, '_getNonStandardColumns');
const mockWorksheet = {} as unknown as WorkSheet;
Expand Down Expand Up @@ -216,7 +216,7 @@ describe('ImportCrittersService', () => {
];

it('should correctly parse collection units and critters and insert into sims / critterbase', async () => {
const service = new ImportCrittersService(mockConnection, 1);
const service = new ImportCrittersStrategy(mockConnection, 1);

const critterbaseBulkCreateStub = sinon.stub(service.critterbaseService, 'bulkCreate');
const simsAddSurveyCrittersStub = sinon.stub(service.surveyCritterService, 'addCrittersToSurvey');
Expand Down Expand Up @@ -255,7 +255,7 @@ describe('ImportCrittersService', () => {
});

it('should throw error if response from critterbase is less than provided critters', async () => {
const service = new ImportCrittersService(mockConnection, 1);
const service = new ImportCrittersStrategy(mockConnection, 1);

const critterbaseBulkCreateStub = sinon.stub(service.critterbaseService, 'bulkCreate');
const simsAddSurveyCrittersStub = sinon.stub(service.surveyCritterService, 'addCrittersToSurvey');
Expand Down Expand Up @@ -314,7 +314,7 @@ describe('ImportCrittersService', () => {
];

it('should return successful', async () => {
const service = new ImportCrittersService(mockConnection, 1);
const service = new ImportCrittersStrategy(mockConnection, 1);

const getColumnsStub = sinon.stub(service, '_getNonStandardColumns');
const surveyAliasesStub = sinon.stub(service.surveyCritterService, 'getUniqueSurveyCritterAliases');
Expand Down Expand Up @@ -377,7 +377,7 @@ describe('ImportCrittersService', () => {
});

it('should return error when sex undefined', async () => {
const service = new ImportCrittersService(mockConnection, 1);
const service = new ImportCrittersStrategy(mockConnection, 1);

const surveyAliasesStub = sinon.stub(service.surveyCritterService, 'getUniqueSurveyCritterAliases');
const getValidTsnsStub = sinon.stub(service, '_getValidTsns');
Expand Down Expand Up @@ -415,7 +415,7 @@ describe('ImportCrittersService', () => {
});

it('should return error when wlh_id invalid regex', async () => {
const service = new ImportCrittersService(mockConnection, 1);
const service = new ImportCrittersStrategy(mockConnection, 1);

const surveyAliasesStub = sinon.stub(service.surveyCritterService, 'getUniqueSurveyCritterAliases');
const getValidTsnsStub = sinon.stub(service, '_getValidTsns');
Expand Down Expand Up @@ -453,7 +453,7 @@ describe('ImportCrittersService', () => {
});

it('should return error when itis_tsn invalid option or undefined', async () => {
const service = new ImportCrittersService(mockConnection, 1);
const service = new ImportCrittersStrategy(mockConnection, 1);

const surveyAliasesStub = sinon.stub(service.surveyCritterService, 'getUniqueSurveyCritterAliases');
const getValidTsnsStub = sinon.stub(service, '_getValidTsns');
Expand Down Expand Up @@ -491,7 +491,7 @@ describe('ImportCrittersService', () => {
});

it('should return error if alias undefined, duplicate or exists in surve', async () => {
const service = new ImportCrittersService(mockConnection, 1);
const service = new ImportCrittersStrategy(mockConnection, 1);

const surveyAliasesStub = sinon.stub(service.surveyCritterService, 'getUniqueSurveyCritterAliases');
const getValidTsnsStub = sinon.stub(service, '_getValidTsns');
Expand Down Expand Up @@ -545,7 +545,7 @@ describe('ImportCrittersService', () => {
});

it('should return error if collection unit invalid value', async () => {
const service = new ImportCrittersService(mockConnection, 1);
const service = new ImportCrittersStrategy(mockConnection, 1);

const surveyAliasesStub = sinon.stub(service.surveyCritterService, 'getUniqueSurveyCritterAliases');
const getValidTsnsStub = sinon.stub(service, '_getValidTsns');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ import {
import { DBService } from '../../db-service';
import { PlatformService } from '../../platform-service';
import { SurveyCritterService } from '../../survey-critter-service';
import { CSVImportService, Row, Validation, ValidationError } from '../csv-import-strategy.interface';
import { CsvCritter, PartialCsvCritter } from './import-critters-service.interface';
import { CSVImportStrategy, Row, Validation, ValidationError } from '../import-csv.interface';
import { CsvCritter, PartialCsvCritter } from './import-critters-strategy.interface';

const defaultLog = getLogger('services/import/import-critters-service');

const CSV_CRITTER_SEX_OPTIONS = ['UNKNOWN', 'MALE', 'FEMALE', 'HERMAPHRODITIC'];

/**
*
* ImportCrittersService - Injected into CSVImportStrategy as the CSV import dependency
* ImportCrittersStrategy - Injected into CSVImportStrategy as the CSV import dependency
*
* @example new CSVImportStrategy(new ImportCrittersService(connection, surveyId)).import(file);
* @example new CSVImportStrategy(new ImportCrittersStrategy(connection, surveyId)).import(file);
*
* @class ImportCrittersService
* @class ImportCrittersStrategy
* @extends DBService
*
*/
export class ImportCrittersService extends DBService implements CSVImportService {
export class ImportCrittersStrategy extends DBService implements CSVImportStrategy {
platformService: PlatformService;
critterbaseService: CritterbaseService;
surveyCritterService: SurveyCritterService;
Expand All @@ -56,7 +56,7 @@ export class ImportCrittersService extends DBService implements CSVImportService
} satisfies IXLSXCSVValidator;

/**
* Instantiates an instance of ImportCrittersService
* Instantiates an instance of ImportCrittersStrategy
*
* @param {IDBConnection} connection - Database connection
* @param {number} surveyId - Survey identifier
Expand Down Expand Up @@ -333,7 +333,7 @@ export class ImportCrittersService extends DBService implements CSVImportService
// In reality this error should not be triggered, safeguard to prevent floating critter ids in SIMS
if (bulkResponse.created.critters !== simsPayload.length) {
throw new ApiGeneralError('Unable to fully import critters from CSV', [
'importCrittersService -> insertCsvCrittersIntoSimsAndCritterbase',
'importCrittersStrategy -> insertCsvCrittersIntoSimsAndCritterbase',
'critterbase bulk create response count !== critterIds.length'
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import { IXLSXCSVValidator } from '../../utils/xlsx-utils/worksheet-utils';
export type Row = Record<string, any>;

/**
* Implementation for CSV Import Services.
* Implementation for CSV Import Strategies.
*
* All CSV import services should implement this interface to be used with `CSVImportStrategy`
* Note: When implementing a service using this interface the generics will be inferred.
* All CSV import strategies should implement this interface to be used with `CSVImport` function.
*
* Note: When implementing a strategy using this interface the generics will be inferred.
*
* @template ValidatedRow
* @template InsertReturn
*/
export interface CSVImportService<ValidatedRow = Record<string, unknown>, InsertReturn = unknown> {
export interface CSVImportStrategy<ValidatedRow = Record<string, unknown>, InsertReturn = unknown> {
/**
* Standard column validator - used to validate the column headers and types.
*
Expand Down
Loading

0 comments on commit 941cd93

Please sign in to comment.