Skip to content

Commit de5d543

Browse files
authored
Adding insert for NPH Participant durring PPSC Opt In Sync (#4067)
* add insert got nph participant table * fix tests
1 parent 8acc770 commit de5d543

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

rdr_service/dao/ppsc_dao.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from rdr_service.dao.base_dao import BaseDao, UpsertableDao
88
from rdr_service.model.ppsc import Participant, Site, NPHOptInEvent, ProfileUpdatesEvent, ParticipantStatusEvent, \
99
ConsentEvent
10+
from rdr_service.model.rex import ParticipantMapping
1011
from rdr_service.model.study_nph import EligibleParticipants
1112

1213

@@ -26,6 +27,10 @@ def get_participant_by_biobank_id(self, *, biobank_id: int):
2627
with self.session() as session:
2728
return session.query(Participant).filter(Participant.biobank_id == biobank_id).all()
2829

30+
def get_all_participants_from_list(self, *, participant_ids: List[int]):
31+
with self.session() as session:
32+
return session.query(Participant).filter(Participant.id.in_(participant_ids)).all()
33+
2934

3035
class SiteDao(UpsertableDao):
3136

@@ -151,6 +156,9 @@ def get_eligible_participant_records(self):
151156
ParticipantStatusEvent.participant_id == ProfileUpdatesEvent.participant_id,
152157
ParticipantStatusEvent.event_type_name.ilike('%Test Account%')
153158
)
159+
).outerjoin(
160+
ParticipantMapping,
161+
ParticipantMapping.primary_participant_id == ProfileUpdatesEvent.participant_id
154162
).filter(
155163
ProfileUpdatesEvent.data_element_name.in_(
156164
['piiname_first',
@@ -162,7 +170,8 @@ def get_eligible_participant_records(self):
162170
),
163171
profile_updates_alias.id.is_(None),
164172
EligibleParticipants.id.is_(None),
165-
ParticipantStatusEvent.id.is_(None)
173+
ParticipantStatusEvent.id.is_(None),
174+
ParticipantMapping.id.is_(None)
166175
).group_by(
167176
ProfileUpdatesEvent.participant_id,
168177
case(

rdr_service/ppsc/ppsc_partner_data_sync.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import logging
2+
from sqlalchemy.exc import IntegrityError
23

34
from rdr_service.config import NPH_STUDY_ID, AOU_STUDY_ID
4-
from rdr_service.dao.ppsc_dao import PPSCNphOptEventInDao
5+
from rdr_service.dao.ppsc_dao import PPSCNphOptEventInDao, ParticipantDao
56
from rdr_service.dao.ppsc_partner_transfer_dao import RTIDataTransferBaseDao
67
from rdr_service.dao.rex_dao import RexParticipantMappingDao
7-
from rdr_service.dao.study_nph_dao import EligibleParticipantsDao
8+
from rdr_service.dao.study_nph_dao import EligibleParticipantsDao, NphParticipantDao
89
from rdr_service.model.ppsc_partner_data_transfer import RTINphOptIn
910

1011

@@ -15,6 +16,8 @@ def __init__(self):
1516
self.eligible_dao = EligibleParticipantsDao()
1617
self.nph_opt_in_event_dao = PPSCNphOptEventInDao()
1718
self.rex_mapping_dao = RexParticipantMappingDao()
19+
self.nph_participant_dao = NphParticipantDao()
20+
self.ppsc_participant_dao = ParticipantDao()
1821
self.usable_nph_objects = None
1922
self.items_ready_for_sync = []
2023

@@ -48,8 +51,12 @@ def get_language_pref(cls, value: str) -> int:
4851

4952
def sync_items(self):
5053
self.get_items_for_sync()
54+
all_participant_data = self.ppsc_participant_dao.get_all_participants_from_list(
55+
participant_ids=[obj.participant_id for obj in self.items_ready_for_sync])
56+
5157
for item in self.items_ready_for_sync:
5258
usable_nph_obj = self.get_nph_obj_from_list()
59+
participant_data = [obj for obj in all_participant_data if obj.id == item.participant_id]
5360
self.nph_opt_in_dao.insert(self.nph_opt_in_dao.model_type(**{
5461
'nph_participant_id': usable_nph_obj.participant_id,
5562
'first_name': item.first_name,
@@ -71,6 +78,14 @@ def sync_items(self):
7178
'primary_study_id': AOU_STUDY_ID
7279

7380
}))
81+
try:
82+
self.nph_participant_dao.insert(self.nph_participant_dao.model_type(**{
83+
'id': usable_nph_obj.participant_id,
84+
'biobank_id': participant_data[0].biobank_id,
85+
}))
86+
except IntegrityError as e:
87+
logging.error(f'The NPH participant record already exists for {usable_nph_obj.participant_id}: {e}')
88+
7489
self.usable_nph_objects.pop(0)
7590

7691
def run_sync(self):

tests/ppsc_tests/test_ppsc_partner_data_sync.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from rdr_service.dao.ppsc_dao import ParticipantDao
77
from rdr_service.dao.ppsc_partner_transfer_dao import RTIDataTransferBaseDao
88
from rdr_service.dao.rex_dao import RexStudyDao, RexParticipantMappingDao
9-
from rdr_service.dao.study_nph_dao import EligibleParticipantsDao
9+
from rdr_service.dao.study_nph_dao import EligibleParticipantsDao, NphParticipantDao
1010
from rdr_service.data_gen.generators.nph import NphDataGenerator
1111
from rdr_service.data_gen.generators.ppsc import PPSCDataGenerator
1212
from rdr_service.model.ppsc_partner_data_transfer import RTINphOptIn
@@ -24,6 +24,7 @@ def setUp(self):
2424
self.nph_opt_in_dao = RTIDataTransferBaseDao(RTINphOptIn)
2525
self.rex_study_dao = RexStudyDao()
2626
self.rex_mapping_dao = RexParticipantMappingDao()
27+
self.nph_participant_dao = NphParticipantDao()
2728
self.faker = Faker()
2829

2930
activities = [
@@ -152,7 +153,6 @@ def test_get_eligible_nph_participants(self) -> None:
152153

153154
# nph opt in records
154155
current_opt_in_records = [obj for obj in self.nph_opt_in_dao.get_all()]
155-
156156
self.assertEqual(len(current_opt_in_records), len(nph_opt_in_sync.items_ready_for_sync))
157157
self.assertTrue(all(obj.first_name is not None for obj in current_opt_in_records))
158158
self.assertTrue(all(obj.last_name is not None for obj in current_opt_in_records))
@@ -166,10 +166,16 @@ def test_get_eligible_nph_participants(self) -> None:
166166
current_mappings = self.rex_mapping_dao.get_all()
167167
self.assertEqual(len(current_mappings), len(nph_opt_in_sync.items_ready_for_sync))
168168

169+
# check NPH record was created
170+
current_nph_participants = self.nph_participant_dao.get_all()
171+
synced_nph_id = current_opt_in_records[0].nph_participant_id
172+
self.assertTrue(synced_nph_id in [obj.id for obj in current_nph_participants])
173+
169174
def tearDown(self):
170175
super().tearDown()
171176
self.clear_table_after_test("ppsc.activity")
172177
self.clear_table_after_test("ppsc.participant")
173178
self.clear_table_after_test("ppsc.participant_event_activity")
174179
self.clear_table_after_test("ppsc.profile_updates_event")
175180
self.clear_table_after_test("ppsc.nph_opt_in_event")
181+
self.clear_table_after_test("nph.participant")

0 commit comments

Comments
 (0)