diff --git a/onprc_ehr/resources/queries/onprc_ehr/ChemistryPanicNotification.sql b/onprc_ehr/resources/queries/onprc_ehr/ChemistryPanicNotification.sql new file mode 100644 index 000000000..588fd71ae --- /dev/null +++ b/onprc_ehr/resources/queries/onprc_ehr/ChemistryPanicNotification.sql @@ -0,0 +1,17 @@ +select a.Id, + a.date, + a.servicerequested, + b.testid, + b.qualresult, + a.vet, + a.created, + a.objectid, + (select j.rowid from ehr.tasks j where j.taskid = a.taskid) as taskid + + +from study.ClinpathRuns a, study.chemistryResults b +Where a.objectid = b.runid + And a.type = 'biochemistry' + And b.qualresult like '%panic%' + And a.qcstate = 18 + And b.qcstate = 18 \ No newline at end of file diff --git a/onprc_ehr/resources/queries/onprc_ehr/ChemistryPanicNotification.xml b/onprc_ehr/resources/queries/onprc_ehr/ChemistryPanicNotification.xml new file mode 100644 index 000000000..45e370226 --- /dev/null +++ b/onprc_ehr/resources/queries/onprc_ehr/ChemistryPanicNotification.xml @@ -0,0 +1,14 @@ + + + + + + + user ID + true + + +
+
+
+
diff --git a/onprc_ehr/resources/scripts/onprc_ehr/onprc_triggers.js b/onprc_ehr/resources/scripts/onprc_ehr/onprc_triggers.js index 6a96e4701..731ca0f96 100644 --- a/onprc_ehr/resources/scripts/onprc_ehr/onprc_triggers.js +++ b/onprc_ehr/resources/scripts/onprc_ehr/onprc_triggers.js @@ -1378,6 +1378,15 @@ exports.init = function(EHR){ }); }); + // Added: 10-6-2025 + EHR.Server.TriggerManager.registerHandlerForQuery(EHR.Server.TriggerManager.Events.AFTER_UPSERT, 'study', 'chemistryResults', function (helper, scriptErrors, row, oldRow) { + + if (row.Id && row.qualresult && row.qualresult.indexOf('panic') !== -1) { + console.log("panic values: " + row.qualresult); + triggerHelper.sendClinpathPanicEmail(row.Id, row.runid); + } + }); + //Added: 10-4-2022 R.Blasa EHR.Server.TriggerManager.registerHandler(EHR.Server.TriggerManager.Events.COMPLETE, function(event, errors, helper){ // Send notifications when requests approved diff --git a/onprc_ehr/src/org/labkey/onprc_ehr/query/ONPRC_EHRTriggerHelper.java b/onprc_ehr/src/org/labkey/onprc_ehr/query/ONPRC_EHRTriggerHelper.java index b7d72a105..babf66144 100644 --- a/onprc_ehr/src/org/labkey/onprc_ehr/query/ONPRC_EHRTriggerHelper.java +++ b/onprc_ehr/src/org/labkey/onprc_ehr/query/ONPRC_EHRTriggerHelper.java @@ -54,6 +54,9 @@ import org.labkey.api.query.QueryService; import org.labkey.api.query.QueryUpdateServiceException; import org.labkey.api.query.UserSchema; +import org.labkey.api.security.Group; +import org.labkey.api.security.MemberType; +import org.labkey.api.security.SecurityManager; import org.labkey.api.security.User; import org.labkey.api.security.UserManager; import org.labkey.api.security.UserPrincipal; @@ -63,6 +66,7 @@ import org.labkey.api.util.MailHelper; import org.labkey.api.util.PageFlowUtil; import org.labkey.api.util.Pair; +import org.labkey.api.util.logging.LogHelper; import org.labkey.onprc_ehr.ONPRC_EHRManager; import org.labkey.onprc_ehr.ONPRC_EHRModule; import org.labkey.onprc_ehr.ONPRC_EHRSchema; @@ -99,7 +103,7 @@ */ public class ONPRC_EHRTriggerHelper { - private static final Logger _log = LogManager.getLogger(ONPRC_EHRTriggerHelper.class); + private static final Logger _log = LogHelper.getLogger(ONPRC_EHRTriggerHelper.class, "Fill in description"); private static final String NONRESTRICTED = "Nonrestricted"; private static final String EXPERIMENTAL_EUTHANASIA = "EUTHANASIA, EXPERIMENTAL"; private static final String NON_EXPERIMENTAL_EUTHANASIA = "EUTHANASIA, NONEXPERIMENTAL"; @@ -2600,7 +2604,6 @@ public void updateArrivalrecords(String id, Date date) throws Exception keys.add(FieldKey.fromString("lsid")); final Map colMap = QueryService.get().getColumns(ti, keys); - final List> toUpdate = new ArrayList<>(); final List> oldKeys = new ArrayList<>(); TableSelector ts = new TableSelector(ti, colMap.values(), new SimpleFilter(FieldKey.fromString("Id"), id, CompareType.IN), null); @@ -2633,8 +2636,148 @@ public void exec(ResultSet object) throws SQLException } } + public void sendClinpathPanicEmail(String id, String runid) + { + String subject = "Chemistry Results with Panic values"; + + + final TableInfo ti = getTableInfo("onprc_ehr", "ChemistryPanicNotification"); + SimpleFilter filter = new SimpleFilter(FieldKey.fromString("Id"), id, CompareType.EQUAL); + filter.addCondition(FieldKey.fromString("objectid"), runid, CompareType.EQUAL); + + + List names= new ArrayList<>(); + FieldKey clinpathFieldKey = FieldKey.fromString("objectid"); + names.add(clinpathFieldKey); + names.add(FieldKey.fromString("qualResult")); + names.add(FieldKey.fromString("servicerequested")); + names.add(FieldKey.fromString("testid")); + names.add(FieldKey.fromString("date")); + names.add(FieldKey.fromString("Id")); + names.add(FieldKey.fromString("vet")); + names.add(FieldKey.fromString("taskid")); + + + final Map colKeys = QueryService.get().getColumns(ti, names); + final ColumnInfo clinpathColumn = colKeys.get(clinpathFieldKey); + TableSelector ts = new TableSelector(ti, colKeys.values(), filter, null); + + final StringBuilder html = new StringBuilder(); + + + if (ts.getRowCount() == 0) + { + html.append("There are no Chemistry Panlc Values to display"); + + return; + } + else + { + + html.append(""); + html.append("\n"); + ts.forEach(new Selector.ForEachBlock() + { + + @Override + public void exec(ResultSet rs) throws SQLException + { + + TableInfo ti2 = getTableInfo("onprc_ehr", "Labwork_Requestor_Vets"); + SimpleFilter filter2 = new SimpleFilter(FieldKey.fromString("userid"), rs.getString("vet")); + filter2.addCondition(FieldKey.fromString("DisableDate"), true, CompareType.ISBLANK); + + + TableSelector ts2 = new TableSelector(ti2, PageFlowUtil.set("LastName"), filter2, null); + List ret2 = ts2.getArrayList(String.class); + if (!ret2.isEmpty()) + { + for (String Vetname : ret2) + { + html.append("\n"); + break; + + } + } + + } + + } + + ); + + } + final TableInfo tt = getTableInfo("study", "clinpathRuns"); + SimpleFilter filtert = new SimpleFilter(FieldKey.fromString("objectid"), runid, CompareType.EQUAL); + filter.addCondition(FieldKey.fromString("id"), id, CompareType.EQUAL); + + TableSelector tst = new TableSelector(ti, PageFlowUtil.set("vet"), filter, null); + tst.forEach(new Selector.ForEachBlock<>() + { + @Override + public void exec(ResultSet rs) throws SQLException + { + Integer vetId = rs.getInt("vet"); + Set recipients = getRecipients(vetId); + if (recipients.isEmpty()) + { + _log.warn("No recipients, unable to send EHR trigger script email"); + return; + } + else + { + html.append("
Animal IDDateService Requested Panel Test Name Qual ResultsTask ID Vet/PI Name
" + PageFlowUtil.filter(rs.getString("Id")) + + "" + PageFlowUtil.filter(rs.getString("date")) + + "" + PageFlowUtil.filter(rs.getString("servicerequested")) + + " " + PageFlowUtil.filter(rs.getString("testid")) + + " " + PageFlowUtil.filter(rs.getString("qualResult")) + + " " + PageFlowUtil.filter(rs.getString("taskid")) + + "" + PageFlowUtil.filter(Vetname) + "
\n"); + + sendMessage(subject, html.toString(), recipients); + + } + + } + + + }); + + + } + + + private Set getRecipients(Integer... userIds) + { + Set recipients = new HashSet<>(); + for (Integer userId : userIds) + { + if (userId > 0) + { + UserPrincipal up = SecurityManager.getPrincipal(userId); + if (up != null) + { + if (up instanceof User) + { + recipients.add(up); + } + else + { + for (UserPrincipal u : SecurityManager.getAllGroupMembers((Group)up, MemberType.ACTIVE_USERS)) + { + if (u.isActive()) + recipients.add(u); + } + } + } + } + } + + return recipients; + } + //Added 9-30-2025 public String retrieveGeographic_Origin(String Id)