173 lines
8.0 KiB
C#
173 lines
8.0 KiB
C#
using NTP.Application.Abstractions;
|
|
using NTP.Domain.Grains.Permits;
|
|
|
|
namespace NTP.Application.Grains.Permits;
|
|
|
|
/// <summary>
|
|
/// Legacy role codes (session.userrolecode) that gate Grains permit operations. Legacy used per-action permissions
|
|
/// (application.objAuth.isActAuthorized(id, session.lstGID)); those ids are not in the repository, so the actions are
|
|
/// grouped by the role codes legacy code mentions (SU, GPO, GNSU, GNCO, GTO, GATEIN, GATEOUT, THRU). UNVERIFIED mapping.
|
|
/// </summary>
|
|
public static class GrainsRoles
|
|
{
|
|
/// <summary>Back-office operators: distribution, phone confirmation, manual operations, queue creation.</summary>
|
|
public const string Operators = "SU,GPO,GNSU,GNCO,GTO,CO";
|
|
|
|
/// <summary>Weighbridge / gate users plus operators.</summary>
|
|
public const string Weighbridge = "SU,GPO,GNSU,GNCO,GTO,CO,GATEIN,GATEOUT,THRU";
|
|
|
|
/// <summary>ConvertToResd button: legacy checks role SU or GPO only.</summary>
|
|
public const string ConvertToResd = "SU,GPO";
|
|
|
|
/// <summary>Supervisors: configuration of destinations, activation of orders, absence feature.</summary>
|
|
public const string Supervisors = "SU,GPO,GNSU,GTO,CO";
|
|
}
|
|
|
|
/// <summary>Helpers that replace legacy session.* lookups.</summary>
|
|
public static class PermitUserContext
|
|
{
|
|
/// <summary>Role code of a company dispatcher: sees and changes only the permits of its own company.</summary>
|
|
public const string DispatcherRole = "DISP";
|
|
|
|
/// <summary>session.stkid = 62 (a trucking company user) or a DISP dispatcher: sees and changes only its own company's permits.</summary>
|
|
public static bool IsCompany(this ICurrentUser user, GrainsPermitsOptions options) =>
|
|
user.StkId == options.CompanyStakeholderId
|
|
|| string.Equals(user.RoleCode, DispatcherRole, StringComparison.OrdinalIgnoreCase);
|
|
|
|
/// <summary>session.companyCodeNO = mid(CompanyCode, 4, 6) * 1; null when the user has no company code.</summary>
|
|
public static long? CompanyCodeNo(this ICurrentUser user) => ParseCompanyCodeNo(user.CompanyCode);
|
|
|
|
public static long? ParseCompanyCodeNo(string? companyCode)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(companyCode) || companyCode.Length < 4) return null;
|
|
var len = Math.Min(6, companyCode.Length - 3);
|
|
return long.TryParse(companyCode.AsSpan(3, len), out var v) ? v : null;
|
|
}
|
|
|
|
/// <summary>session.locationid as a nullable string for EVENTS.LOCATIONID.</summary>
|
|
public static string? LocationText(this ICurrentUser user) => user.LocationId?.ToString();
|
|
}
|
|
|
|
/// <summary>Which action buttons the caller may use on a permit (createGnOrder.cfm / GNPermits grid rules).</summary>
|
|
public static class PermitActionRules
|
|
{
|
|
public const string Submit = "submit";
|
|
public const string Edit = "edit";
|
|
public const string Accept = "accept";
|
|
public const string Reject = "reject";
|
|
public const string RevokeAccepted = "revokeAccepted";
|
|
public const string ConvertToResd = "convertToResd";
|
|
public const string PhoneAccept = "phoneAccept";
|
|
public const string PhoneReject = "phoneReject";
|
|
public const string CancelOrder = "cancelOrder";
|
|
public const string RevokePermit = "revokePermit";
|
|
public const string Reactivate = "reactivate";
|
|
public const string Print = "print";
|
|
public const string View = "view";
|
|
|
|
public static IReadOnlyList<string> For(string? status, long? companyCode, bool isCompany, bool isOperator, bool isConvertRole,
|
|
bool isReactivator, long badiyaScac)
|
|
{
|
|
var a = new List<string> { View };
|
|
switch (status)
|
|
{
|
|
case PermitStatus.Requested:
|
|
a.Add(Submit); a.Add(Edit);
|
|
break;
|
|
case PermitStatus.Planned:
|
|
a.Add(Accept); a.Add(Reject); a.Add(Edit);
|
|
break;
|
|
case PermitStatus.Open:
|
|
a.Add(Edit); // on the turn: the truck / trailer / driver can still be corrected, as for a confirmed (PLND) permit
|
|
break;
|
|
case PermitStatus.Accepted:
|
|
case PermitStatus.Released:
|
|
a.Add(RevokeAccepted); a.Add(Print);
|
|
if (status == PermitStatus.Accepted && isConvertRole) a.Add(ConvertToResd);
|
|
break;
|
|
case PermitStatus.Closed:
|
|
a.Add(Print);
|
|
break;
|
|
}
|
|
|
|
if (isOperator && status is PermitStatus.Requested or PermitStatus.Planned or PermitStatus.Accepted or PermitStatus.Released)
|
|
{
|
|
a.Add(PhoneAccept); a.Add(PhoneReject);
|
|
}
|
|
if (isOperator && status == PermitStatus.Released) a.Add(CancelOrder);
|
|
if (isOperator && status == PermitStatus.Open) a.Add(RevokePermit);
|
|
if (status == PermitStatus.Revoked && isReactivator && companyCode == badiyaScac) a.Add(Reactivate);
|
|
|
|
if (isCompany)
|
|
{
|
|
// Company users never see the staff-only buttons. Confirming (accept / reject / cancel the confirmation) is staff work in
|
|
// legacy too: actions 74, 85 and 88 belong to the staff groups, the company dispatcher group DISP only holds submit (73).
|
|
a.RemoveAll(x => x is PhoneAccept or PhoneReject or CancelOrder or RevokePermit or Reactivate or ConvertToResd
|
|
or Accept or Reject or RevokeAccepted);
|
|
}
|
|
return a;
|
|
}
|
|
}
|
|
|
|
/// <summary>Process-wide switch of the "absence" feature (legacy application.GNABSENCEFEATURE, in-memory).</summary>
|
|
public static class AbsenceFeatureState
|
|
{
|
|
private static int _state = -1;
|
|
|
|
public static bool IsEnabled(bool initial) => (Volatile.Read(ref _state) is var s && s >= 0) ? s == 1 : initial;
|
|
|
|
public static bool Toggle(bool initial)
|
|
{
|
|
var next = !IsEnabled(initial);
|
|
Volatile.Write(ref _state, next ? 1 : 0);
|
|
return next;
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------
|
|
// Outbound integration gateways (configurable URLs; implementations live in Infrastructure)
|
|
// ---------------------------------------------------------------------------------------------------------------
|
|
|
|
/// <summary>TCS petroleum permit web service (legacy application.PetWS): createPermit / getFopDetail.</summary>
|
|
public interface ITcsPermitGatewayService
|
|
{
|
|
Task<TcsPermitResult> CreatePermitAsync(TcsPermitRequest request, CancellationToken ct = default);
|
|
Task<TcsFopDetail> GetFopDetailAsync(string fop, CancellationToken ct = default);
|
|
}
|
|
|
|
/// <summary>Mobile notifications (legacy NTP.components.MobileNotifications.PrepareNotificationMessage).</summary>
|
|
public interface IMobileNotificationGatewayService
|
|
{
|
|
Task SendAsync(string template, IReadOnlyDictionary<string, object?> payload, CancellationToken ct = default);
|
|
}
|
|
|
|
/// <summary>Auto dialer / IVR (GrainsFactoryWeb.autoCallService2 / autoCallService).</summary>
|
|
public interface IAutoCallGatewayService
|
|
{
|
|
/// <summary>POSTs {"TYPE":"TEST","FILE":base64} to the configured dialer URL; false when not reachable / not 200.</summary>
|
|
Task<bool> SendCallFileAsync(byte[] csv, CancellationToken ct = default);
|
|
|
|
/// <summary>Calls the IVR web service (callAPI). That call dials every requested truck one by one and can take minutes.</summary>
|
|
Task<IvrTriggerResult> TriggerIvrAsync(CancellationToken ct = default);
|
|
}
|
|
|
|
/// <summary>Outcome of <see cref="IAutoCallGatewayService.TriggerIvrAsync"/>.</summary>
|
|
public enum IvrTriggerResult
|
|
{
|
|
/// <summary>The IVR service answered OK.</summary>
|
|
Done,
|
|
/// <summary>The IVR service accepted the call and is still dialing (it answers when it has finished).</summary>
|
|
Started,
|
|
/// <summary>A previous call is still running: a second one would dial every truck twice.</summary>
|
|
Busy,
|
|
/// <summary>Disabled, not configured, unreachable or refused.</summary>
|
|
Failed
|
|
}
|
|
|
|
/// <summary>Reads new rows from the external weighbridge database (legacy datasource GrainsWeighbridge).</summary>
|
|
public interface IWeighbridgeSourceService
|
|
{
|
|
bool IsConfigured { get; }
|
|
Task<IReadOnlyList<ExternalCargoRow>> ReadNewAsync(long locationId, DateTime? after, CancellationToken ct = default);
|
|
}
|