diff --git a/api/src/services/import-services/critter/critter-header-configs.ts b/api/src/services/import-services/critter/critter-header-configs.ts index 23fea4a64e..d65df3f829 100644 --- a/api/src/services/import-services/critter/critter-header-configs.ts +++ b/api/src/services/import-services/critter/critter-header-configs.ts @@ -8,6 +8,11 @@ import { CritterCSVConfig } from './import-critters-service'; /** * Get the critter alias cell validator. * + * Rules: + * 1. The cell must be a string with a length between 1 and 50 + * 2. The cell must be unique in the survey + * 3. The cell must be unique in the CSV + * * @param {Set} surveyAliases The survey aliases. * @param {CSVConfigUtils} configUtils The CSV config utils. * @returns {*} {(params: CSVParams) => CSVError[]} The validate cell callback @@ -45,6 +50,11 @@ export const getCritterAliasCellValidator = ( /** * Get the critter collection unit cell validator. * + * Rules: + * 1. The cell must be a string with a max length of 50 or empty + * 2. The header must be a valid collection category for the TSN + * 3. The cell value must be a valid collection unit for the collection category + * * @param {Object} rowDictionary The row dictionary. * @param {CSVConfigUtils} configUtils The CSV config utils. * @returns {*} {(params: CSVParams) => CSVError[]} The validate cell callback @@ -54,7 +64,7 @@ export const getCritterCollectionUnitCellValidator = ( configUtils: CSVConfigUtils ): ((params: CSVParams) => CSVError[]) => { return (params: CSVParams) => { - const cellErrors = validateZodCell(params, z.string().trim().min(1).max(50).optional()); + const cellErrors = validateZodCell(params, z.string().max(50).optional()); if (cellErrors.length || !params.cell) { return cellErrors; @@ -116,6 +126,11 @@ export const getCritterCollectionUnitCellSetter = ( /** * Get the critter sex cell validator. * + * Rules: + * 1. The cell must be a string with a min length of 1 and max length of 50 + * 2. The TSN must have sex measurements available + * 3. The cell value must be a valid sex option for the TSN + * * @param {Object} rowDictionary The row dictionary. * @param {CSVConfigUtils} configUtils The CSV config utils. * @returns {*} {(params: CSVParams) => CSVError[]} The validate cell callback diff --git a/api/src/services/import-services/critter/import-critters-service.ts b/api/src/services/import-services/critter/import-critters-service.ts index 2c80f697c3..d03f1ccc65 100644 --- a/api/src/services/import-services/critter/import-critters-service.ts +++ b/api/src/services/import-services/critter/import-critters-service.ts @@ -2,6 +2,7 @@ import { merge, set } from 'lodash'; import { v4 } from 'uuid'; import { WorkSheet } from 'xlsx'; import { IDBConnection } from '../../../database/db'; +import { ApiGeneralError } from '../../../errors/api-error'; import { CSVConfigUtils } from '../../../utils/csv-utils/csv-config-utils'; import { validateCSVWorksheet } from '../../../utils/csv-utils/csv-config-validation'; import { @@ -104,22 +105,20 @@ export class ImportCrittersService extends DBService { const payloads = await this._getImportPayloads(rows); - console.log({ payloads }); - - //// Add critters to Critterbase - //const bulkResponse = await this.critterbaseService.bulkCreate(payloads.critterbasePayload); - // - //// Check critterbase inserted the full list of critters - //// In reality this error should not be triggered, safeguard to prevent floating critter ids in SIMS - //if (bulkResponse.created.critters !== payloads.simsPayload.length) { - // throw new ApiGeneralError('Unable to fully import critters from CSV', [ - // 'importCrittersStrategy -> insertCsvCrittersIntoSimsAndCritterbase', - // 'critterbase bulk create response count !== critterIds.length' - // ]); - //} - // - //// Add Critters to SIMS survey - //await this.surveyCritterService.addCrittersToSurvey(this.surveyId, payloads.simsPayload); + // Add critters to Critterbase + const bulkResponse = await this.critterbaseService.bulkCreate(payloads.critterbasePayload); + + // Check critterbase inserted the full list of critters + // In reality this error should not be triggered, safeguard to prevent floating critter ids in SIMS + if (bulkResponse.created.critters !== payloads.simsPayload.length) { + throw new ApiGeneralError('Unable to fully import critters from CSV', [ + 'importCrittersService->importCSVWorksheet', + 'critterbase bulk create response count !== critterIds.length' + ]); + } + + // Add Critters to SIMS survey + await this.surveyCritterService.addCrittersToSurvey(this.surveyId, payloads.simsPayload); return []; }