-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhousing.js
More file actions
161 lines (138 loc) · 6.22 KB
/
housing.js
File metadata and controls
161 lines (138 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
require("ehr/triggers").initScript(this);
var prevAnimalId;
var prevDate;
let triggerHelper = new org.labkey.nirc_ehr.query.NIRC_EHRTriggerHelper(LABKEY.Security.currentUser.id, LABKEY.Security.currentContainer.id);
let animalIds = [];
function onInit(event, helper){
helper.setScriptOptions({
skipHousingCheck: true
});
helper.decodeExtraContextProperty('housingInTransaction');
helper.registerRowProcessor(function(helper, row){
if (!row)
return;
if (!row.Id || !row.room){
return;
}
var housingInTransaction = helper.getProperty('housingInTransaction');
housingInTransaction = housingInTransaction || {};
housingInTransaction[row.Id] = housingInTransaction[row.Id] || [];
// this is a failsafe in case the client did not provide housing JSON. it ensures
// the current row is part of housingInTransaction
var shouldAdd = true;
if (row.objectid){
LABKEY.ExtAdapter.each(housingInTransaction[row.Id], function(r){
if (r.objectid == row.objectid){
shouldAdd = false;
return false;
}
}, this);
}
if (shouldAdd){
housingInTransaction[row.Id].push({
objectid: row.objectid,
date: row.date,
enddate: row.enddate,
qcstate: row.QCState,
room: row.room,
cage: row.cage,
divider: row.divider
});
}
helper.setProperty('housingInTransaction', housingInTransaction);
});
}
function onUpsert(helper, scriptErrors, row, oldRow){
//verify we dont have 2 opened records for the same ID
if (!helper.isETL() && !row.enddate && row.Id){
var map = helper.getProperty('housingInTransaction');
if (map && map[row.Id]){
var housingRecords = map[row.Id];
for (var i=0;i<housingRecords.length;i++){
if (row.objectid == housingRecords[i].objectid){
console.log ('same housing record');
continue;
}
if (!housingRecords[i].enddate){
EHR.Server.Utils.addError(scriptErrors, 'enddate', 'Cannot enter multiple open-ended housing records for the same animal', 'WARN');
}
}
}
}
if (!helper.isETL() && row && row.Id && row.date && !row.enddate){
var objectid = row.objectid || null;
//if this record is active and public, deactivate any old housing records
var map = helper.getProperty('housingInTransaction');
var housingRecords = [];
if (map && map[row.Id]){
housingRecords = map[row.Id];
}
//NOTE: downstream java code should handle type conversion of housingInTransaction
var msg = helper.getJavaHelper().validateFutureOpenEndedHousing(row.Id, row.date, objectid, housingRecords);
if (msg){
EHR.Server.Utils.addError(scriptErrors, 'Id', msg, 'WARN');
}
}
}
EHR.Server.TriggerManager.registerHandlerForQuery(EHR.Server.TriggerManager.Events.BEFORE_INSERT, 'study', 'housing', function (helper, scriptErrors, row, oldRow) {
if (helper.isETL()) {
var isBatch = prevAnimalId === row.Id;
if (isBatch) {
row.endDate = prevDate;
}
prevAnimalId = row.Id;
prevDate = row.date;
}
else {
if (row.reason === 'Veterinary Treatment' && !row.remark) {
EHR.Server.Utils.addError(scriptErrors, 'remark', 'Reason For Move - Veterinary Treatment requires a remark.', 'ERROR');
}
if (!helper.isValidateOnly() && row.reason === 'Veterinary Treatment' && (!oldRow || oldRow.reason !== 'Veterinary Treatment')) {
triggerHelper.clinicalMoveNotification(row.Id, row.date);
}
}
});
function onComplete(event, errors, helper){
// Similar to EHR housing trigger onComplete with minor differences to handle ETL
if (!helper.isValidateOnly()) {
var updateRows = helper.getRows();
if (updateRows && updateRows.length > 0) {
// Only need boundary rows for batch. Changing batch size on ETL may require change here. Incremental updates
// should update all incoming rows so don't do this.
if (helper.isETL() && updateRows.length > 4000) {
console.log("Batch import: only closing boundary housing record")
updateRows = [updateRows[0]];
}
var idsToClose = [];
for (var i = 0; i < updateRows.length; i++) {
if (EHR.Server.Security.getQCStateByLabel(updateRows[i].row.QCStateLabel).PublicData && updateRows[i].row.date) {
idsToClose.push({
Id: updateRows[i].row.Id,
date: EHR.Server.Utils.datetimeToString(updateRows[i].row.date), //stringify to serialize properly
objectid: updateRows[i].row.objectid
});
if (animalIds.indexOf(updateRows[i].row.Id) === -1) {
animalIds.push(updateRows[i].row.Id);
}
}
}
if (idsToClose.length){
helper.getJavaHelper().closeHousingRecords(idsToClose);
}
// When closing previous records, we don't need to update the orchard file. Use the same flag as demographics providers.
var skipAnnounceChangedParticipants = false;
var extraContext = helper.getProperty('extraContext');
if (extraContext) {
skipAnnounceChangedParticipants = extraContext.skipAnnounceChangedParticipants;
}
if (updateRows && updateRows.length > 0 &&
updateRows[0].row.taskid &&
updateRows[0].row.QCStateLabel &&
EHR.Server.Security.getQCStateByLabel(updateRows[0].row.QCStateLabel).PublicData &&
!skipAnnounceChangedParticipants
) {
triggerHelper.generateOrchardFile(updateRows[0].row.taskid);
}
}
}
}