146 lines
5.7 KiB
C#
146 lines
5.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
using C4IT.FASD.Base;
|
|
using C4IT.Logging;
|
|
|
|
using FasdDesktopUi.Basics;
|
|
|
|
using static C4IT.Logging.cLogManager;
|
|
|
|
namespace FasdDesktopUi.Basics.Helper
|
|
{
|
|
internal static class TicketExternalLinkHelper
|
|
{
|
|
private const string DefaultTicketActivityType = "SPSActivityTypeTicket";
|
|
|
|
internal static bool TryOpenTicketRelationExternally(cF4sdApiSearchResultRelation relation, bool forceExternal = false)
|
|
{
|
|
try
|
|
{
|
|
if (relation == null || relation.Type != enumF4sdSearchResultClass.Ticket)
|
|
return false;
|
|
|
|
// check how we should open this ticket (intern, extern or both)
|
|
var ticketType = GetTicketType(relation);
|
|
var processing = ShouldOpenExternally(ticketType);
|
|
|
|
// check if we have a valid user id in the id list => if not we could open this ticket only extern.
|
|
var hasUser = HasValidUserIdentity(relation);
|
|
if (forceExternal || !hasUser)
|
|
processing = enumTicketProcessing.Extern;
|
|
|
|
if (processing == enumTicketProcessing.Intern)
|
|
return false;
|
|
|
|
var m42Server = cCockpitConfiguration.Instance?.m42ServerConfiguration?.Server;
|
|
if (!TryGetTicketLink(relation, m42Server, out var ticketLink))
|
|
{
|
|
LogEntry($"Could not resolve an external Matrix42 link for ticket '{relation.DisplayName ?? relation.Name}'.", LogLevels.Warning);
|
|
return false;
|
|
}
|
|
|
|
new cBrowsers().Start("default", ticketLink);
|
|
|
|
return processing == enumTicketProcessing.Extern;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogException(ex);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
internal static bool HasValidUserIdentity(cF4sdApiSearchResultRelation relation)
|
|
{
|
|
return relation?.Identities?.Any(identity =>
|
|
identity.Class == enumFasdInformationClass.User && identity.Id != Guid.Empty) == true;
|
|
}
|
|
|
|
internal static bool TryGetTicketLink(cF4sdApiSearchResultRelation relation, string m42Server, out string ticketLink)
|
|
{
|
|
ticketLink = null;
|
|
if (relation == null || relation.Type != enumF4sdSearchResultClass.Ticket)
|
|
return false;
|
|
|
|
if (TryGetConfiguredLink(relation, "TicketLink", out ticketLink) ||
|
|
TryGetConfiguredLink(relation, "DirectLinkPreview", out ticketLink))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (relation.id == Guid.Empty || string.IsNullOrWhiteSpace(m42Server))
|
|
return false;
|
|
|
|
var server = m42Server.Trim();
|
|
if (!server.Contains("://"))
|
|
server = $"https://{server}";
|
|
|
|
if (!Uri.TryCreate(server, UriKind.Absolute, out var serverUri) ||
|
|
(serverUri.Scheme != Uri.UriSchemeHttp && serverUri.Scheme != Uri.UriSchemeHttps))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var activityType = GetActivityType(relation);
|
|
var encodedActivityType = Uri.EscapeDataString(activityType);
|
|
ticketLink = $"{serverUri.Scheme}://{serverUri.Authority}/wm/app-ServiceDesk/notSet/preview-object/{encodedActivityType}/{relation.id:D}/0/";
|
|
return true;
|
|
}
|
|
|
|
private static bool TryGetConfiguredLink(cF4sdApiSearchResultRelation relation, string key, out string ticketLink)
|
|
{
|
|
ticketLink = null;
|
|
if (relation?.Infos?.TryGetValue(key, out var configuredLink) != true || string.IsNullOrWhiteSpace(configuredLink))
|
|
return false;
|
|
|
|
ticketLink = configuredLink;
|
|
return true;
|
|
}
|
|
|
|
private static string GetActivityType(cF4sdApiSearchResultRelation relation)
|
|
{
|
|
if (relation?.Infos?.TryGetValue("ActivityType", out var activityType) == true && !string.IsNullOrWhiteSpace(activityType))
|
|
return activityType.Trim();
|
|
|
|
if (relation?.Infos?.TryGetValue("TicketType", out var ticketTypeValue) == true &&
|
|
Enum.TryParse(ticketTypeValue, true, out enumTicketType ticketType))
|
|
{
|
|
switch (ticketType)
|
|
{
|
|
case enumTicketType.Incident:
|
|
return "SPSActivityTypeIncident";
|
|
case enumTicketType.ServiceRequest:
|
|
return "SPSActivityTypeServiceRequest";
|
|
}
|
|
}
|
|
|
|
return DefaultTicketActivityType;
|
|
}
|
|
|
|
private static enumTicketType GetTicketType(cF4sdApiSearchResultRelation relation)
|
|
{
|
|
if (relation?.Infos != null && relation.Infos.TryGetValue("TicketType", out var ticketTypeValue))
|
|
{
|
|
if (Enum.TryParse<enumTicketType>(ticketTypeValue, true, out var ticketType))
|
|
return ticketType;
|
|
}
|
|
return enumTicketType.Ticket;
|
|
}
|
|
|
|
private static enumTicketProcessing ShouldOpenExternally(enumTicketType ticketType)
|
|
{
|
|
var ticketConfig = cFasdCockpitConfig.Instance?.Global?.TicketConfiguration;
|
|
if (ticketConfig == null)
|
|
return enumTicketProcessing.Extern;
|
|
|
|
if (ticketConfig.TicketProcessing?.TryGetValue(ticketType, out var processing) == true)
|
|
return processing;
|
|
|
|
return ticketConfig.DefaultProcessing;
|
|
}
|
|
}
|
|
}
|