﻿<?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="1">
      <Name>Auto Close Inactive Tickets</Name>
      <Description>For resolved tickets, it is checked whether the deadline for reopening the ticket by the applicant has elapsed (in days) and they have turned off the automatic transition to the Closed status. If these tickets are not closed within 14 days, they are closed automatically.</Description>
      <Scripts>
        <Script id="1">
          <Name>AutoCloseInactiveTickets</Name>
          <Code>using System;
using Microsoft.Data.SqlClient;
using Alvao.Apps.API;
using System.Collections.Generic;
using Dapper;

namespace Alvao.Helpdesk
{
    class AutoCloseInactiveTickets : IPeriodicAction
    {
        public string Name { get; set; }

        public AutoCloseInactiveTickets()
        {
            Name = "Automatic closure of resolved tickets after the set deadline for reopening by the applicant";
        }

        public void OnPeriod(SqlConnection con)
        {
            string sql = @"
            select
                t.iHdTicketId id
            from tHdTicket t
                left join tHdSection s on s.iHdSectionId=t.liHdTicketHdSectionId
                join TicketState ts on ts.TicketStateBehaviorId=3 AND ts.TicketTypeId=s.TicketTypeId
            where t.dHdTicketResolved is not null
                and t.dHdTicketRemoved is null
                and ClosedDate is null
                and dateadd(day, isnull(s.nHdSectionUserReopenDays, 0), t.dHdTicketResolved)&lt;getutcdate()";

            var ids = con.Query&lt;int&gt;(sql);

            var systemPerson = API.Common.Person.GetSystem();

            foreach (int id in ids)
            {
                API.SD.Ticket.Close(id, systemPerson.iPersonId, API.SD.Ticket.CloseFlags.IgnoreRights);
            }
        }
    }
}</Code>
          <IsLibCode>false</IsLibCode>
        </Script>
      </Scripts>
    </Application>
  </Applications>
</AlvaoApplication>