﻿<?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="49">
      <Name>TicketCreateOutlookMeeting</Name>
      <Description>To the Request page, adds the “Create meeting” command that creates a new meeting in the MS Outlook calendar.</Description>
      <Scripts>
        <Script id="130">
          <Name>TicketCreateOutlookMeetingCommand</Name>
          <Code>using System;
using Alvao.API.Common;
using Alvao.API.Common.Model.CustomApps;
using Alvao.Apps.API;
using Alvao.API.Common.Model.Database;
using MessageType = Alvao.API.Common.Model.CustomApps.MessageType;

public class TicketCreateOutlookMeetingCommand : IEntityCommand
{
    public string Id { get; set; }
    public Entity Entity { get; set; }

    public TicketCreateOutlookMeetingCommand()
    {
        Id = "TicketCreateOutlookMeeting".GetHashCode().ToString();
        Entity = Entity.Request;
    }

    public EntityCommandShowResult Show(int entityId, int personId)
    {
        int position = 2;
        string icon = "OutlookLogo";
        string name = "Create meeting";
        bool show = false;
        
        tHdTicket ticket = Alvao.API.SD.Ticket.GetById(entityId);

        if (ticket != null &amp;&amp; ticket.liHdTicketHdSectionId &gt; 0)
        {
            var teamRoles = PersonRights.TicketRoles.SectionManager | PersonRights.TicketRoles.SectionMainSolver | PersonRights.TicketRoles.SectionSolver;
            show = PersonRights.HaveTicketRole(personId, entityId, ticket.liHdTicketHdSectionId, teamRoles, true);
        }

        return new EntityCommandShowResult(show, name, icon, position);
    }

    public CommandResult Run(int entityId, int personId)
    {
        MessageType messageType = MessageType.None;
        string messageText = string.Empty;
        string navigateToUrl = string.Empty;

        try
        {
            var userMail = Person.GetById(personId).sPersonEmail;

            if (!string.IsNullOrEmpty(userMail))
            {
                // Get ticket info
                var waUrl = DbProperty.WebAppUrl;
                var ticket = Alvao.API.SD.Ticket.GetById(entityId);
                var ticketFullName = Settings.GetSubject(ticket.sHdTicket, ticket.sHdTicketMessageTag);
                var ticketUrl = "";
                if (!string.IsNullOrEmpty(waUrl))
                {
                    ticketUrl = waUrl + "/Ticket/" + entityId;
                }

                navigateToUrl = "https://outlook.office.com/calendar/deeplink/compose?subject=" + System.Net.WebUtility.UrlEncode(ticketFullName) + "&amp;body=" + System.Net.WebUtility.UrlEncode(ticketUrl);
            }
        }
        catch (Exception e)
        {
            messageType = MessageType.Error;
            messageText = e.Message;
            return new CommandResult(messageType, messageText, navigateToUrl);
        }

        return new CommandResult(messageType, messageText, navigateToUrl);
    }
}
</Code>
          <IsLibCode>false</IsLibCode>
        </Script>
        <Script id="262">
          <Name>Settings</Name>
          <Code>public class Settings
{
    private static int subjectMaxLength = 150;
    private static string dots = "...";

    public static string GetSubject (string name, string tag)
    {
        string ticketFullName;
        if (name.Length &gt; 150)
        {
            name = $"{name.Trim().Substring(0, subjectMaxLength - 1)}";
            ticketFullName = $"{name}{dots} ({tag})"; 
        }
        else
        {
            ticketFullName = $"{name} ({tag})"; 
        }

        return ticketFullName;
    }
}</Code>
          <IsLibCode>true</IsLibCode>
        </Script>
      </Scripts>
    </Application>
  </Applications>
</AlvaoApplication>