From 130364ff1e9c43cf8a555d8af54ffbaa457b286c Mon Sep 17 00:00:00 2001 From: Nick Phura Date: Thu, 31 Aug 2023 10:24:29 -0700 Subject: [PATCH 1/2] fix bad error handling on survey participation repo function (#1080) * Fix bad error handling on a repo function. Function was throwing on empty array, when empty array should be expected/allowed. --- .../survey-participation-repository.test.ts | 11 ++++------- .../repositories/survey-participation-repository.ts | 11 ++--------- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/api/src/repositories/survey-participation-repository.test.ts b/api/src/repositories/survey-participation-repository.test.ts index 767145a5d6..7467352c97 100644 --- a/api/src/repositories/survey-participation-repository.test.ts +++ b/api/src/repositories/survey-participation-repository.test.ts @@ -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>; 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([]); }); }); diff --git a/api/src/repositories/survey-participation-repository.ts b/api/src/repositories/survey-participation-repository.ts index 440b19b89b..4256a9d4fc 100644 --- a/api/src/repositories/survey-participation-repository.ts +++ b/api/src/repositories/survey-participation-repository.ts @@ -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 @@ -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} @@ -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; } From ec42e3840e390ffc9be5dbef6caf58723481cadf Mon Sep 17 00:00:00 2001 From: Nick Phura Date: Thu, 31 Aug 2023 11:13:15 -0700 Subject: [PATCH 2/2] TechDebt: Update existing migration: remove observation table (for now) (#1084) Update existing migration: remove observation table (for now) --- ...230817000000_critter_deployment_tables.ts} | 101 ++---------------- 1 file changed, 10 insertions(+), 91 deletions(-) rename database/src/migrations/{20230817000000_critter_observation_deployment_tables.ts => 20230817000000_critter_deployment_tables.ts} (53%) diff --git a/database/src/migrations/20230817000000_critter_observation_deployment_tables.ts b/database/src/migrations/20230817000000_critter_deployment_tables.ts similarity index 53% rename from database/src/migrations/20230817000000_critter_observation_deployment_tables.ts rename to database/src/migrations/20230817000000_critter_deployment_tables.ts index 5bc5bd3794..9bee41942d 100644 --- a/database/src/migrations/20230817000000_critter_observation_deployment_tables.ts +++ b/database/src/migrations/20230817000000_critter_deployment_tables.ts @@ -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 @@ -11,48 +11,14 @@ import { Knex } from 'knex'; export async function up(knex: Knex): Promise { 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, @@ -63,25 +29,25 @@ export async function up(knex: Knex): Promise { ); 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(); @@ -128,59 +94,12 @@ export async function up(knex: Knex): Promise { 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 - -- ---------------- ----------- -------------------------------------- - -- mac.deluca@quartech.com - -- 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; `);