-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbirth.js
More file actions
164 lines (135 loc) · 6.12 KB
/
birth.js
File metadata and controls
164 lines (135 loc) · 6.12 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
161
162
163
164
require("ehr/triggers").initScript(this);
EHR.Server.Utils = require("ehr/utils").EHR.Server.Utils;
var triggerHelper = new org.labkey.nirc_ehr.query.NIRC_EHRTriggerHelper(LABKEY.Security.currentUser.id, LABKEY.Security.currentContainer.id);
function onInit(event, helper){
helper.setScriptOptions({
allowAnyId: true,
requiresStatusRecalc: true,
allowDeadIds: true,
skipIdFormatCheck: true,
skipHousingCheck: true,
announceAllModifiedParticipants: true,
allowDatesInDistantPast: true,
removeTimeFromDate: true,
skipAssignmentCheck: true,
});
helper.decodeExtraContextProperty('birthsInTransaction');
}
EHR.Server.TriggerManager.registerHandlerForQuery(EHR.Server.TriggerManager.Events.BEFORE_UPSERT, 'study', 'birth', function(helper, scriptErrors, row, oldRow) {
if (!oldRow && row.Id && triggerHelper.birthExists(row.Id)) {
EHR.Server.Utils.addError(scriptErrors, 'Id', 'Birth record already exists for this Id, update existing record to change birth information', 'ERROR');
}
if (!helper.isETL()) {
if (row.QCStateLabel) {
row.qcstate = helper.getJavaHelper().getQCStateForLabel(row.QCStateLabel).getRowId();
}
if (row.Id && row.date) {
let assignmentRec = {
Id: row.Id,
date: row.date,
taskid: row.taskid,
remark: row.remark,
qcstate: row.qcstate,
performedby: row.performedby
}
if (row.project) {
assignmentRec['project'] = row.project;
triggerHelper.createAssignmentRecord("assignment", row.Id, assignmentRec);
}
if (row.birthProtocol) {
assignmentRec['protocol'] = row.birthProtocol;
triggerHelper.createAssignmentRecord("protocolAssignment", row.Id, assignmentRec);
}
}
if (!helper.isGeneratedByServer() && !helper.isValidateOnly()) {
// if 'cage', labeled as "Birth Location" is provided, then insert into housing.
if (row.cage && row.Id && row.date) {
var housingRec = {
Id: row.Id,
date: row.date,
cage: row.cage,
taskid: row.taskid,
qcstate: row.qcstate,
reason: 'Husbandry',
performedby: row.performedby
}
var housingErrors = triggerHelper.createHousingRecord(row.Id, housingRec, "birth");
if (housingErrors) {
EHR.Server.Utils.addError(scriptErrors, 'Id', housingErrors, 'ERROR');
}
}
// this allows demographic records in qcstates other than completed
var extraDemographicsFieldMappings = {
'qcstate': helper.getJavaHelper().getQCStateForLabel(row.QCStateLabel).getRowId()
}
var calc_status = (row.QCStateLabel.toUpperCase() === 'IN PROGRESS' || row.QCStateLabel.toUpperCase() === 'REVIEW REQUIRED') ? 'Alive - In Progress' : 'Alive';
var obj = {
Id: row.Id,
date: row.date,
calculated_status: calc_status,
dam: row['Id/demographics/dam'] || null,
sire: row['Id/demographics/sire'] || null,
species: row['Id/demographics/species'] || null,
birth: row.date || null,
gender: row['Id/demographics/gender'] || null,
taskid: row.taskid,
remark: row.remark,
QCStateLabel: row.QCStateLabel,
performedby: row.performedby
};
//find dam, if provided
if (obj.dam && !obj.origin) {
obj.origin = helper.getJavaHelper().getGeographicOrigin(obj.dam);
}
if (obj.dam && !obj.species) {
obj.species = helper.getJavaHelper().getSpecies(obj.dam);
}
if (!oldRow) {
//if not already present, we insert into demographics
helper.getJavaHelper().createDemographicsRecord(row.Id, obj, extraDemographicsFieldMappings);
}
else {
//Update demographics records
var ar = helper.getJavaHelper().getDemographicRecord(obj.Id);
var data = ar || {};
var record = {};
var hasUpdates = false;
if (obj.gender && obj.gender !== data.gender) {
record.gender = obj.gender;
hasUpdates = true;
}
if (obj.species && obj.species !== data.species) {
record.species = obj.species;
hasUpdates = true;
}
if (obj.birth && obj.birth !== data.birth) {
record.birth = obj.birth;
hasUpdates = true;
}
if (obj.sire && obj.sire !== data.sire) {
record.sire = obj.sire;
hasUpdates = true;
}
if (obj.dam && obj.dam !== data.dam) {
record.dam = obj.dam;
hasUpdates = true;
}
if (obj.performedby && obj.performedby !== data.performedby) {
record.performedby = obj.performedby;
hasUpdates = true;
}
if (obj.QCStateLabel && obj.QCStateLabel !== data.QCStateLabel) {
record.QCStateLabel = obj.QCStateLabel;
hasUpdates = true;
}
if (hasUpdates) {
console.info("Birth update for animal Id " + row.Id + " included demographic changes. Demographic record updated.");
record.Id = obj.Id;
var demographicsUpdates = [record];
helper.getJavaHelper().updateDemographicsRecord(demographicsUpdates);
helper.cacheDemographics(row.Id, row);
}
}
}
}
});