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

Update template validation schemas #612

Merged
merged 8 commits into from
Oct 20, 2021
8 changes: 8 additions & 0 deletions app/src/features/resources/ResourcesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ const ResourcesPage: React.FC = () => {
type: mdiFileExcelOutline,
lastModified: 'Today',
fileSize: '36 KB'
},
{
id: '3',
name: 'Moose Summary Statistics',
url: 'https://nrs.objectstore.gov.bc.ca/gblhvt/templates/Moose_Summary_Statistics.xlsx',
type: mdiFileExcelOutline,
lastModified: 'Today',
fileSize: '10 KB'
}
];

Expand Down
2 changes: 1 addition & 1 deletion app/src/features/search/SearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const SearchPage: React.FC = () => {
dialogErrorDetails: apiError?.errors
});
}
}, [biohubApi.search, showFilterErrorDialog]);
}, [biohubApi.search, biohubApi.public.search, showFilterErrorDialog, keycloakWrapper]);

useEffect(() => {
if (performSearch) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/features/surveys/view/SurveySummaryResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const SurveySummaryResults = () => {
} catch (error) {
return error;
}
}, [biohubApi.project, biohubApi.survey, surveyId, projectId, hasErrorMessages]);
}, [biohubApi.survey, surveyId, projectId]);

useEffect(() => {
if (isLoading) {
Expand Down
6 changes: 3 additions & 3 deletions app/src/pages/access/AccessRequestPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ describe('AccessRequestPage', () => {
id: 1
});

const { getByText, getAllByRole, getByRole, getByTestId } = renderContainer();
const { getByText, getAllByRole, getByRole } = renderContainer();

fireEvent.mouseDown(getAllByRole('button')[0]);

Expand Down Expand Up @@ -225,7 +225,7 @@ describe('AccessRequestPage', () => {

mockBiohubApi().admin.createAdministrativeActivity = jest.fn(() => Promise.reject(new Error('API Error is Here')));

const { getByText, getAllByRole, getByRole, getByTestId, queryByText } = renderContainer();
const { getByText, getAllByRole, getByRole, queryByText } = renderContainer();

fireEvent.mouseDown(getAllByRole('button')[0]);

Expand Down Expand Up @@ -260,7 +260,7 @@ describe('AccessRequestPage', () => {
id: null
});

const { getByText, getAllByRole, getByRole, getByTestId, queryByText } = renderContainer();
const { getByText, getAllByRole, getByRole, queryByText } = renderContainer();

fireEvent.mouseDown(getAllByRole('button')[0]);

Expand Down
127 changes: 127 additions & 0 deletions database/src/migrations/20211020095230_update_validation_schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import * as fs from 'fs';
import Knex from 'knex';
import path from 'path';

const DB_SCHEMA = process.env.DB_SCHEMA;

const VALIDATION_SCHEMAS_FOLDER = 'template_methodology_species_validations';

const moose_srb_or_composition = fs.readFileSync(
path.join(__dirname, VALIDATION_SCHEMAS_FOLDER, 'moose_srb_or_composition_survey_3.json')
);

const moose_recruitment_survey = fs.readFileSync(
path.join(__dirname, VALIDATION_SCHEMAS_FOLDER, 'moose_recruitment_survey_3.json')
);

enum COMMON_SURVEY_METHODOLOGY {
STRATIFIED_RANDOM_BLOCK = 'Stratified Random Block',
COMPOSITION = 'Composition',
RECRUITMENT = 'Recruitment'
}

enum SPECIES_NAME {
MOOSE = 'Moose'
}

enum TEMPLATE_NAME {
MOOSE_SRB_OR_COMPOSITION_SURVEY = 'Moose SRB or Composition Survey',
MOOSE_RECRUITMENT_SURVEY = 'Moose Recruitment Survey'
}

const validationSchemas = [
// Common Survey Methodology: Stratified Random Block or Composition
{
schema: moose_srb_or_composition.toString(),
cms: COMMON_SURVEY_METHODOLOGY.STRATIFIED_RANDOM_BLOCK,
species: SPECIES_NAME.MOOSE,
template: TEMPLATE_NAME.MOOSE_SRB_OR_COMPOSITION_SURVEY
},
{
schema: moose_srb_or_composition.toString(),
cms: COMMON_SURVEY_METHODOLOGY.COMPOSITION,
species: SPECIES_NAME.MOOSE,
template: TEMPLATE_NAME.MOOSE_SRB_OR_COMPOSITION_SURVEY
},
// Common Survey Methodology: Recruitment
{
schema: moose_recruitment_survey.toString(),
cms: COMMON_SURVEY_METHODOLOGY.RECRUITMENT,
species: SPECIES_NAME.MOOSE,
template: TEMPLATE_NAME.MOOSE_RECRUITMENT_SURVEY
}
];

/**
* Populate template validations.
*
* @export
* @param {Knex} knex
* @return {*} {Promise<void>}
*/
export async function up(knex: Knex): Promise<void> {
await knex.raw(`
set schema '${DB_SCHEMA}';
set search_path = ${DB_SCHEMA},public;
`);

for (const validationSchema of validationSchemas) {
await knex.raw(`
${updateValidation(
validationSchema.schema,
validationSchema.cms,
validationSchema.species,
validationSchema.template
)}
`);
}
}

export async function down(knex: Knex): Promise<void> {
await knex.raw(``);
}

/**
* SQL to update the template_methodology_species validation row.
* The select statement returns the template_methodology_species_id
* The validation field is filled with a JSON schema based on the combination of:
* 1) common_survey_methodology
* 2) species name
* 3) template name
*
* @param {string} validationJSON validation rules config
* @param {string} csm common survey methodology needed for the query
* @param {string} species species `english name` from the wldtaxonomic_units table needed for the query
* @param {string} template name of the template
*/
const updateValidation = (validationJSON: string, csm: string, species: string, template: string) => `
UPDATE
biohub.template_methodology_species tms
SET
validation = '${validationJSON}'
WHERE
tms.template_methodology_species_id =
(SELECT
template_methodology_species_id
FROM
template_methodology_species tms
LEFT JOIN
common_survey_methodology csm
ON
tms.common_survey_methodology_id = csm.common_survey_methodology_id
LEFT JOIN
wldtaxonomic_units wu
ON
tms.wldtaxonomic_units_id = wu.wldtaxonomic_units_id
LEFT JOIN
template t
ON
tms.template_id = t.template_id
WHERE
csm.name = '${csm}'
AND
wu.english_name = '${species}'
AND
t.name = '${template}'
);
`;
Loading