﻿<?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="51">
      <Name>Assign To Tester</Name>
      <Description>Assign the ticket to solver from the Testers group if the ticket is in the Program modifications service and in the status with ID 1.</Description>
      <Scripts>
        <Script id="116">
          <Name>AssignToTester</Name>
          <Code>using System;
using System.Data;
using Microsoft.Data.SqlClient;
using Alvao.Apps.API;
using Alvao.API.SD;
using Alvao.API.Common.Model;
using Dapper;

namespace Alvao.Helpdesk
{
    class AssignToTester : ITicketAutoAction
    {
        public string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public AssignToTester()
        {
            name = Settings.ActionName;
        }

        public void OnTicketCreated(SqlConnection con, SqlTransaction trans, int ticketId, int personId)
        {
            throw new NotImplementedException();
        }

        public void OnTicketChanged(SqlConnection con, SqlTransaction trans, int ticketId, int personId, string properties)
        {
            if (properties.Contains("tHdTicketCust.solvedInVersion"))
            {
                // Get values.
                string solvedInVersion = API.Common.Database.ReadColumn(ticketId, "tHdTicketCust", "solvedInVersion");
                string section = API.Common.Database.ReadColumn(ticketId, "TicketForeignKeyInfo", "SectionName");
                int stateId = int.Parse(API.Common.Database.ReadColumn(ticketId, "tHdTicket", "TicketStateId"));

                // Values check.
                if (!String.IsNullOrEmpty(solvedInVersion) &amp;&amp; section == Settings.ServiceName &amp;&amp; stateId == Settings.ControlStateId)
                {
                    // Get solver.
                    int? newSolverId = con.QueryFirstOrDefault&lt;int?&gt;($@"SELECT TOP 1 TRP.liRolePersonPersonId newSolverId
                        FROM tRolePerson TRP JOIN tRole TR ON TRP.liRolePersonRoleId=TR.iRoleId WHERE TR.sRole = N'{Settings.SolverGroupName}'", transaction: trans);

                    // Assign to solver.
                    if (newSolverId.HasValue)
                        Ticket.ChangeSolverOrGroup(ticketId, newSolverId, null, new HtmlTextModel(string.Empty));                    
                }
            }
        }
    }
}</Code>
          <IsLibCode>false</IsLibCode>
        </Script>
        <Script id="117">
          <Name>Settings</Name>
          <Code>public static class Settings 
{
    public const string ActionName = "Resolved in the version - pass to the test";

    public const string ServiceName = "Program modifications"; // Name of the service in which the ticket should be passed to Testers.
    public const int ControlStateId = 1; // Status ID in which the ticket should be passed to the Testers.
    public const string SolverGroupName = "Testers";
}</Code>
          <IsLibCode>true</IsLibCode>
        </Script>
      </Scripts>
    </Application>
  </Applications>
</AlvaoApplication>