Skip to content

Commit

Permalink
feat: partial PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
MacQSL committed Nov 29, 2024
1 parent 2427513 commit e1e563f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>} surveyAliases The survey aliases.
* @param {CSVConfigUtils<CritterCSVConfig>} configUtils The CSV config utils.
* @returns {*} {(params: CSVParams) => CSVError[]} The validate cell callback
Expand Down Expand Up @@ -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<CritterCSVConfig>} configUtils The CSV config utils.
* @returns {*} {(params: CSVParams) => CSVError[]} The validate cell callback
Expand All @@ -54,7 +64,7 @@ export const getCritterCollectionUnitCellValidator = (
configUtils: CSVConfigUtils<CritterCSVConfig>
): ((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;
Expand Down Expand Up @@ -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<CritterCSVConfig>} configUtils The CSV config utils.
* @returns {*} {(params: CSVParams) => CSVError[]} The validate cell callback
Expand Down
31 changes: 15 additions & 16 deletions api/src/services/import-services/critter/import-critters-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 [];
}
Expand Down

0 comments on commit e1e563f

Please sign in to comment.