Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion nirc_ehr/resources/queries/study/pregnancy.js
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
require("ehr/triggers").initScript(this);
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);
}
});
2 changes: 2 additions & 0 deletions nirc_ehr/src/org/labkey/nirc_ehr/NIRC_EHRModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,21 @@
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;
import java.text.SimpleDateFormat;
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
Expand Down Expand Up @@ -900,4 +901,81 @@ public void markProcedureOrderComplete(List<String> orderids)
_log.error("Error marking procedure order complete", e);
}
}

public void sendPregnancyOutcomeNotification(final String animalId, Map<String, Object> 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<UserPrincipal> 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("<br><br>");

//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<String, Object> 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;
}
}