Skip to content

Commit

Permalink
Merge branch 'SIMSBIOHUB-262' of github.com:bcgov/biohubbc into SIMSB…
Browse files Browse the repository at this point in the history
…IOHUB-262
  • Loading branch information
curtisupshall committed Aug 31, 2023
2 parents 0d97250 + 24812b8 commit 56d6152
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 107 deletions.
11 changes: 4 additions & 7 deletions api/src/repositories/survey-participation-repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,15 @@ describe('SurveyParticipationRepository', () => {
expect(response).to.eql([{ id: 1 }]);
});

it('should throw an error when no rows returned', async () => {
it('should return no rows', async () => {
const mockResponse = ({ rows: [], rowCount: 0 } as any) as Promise<QueryResult<any>>;
const dbConnection = getMockDBConnection({ sql: () => mockResponse });

const repository = new SurveyParticipationRepository(dbConnection);

try {
await repository.getSurveyParticipants(1);
expect.fail();
} catch (error) {
expect((error as Error).message).to.equal('Failed to get survey participants');
}
const response = await repository.getSurveyParticipants(1);

expect(response).to.eql([]);
});
});

Expand Down
11 changes: 2 additions & 9 deletions api/src/repositories/survey-participation-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class SurveyParticipationRepository extends BaseRepository {
}

/**
* Get a survey participant record with job name.
* Get a survey participant record.
*
* @param {number} surveyId
* @param {number} systemUserId
Expand Down Expand Up @@ -155,7 +155,7 @@ export class SurveyParticipationRepository extends BaseRepository {
}

/**
* Get a survey participant record with job name.
* Get survey participant records.
*
* @param {number} surveyId
* @return {*} {Promise<SurveyUser[]>}
Expand Down Expand Up @@ -215,13 +215,6 @@ export class SurveyParticipationRepository extends BaseRepository {

const response = await this.connection.sql(sqlStatement, SurveyUser.merge(SystemUser));

if (!response.rows.length) {
throw new ApiExecuteSQLError('Failed to get survey participants', [
'SurveyParticipationRepository->getSurveyParticipants',
'rows was null or undefined, expected rows != null'
]);
}

return response.rows;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Knex } from 'knex';

/**
* Added critter and observation tables with trigger to check if critter instances is less than observation.total_count
* Added critter and deployment tables.
*
* @export
* @param {Knex} knex
Expand All @@ -11,48 +11,14 @@ import { Knex } from 'knex';
export async function up(knex: Knex): Promise<void> {
await knex.raw(`--sql
----------------------------------------------------------------------------------------
-- Create Observation Table
----------------------------------------------------------------------------------------
set search_path=biohub;
CREATE TABLE observation(
observation_id integer GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1),
survey_id integer NOT NULL,
total_count integer NOT NULL CHECK(total_count > 0),
observation_date date,
create_date timestamptz(6) DEFAULT now() NOT NULL,
create_user integer NOT NULL,
update_date timestamptz(6),
update_user integer,
revision_count integer DEFAULT 0 NOT NULL,
CONSTRAINT observation_pk PRIMARY KEY (observation_id)
);
COMMENT ON COLUMN observation.observation_id IS 'System generated surrogate primary key identifier.';
COMMENT ON COLUMN observation.survey_id IS 'The id of the survey.';
COMMENT ON COLUMN observation.observation_date IS 'Date the observation occured.';
COMMENT ON COLUMN observation.total_count IS 'The number of individuals recorded in observation. Associated critters must be less than or equal.';
COMMENT ON COLUMN observation.create_date IS 'The datetime the record was created.';
COMMENT ON COLUMN observation.create_user IS 'The id of the user who created the record as identified in the system user table.';
COMMENT ON COLUMN observation.update_date IS 'The datetime the record was updated.';
COMMENT ON COLUMN observation.update_user IS 'The id of the user who updated the record as identified in the system user table.';
COMMENT ON COLUMN observation.revision_count IS 'Revision count used for concurrency control.';
COMMENT ON TABLE observation IS 'Information about taxon counts.';
-- Create audit and journal triggers
create trigger audit_observation before insert or update or delete on observation for each row execute procedure tr_audit_trigger();
create trigger journal_observation after insert or update or delete on observation for each row execute procedure tr_journal_trigger();
----------------------------------------------------------------------------------------
-- Create Critter table
----------------------------------------------------------------------------------------
set search_path=biohub;
CREATE TABLE critter(
critter_id integer GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1),
observation_id integer NOT NULL,
survey_id integer NOT NULL,
critterbase_critter_id uuid NOT NULL,
create_date timestamptz(6) DEFAULT now() NOT NULL,
create_user integer NOT NULL,
Expand All @@ -63,25 +29,25 @@ export async function up(knex: Knex): Promise<void> {
);
COMMENT ON COLUMN critter.critter_id IS 'System generated surrogate primary key identifier.';
COMMENT ON COLUMN critter.observation_id IS 'The id of the observation';
COMMENT ON COLUMN critter.survey_id IS 'The id of the survey.';
COMMENT ON COLUMN critter.critterbase_critter_id IS 'The external system id of a Critterbase critter.';
COMMENT ON COLUMN critter.create_date IS 'The datetime the record was created.';
COMMENT ON COLUMN critter.create_user IS 'The id of the user who created the record as identified in the system user table.';
COMMENT ON COLUMN critter.update_date IS 'The datetime the record was updated.';
COMMENT ON COLUMN critter.update_user IS 'The id of the user who updated the record as identified in the system user table.';
COMMENT ON COLUMN critter.revision_count IS 'Revision count used for concurrency control.';
COMMENT ON TABLE critter IS 'Information about individual animals associated to specific observations.';
COMMENT ON TABLE critter IS 'Information about individual animals associated.';
-- Add foreign key constraint from child table to parent table on observation_id
-- Add foreign key constraint from child table to parent table on survey_id
ALTER TABLE critter ADD CONSTRAINT critter_fk1
FOREIGN KEY (observation_id)
REFERENCES observation(observation_id);
FOREIGN KEY (survey_id)
REFERENCES survey(survey_id);
-- Add foreign key index
CREATE INDEX critter_idx1 ON critter(observation_id);
CREATE INDEX critter_idx1 ON critter(survey_id);
-- Add unique constraint
CREATE UNIQUE INDEX critter_uk1 ON critter(observation_id, critterbase_critter_id);
CREATE UNIQUE INDEX critter_uk1 ON critter(survey_id, critterbase_critter_id);
-- Create audit and journal triggers
create trigger audit_critter before insert or update or delete on critter for each row execute procedure tr_audit_trigger();
Expand Down Expand Up @@ -128,59 +94,12 @@ export async function up(knex: Knex): Promise<void> {
create trigger audit_critter before insert or update or delete on deployment for each row execute procedure tr_audit_trigger();
create trigger journal_critter after insert or update or delete on deployment for each row execute procedure tr_journal_trigger();
----------------------------------------------------------------------------------------
-- Create Critter Observation count trigger
----------------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION biohub.tr_critter_observation_count()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
-- *******************************************************************
-- Procedure: tr_critter_observation_count
-- Purpose: Validates amount of critters of a observation is less than or equal to observation count.
--
-- MODIFICATION HISTORY
-- Person Date Comments
-- ---------------- ----------- --------------------------------------
-- [email protected]
-- 2023-17-08 initial release
-- *******************************************************************
DECLARE
num_critters integer := (SELECT count(*) FROM critter WHERE critter.observation_id = NEW.observation_id);
observation_total_count integer := (SELECT total_count FROM observation WHERE observation.observation_id = NEW.observation_id);
BEGIN
IF (num_critters > observation_total_count) THEN
RAISE EXCEPTION 'More individual critters than specified by observation total count';
END IF;
RETURN NEW;
end;
$function$
;
COMMENT ON FUNCTION biohub.tr_critter_observation_count() IS 'Validates amount of individual critters in an observation is less than or equal to observation total count.';
-- Create observation count trigger
CREATE TRIGGER tr_critter_observation_count AFTER INSERT ON critter FOR EACH ROW EXECUTE PROCEDURE tr_critter_observation_count();
CREATE TRIGGER tr_critter_observation_count AFTER UPDATE ON observation FOR EACH ROW EXECUTE PROCEDURE tr_critter_observation_count();
----------------------------------------------------------------------------------------
-- Create views
----------------------------------------------------------------------------------------
set search_path=biohub_dapi_v1;
create or replace view observation as select * from biohub.observation;
create or replace view critter as select * from biohub.critter;
create or replace view deployment as select * from biohub.deployment;
`);
Expand Down

0 comments on commit 56d6152

Please sign in to comment.