Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
SIMSBIOHUB-288: Sample Site Dropdowns (#1110)
* Add datagrid autocomplete components
* Add datagrid support for sample site, method, and period columns
* Add time component to data grid.
* Added node-pg float type parser
* Save/Edit datagrid observations working
  • Loading branch information
al-rosenthal authored Oct 11, 2023
commit e976b831438724a3acae793e8a0588b46dc7872e
12 changes: 6 additions & 6 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions api/src/database/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ pg.types.setTypeParser(pg.types.builtins.TIMESTAMP, (stringValue: string) => {
pg.types.setTypeParser(pg.types.builtins.TIMESTAMPTZ, (stringValue: string) => {
return stringValue; // 1082 for `DATE` type
});
// NUMERIC column types return as strings to maintain precision. Converting this to a float so it is usable by the system
// Explanation of why Numeric returns as a string: https://github.com/brianc/node-postgres/issues/811
pg.types.setTypeParser(pg.types.builtins.NUMERIC, (stringValue: string) => {
return parseFloat(stringValue);
});

// singleton pg pool instance used by the api
let DBPool: pg.Pool | undefined;
Expand Down
2 changes: 1 addition & 1 deletion api/src/paths/project/{projectId}/survey/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { authorizeRequestHandler } from '../../../../request-handlers/security/a
import { SurveyService } from '../../../../services/survey-service';
import { getLogger } from '../../../../utils/logger';

const defaultLog = getLogger('paths/project/{projectId}/surveys');
const defaultLog = getLogger('paths/project/{projectId}/survey/list');

export const GET: Operation = [
authorizeRequestHandler((req) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,20 +462,26 @@ describe('insertUpdateSurveyObservations', () => {
latitude: 48.103322,
longitude: -122.798892,
observation_date: '1970-01-01',
observation_time: '00:00:00'
observation_time: '00:00:00',
survey_sample_site_id: 1,
survey_sample_method_id: 1,
survey_sample_period_id: 1
},
{
wldtaxonomic_units_id: 1234,
count: 99,
latitude: 48.103322,
longitude: -122.798892,
observation_date: '1970-01-01',
observation_time: '00:00:00'
observation_time: '00:00:00',
survey_sample_site_id: 1,
survey_sample_method_id: 1,
survey_sample_period_id: 1
}
]
};

const requestHandler = observationRecords.insertUpdateSurveyObservations();
const requestHandler = observationRecords.insertUpdateDeleteSurveyObservations();
await requestHandler(mockReq, mockRes, mockNext);

expect(insertUpdateSurveyObservationsStub).to.have.been.calledOnceWith(2, [
Expand All @@ -486,7 +492,10 @@ describe('insertUpdateSurveyObservations', () => {
longitude: -122.798892,
count: 99,
observation_date: '1970-01-01',
observation_time: '00:00:00'
observation_time: '00:00:00',
survey_sample_site_id: 1,
survey_sample_method_id: 1,
survey_sample_period_id: 1
},
{
survey_observation_id: undefined,
Expand All @@ -495,7 +504,10 @@ describe('insertUpdateSurveyObservations', () => {
longitude: -122.798892,
count: 99,
observation_date: '1970-01-01',
observation_time: '00:00:00'
observation_time: '00:00:00',
survey_sample_site_id: 1,
survey_sample_method_id: 1,
survey_sample_period_id: 1
}
]);
expect(mockRes.statusValue).to.equal(200);
Expand Down Expand Up @@ -533,7 +545,7 @@ describe('insertUpdateSurveyObservations', () => {
};

try {
const requestHandler = observationRecords.insertUpdateSurveyObservations();
const requestHandler = observationRecords.insertUpdateDeleteSurveyObservations();

await requestHandler(mockReq, mockRes, mockNext);
expect.fail();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const PUT: Operation = [
]
};
}),
insertUpdateSurveyObservations()
insertUpdateDeleteSurveyObservations()
];

const surveyObservationsResponseSchema: SchemaObject = {
Expand Down Expand Up @@ -127,7 +127,7 @@ const surveyObservationsResponseSchema: SchemaObject = {
};

GET.apiDoc = {
description: 'Fetches observation records for the given survey.',
description: 'Get all observations for the survey.',
tags: ['observation'],
security: [
{
Expand Down Expand Up @@ -170,7 +170,7 @@ GET.apiDoc = {
$ref: '#/components/responses/401'
},
403: {
$ref: '#/components/responses/401'
$ref: '#/components/responses/403'
},
500: {
$ref: '#/components/responses/500'
Expand All @@ -182,8 +182,8 @@ GET.apiDoc = {
};

PUT.apiDoc = {
description: 'Fetches observation records for the given survey.',
tags: ['attachments'],
description: 'Insert/update/delete observations for the survey.',
tags: ['observation'],
security: [
{
Bearer: []
Expand Down Expand Up @@ -250,7 +250,7 @@ PUT.apiDoc = {
},
responses: {
200: {
description: 'Upload OK',
description: 'Update OK',
content: {
'application/json': {
schema: { ...surveyObservationsResponseSchema }
Expand All @@ -264,7 +264,7 @@ PUT.apiDoc = {
$ref: '#/components/responses/401'
},
403: {
$ref: '#/components/responses/401'
$ref: '#/components/responses/403'
},
500: {
$ref: '#/components/responses/500'
Expand All @@ -275,6 +275,12 @@ PUT.apiDoc = {
}
};

/**
* Fetch all observations for a survey.
*
* @export
* @return {*} {RequestHandler}
*/
export function getSurveyObservations(): RequestHandler {
return async (req, res) => {
const surveyId = Number(req.params.surveyId);
Expand All @@ -300,11 +306,19 @@ export function getSurveyObservations(): RequestHandler {
};
}

export function insertUpdateSurveyObservations(): RequestHandler {
/**
* Inserts new observation records.
* Updates existing observation records.
* Deletes missing observation records.
*
* @export
* @return {*} {RequestHandler}
*/
export function insertUpdateDeleteSurveyObservations(): RequestHandler {
return async (req, res) => {
const surveyId = Number(req.params.surveyId);

defaultLog.debug({ label: 'insertUpdateSurveyObservations', surveyId });
defaultLog.debug({ label: 'insertUpdateDeleteSurveyObservations', surveyId });

const connection = getDBConnection(req['keycloak_token']);

Expand All @@ -318,12 +332,15 @@ export function insertUpdateSurveyObservations(): RequestHandler {
return {
survey_observation_id: record.survey_observation_id,
wldtaxonomic_units_id: Number(record.wldtaxonomic_units_id),
survey_sample_site_id: record.survey_sample_site_id,
survey_sample_method_id: record.survey_sample_method_id,
survey_sample_period_id: record.survey_sample_period_id,
latitude: record.latitude,
longitude: record.longitude,
count: record.count,
observation_date: record.observation_date,
observation_time: record.observation_time
};
} as InsertObservation | UpdateObservation;
});

const surveyObservations = await observationService.insertUpdateDeleteSurveyObservations(surveyId, records);
Expand All @@ -332,7 +349,7 @@ export function insertUpdateSurveyObservations(): RequestHandler {

return res.status(200).json({ surveyObservations });
} catch (error) {
defaultLog.error({ label: 'insertUpdateSurveyObservations', message: 'error', error });
defaultLog.error({ label: 'insertUpdateDeleteSurveyObservations', message: 'error', error });
await connection.rollback();
throw error;
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export function getSurveySampleLocationRecords(): RequestHandler {
return res.status(200).json({ sampleSites: result });
} catch (error) {
defaultLog.error({ label: 'getSurveySampleLocationRecords', message: 'error', error });
await connection.rollback();
throw error;
} finally {
connection.release();
Expand Down Expand Up @@ -309,6 +310,7 @@ export function createSurveySampleSiteRecord(): RequestHandler {
return res.status(201).send();
} catch (error) {
defaultLog.error({ label: 'insertProjectParticipants', message: 'error', error });
await connection.rollback();
throw error;
} finally {
connection.release();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export function updateSurveySampleSite(): RequestHandler {
return res.status(204).send();
} catch (error) {
defaultLog.error({ label: 'updateSurveySampleSite', message: 'error', error });
await connection.rollback();
throw error;
} finally {
connection.release();
Expand Down Expand Up @@ -253,6 +254,7 @@ export function deleteSurveySampleSiteRecord(): RequestHandler {
return res.status(204).send();
} catch (error) {
defaultLog.error({ label: 'deleteSurveySampleSiteRecord', message: 'error', error });
await connection.rollback();
throw error;
} finally {
connection.release();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export function getSurveySampleMethodRecords(): RequestHandler {
return res.status(200).json({ sampleMethods: result });
} catch (error) {
defaultLog.error({ label: 'getSurveySampleMethodRecords', message: 'error', error });
await connection.rollback();
throw error;
} finally {
connection.release();
Expand Down Expand Up @@ -298,6 +299,7 @@ export function createSurveySampleSiteRecord(): RequestHandler {
return res.status(201).send();
} catch (error) {
defaultLog.error({ label: 'insertProjectParticipants', message: 'error', error });
await connection.rollback();
throw error;
} finally {
connection.release();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export function updateSurveySampleMethod(): RequestHandler {
return res.status(204).send();
} catch (error) {
defaultLog.error({ label: 'updateSurveySampleMethod', message: 'error', error });
await connection.rollback();
throw error;
} finally {
connection.release();
Expand Down Expand Up @@ -250,6 +251,7 @@ export function deleteSurveySampleMethodRecord(): RequestHandler {
return res.status(204).send();
} catch (error) {
defaultLog.error({ label: 'deleteSurveySampleMethodRecord', message: 'error', error });
await connection.rollback();
throw error;
} finally {
connection.release();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export function getSurveySamplePeriodRecords(): RequestHandler {
return res.status(200).json({ samplePeriods: result });
} catch (error) {
defaultLog.error({ label: 'getSurveySamplePeriodRecords', message: 'error', error });
await connection.rollback();
throw error;
} finally {
connection.release();
Expand Down Expand Up @@ -311,6 +312,7 @@ export function createSurveySamplePeriodRecord(): RequestHandler {
return res.status(201).send();
} catch (error) {
defaultLog.error({ label: 'createSurveySamplePeriodRecord', message: 'error', error });
await connection.rollback();
throw error;
} finally {
connection.release();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export function updateSurveySamplePeriod(): RequestHandler {
return res.status(204).send();
} catch (error) {
defaultLog.error({ label: 'updateSurveySamplePeriod', message: 'error', error });
await connection.rollback();
throw error;
} finally {
connection.release();
Expand Down Expand Up @@ -287,6 +288,7 @@ export function deleteSurveySamplePeriodRecord(): RequestHandler {
return res.status(204).send();
} catch (error) {
defaultLog.error({ label: 'deleteSurveySamplePeriodRecord', message: 'error', error });
await connection.rollback();
throw error;
} finally {
connection.release();
Expand Down
Loading