Skip to content

Commit

Permalink
Spi migrate users ups into test spi (#1364)
Browse files Browse the repository at this point in the history
* initial commit for study areas. File creation and first attempt at the script. Should work...maybe. Needs to be tested and debugged before merge

* finished ?? sampling methods, only need to replace PLACEHOLDER method lookup names once migation confirms final table values

* trying to fix indentations on when statements

* currently very broken and returning way too many rows. Not sure how to fix yet

* study areas troubleshooting

* removed a migrations file i had created in this branch by accident as opposed to method lookups branch

* sudy area script, still need to loop or something to fix timeout error

* new branch for transforming DC visits into sampling periods

* untested materials

* changes to sampling period

* ready for review

* ready for review, but actually this time

* please review possibilty of creating new temp table to use for spi wlo, with period id and visit id

* changing progress ID portion to reflect status according to dates

* added some logic that pulls some email addresses from spi_secured_persons to determine if they have email addresses and give the collab role based on this. Joined on names. Not tested in current state

* ;

* added additional sql for comparison

* changed email address to email address when we can link one via firstname and lastname else default. Untested. May also be a bad idea, needs a review of logic

* study area updates

* updates to survey

* added join and condition. If no match on first and last names, system_user_id is null and will then be inserted.

* reverting back because I made mistakes

* updates to check if user already exists based off of name and setting biohub_user_id as system_user_id

* a commit of defeat and sadness

* add comment

* merge latest

* pr fixes

* pr edits, again

* make fix

---------

Co-authored-by: Andrew <[email protected]>
  • Loading branch information
AMEIJER1 and LouisThedroux authored Sep 13, 2024
1 parent 2af93d3 commit cd09bc2
Show file tree
Hide file tree
Showing 9 changed files with 1,896 additions and 2,018 deletions.
2,671 changes: 783 additions & 1,888 deletions spi/package-lock.json

Large diffs are not rendered by default.

14 changes: 13 additions & 1 deletion spi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { insertMappedSpecies } from './transformations/species-map';
import { transformSurveyStratums } from './transformations/stratum';
import { transformSurveys } from './transformations/survey';
import { transformUsers } from './transformations/user';
import { truncateTables } from './utils/truncate-tables';
import { truncateTables } from './utils/truncateTables';
import { transformStudyAreas } from './transformations/study-area';
import { transformSamplingMethods } from './transformations/sampling_methods';
import { transformSampleVisits } from './transformations/sampling_period';

let connection: IDBConnection; // Declare connection variable at the module level

Expand Down Expand Up @@ -55,6 +58,15 @@ async function main() {
// STEP 7. Transforms SPI Design Components into SIMS Sampling Sites
await transformSampleSites(connection);

//STEP 7. Transforms SPI Survey Areas into SIMS Survey Locations
await transformStudyAreas(connection);

//STEP 8. Transforms SPI Sampling Method
await transformSamplingMethods(connection);

//STEP 9. Transforms SPI Sampling Period
await transformSampleVisits(connection);

// Commit the transactions
connection.commit();

Expand Down
84 changes: 69 additions & 15 deletions spi/src/transformations/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,78 @@ export const transformProjects = async (connection: IDBConnection): Promise<void
SELECT
p.spi_project_id,
pp.person_id,
b.project_id
b.project_id,
CONCAT(pp.first_given_name, ' ', pp.surname) AS full_name,
spp.email_address
FROM
public.spi_projects p
INNER JOIN
biohub.project b ON p.spi_project_id = b.spi_project_id
INNER JOIN
spi_persons pp ON pp.spi_project_id = p.spi_project_id
INNER JOIN biohub.project b
ON p.spi_project_id = b.spi_project_id
INNER JOIN spi_persons pp
ON pp.spi_project_id = p.spi_project_id
LEFT JOIN public.spi_secure_persons spp
ON spp.first_name = pp.first_given_name
AND spp.last_name = pp.surname
),
w_insert_participation AS
(
INSERT INTO
biohub.project_participation (project_id, system_user_id, project_role_id, create_user)
SELECT DISTINCT
w_mapping.project_id,
COALESCE (
(SELECT biohub_user_id
FROM public.migrate_spi_user_deduplication
WHERE w_mapping.person_id = ANY (spi_person_ids)
),
(SELECT system_user_id
FROM biohub.system_user
WHERE user_identifier = 'spi')
) AS system_user_id,
CASE
WHEN TRIM(p.coordinator) = w_mapping.full_name AND spp.email_address LIKE '%@gov.bc.ca%' THEN
(SELECT project_role_id FROM biohub.project_role WHERE name = 'Coordinator')
WHEN spp.email_address LIKE '%@gov.bc.ca%' THEN
(SELECT project_role_id FROM biohub.project_role WHERE name = 'Collaborator')
ELSE
(SELECT project_role_id FROM biohub.project_role WHERE name = 'Observer')
END AS project_role_id,
(SELECT system_user_id FROM biohub.system_user WHERE user_identifier = 'spi') AS create_user
FROM
w_mapping
JOIN public.spi_projects p
ON p.spi_project_id = w_mapping.spi_project_id
JOIN public.spi_secure_persons spp
ON spp.first_name = pp.first_given_name
AND spp.last_name = pp.surname
),
w_assign_coordinator AS
(
INSERT INTO biohub.project_participation
(project_id, system_user_id, project_role_id, create_user)
SELECT
b.project_id,
(SELECT system_user_id FROM biohub.system_user WHERE user_identifier = 'spi') AS system_user_id,
(SELECT project_role_id FROM biohub.project_role WHERE name = 'Coordinator') AS project_role_id,
(SELECT system_user_id FROM biohub.system_user WHERE user_identifier = 'spi') AS create_user
FROM
biohub.project b
WHERE NOT EXISTS (
SELECT 1
FROM biohub.project_participation pp
JOIN biohub.project_role pr
ON pp.project_role_id = pr.project_role_id
WHERE b.project_id = pp.project_id
AND pr.name = 'Coordinator'
)
AND b.spi_project_id IS NOT NULL
)
INSERT INTO
biohub.project_participation (project_id, system_user_id, project_role_id, create_user)
SELECT DISTINCT
w_mapping.project_id,
(SELECT biohub_user_id FROM public.migrate_spi_user_deduplication WHERE w_mapping.person_id = ANY (spi_person_ids)),
(SELECT project_role_id FROM biohub.project_role WHERE name = 'Collaborator'),
(SELECT system_user_id FROM biohub.system_user WHERE user_identifier = 'spi')
FROM
w_mapping
ON CONFLICT DO NOTHING;
SELECT * FROM w_insert_participation;
SELECT * FROM w_assign_coordinator;
`;

await connection.sql(sql);
Expand Down
Loading

0 comments on commit cd09bc2

Please sign in to comment.