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

activity current changes #2984

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
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
32 changes: 16 additions & 16 deletions api/src/queries/activity-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ CurrentNegativeObservations AS (
}

sqlStatement.append(
SQL` FROM activity_incoming_data a inner join activity_current b on a.activity_incoming_data_id = b.incoming_data_id `
SQL` FROM activity_incoming_data a `
);

if (searchCriteria.search_feature || searchCriteria.search_feature_server_id) {
Expand All @@ -374,68 +374,68 @@ LEFT JOIN
switch (searchCriteria.CSVType) {
case 'terrestrial_plant_observation':
sqlStatement.append(
'join observation_terrestrial_plant_summary extract ON extract.activity_id = b.activity_id '
'join observation_terrestrial_plant_summary extract ON extract.activity_id = a.activity_id '
);
break;
case 'aquatic_plant_observation':
sqlStatement.append('join observation_aquatic_plant_summary extract ON extract.activity_id = b.activity_id ');
sqlStatement.append('join observation_aquatic_plant_summary extract ON extract.activity_id = a.activity_id ');
break;
case 'terrestrial_chemical_treatment':
sqlStatement.append(
'join treatment_chemical_terrestrial_plant_summary extract ON extract.activity_id = b.activity_id '
'join treatment_chemical_terrestrial_plant_summary extract ON extract.activity_id = a.activity_id '
);
break;
case 'aquatic_chemical_treatment':
sqlStatement.append(
'join treatment_chemical_aquatic_plant_summary extract ON extract.activity_id = b.activity_id '
'join treatment_chemical_aquatic_plant_summary extract ON extract.activity_id = a.activity_id '
);
break;
case 'terrestrial_mechanical_treatment':
sqlStatement.append(
'join treatment_mechanical_terrestrial_plant_summary extract ON extract.activity_id = b.activity_id '
'join treatment_mechanical_terrestrial_plant_summary extract ON extract.activity_id = a.activity_id '
);
break;
case 'aquatic_mechanical_treatment':
sqlStatement.append(
'join treatment_mechanical_aquatic_plant_summary extract ON extract.activity_id = b.activity_id '
'join treatment_mechanical_aquatic_plant_summary extract ON extract.activity_id = a.activity_id '
);
break;
case 'biocontrol_release':
sqlStatement.append('join biocontrol_release_summary extract ON extract.activity_id = b.activity_id ');
sqlStatement.append('join biocontrol_release_summary extract ON extract.activity_id = a.activity_id ');
break;
case 'biocontrol_collection':
sqlStatement.append('join biocontrol_collection_summary extract ON extract.activity_id = b.activity_id ');
sqlStatement.append('join biocontrol_collection_summary extract ON extract.activity_id = a.activity_id ');
break;
case 'biocontrol_dispersal':
sqlStatement.append(
'join biocontrol_dispersal_monitoring_summary extract ON extract.activity_id = b.activity_id '
'join biocontrol_dispersal_monitoring_summary extract ON extract.activity_id = a.activity_id '
);
break;
case 'chemical_treatment_monitoring':
sqlStatement.append(
'join chemical_treatment_monitoring_summary extract ON extract.activity_id = b.activity_id '
'join chemical_treatment_monitoring_summary extract ON extract.activity_id = a.activity_id '
);
break;
case 'mechanical_treatment_monitoring':
sqlStatement.append(
'join mechanical_treatment_monitoring_summary extract ON extract.activity_id = b.activity_id '
'join mechanical_treatment_monitoring_summary extract ON extract.activity_id = a.activity_id '
);
break;
case 'biocontrol_release_monitoring':
sqlStatement.append(
'join biocontrol_release_monitoring_summary extract ON extract.activity_id = b.activity_id '
'join biocontrol_release_monitoring_summary extract ON extract.activity_id = a.activity_id '
);
break;

default:
sqlStatement.append(
'join observation_terrestrial_plant_summary extract ON extract.activity_id = b.activity_id '
'join observation_terrestrial_plant_summary extract ON extract.activity_id = a.activity_id '
);
break;
}
}

sqlStatement.append(SQL` where 1 = 1`);
sqlStatement.append(SQL` where 1 = 1 and a.iscurrent = true `);

if (lean) {
sqlStatement.append(SQL` and a.activity_incoming_data_id > 36345`);
Expand Down Expand Up @@ -762,8 +762,8 @@ LEFT JOIN
export const getActivitySQL = (activityId: string): SQLStatement => {
return SQL`
SELECT a.* FROM activity_incoming_data a
join activity_current b on a.activity_incoming_data_id = b.incoming_data_id
WHERE a.activity_id = ${activityId}
and a.iscurrent = true
`;
};

Expand Down
56 changes: 56 additions & 0 deletions database/src/migrations/0079_current_activity_refactor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Knex } from 'knex';

export async function up(knex: Knex): Promise<void> {
await knex.raw(
`
set search_path=invasivesbc,public;
ALTER TABLE invasivesbc.activity_incoming_data add if not exists iscurrent boolean NOT NULL DEFAULT true;
CREATE OR REPLACE FUNCTION invasivesbc.current_activity_function()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
BEGIN
update invasivesbc.activity_incoming_data
set iscurrent = false
where activity_id = new.activity_id
and activity_incoming_data_id != new.activity_incoming_data_id
and new.deleted_timestamp is null;
update invasivesbc.activity_incoming_data
set deleted_timestamp = NOW()
where activity_id = new.activity_id
and activity_incoming_data_id != new.activity_incoming_data_id
and deleted_timestamp is null;
RETURN NEW;
END;
$function$
;
create or replace trigger update_activity_current after
insert
on
invasivesbc.activity_incoming_data for each row execute function invasivesbc.current_activity_function() ;
update invasivesbc.activity_incoming_data
set iscurrent = false ,
deleted_timestamp = NOW()
where activity_incoming_data_id not in (select incoming_data_id from activity_current);
CREATE INDEX if not exists activity_incoming_data_activity_id_and_current_idx ON invasivesbc.activity_incoming_data (activity_id,iscurrent);
create index if not exists activity_incoming_data_iscurrent_idx ON invasivesbc.activity_incoming_data (iscurrent);
CREATE INDEX if not exists activity_incoming_data_form_status_idx ON invasivesbc.activity_incoming_data (form_status);
`
);
}

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