Skip to content

Commit

Permalink
feat: added symbol for worksheet rows + PR nits
Browse files Browse the repository at this point in the history
  • Loading branch information
MacQSL committed Dec 20, 2024
1 parent a763c3b commit aa9d2b6
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ describe('ImportCrittersService', () => {

const sexHeaderConfig = await service._getSexHeaderConfig();

expect(getTaxonMeasurementsStub).to.have.been.calledWithExactly(1234);
expect(getTaxonMeasurementsStub).to.have.been.calledWithExactly('1234');
expect(getSexCellValidatorStub).to.have.been.calledWithExactly(
new NestedRecord({
1234: { male: 'maleUUID', female: 'femaleUUID' }
Expand Down Expand Up @@ -224,7 +224,7 @@ describe('ImportCrittersService', () => {

const config = await service._getCollectionUnitDynamicHeaderConfig();

expect(findTaxonCollectionUnitsStub).to.have.been.calledOnceWithExactly(1234);
expect(findTaxonCollectionUnitsStub).to.have.been.calledOnceWithExactly('1234');

expect(getCollectionUnitCellValidatorStub).to.have.been.calledWithExactly(
new NestedRecord({ 1234: { category: { unit: 'uuid' } } }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export class ImportCrittersService extends DBService {
* @returns {*} {Promise<CSVHeaderConfig>} The TSN header config
*/
async _getTsnHeaderConfig(): Promise<CSVHeaderConfig> {
const rowTsns = this.configUtils.getUniqueCellValues('ITIS_TSN').map((tsn) => Number(tsn));
const rowTsns = this.configUtils.getUniqueCellValues('ITIS_TSN').map((tsn) => String(tsn));
const taxonomy = await this.platformService.getTaxonomyByTsns(rowTsns);
const allowedTsns = new Set(taxonomy.map((taxon) => taxon.tsn));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,18 @@ export const getMarkingAliasCellValidator = (surveyAliasMap: Map<string, ICritte
* Rules:
* 1. The cell must be a valid marking type ie: exists in the markingTypes set
*
* @param {Set<string>} _markingTypes The marking types
* @param {Set<string>} markingTypes The marking types set (case insensitive)
* @returns {*} {CSVCellValidator} The validate cell callback
*/
export const getMarkingTypeCellValidator = (_markingTypes: Set<string>): CSVCellValidator => {
const markingTypes = setToLowercase(_markingTypes);
export const getMarkingTypeCellValidator = (markingTypes: Set<string>): CSVCellValidator => {
const markingTypesLowerCased = setToLowercase(markingTypes);

return (params: CSVParams) => {
if (params.cell === undefined) {
return [];
}

if (!markingTypes.has(String(params.cell).toLowerCase())) {
if (!markingTypesLowerCased.has(String(params.cell).toLowerCase())) {
return [
{
error: `Marking type not supported`,
Expand All @@ -104,18 +104,18 @@ export const getMarkingTypeCellValidator = (_markingTypes: Set<string>): CSVCell
/**
* Get the marking type cell setter.
*
* @param {Set<string>} _colours The colours
* @param {Set<string>} colours The colours set (case insensitive)
* @returns {*} {CSVCellSetter} The set cell callback
*/
export const getMarkingColourCellValidator = (_colours: Set<string>): CSVCellValidator => {
const colours = setToLowercase(_colours);
export const getMarkingColourCellValidator = (colours: Set<string>): CSVCellValidator => {
const coloursLowerCased = setToLowercase(colours);

return (params: CSVParams) => {
if (params.cell === undefined) {
return [];
}

if (colours.has(String(params.cell).toLowerCase())) {
if (coloursLowerCased.has(String(params.cell).toLowerCase())) {
return [];
}

Expand Down
11 changes: 8 additions & 3 deletions api/src/utils/csv-utils/csv-config-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from 'chai';
import xlsx, { WorkSheet } from 'xlsx';
import { WorksheetRowIndexSymbol } from '../xlsx-utils/worksheet-utils';
import { CSVConfigUtils } from './csv-config-utils';
import { CSVConfig } from './csv-config-validation.interface';

Expand All @@ -22,9 +23,13 @@ describe('CSVConfigUtils', () => {
expect(utils).to.be.instanceOf(CSVConfigUtils);
expect(utils._config).to.be.equal(mockConfig);
expect(utils.worksheet).to.be.equal(worksheet);
expect(utils.worksheetRows).to.be.deep.equal([
{ TEST: 'cellValue', ALIASED_HEADER: 'cellValue2', DYNAMIC_HEADER: 'dynamicValue' }
]);

expect(utils.worksheetRows[0]).to.deep.equal({
TEST: 'cellValue',
ALIASED_HEADER: 'cellValue2',
DYNAMIC_HEADER: 'dynamicValue',
[WorksheetRowIndexSymbol]: 1
});
expect(utils.worksheetHeaders).to.be.deep.equal(['TEST', 'ALIASED_HEADER', 'DYNAMIC_HEADER']);
expect(utils.worksheetAliasedStaticHeaders).to.be.deep.equal(['TEST', 'ALIASED_HEADER']);
expect(utils.worksheetStaticHeaders).to.be.deep.equal(['TEST', 'TEST_ALIAS']);
Expand Down
7 changes: 4 additions & 3 deletions api/src/utils/csv-utils/csv-config-validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import chai, { expect } from 'chai';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import xlsx, { WorkSheet } from 'xlsx';
import { WorksheetRowIndexSymbol } from '../xlsx-utils/worksheet-utils';
import {
executeSetCellValue,
executeValidateCell,
Expand Down Expand Up @@ -309,7 +310,7 @@ describe('csv-config-validation', () => {
cell: 'cellValue',
header: 'TEST_ALIAS',
rowIndex: 0,
row: { TEST_ALIAS: 'cellValue', DYNAMIC_HEADER: 'dynamicValue' },
row: { TEST_ALIAS: 'cellValue', DYNAMIC_HEADER: 'dynamicValue', [WorksheetRowIndexSymbol]: 1 },
staticHeader: 'TEST',
mutateCell: 'cellValue'
},
Expand All @@ -324,7 +325,7 @@ describe('csv-config-validation', () => {
cell: 'dynamicValue',
header: 'DYNAMIC_HEADER',
rowIndex: 0,
row: { TEST_ALIAS: 'cellValue', DYNAMIC_HEADER: 'dynamicValue' },
row: { TEST_ALIAS: 'cellValue', DYNAMIC_HEADER: 'dynamicValue', [WorksheetRowIndexSymbol]: 1 },
staticHeader: undefined, // Dynamic headers have no static header mapping
mutateCell: 'dynamicValue'
},
Expand All @@ -346,7 +347,7 @@ describe('csv-config-validation', () => {
cell: 'cellValue',
header: 'TEST',
rowIndex: 0,
row: { TEST: 'cellValue' },
row: { TEST: 'cellValue', [WorksheetRowIndexSymbol]: 1 },
staticHeader: 'TEST',
mutateCell: 'cellValue'
};
Expand Down
5 changes: 3 additions & 2 deletions api/src/utils/csv-utils/csv-config-validation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WorkSheet } from 'xlsx';
import { getWorksheetRowObjects } from '../xlsx-utils/worksheet-utils';
import { getWorksheetRowObjects, WorksheetRowIndexSymbol } from '../xlsx-utils/worksheet-utils';
import { CSVConfigUtils } from './csv-config-utils';
import {
CSVConfig,
Expand Down Expand Up @@ -220,7 +220,8 @@ export const executeValidateCell = (
values: error.values ?? null,
cell: (error.cell === undefined ? params.cell : error.cell) ?? null, // Use cell value if intentionally null
header: (error.header === undefined ? params.header : error.header) ?? null, // Use header value if intentionally null
row: error.row ?? params.rowIndex + 2 // headers: 1, data row: 2
// WorksheetRowIndexSymbol is the original row index from the worksheet ie: before filtering empty rows
row: error.row ?? params.row[WorksheetRowIndexSymbol] + 1 ?? params.rowIndex + 2 // headers: 1, data row: 2
});
});
}
Expand Down
6 changes: 4 additions & 2 deletions api/src/utils/xlsx-utils/worksheet-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dayjs.extend(customParseFormat);

const defaultLog = getLogger('src/utils/xlsx-utils/worksheet-utils');

const RowIndex = Symbol();
export const WorksheetRowIndexSymbol = Symbol('WorksheetRowIndex');

export interface IXLSXCSVColumn {
/**
Expand Down Expand Up @@ -223,7 +223,9 @@ export const getWorksheetRowObjects = (worksheet: xlsx.WorkSheet): Record<symbol
rowHasValues = true;
}

rowObject[RowIndex] = i;
// Add the original row index to the row object
// Symbols are non-enumerable, so they won't be included in the JSON output
rowObject[WorksheetRowIndexSymbol] = i;

if (rowHasValues) {
// Add the row object to the array if it has at least one non-empty cell
Expand Down

0 comments on commit aa9d2b6

Please sign in to comment.