using NTP.Application.Abstractions;
using NTP.Domain.Grains.Permits;
namespace NTP.Application.Grains.Permits;
///
/// 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.
///
public static class GrainsRoles
{
/// Back-office operators: distribution, phone confirmation, manual operations, queue creation.
public const string Operators = "SU,GPO,GNSU,GNCO,GTO";
/// Weighbridge / gate users plus operators.
public const string Weighbridge = "SU,GPO,GNSU,GNCO,GTO,GATEIN,GATEOUT,THRU";
/// ConvertToResd button: legacy checks role SU or GPO only.
public const string ConvertToResd = "SU,GPO";
/// Supervisors: configuration of destinations, activation of orders, absence feature.
public const string Supervisors = "SU,GPO,GNSU,GTO";
}
/// Helpers that replace legacy session.* lookups.
public static class PermitUserContext
{
/// Role code of a company dispatcher: sees and changes only the permits of its own company.
public const string DispatcherRole = "DISP";
/// session.stkid = 62 (a trucking company user) or a DISP dispatcher: sees and changes only its own company's permits.
public static bool IsCompany(this ICurrentUser user, GrainsPermitsOptions options) =>
user.StkId == options.CompanyStakeholderId
|| string.Equals(user.RoleCode, DispatcherRole, StringComparison.OrdinalIgnoreCase);
/// session.companyCodeNO = mid(CompanyCode, 4, 6) * 1; null when the user has no company code.
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;
}
/// session.locationid as a nullable string for EVENTS.LOCATIONID.
public static string? LocationText(this ICurrentUser user) => user.LocationId?.ToString();
}
/// Which action buttons the caller may use on a permit (createGnOrder.cfm / GNPermits grid rules).
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 For(string? status, long? companyCode, bool isCompany, bool isOperator, bool isConvertRole,
bool isReactivator, long badiyaScac)
{
var a = new List { 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 phone / cancel / revoke / reactivate buttons.
a.RemoveAll(x => x is PhoneAccept or PhoneReject or CancelOrder or RevokePermit or Reactivate or ConvertToResd);
}
return a;
}
}
/// Process-wide switch of the "absence" feature (legacy application.GNABSENCEFEATURE, in-memory).
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)
// ---------------------------------------------------------------------------------------------------------------
/// TCS petroleum permit web service (legacy application.PetWS): createPermit / getFopDetail.
public interface ITcsPermitGatewayService
{
Task CreatePermitAsync(TcsPermitRequest request, CancellationToken ct = default);
Task GetFopDetailAsync(string fop, CancellationToken ct = default);
}
/// Mobile notifications (legacy NTP.components.MobileNotifications.PrepareNotificationMessage).
public interface IMobileNotificationGatewayService
{
Task SendAsync(string template, IReadOnlyDictionary payload, CancellationToken ct = default);
}
/// Auto dialer / IVR (GrainsFactoryWeb.autoCallService2 / autoCallService).
public interface IAutoCallGatewayService
{
/// POSTs {"TYPE":"TEST","FILE":base64} to the configured dialer URL; false when not reachable / not 200.
Task SendCallFileAsync(byte[] csv, CancellationToken ct = default);
/// Calls the IVR web service (callAPI).
Task TriggerIvrAsync(CancellationToken ct = default);
}
/// Reads new rows from the external weighbridge database (legacy datasource GrainsWeighbridge).
public interface IWeighbridgeSourceService
{
bool IsConfigured { get; }
Task> ReadNewAsync(long locationId, DateTime? after, CancellationToken ct = default);
}