﻿<?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="54">
      <Name>Assign To Solver By Email</Name>
      <Description>When retrieving the message, the text of the message is examined for existing tickets to see if it contains a command "ASSIGNTO: solver name". If it contains it and the solver is found by his name, the ticket will be forwarded to him automatically and the message will not be recorded in the ticket's log. Otherwise, the message is processed in the standard way.</Description>
      <Scripts>
        <Script id="124">
          <Name>AssignToSolverByEmail</Name>
          <Code>using System.Data;
using System.Linq;
using Microsoft.Data.SqlClient;
using Alvao.Apps.API;
using Alvao.API;
using Dapper;

namespace Alvao.Helpdesk
{
    public class AssignToSolverByEmail : IMailMessageAutoAction
    {
        public string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public AssignToSolverByEmail()
        {
            name = Settings.ActionName;
        }

        public bool OnMessageReceived(SqlConnection con, SqlTransaction trans, Rebex.Mail.MailMessage message, int sectionId, int ticketId, int fromPersonId)
        {
            if (ticketId == 0)
                return true;

            if (message.BodyText != null)
            {
                int startIndex = message.BodyText.IndexOf("ASSIGNTO:");
                if (startIndex &gt;= 0)
                {
                    int length = message.BodyText.IndexOf("\n", startIndex) - (startIndex + 9);
                    string user = message.BodyText.Substring(startIndex + 9, length).Trim();
                    int? solverId = null;
                    var users = con.Query&lt;FoundPerson&gt;(@"exec spPersonSearch @sample, NULL, 0, NULL, '', 1", new { sample = user }, trans);
                    if (users.Any())
                    {
                        solverId = users.First().PersonId;
                    }

                    if (solverId.HasValue)
                    {
                        Alvao.API.SD.Ticket.ChangeSolverOrGroup(ticketId, solverId, null, new API.Common.Model.HtmlTextModel(string.Empty), null, fromPersonId);
                        return false;
                    }
                }
            }
            return true;
        }

        class FoundPerson
        {
            public int? PersonId { get; set; }
        }
    }
}</Code>
          <IsLibCode>false</IsLibCode>
        </Script>
        <Script id="125">
          <Name>Settings</Name>
          <Code>public static class Settings 
{
    public const string ActionName = "Assign to the solver via e-mail";
}</Code>
          <IsLibCode>true</IsLibCode>
        </Script>
      </Scripts>
    </Application>
  </Applications>
</AlvaoApplication>