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

BHBC-2078: Amend summary template validation schema for taxonomic codes #882

Merged
merged 5 commits into from
Dec 7, 2022
Merged
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { Knex } from 'knex';
import {
summaryTemplateValidationJson,
SUMMARY_TEMPLATE_NAME
} from './summary_template_species_validations/summary_template_validation';

const DB_SCHEMA = process.env.DB_SCHEMA;

const taxonIdLists = {
// M-OVCA; M-OVCA-CA; M-OVDA; M-OVDA-DA; M-OVDA-ST.
SHEEP: ['4166', '474', '8619', '475', '476'],

// M-CEEL; M-CEEL-RO; M-CECA; M-CECA-RO.
ELK: ['2227', '2228', '4149', '6901'],

// M-ALAM; M-ALAM-AN; M-ALAM-GI; M-ALAM-SH.
MOOSE: ['4147', '6897', '6898', '6899'],

// M-ORAM
GOAT: ['4165']
};

interface IValidationSchema {
validation: string; // Validation JSON
summaryTemplateName: SUMMARY_TEMPLATE_NAME; // Name of the summary template
species: keyof typeof taxonIdLists; // Wild taxonomic units code
}

const validationSchema: IValidationSchema[] = [
{
// Moose
validation: JSON.stringify(summaryTemplateValidationJson),
summaryTemplateName: SUMMARY_TEMPLATE_NAME.MOOSE_SUMMARY_RESULTS,
species: 'MOOSE'
},
{
// Sheep
validation: JSON.stringify(summaryTemplateValidationJson),
summaryTemplateName: SUMMARY_TEMPLATE_NAME.SHEEP_SUMMARY_RESULTS,
species: 'SHEEP'
},
{
// Goat
validation: JSON.stringify(summaryTemplateValidationJson),
summaryTemplateName: SUMMARY_TEMPLATE_NAME.GOAT_SUMMARY_RESULTS,
species: 'GOAT'
},
{
// Elk
validation: JSON.stringify(summaryTemplateValidationJson),
summaryTemplateName: SUMMARY_TEMPLATE_NAME.ELK_SUMMARY_RESULTS,
species: 'ELK'
}
];

/**
* Populate summary 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 schema of validationSchema) {
for (const taxonomicCode of taxonIdLists[schema.species]) {
await knex.raw(`
INSERT INTO
${DB_SCHEMA}.summary_template_species (summary_template_id, wldtaxonomic_units_id, validation, create_date)
VALUES (
(SELECT summary_template_id FROM ${DB_SCHEMA}.summary_template WHERE name = '${schema.summaryTemplateName}'),
'${taxonomicCode}',
'${schema.validation}',
now()
)
ON CONFLICT (summary_template_id, wldtaxonomic_units_id) DO NOTHING;
`);
}
}
}

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