From 941cd933828fff602c0d0a795aa3f9f09db6c605 Mon Sep 17 00:00:00 2001 From: Mac Deluca <99926243+MacQSL@users.noreply.github.com> Date: Tue, 13 Aug 2024 15:27:08 -0700 Subject: [PATCH] Techdebt: Import Service -> Strategy Rename (#1340) --- .../{surveyId}/critters/captures/import.ts | 6 ++-- .../survey/{surveyId}/critters/import.test.ts | 2 +- .../survey/{surveyId}/critters/import.ts | 8 ++--- .../{surveyId}/critters/markings/import.ts | 2 +- ... => import-captures-strategy.interface.ts} | 0 ...st.ts => import-captures-strategy.test.ts} | 18 +++++----- ...service.ts => import-captures-strategy.ts} | 10 +++--- ... => import-critters-strategy.interface.ts} | 0 ...st.ts => import-critters-strategy.test.ts} | 34 +++++++++---------- ...service.ts => import-critters-strategy.ts} | 16 ++++----- ...y.interface.ts => import-csv.interface.ts} | 9 ++--- ...rt-strategy.test.ts => import-csv.test.ts} | 12 +++---- .../{csv-import-strategy.ts => import-csv.ts} | 20 +++++------ .../marking/import-markings-strategy.test.ts | 2 +- .../marking/import-markings-strategy.ts | 7 ++-- .../xlsx-utils/column-validator-utils.ts | 2 +- 16 files changed, 73 insertions(+), 75 deletions(-) rename api/src/services/import-services/capture/{import-captures-service.interface.ts => import-captures-strategy.interface.ts} (100%) rename api/src/services/import-services/capture/{import-captures-service.test.ts => import-captures-strategy.test.ts} (91%) rename api/src/services/import-services/capture/{import-captures-service.ts => import-captures-strategy.ts} (95%) rename api/src/services/import-services/critter/{import-critters-service.interface.ts => import-critters-strategy.interface.ts} (100%) rename api/src/services/import-services/critter/{import-critters-service.test.ts => import-critters-strategy.test.ts} (93%) rename api/src/services/import-services/critter/{import-critters-service.ts => import-critters-strategy.ts} (95%) rename api/src/services/import-services/{csv-import-strategy.interface.ts => import-csv.interface.ts} (86%) rename api/src/services/import-services/{csv-import-strategy.test.ts => import-csv.test.ts} (90%) rename api/src/services/import-services/{csv-import-strategy.ts => import-csv.ts} (75%) diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/critters/captures/import.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/critters/captures/import.ts index 6d997bbeee..c03e54f881 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/critters/captures/import.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/critters/captures/import.ts @@ -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'; @@ -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); diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/critters/import.test.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/critters/import.test.ts index 138c649592..5584dd79ad 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/critters/import.test.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/critters/import.test.ts @@ -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'; diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/critters/import.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/critters/import.ts index aecdd849dd..01a71c02aa 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/critters/import.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/critters/import.ts @@ -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'; @@ -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); diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/critters/markings/import.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/critters/markings/import.ts index 378e077f02..4b5126bc5b 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/critters/markings/import.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/critters/markings/import.ts @@ -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'; diff --git a/api/src/services/import-services/capture/import-captures-service.interface.ts b/api/src/services/import-services/capture/import-captures-strategy.interface.ts similarity index 100% rename from api/src/services/import-services/capture/import-captures-service.interface.ts rename to api/src/services/import-services/capture/import-captures-strategy.interface.ts diff --git a/api/src/services/import-services/capture/import-captures-service.test.ts b/api/src/services/import-services/capture/import-captures-strategy.test.ts similarity index 91% rename from api/src/services/import-services/capture/import-captures-service.test.ts rename to api/src/services/import-services/capture/import-captures-strategy.test.ts index 4100face6d..b7df1d6eff 100644 --- a/api/src/services/import-services/capture/import-captures-service.test.ts +++ b/api/src/services/import-services/capture/import-captures-strategy.test.ts @@ -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', () => { @@ -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' ); @@ -72,7 +72,7 @@ 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); }); @@ -80,7 +80,7 @@ describe('import-captures-service', () => { 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( @@ -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( @@ -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( diff --git a/api/src/services/import-services/capture/import-captures-service.ts b/api/src/services/import-services/capture/import-captures-strategy.ts similarity index 95% rename from api/src/services/import-services/capture/import-captures-service.ts rename to api/src/services/import-services/capture/import-captures-strategy.ts index c71d3f915a..e238ec59e3 100644 --- a/api/src/services/import-services/capture/import-captures-service.ts +++ b/api/src/services/import-services/capture/import-captures-strategy.ts @@ -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; @@ -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 diff --git a/api/src/services/import-services/critter/import-critters-service.interface.ts b/api/src/services/import-services/critter/import-critters-strategy.interface.ts similarity index 100% rename from api/src/services/import-services/critter/import-critters-service.interface.ts rename to api/src/services/import-services/critter/import-critters-strategy.interface.ts diff --git a/api/src/services/import-services/critter/import-critters-service.test.ts b/api/src/services/import-services/critter/import-critters-strategy.test.ts similarity index 93% rename from api/src/services/import-services/critter/import-critters-service.test.ts rename to api/src/services/import-services/critter/import-critters-strategy.test.ts index 38699d3d44..0b48f8d2be 100644 --- a/api/src/services/import-services/critter/import-critters-service.test.ts +++ b/api/src/services/import-services/critter/import-critters-strategy.test.ts @@ -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 = [ @@ -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]; @@ -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); @@ -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); @@ -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' }, @@ -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; @@ -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; @@ -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'); @@ -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'); @@ -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'); @@ -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'); @@ -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'); @@ -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'); @@ -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'); @@ -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'); diff --git a/api/src/services/import-services/critter/import-critters-service.ts b/api/src/services/import-services/critter/import-critters-strategy.ts similarity index 95% rename from api/src/services/import-services/critter/import-critters-service.ts rename to api/src/services/import-services/critter/import-critters-strategy.ts index bf6b79c0b5..a50f32fd54 100644 --- a/api/src/services/import-services/critter/import-critters-service.ts +++ b/api/src/services/import-services/critter/import-critters-strategy.ts @@ -17,8 +17,8 @@ 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'); @@ -26,15 +26,15 @@ 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; @@ -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 @@ -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' ]); } diff --git a/api/src/services/import-services/csv-import-strategy.interface.ts b/api/src/services/import-services/import-csv.interface.ts similarity index 86% rename from api/src/services/import-services/csv-import-strategy.interface.ts rename to api/src/services/import-services/import-csv.interface.ts index 81bdb4c1e0..43c2faf622 100644 --- a/api/src/services/import-services/csv-import-strategy.interface.ts +++ b/api/src/services/import-services/import-csv.interface.ts @@ -9,15 +9,16 @@ import { IXLSXCSVValidator } from '../../utils/xlsx-utils/worksheet-utils'; export type Row = Record; /** - * 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, InsertReturn = unknown> { +export interface CSVImportStrategy, InsertReturn = unknown> { /** * Standard column validator - used to validate the column headers and types. * diff --git a/api/src/services/import-services/csv-import-strategy.test.ts b/api/src/services/import-services/import-csv.test.ts similarity index 90% rename from api/src/services/import-services/csv-import-strategy.test.ts rename to api/src/services/import-services/import-csv.test.ts index bff6e76008..66955e21ee 100644 --- a/api/src/services/import-services/csv-import-strategy.test.ts +++ b/api/src/services/import-services/import-csv.test.ts @@ -4,8 +4,8 @@ import sinon from 'sinon'; import sinonChai from 'sinon-chai'; import { MediaFile } from '../../utils/media/media-file'; import * as worksheetUtils from '../../utils/xlsx-utils/worksheet-utils'; -import { importCSV } from './csv-import-strategy'; -import { CSVImportService } from './csv-import-strategy.interface'; +import { importCSV } from './import-csv'; +import { CSVImportStrategy } from './import-csv.interface'; chai.use(sinonChai); @@ -18,7 +18,7 @@ describe('importCSV', () => { const mockCsv = new MediaFile('file', 'file', Buffer.from('')); const mockWorksheet = {}; - const importer: CSVImportService = { + const importer: CSVImportStrategy = { columnValidator: { ID: { type: 'string' } }, validateRows: sinon.stub().resolves({ success: true, data: true }), insert: sinon.stub().resolves(true) @@ -38,7 +38,7 @@ describe('importCSV', () => { it('should throw error if column validator fails', async () => { const mockCsv = new MediaFile('file', 'file', Buffer.from('')); - const importer: CSVImportService = { + const importer: CSVImportStrategy = { columnValidator: { ID: { type: 'string' } }, validateRows: sinon.stub().resolves({ success: true, data: true }), insert: sinon.stub().resolves(true) @@ -65,12 +65,12 @@ describe('importCSV', () => { } }); - it('should throw error if import service validateRows fails', async () => { + it('should throw error if import strategy validateRows fails', async () => { const mockCsv = new MediaFile('file', 'file', Buffer.from('')); const mockWorksheet = {}; const mockValidation = { success: false, error: { issues: [{ row: 1, message: 'invalidated' }] } }; - const importer: CSVImportService = { + const importer: CSVImportStrategy = { columnValidator: { ID: { type: 'string' } }, validateRows: sinon.stub().returns(mockValidation), insert: sinon.stub().resolves(true) diff --git a/api/src/services/import-services/csv-import-strategy.ts b/api/src/services/import-services/import-csv.ts similarity index 75% rename from api/src/services/import-services/csv-import-strategy.ts rename to api/src/services/import-services/import-csv.ts index 544edee027..6cee607e8b 100644 --- a/api/src/services/import-services/csv-import-strategy.ts +++ b/api/src/services/import-services/import-csv.ts @@ -7,30 +7,30 @@ import { getWorksheetRowObjects, validateCsvFile } from '../../utils/xlsx-utils/worksheet-utils'; -import { CSVImportService } from './csv-import-strategy.interface'; +import { CSVImportStrategy } from './import-csv.interface'; /** - * Import CSV (Strategy) - Used with `CSVImportService` classes. + * Import CSV - Used with `CSVImportStrategy` classes. * - * How to?: Inject a media-file and import service that implements the CSVImportService. + * How to?: Inject a media-file and import strategy that implements `CSVImportStrategy`. * * Flow: * 1. Get the worksheet from the CSV MediaFile - _getWorksheet - * 2. Validate the standard columns with the `importCsvService` column validator - _validate -> validateCsvFile - * 3. Validate row data with import service - _validate -> importCsvService.validateRows - * 4. Insert the data into database or send to external system - import -> importCsvService.insert + * 2. Validate the standard columns with the `importCsvStrategy` column validator - _validate -> validateCsvFile + * 3. Validate row data with import strategy - _validate -> importCsvService.validateRows + * 4. Insert the data into database or send to external system - import -> importCsvStrategy.insert * * @async * @template ValidatedRow - Validated row object * @template InsertReturn - Return type of the importer insert method * @param {MediaFile} csvMediaFile - CSV converted to MediaFile - * @param {CSVImportService} importer - Import service + * @param {CSVImportStrategy} importer - Import strategy * @throws {ApiGeneralError} - If validation fails * @returns {Promise} Generic return type */ export const importCSV = async ( csvMediaFile: MediaFile, - importer: CSVImportService + importer: CSVImportStrategy ) => { const _worksheet = getDefaultWorksheet(constructXLSXWorkbook(csvMediaFile)); @@ -42,7 +42,7 @@ export const importCSV = async ( if (!validateCsvFile(worksheet, importer.columnValidator)) { throw new ApiGeneralError(`Column validator failed. Column headers or cell data types are incorrect.`, [ { csv_column_errors: getColumnValidatorSpecification(importer.columnValidator) }, - 'importCapturesService->_validate->validateCsvFile' + 'importCSV->_validate->validateCsvFile' ]); } @@ -57,7 +57,7 @@ export const importCSV = async ( if (!validation.success) { throw new ApiGeneralError(`Failed to import Critter CSV. Column data validator failed.`, [ { csv_row_errors: validation.error.issues }, - 'importCapturesService->_validate->_validateRows' + 'importCSV->_validate->_validateRows' ]); } diff --git a/api/src/services/import-services/marking/import-markings-strategy.test.ts b/api/src/services/import-services/marking/import-markings-strategy.test.ts index a4ebb6e677..4cfc6e192a 100644 --- a/api/src/services/import-services/marking/import-markings-strategy.test.ts +++ b/api/src/services/import-services/marking/import-markings-strategy.test.ts @@ -4,7 +4,7 @@ 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 { importCSV } from '../import-csv'; import { ImportMarkingsStrategy } from './import-markings-strategy'; import { CsvMarking } from './import-markings-strategy.interface'; diff --git a/api/src/services/import-services/marking/import-markings-strategy.ts b/api/src/services/import-services/marking/import-markings-strategy.ts index 25d1aefdcb..e1fa92f78a 100644 --- a/api/src/services/import-services/marking/import-markings-strategy.ts +++ b/api/src/services/import-services/marking/import-markings-strategy.ts @@ -7,13 +7,10 @@ import { IXLSXCSVValidator } from '../../../utils/xlsx-utils/worksheet-utils'; import { IAsSelectLookup, ICritterDetailed } 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 } from '../utils/datetime'; import { CsvMarking, getCsvMarkingSchema } from './import-markings-strategy.interface'; -// TODO: Update all import services to use language ImportStrategy -// TODO: Update CSVImportService interface -> CSVImportStrategy - const defaultLog = getLogger('services/import/import-markings-strategy'); /** @@ -23,7 +20,7 @@ const defaultLog = getLogger('services/import/import-markings-strategy'); * @see CSVImport * */ -export class ImportMarkingsStrategy extends DBService implements CSVImportService { +export class ImportMarkingsStrategy extends DBService implements CSVImportStrategy { surveyCritterService: SurveyCritterService; surveyId: number; diff --git a/api/src/utils/xlsx-utils/column-validator-utils.ts b/api/src/utils/xlsx-utils/column-validator-utils.ts index ade160f4a8..b570cb321d 100644 --- a/api/src/utils/xlsx-utils/column-validator-utils.ts +++ b/api/src/utils/xlsx-utils/column-validator-utils.ts @@ -1,4 +1,4 @@ -import { Row } from '../../services/import-services/csv-import-strategy.interface'; +import { Row } from '../../services/import-services/import-csv.interface'; import { IXLSXCSVColumn, IXLSXCSVValidator } from './worksheet-utils'; // TODO: Move the IXLSXCSVValidator type to this file