RSS Git Download  Clone
Raw Blame History
#!/usr/bin/perl

=begin -------------------------------------------------------------------------
emulates function of email_alert() for records matching complex rules:

notification of:
 new/relapsed diagnosis
 where age <= 25
 and organisation code like 'RR8%'
 and authorised yesterday
 to:
    Teenage & Young Adult Oncology email contact (age 15 - 25)
    Paediatric email contact (age < 18)
=cut ---------------------------------------------------------------------------

use strict;
use warnings;

my $JUST_TESTING = 1; # email to ra.jones only

############ recipients from contacts.lib ######################################
my @young_adult = qw( young.adult.secure ); # 15 - 25 yo
my @paediatric  = qw( paediatrics.secure ); # < 18 yo
################################################################################

BEGIN {
    use FindBin qw($Bin); # warn $Bin;
    use lib (
        "$Bin/../../../lib",
        '/home/raj/perl5/lib/perl5',
    );
}

use LIMS::Local::ScriptHelpers;
use Data::Dumper;

my $tools = LIMS::Local::ScriptHelpers->new();
$tools->test_only($JUST_TESTING);

# get tools from LIMS::Local::ScriptHelpers:
my $sql_lib  = $tools->sql_lib();
my $config   = $tools->config();
my $dbix     = $tools->dbix();

# get sql statment:
my $requests = $sql_lib->retr( 'juvenile_diagnosis_status_alert' );
# template:
my $tt_file  = 'request/message/body.tt';
# email subject line:
my $subject   = 'HMDS Diagnosis Status Notice';

my $result = $dbix->query($requests);
while (my $vars = $result->hash) { # warn Dumper $vars; next;
	my $data = $tools->get_diagnosis_alert_data($vars);

    my $message_body = $tools->process_template($tt_file, $data);

	my %mail = (		
		config  => $config,
		message => $message_body,
		subject => $subject, 	
	); # warn Dumper \%mail; next;

    # reset @contacts:
    my @contacts = qw( raj.secure );
    
    # query retrieves patients <= 25 yo:
    push @contacts, @paediatric  if $vars->{age} < 18;
    push @contacts, @young_adult if $vars->{age} >= 15;
    
    $tools->send_mail(\%mail, \@contacts);
}