Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SIMSBIOHUB-656: Import CSV Markings #1451

Merged
merged 79 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
fe045e6
chore: partially done structure for new csv import
MacQSL Oct 28, 2024
8c0cc12
feat: validate method
MacQSL Oct 30, 2024
8062962
chore: wip
MacQSL Nov 19, 2024
a999b2d
feat: progress...
MacQSL Nov 20, 2024
79f488f
feat: added validators for cell values
MacQSL Nov 20, 2024
853cfe1
chore: moved files
MacQSL Nov 20, 2024
a0c4f87
feat: updated docs for types
MacQSL Nov 21, 2024
cb0a7b9
feat: created new critter strategy
MacQSL Nov 21, 2024
73a1f50
chore: still working on strucutre for config
MacQSL Nov 21, 2024
1cd7235
feat: updated helper functions for csv-utils
MacQSL Nov 25, 2024
c5d9b71
feat: updated import csv critters
MacQSL Nov 26, 2024
0dffc39
feat: added new config header util files
MacQSL Nov 27, 2024
834934f
feat: added files for csv-header-config validation / setter functions
MacQSL Nov 27, 2024
08709c1
chore: updated tests for cell validators
MacQSL Nov 28, 2024
4e742e6
feat: updated some structure + moved validation logic into class
MacQSL Nov 28, 2024
110766d
feat: moved some redundant params around + seperated CSVHeaderConfig …
MacQSL Nov 29, 2024
2b0469b
fix: updated some functions in csv-config-validation
MacQSL Nov 29, 2024
29c51b6
chore: tests for rorEachCSVCell
MacQSL Nov 29, 2024
2427513
feat: updated error formatting to be in the parent
MacQSL Nov 29, 2024
e1e563f
feat: partial PR comments
MacQSL Nov 29, 2024
61aad38
feat: PR comments + additional JSDocs
MacQSL Nov 29, 2024
410d0f4
feat: created type for cell setter and validator
MacQSL Nov 29, 2024
eb2b8aa
feat: updated tests for CSVConfigUtils
MacQSL Nov 29, 2024
7807f4e
feat: updated types in CSVConfigUtils
MacQSL Nov 29, 2024
75374ca
feat: updated getters in CSVConfigUtils
MacQSL Nov 30, 2024
2e230fa
chore: resolving broken tests
MacQSL Nov 30, 2024
d0494f4
feat: updated tests
MacQSL Nov 30, 2024
1ae762d
feat: added nested record class
MacQSL Dec 2, 2024
7cc6f4a
feat: added tests for endpoint
MacQSL Dec 3, 2024
32b4c89
fix: pr comments
MacQSL Dec 3, 2024
52f5c46
Merge branch 'dev' into mdColumnValidator
MacQSL Dec 4, 2024
ace5d05
fix: updated tests + included unique check for wlh_id
MacQSL Dec 4, 2024
e3b3435
Merge branch 'dev' into mdColumnValidator
MacQSL Dec 4, 2024
4b55647
feat: added errors to error dialog in frontend
MacQSL Dec 4, 2024
e7cd180
Merge branch 'mdColumnValidator' of https://github.com/bcgov/biohubbc…
MacQSL Dec 4, 2024
225cd17
feat: partially done markings
MacQSL Dec 6, 2024
c0e2d8b
update solutions text for import errors
mauberti-bc Dec 6, 2024
d4f0403
fix: adding optional flag for header
MacQSL Dec 6, 2024
99996e0
Merge branch 'mdColumnValidator' of https://github.com/bcgov/biohubbc…
MacQSL Dec 6, 2024
6b52461
fix: sex now optional
MacQSL Dec 6, 2024
3d114c4
Merge branch 'mdColumnValidator' into SIMSBIOHUB-633-markings
MacQSL Dec 6, 2024
c874004
fix: sex now optional
MacQSL Dec 6, 2024
1fc0b64
Merge branch 'mdColumnValidator' of https://github.com/bcgov/biohubbc…
MacQSL Dec 6, 2024
bd0fba5
Merge branch 'mdColumnValidator' into SIMSBIOHUB-633-markings
MacQSL Dec 6, 2024
31a6c7d
chore: consolidated methods into getCSVConfig
MacQSL Dec 10, 2024
2d2db7b
chore: updated tests for marking import
MacQSL Dec 11, 2024
3f1c9fc
chore: dropped old marking import files
MacQSL Dec 11, 2024
8932360
feat: added tests for import markings service
MacQSL Dec 11, 2024
f38a5f2
chore: merge conflicts with DEV resolved
MacQSL Dec 15, 2024
000fbdb
fix: some type issues with CSVConfigUtils
MacQSL Dec 16, 2024
e0d4488
feat: created custom error to throw for CSV validation
MacQSL Dec 17, 2024
049a33c
chore: partially done refactoring marking dictionary
MacQSL Dec 17, 2024
85d74a5
feat: basic table rendering CSV Errors implemented
MacQSL Dec 17, 2024
f8b1e0c
feat: created table container for CSV errors
MacQSL Dec 18, 2024
8683abc
fix: tweaked body locations
MacQSL Dec 18, 2024
2ad0df2
Merge branch 'dev' of https://github.com/bcgov/biohubbc into SIMSBIOH…
MacQSL Dec 18, 2024
535751b
Merge branch 'dev' of https://github.com/bcgov/biohubbc into SIMSBIOH…
MacQSL Dec 18, 2024
5ded2f6
Merge branch 'SIMSBIOHUB-633-markings' of https://github.com/bcgov/bi…
MacQSL Dec 18, 2024
190e4c0
chore: fixed tests
MacQSL Dec 18, 2024
3e27502
chore: code smells
MacQSL Dec 18, 2024
06c1aab
chore: moved cell column
MacQSL Dec 18, 2024
eb00d42
chore: updated tests
MacQSL Dec 19, 2024
fdb4f04
fix: updated tests for markings
MacQSL Dec 19, 2024
101010a
fix: gridcoldef null value lowercase
MacQSL Dec 19, 2024
db11a27
fix: issue with undefined values bubbling up
MacQSL Dec 20, 2024
32d13b2
fix: broken test
MacQSL Dec 20, 2024
f4ea8f3
fix: created component for options menu to prevent multiple menus ope…
MacQSL Dec 20, 2024
8e631bd
fix: updated openapi values type
MacQSL Dec 20, 2024
a9bca4b
fix: tests
MacQSL Dec 20, 2024
296ed60
fix: pr comments
MacQSL Dec 20, 2024
695ff2d
Update getWorksheetRowObjects.
NickPhura Dec 20, 2024
8eeb9c5
fix: dropped symbol
MacQSL Dec 20, 2024
785a9bd
Merge branch 'SIMSBIOHUB-633-markings' of https://github.com/bcgov/bi…
NickPhura Dec 20, 2024
d9f50ed
Merge branch 'SIMSBIOHUB-633-markings' of https://github.com/bcgov/bi…
NickPhura Dec 20, 2024
a763c3b
Fix getWorksheetRowObjects
NickPhura Dec 20, 2024
aa9d2b6
feat: added symbol for worksheet rows + PR nits
MacQSL Dec 20, 2024
71076df
fix: doc language
MacQSL Dec 20, 2024
db6c944
fix: created csv-utils file for types and helper functions
MacQSL Dec 20, 2024
c138cf2
fix: removed optional for taxon body location
MacQSL Dec 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion api/src/errors/http-error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DatabaseError } from 'pg';
import { CSVError } from '../utils/csv-utils/csv-config-validation.interface';
import { ApiError } from './api-error';
import { BaseError } from './base-error';

Expand All @@ -10,10 +11,20 @@
INTERNAL_SERVER_ERROR = 'Internal Server Error'
}

export enum HTTPCustomErrorType {
CSV_VALIDATION_ERROR = 'CSV Validation Error'
}

export class HTTPError extends BaseError {
status: number;

constructor(name: HTTPErrorType, status: number, message: string, errors?: (string | object)[], stack?: string) {
constructor(
name: HTTPErrorType | HTTPCustomErrorType,
status: number,
message: string,
errors?: (string | object)[],
stack?: string
) {
super(name, message, errors, stack);

this.status = status;
Expand Down Expand Up @@ -85,6 +96,19 @@
}
}

/**
* A HTTP `422 CSV Validation Error` error.
*
* @export
* @class CSVValidationError
* @extends {HTTPError}
*/
export class HTTP422CSVValidationError extends HTTPError {
constructor(message: string, errors: CSVError[]) {
super(HTTPCustomErrorType.CSV_VALIDATION_ERROR, 422, message, errors);

Check warning on line 108 in api/src/errors/http-error.ts

View check run for this annotation

Codecov / codecov/patch

api/src/errors/http-error.ts#L108

Added line #L108 was not covered by tests
}
}

/**
* Ensures that the incoming error is converted into an `HTTPError` if it is not one already.
* If `error` is a `HTTPError`, then change nothing and return it.
Expand Down
60 changes: 56 additions & 4 deletions api/src/openapi/schemas/csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const CSVErrorSchema: OpenAPIV3.SchemaObject = {
title: 'CSV validation error object',
type: 'object',
additionalProperties: false,
required: ['error', 'solution', 'row'],
required: ['error', 'solution', 'values', 'cell', 'header', 'row'],
properties: {
error: {
description: 'The error message',
Expand All @@ -21,21 +21,73 @@ export const CSVErrorSchema: OpenAPIV3.SchemaObject = {
values: {
description: 'The list of allowed values if applicable',
type: 'array',
nullable: true,
items: {
oneOf: [{ type: 'string' }, { type: 'number' }]
oneOf: [
{
type: 'string'
},
{
type: 'number'
}
]
}
},
cell: {
description: 'The CSV cell value',
oneOf: [{ type: 'string' }, { type: 'number' }]
oneOf: [
{
type: 'string'
},
{
type: 'number'
}
],
nullable: true
},
header: {
description: 'The header name used in the CSV file',
type: 'string'
type: 'string',
nullable: true
},
row: {
description: 'The row index the error occurred. Header row index 0. First data row index 1.',
type: 'number'
}
}
};

/**
* CSV validation error response schema
*
*/
export const CSVValidationErrorResponse: OpenAPIV3.ResponseObject = {
description: 'CSV validation errors response',
content: {
'application/json': {
schema: {
description: 'CSV validation error response object',
required: ['name', 'status', 'message', 'errors'],
properties: {
name: {
description: 'Error name',
type: 'string'
},
status: {
description: 'HTTP status code',
type: 'number'
},
message: {
description: 'Error message',
type: 'string'
},
errors: {
type: 'array',
description: 'List of CSV errors which occurred during validation',
items: CSVErrorSchema
}
}
}
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ describe('importCsv', () => {

const importCSVWorksheetStub = sinon.stub(ImportCrittersService.prototype, 'importCSVWorksheet');

importCSVWorksheetStub.resolves([]);

const mockFile = { originalname: 'test.csv', mimetype: 'test.csv', buffer: Buffer.alloc(1) } as Express.Multer.File;

const { mockReq, mockRes, mockNext } = getRequestHandlerMocks();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
import { Operation } from 'express-openapi';
import { PROJECT_PERMISSION, SYSTEM_ROLE } from '../../../../../../constants/roles';
import { getDBConnection } from '../../../../../../database/db';
import { HTTP422CSVValidationError } from '../../../../../../errors/http-error';
import { CSVValidationErrorResponse } from '../../../../../../openapi/schemas/csv';
import { csvFileSchema } from '../../../../../../openapi/schemas/file';
import { authorizeRequestHandler } from '../../../../../../request-handlers/security/authorization';
import { ImportCrittersService } from '../../../../../../services/import-services/critter/import-critters-service';
import { CSV_ERROR_MESSAGE } from '../../../../../../utils/csv-utils/csv-config-validation.interface';
import { getLogger } from '../../../../../../utils/logger';
import { parseMulterFile } from '../../../../../../utils/media/media-utils';
import { getFileFromRequest } from '../../../../../../utils/request';
Expand Down Expand Up @@ -94,6 +97,7 @@
403: {
$ref: '#/components/responses/403'
},
422: CSVValidationErrorResponse,
500: {
$ref: '#/components/responses/500'
},
Expand Down Expand Up @@ -123,7 +127,11 @@

const importService = new ImportCrittersService(connection, worksheet, surveyId);

await importService.importCSVWorksheet();
const errors = await importService.importCSVWorksheet();

if (errors.length) {
throw new HTTP422CSVValidationError(CSV_ERROR_MESSAGE, errors);

Check warning on line 133 in api/src/paths/project/{projectId}/survey/{surveyId}/critters/import.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/critters/import.ts#L133

Added line #L133 was not covered by tests
}

await connection.commit();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
import { Operation } from 'express-openapi';
import { PROJECT_PERMISSION, SYSTEM_ROLE } from '../../../../../../../constants/roles';
import { getDBConnection } from '../../../../../../../database/db';
import { HTTP422CSVValidationError } from '../../../../../../../errors/http-error';
import { CSVValidationErrorResponse } from '../../../../../../../openapi/schemas/csv';
import { csvFileSchema } from '../../../../../../../openapi/schemas/file';
import { authorizeRequestHandler } from '../../../../../../../request-handlers/security/authorization';
import { importCSV } from '../../../../../../../services/import-services/import-csv';
import { ImportMarkingsStrategy } from '../../../../../../../services/import-services/marking/import-markings-strategy';
import { ImportMarkingsService } from '../../../../../../../services/import-services/marking/import-markings-service';
import { CSV_ERROR_MESSAGE } from '../../../../../../../utils/csv-utils/csv-config-validation.interface';
import { getLogger } from '../../../../../../../utils/logger';
import { parseMulterFile } from '../../../../../../../utils/media/media-utils';
import { getFileFromRequest } from '../../../../../../../utils/request';
import { constructXLSXWorkbook, getDefaultWorksheet } from '../../../../../../../utils/xlsx-utils/worksheet-utils';

const defaultLog = getLogger('/api/project/{projectId}/survey/{surveyId}/markings/import');

Expand Down Expand Up @@ -85,21 +88,7 @@
},
responses: {
201: {
description: 'Marking import success.',
content: {
'application/json': {
schema: {
type: 'object',
additionalProperties: false,
properties: {
markingsCreated: {
description: 'Number of Critterbase markings created.',
type: 'integer'
}
}
}
}
}
description: 'Marking import success.'
},
400: {
$ref: '#/components/responses/400'
Expand All @@ -110,6 +99,7 @@
403: {
$ref: '#/components/responses/403'
},
422: CSVValidationErrorResponse,
500: {
$ref: '#/components/responses/500'
},
Expand All @@ -131,17 +121,23 @@

const connection = getDBConnection(req.keycloak_token);

const mediaFile = parseMulterFile(rawFile);
const worksheet = getDefaultWorksheet(constructXLSXWorkbook(mediaFile));

Check warning on line 125 in api/src/paths/project/{projectId}/survey/{surveyId}/critters/markings/import.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/critters/markings/import.ts#L124-L125

Added lines #L124 - L125 were not covered by tests

try {
await connection.open();

const importCsvMarkingsStrategy = new ImportMarkingsStrategy(connection, surveyId);
const importMarkings = new ImportMarkingsService(connection, worksheet, surveyId);

Check warning on line 130 in api/src/paths/project/{projectId}/survey/{surveyId}/critters/markings/import.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/critters/markings/import.ts#L130

Added line #L130 was not covered by tests

const errors = await importMarkings.importCSVWorksheet();

Check warning on line 132 in api/src/paths/project/{projectId}/survey/{surveyId}/critters/markings/import.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/critters/markings/import.ts#L132

Added line #L132 was not covered by tests

// Pass CSV file and importer as dependencies
const markingsCreated = await importCSV(parseMulterFile(rawFile), importCsvMarkingsStrategy);
if (errors.length) {
throw new HTTP422CSVValidationError(CSV_ERROR_MESSAGE, errors);

Check warning on line 135 in api/src/paths/project/{projectId}/survey/{surveyId}/critters/markings/import.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/critters/markings/import.ts#L135

Added line #L135 was not covered by tests
}

await connection.commit();

return res.status(201).json({ markingsCreated });
return res.status(201).send();

Check warning on line 140 in api/src/paths/project/{projectId}/survey/{surveyId}/critters/markings/import.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/critters/markings/import.ts#L140

Added line #L140 was not covered by tests
} catch (error) {
defaultLog.error({ label: 'importMarkingsCSV', message: 'error', error });
await connection.rollback();
Expand Down
Loading
Loading