﻿<?xml version="1.0"?>
<AlvaoApplication xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ModelVersion="1">
  <Applications>
    <Application id="53">
      <Name>Send Mails About New Event</Name>
      <Description>When an event is created in a ticket in the Program modifications service, this act is forwarded in an e-mail to each service manager, unless it is the author or recipient of the event.</Description>
      <Scripts>
        <Script id="121">
          <Name>SendMailsAboutNewEvent</Name>
          <Code>using System;
using System.Collections.Generic;
using System.Data;
using System.Net.Mail;
using Microsoft.Data.SqlClient;
using Alvao.Apps.API;
using Dapper;

namespace Alvao.Helpdesk
{
    class SendMailsAboutNewEvent : IActAutoAction
    {
        public string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public SendMailsAboutNewEvent()
        {
            name = Settings.ActionName;
        }

        public void OnActChanged(SqlConnection con, SqlTransaction trans, int ticketId, int personId, string properties) {
            throw new NotImplementedException();
        }
        public void OnActRemoved(SqlConnection con, SqlTransaction trans, int ticketId, int personId) {
            throw new NotImplementedException();
        }

        public void OnActCreated(SqlConnection con, SqlTransaction trans, int actId, int personId)
        {
            // Get values.
            var dataModel = con.QueryFirstOrDefault&lt;DataModel&gt;(@"SELECT liActHdTicketId as TicketId, sActFrom as ActFrom, sActFromEmail as ActFromEmail, 
                sActTo as ActTo, sActToEmail as ActToEmail, sAct as ActSubject, mActNotice as ActPlainText, sHdSection as SectionName, sHdSectionEmail as SectionEmail, iHdSectionId as SectionId
                FROM tAct a
                join tHdTicket t on a.liActHdTicketId=t.iHdTicketId
                join tHdSection s on t.liHdTicketHdSectionId=s.iHdSectionId 
                WHERE iActId = @actId", new { actId }, trans);
                
            if (dataModel == null)
                return;

            dataModel.ActPlainText = "Hello,\n\n a new note was created on the ticket number " + dataModel.TicketId.ToString() + ":\n\n" + dataModel.ActPlainText;

            // Check values.
            if (dataModel.SectionName == Settings.ServiceName)
            {
                List&lt;string&gt; recipients = new List&lt;string&gt;();
                var personEmails = con.Query&lt;string&gt;(@"select sPersonEmail from HdSectionManager 
                    left join tPerson on iPersonId = PersonId 
                    where sPersonEmail is not null
                        and bPersonAccountDisabled = 0
                        and HdSectionId = @sectionId", new { sectionId = dataModel.SectionId }, trans);
                foreach(var personEmail in personEmails)
                {    
                    if (!dataModel.ActTo.Contains(personEmail) &amp;&amp; !dataModel.ActToEmail.Contains(personEmail) &amp;&amp; !dataModel.ActFrom.Contains(personEmail) &amp;&amp; !dataModel.ActFromEmail.Contains(personEmail))
                        recipients.Add(personEmail);
                }

                // Send e-mail.
                if (recipients.Count &gt; 0)
                {
                    string to = String.Join(";", recipients);
                    string from = dataModel.SectionEmail;
                    string subject = dataModel.ActSubject;
                    string body = dataModel.ActPlainText;
                    System.Net.Mail.MailMessage message = new MailMessage(from, to, subject, body);
                    Alvao.API.Common.Email.Queue(message);
                }
            }
        }

        class DataModel
        {
            public int TicketId {get;set;} = 0;
            public int SectionId {get;set;} = 0;
            public string ActFrom {get;set;} = "";
            public string ActFromEmail {get;set;} = "";
            public string ActTo {get;set;} = "";
            public string ActToEmail {get;set;} = "";
            public string ActSubject {get;set;} = "";
            public string ActPlainText {get;set;} = "";
            public string SectionName {get;set;} = "";
            public string SectionEmail {get;set;} = "";
        }
    } 
}</Code>
          <IsLibCode>false</IsLibCode>
        </Script>
        <Script id="122">
          <Name>Settings</Name>
          <Code>public class Settings 
{
    public static string ActionName = "New note – send e-mails";
    public static string ServiceName = "Program modifications"; // The name of the service on which ticket the event will be created.
}</Code>
          <IsLibCode>true</IsLibCode>
        </Script>
      </Scripts>
    </Application>
  </Applications>
</AlvaoApplication>