@@ -5,6 +5,89 @@ var prevDate;
55let triggerHelper = new org . labkey . nirc_ehr . query . NIRC_EHRTriggerHelper ( LABKEY . Security . currentUser . id , LABKEY . Security . currentContainer . id ) ;
66let animalIds = [ ] ;
77
8+ function onInit ( event , helper ) {
9+ helper . setScriptOptions ( {
10+ skipHousingCheck : true
11+ } ) ;
12+
13+ helper . decodeExtraContextProperty ( 'housingInTransaction' ) ;
14+
15+ helper . registerRowProcessor ( function ( helper , row ) {
16+ if ( ! row )
17+ return ;
18+
19+ if ( ! row . Id || ! row . room ) {
20+ return ;
21+ }
22+
23+ var housingInTransaction = helper . getProperty ( 'housingInTransaction' ) ;
24+ housingInTransaction = housingInTransaction || { } ;
25+ housingInTransaction [ row . Id ] = housingInTransaction [ row . Id ] || [ ] ;
26+
27+ // this is a failsafe in case the client did not provide housing JSON. it ensures
28+ // the current row is part of housingInTransaction
29+ var shouldAdd = true ;
30+ if ( row . objectid ) {
31+ LABKEY . ExtAdapter . each ( housingInTransaction [ row . Id ] , function ( r ) {
32+ if ( r . objectid == row . objectid ) {
33+ shouldAdd = false ;
34+ return false ;
35+ }
36+ } , this ) ;
37+ }
38+
39+ if ( shouldAdd ) {
40+ housingInTransaction [ row . Id ] . push ( {
41+ objectid : row . objectid ,
42+ date : row . date ,
43+ enddate : row . enddate ,
44+ qcstate : row . QCState ,
45+ room : row . room ,
46+ cage : row . cage ,
47+ divider : row . divider
48+ } ) ;
49+ }
50+
51+ helper . setProperty ( 'housingInTransaction' , housingInTransaction ) ;
52+ } ) ;
53+ }
54+
55+ function onUpsert ( helper , scriptErrors , row , oldRow ) {
56+ //verify we dont have 2 opened records for the same ID
57+ if ( ! helper . isETL ( ) && ! row . enddate && row . Id ) {
58+ var map = helper . getProperty ( 'housingInTransaction' ) ;
59+ if ( map && map [ row . Id ] ) {
60+ var housingRecords = map [ row . Id ] ;
61+ for ( var i = 0 ; i < housingRecords . length ; i ++ ) {
62+ if ( row . objectid == housingRecords [ i ] . objectid ) {
63+ console . log ( 'same housing record' ) ;
64+ continue ;
65+ }
66+
67+ if ( ! housingRecords [ i ] . enddate ) {
68+ EHR . Server . Utils . addError ( scriptErrors , 'enddate' , 'Cannot enter multiple open-ended housing records for the same animal' , 'WARN' ) ;
69+ }
70+ }
71+ }
72+ }
73+
74+ if ( ! helper . isETL ( ) && row && row . Id && row . date && ! row . enddate ) {
75+ var objectid = row . objectid || null ;
76+ //if this record is active and public, deactivate any old housing records
77+ var map = helper . getProperty ( 'housingInTransaction' ) ;
78+ var housingRecords = [ ] ;
79+ if ( map && map [ row . Id ] ) {
80+ housingRecords = map [ row . Id ] ;
81+ }
82+
83+ //NOTE: downstream java code should handle type conversion of housingInTransaction
84+ var msg = helper . getJavaHelper ( ) . validateFutureOpenEndedHousing ( row . Id , row . date , objectid , housingRecords ) ;
85+ if ( msg ) {
86+ EHR . Server . Utils . addError ( scriptErrors , 'Id' , msg , 'WARN' ) ;
87+ }
88+ }
89+ }
90+
891EHR . Server . TriggerManager . registerHandlerForQuery ( EHR . Server . TriggerManager . Events . BEFORE_INSERT , 'study' , 'housing' , function ( helper , scriptErrors , row , oldRow ) {
992
1093 if ( helper . isETL ( ) ) {
0 commit comments