diff --git a/nirc_ehr/resources/queries/study/pregnancy.js b/nirc_ehr/resources/queries/study/pregnancy.js index 72d57b2b..37782924 100644 --- a/nirc_ehr/resources/queries/study/pregnancy.js +++ b/nirc_ehr/resources/queries/study/pregnancy.js @@ -1 +1,15 @@ -require("ehr/triggers").initScript(this); \ No newline at end of file +require("ehr/triggers").initScript(this); + +var triggerHelper = new org.labkey.nirc_ehr.query.NIRC_EHRTriggerHelper(LABKEY.Security.currentUser.id, LABKEY.Security.currentContainer.id); + +EHR.Server.TriggerManager.registerHandlerForQuery(EHR.Server.TriggerManager.Events.ON_BECOME_PUBLIC, 'study', 'pregnancy', function(scriptErrors, helper, row, oldRow) { + if (!helper.isETL()) { + + var outcomeRec = { + Id: row.Id, + date: row.date, + result: row.result + } + triggerHelper.sendPregnancyOutcomeNotification(row.Id, outcomeRec); + } +}); \ No newline at end of file diff --git a/nirc_ehr/src/org/labkey/nirc_ehr/NIRC_EHRModule.java b/nirc_ehr/src/org/labkey/nirc_ehr/NIRC_EHRModule.java index a7e44b19..9bfefb9e 100644 --- a/nirc_ehr/src/org/labkey/nirc_ehr/NIRC_EHRModule.java +++ b/nirc_ehr/src/org/labkey/nirc_ehr/NIRC_EHRModule.java @@ -55,6 +55,7 @@ import org.labkey.nirc_ehr.history.*; import org.labkey.nirc_ehr.notification.NIRCClinicalMoveNotification; import org.labkey.nirc_ehr.notification.NIRCDeathNotification; +import org.labkey.nirc_ehr.notification.NIRCPregnancyOutcomeNotification; import org.labkey.nirc_ehr.notification.NIRCProcedureOverdueNotification; import org.labkey.nirc_ehr.query.NIRC_EHRUserSchema; import org.labkey.nirc_ehr.security.NIRCEHRVetTechRole; @@ -184,6 +185,7 @@ protected void doStartupAfterSpringConfig(ModuleContext moduleContext) NotificationService.get().registerNotification(new NIRCDeathNotification()); NotificationService.get().registerNotification(new NIRCClinicalMoveNotification()); NotificationService.get().registerNotification(new NIRCProcedureOverdueNotification()); + NotificationService.get().registerNotification(new NIRCPregnancyOutcomeNotification()); // Ensure N: is mounted if it's configured, as it's being mapped in via a symlink/shortcut, so we can't // recognize paths using it based solely on their drive letter and mount just-in-time diff --git a/nirc_ehr/src/org/labkey/nirc_ehr/notification/NIRCPregnancyOutcomeNotification.java b/nirc_ehr/src/org/labkey/nirc_ehr/notification/NIRCPregnancyOutcomeNotification.java new file mode 100644 index 00000000..914ac891 --- /dev/null +++ b/nirc_ehr/src/org/labkey/nirc_ehr/notification/NIRCPregnancyOutcomeNotification.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2025 LabKey Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.labkey.nirc_ehr.notification; + +import org.labkey.api.data.Container; +import org.labkey.api.ehr.notification.AbstractEHRNotification; +import org.labkey.api.security.User; + +import java.util.Date; + +public class NIRCPregnancyOutcomeNotification extends AbstractEHRNotification +{ + @Override + public String getName() + { + return "NIRC Pregnancy Outcome Notification"; + } + + @Override + public String getEmailSubject(Container c) + { + return "Pregnancy Outcome Alert " + getDateTimeFormat(c).format(new Date()); + } + + @Override + public String getCronString() + { + return null; + } + + @Override + public String getScheduleDescription() + { + return "Sent immediately upon Pregnancy Outcome form submission"; + } + + @Override + public String getDescription() + { + return "The report sends an alert whenever a Pregnancy Outcome is reported."; + } + + @Override + public String getMessageBodyHTML(Container c, User u) + { + return null; + } +} diff --git a/nirc_ehr/src/org/labkey/nirc_ehr/query/NIRC_EHRTriggerHelper.java b/nirc_ehr/src/org/labkey/nirc_ehr/query/NIRC_EHRTriggerHelper.java index b3f86cd3..08ab0540 100644 --- a/nirc_ehr/src/org/labkey/nirc_ehr/query/NIRC_EHRTriggerHelper.java +++ b/nirc_ehr/src/org/labkey/nirc_ehr/query/NIRC_EHRTriggerHelper.java @@ -42,6 +42,7 @@ import org.labkey.nirc_ehr.NIRC_EHRManager; import org.labkey.nirc_ehr.notification.NIRCClinicalMoveNotification; import org.labkey.nirc_ehr.notification.NIRCDeathNotification; +import org.labkey.nirc_ehr.notification.NIRCPregnancyOutcomeNotification; import org.labkey.nirc_ehr.notification.TriggerScriptNotification; import java.sql.SQLException; @@ -49,13 +50,13 @@ import java.time.LocalDateTime; import java.time.ZoneId; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; public class NIRC_EHRTriggerHelper @@ -900,4 +901,81 @@ public void markProcedureOrderComplete(List orderids) _log.error("Error marking procedure order complete", e); } } + + public void sendPregnancyOutcomeNotification(final String animalId, Map row) throws Exception + { + //check whether Notification is enabled + if (!NotificationService.get().isActive(new NIRCPregnancyOutcomeNotification(), _container) || !NotificationService.get().isServiceEnabled()) + { + _log.info("NIRC Pregnancy Outcome notification service is not enabled, will not send notification."); + return; + } + + try (DbScope.Transaction transaction = Objects.requireNonNull(StudyService.get()).getDatasetSchema().getScope().ensureTransaction()) + { + // Add a post commit task to run provider update in another thread once this transaction is complete. + transaction.addCommitTask(() -> + JobRunner.getDefault().execute(() -> { + final Container container = _container; + final User user = _user; + String subject = "Pregnancy Outcome Notification for: " + animalId; + + // get recipients + Set recipients = NotificationService.get().getRecipients(new NIRCPregnancyOutcomeNotification(), container); + if (recipients.isEmpty()) + { + _log.warn("No NIRC recipients set for pregnancy outcome notification, skipping notification"); + return; + } + //get pregnancy outcome info + Date date = ConvertHelper.convert(row.get("date"), Date.class); + String result = ConvertHelper.convert(row.get("result"), String.class); + String outcome = null; + try + { + outcome = getPregnancyResultTitle(result); + } + catch (SQLException e) + { + throw new RuntimeException("Unable to find the outcome for value '" + result + "'", e); + } + + //construct html for email notification + final StringBuilder html = new StringBuilder(); + html.append("Pregnancy outcome for animal '").append(PageFlowUtil.filter(animalId)). + append("' recorded on '").append(_dateFormat.format(date)).append("': "). + append(PageFlowUtil.filter(outcome)).append("

"); + + //append animal details + appendAnimalDetails(html, animalId, container); + + // send Pregnancy Outcome notification + _log.debug("NIRC Pregnancy Outcome notification job sending email for animal " + animalId + " in container " + container.getPath()); + TriggerScriptNotification.sendMessage(subject, html.toString(), recipients, container, user); + }), DbScope.CommitTaskOption.POSTCOMMIT); + + transaction.commit(); + } + } + + public String getPregnancyResultTitle(String val) throws SQLException + { + TableInfo ti = getTableInfo("ehr_lookups", "pregnancy_result"); + SimpleFilter filter = new SimpleFilter(FieldKey.fromString("value"), val); + TableSelector ts = new TableSelector(ti, PageFlowUtil.set("value", "title"), filter, null); + String title = null; + try (Results rs = ts.getResults()) + { + for (Map r : rs) + { + String value = ConvertHelper.convert(r.get("value"), String.class); + + if (null != value && value.equals(val)) + { + title = ConvertHelper.convert(r.get("title"), String.class); + } + } + } + return title; + } } \ No newline at end of file