1
1
import logging
2
+ from sqlalchemy .exc import IntegrityError
2
3
3
4
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
5
6
from rdr_service .dao .ppsc_partner_transfer_dao import RTIDataTransferBaseDao
6
7
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
8
9
from rdr_service .model .ppsc_partner_data_transfer import RTINphOptIn
9
10
10
11
@@ -15,6 +16,8 @@ def __init__(self):
15
16
self .eligible_dao = EligibleParticipantsDao ()
16
17
self .nph_opt_in_event_dao = PPSCNphOptEventInDao ()
17
18
self .rex_mapping_dao = RexParticipantMappingDao ()
19
+ self .nph_participant_dao = NphParticipantDao ()
20
+ self .ppsc_participant_dao = ParticipantDao ()
18
21
self .usable_nph_objects = None
19
22
self .items_ready_for_sync = []
20
23
@@ -48,8 +51,12 @@ def get_language_pref(cls, value: str) -> int:
48
51
49
52
def sync_items (self ):
50
53
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
+
51
57
for item in self .items_ready_for_sync :
52
58
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 ]
53
60
self .nph_opt_in_dao .insert (self .nph_opt_in_dao .model_type (** {
54
61
'nph_participant_id' : usable_nph_obj .participant_id ,
55
62
'first_name' : item .first_name ,
@@ -71,6 +78,14 @@ def sync_items(self):
71
78
'primary_study_id' : AOU_STUDY_ID
72
79
73
80
}))
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
+
74
89
self .usable_nph_objects .pop (0 )
75
90
76
91
def run_sync (self ):
0 commit comments