Skip to content

Commit

Permalink
Bhbc 1173 2 (#394)
Browse files Browse the repository at this point in the history
* BHBC-1173
  - introduced survey_status view to encapsulate business rules for determining status of survey_status
  - incorporated survey status into occurrence submission, survey delete and project delete db api functions
  - chained occurrence submission, survey delete and project delete db api functions
  - introduced db api function api_user_is_administrator
  - introduced db api functions api_get_character_system_constant and api_get_numeric_system_constant
  - various data integrity checks

* BHBC-1173
  - introduced survey_status view to encapsulate business rules for determining status of survey_status
  - incorporated survey status into occurrence submission, survey delete and project delete db api functions
  - chained occurrence submission, survey delete and project delete db api functions
  - introduced db api function api_user_is_administrator
  - introduced db api functions api_get_character_system_constant and api_get_numeric_system_constant
  - various data integrity checks

Co-authored-by: charlie garrett-jones <charlie.garrettjones@bayseumcom>
  • Loading branch information
cgarrettjones and charlie garrett-jones authored Jul 2, 2021
1 parent 0e483fb commit faf2f2d
Show file tree
Hide file tree
Showing 47 changed files with 305 additions and 382 deletions.
28 changes: 18 additions & 10 deletions database/src/migrations/20210225205948_biohub_release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import path from 'path';
const DB_USER_API_PASS = process.env.DB_USER_API_PASS;
const DB_USER_API = process.env.DB_USER_API;

const DB_RELEASE = 'release.0.18';
const DB_RELEASE = 'release.0.19';

/**
* Apply biohub release changes.
Expand All @@ -29,16 +29,24 @@ export async function up(knex: Knex): Promise<void> {
const api_get_context_system_user_role_id = fs.readFileSync(
path.join(__dirname, DB_RELEASE, 'api_get_context_system_user_role_id.sql')
);
const api_user_is_administrator = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'api_user_is_administrator.sql'));
const tr_journal_trigger = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'tr_journal_trigger.sql'));
const project_journal_triggers = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'project_journal_triggers.sql'));
const tr_project_funding_source = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'tr_project_funding_source.sql'));
const tr_survey_proprietor = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'tr_survey_proprietor.sql'));
const tr_project = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'tr_project.sql'));
const tr_survey = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'tr_survey.sql'));
const tr_permit = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'tr_permit.sql'));
const api_get_system_constant = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'api_get_system_constant.sql'));
const vw_survey_status = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'vw_survey_status.sql'));

const api_delete_occurrence_submission = fs.readFileSync(
path.join(__dirname, DB_RELEASE, 'api_delete_occurrence_submission.sql')
);
const api_delete_survey = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'api_delete_survey.sql'));
const api_delete_project = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'api_delete_project.sql'));

const populate_system_constants = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'populate_system_constants.sql'));
const populate_first_nations = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'populate_first_nations.sql'));
const populate_climate_change_initiatives = fs.readFileSync(
path.join(__dirname, DB_RELEASE, 'populate_climate_change_initiatives.sql')
Expand Down Expand Up @@ -72,15 +80,12 @@ export async function up(knex: Knex): Promise<void> {
path.join(__dirname, DB_RELEASE, 'populate_submission_message_type.sql')
);

const secured_objects = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'secured_objects.sql'));

const indexes = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'indexes.sql'));

const populate_wldtaxonomic_units = fs.readFileSync(
path.join(__dirname, DB_RELEASE, 'populate_wldtaxonomic_units.sql')
);

const project_dapi_views = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'project_dapi_views.sql'));
const dapi_custom_views = fs.readFileSync(path.join(__dirname, DB_RELEASE, 'dapi_custom_views.sql'));

await knex.raw(`
-- set up spatial extensions
Expand Down Expand Up @@ -117,18 +122,24 @@ export async function up(knex: Knex): Promise<void> {
${project_audit_triggers}
${api_get_context_user_id}
${api_get_context_system_user_role_id}
${api_user_is_administrator}
${tr_journal_trigger}
${project_journal_triggers}
${tr_project_funding_source}
${tr_survey_proprietor}
${tr_project}
${tr_survey}
${tr_permit}
${api_get_system_constant}
${vw_survey_status}
${api_delete_occurrence_submission}
${api_delete_survey}
${api_delete_project}
-- populate look up tables
set search_path = biohub;
${populate_system_constants}
${populate_first_nations}
${populate_climate_change_initiatives}
${populate_management_action_type}
Expand All @@ -145,19 +156,16 @@ export async function up(knex: Knex): Promise<void> {
${populate_submission_status_type}
${populate_submission_message_type}
${secured_objects}
${indexes}
-- temporary external interface tables
${populate_wldtaxonomic_units}
-- create the views
set search_path = biohub_dapi_v1;
set role biohub_api;
${project_dapi_views}
${dapi_custom_views}
set role postgres;
set search_path = biohub;
grant execute on function biohub.api_set_context(_system_user_identifier system_user.user_identifier%type, _user_identity_source_name user_identity_source.name%type) to ${DB_USER_API};
`);
Expand Down
55 changes: 0 additions & 55 deletions database/src/migrations/20210618115350_delete_survey.ts

This file was deleted.

This file was deleted.

16 changes: 0 additions & 16 deletions database/src/migrations/release.0.18/indexes.sql

This file was deleted.

84 changes: 0 additions & 84 deletions database/src/migrations/release.0.18/secured_objects.sql

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-- api_delete_occurrence_submission.sql
drop procedure if exists api_delete_occurrence_submission;

create or replace procedure api_delete_occurrence_submission(__occurrence_submission_id occurrence_submission.id%type)
language plpgsql
security definer
as
$$
-- *******************************************************************
-- Procedure: api_delete_occurrence_submission
-- Purpose: deletes an occurrence submission
--
-- MODIFICATION HISTORY
-- Person Date Comments
-- ---------------- ----------- --------------------------------------
-- [email protected]
-- 2021-06-18 initial release
-- *******************************************************************
declare
__is_published boolean;
__is_system_administrator boolean;
begin
select exists into __is_published (select 1 from survey_status ss, occurrence_submission os
where os.id = __occurrence_submission_id
and ss.survey_id = os.s_id
and ss.survey_status = (select api_get_character_system_constant('SURVEY_STATE_PUBLISHED')));

if __is_published then
select api_user_is_administrator() into __is_system_administrator;

if not __is_system_administrator then
raise exception 'Delete cannot proceed as published occurrence submissions exist and user is not a member of the system administrator role.';
end if;
end if;

delete from submission_message where subs_id in (select id from submission_status where os_id = __occurrence_submission_id);
delete from submission_status where os_id = __occurrence_submission_id;
delete from occurrence where os_id = __occurrence_submission_id;
delete from occurrence_submission where id = __occurrence_submission_id;

exception
when others THEN
raise;
end;
$$;
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ $$
-- ---------------- ----------- --------------------------------------
-- [email protected]
-- 2021-04-19 initial release
-- 2021-06-21 added delete survey
-- *******************************************************************
declare
v_id survey.id%type;
begin
for v_id in (select id from survey where p_id = __project_id) loop
call api_delete_survey(v_id);
end loop;

delete from survey_proprietor where s_id in (select id from survey where p_id = __project_id);
delete from survey_attachment where s_id in (select id from survey where p_id = __project_id);
delete from study_species where s_id in (select id from survey where p_id = __project_id);
delete from block_observation where s_id in (select id from survey where p_id = __project_id);
delete from permit where p_id = __project_id;
delete from survey where p_id = __project_id;
delete from stakeholder_partnership where p_id = __project_id;
Expand All @@ -34,6 +40,7 @@ begin
delete from project_first_nation where p_id = __project_id;
delete from project_participation where p_id = __project_id;
delete from project where id = __project_id;

exception
when others THEN
raise;
Expand Down
Loading

0 comments on commit faf2f2d

Please sign in to comment.