NTP_V2/src/NTP.Application/Grains/Permits/NotificationRules.cs

78 lines
3.9 KiB
C#

using System.Collections;
using System.Globalization;
namespace NTP.Application.Grains.Permits;
/// <summary>Pure rules of the mobile notifications (legacy MobileNotifications.cfc), kept apart so they can be tested.</summary>
public static class NotificationRules
{
/// <summary>Template types (NOTIFICATIONMESSAGETEMPLATE.TYPE) used by the grains screens.</summary>
public const string DistributeOwners = "DISTRIBUTE_PACKED_BULK_OWNERS";
public const string DistributeOwnersCompanies = "DISTRIBUTE_PACKED_BULK_OWNERS_COMPANIES";
public const string DistributeCompanies = "DISTRIBUTE_PACKED_BULK_COMP";
public const string ConfirmPn = "CONFIRM_PN";
public const string PhoneReject = "PHONE_REJECT";
public const string DelayPn = "DELAY_PN";
public const string SmsWithoutApp = "DISTRIBUTE_GRAINS_SMS_TO_USER_WITHNO_APP";
/// <summary>Driver and truck-owner app accounts are found by the truck's phone number.</summary>
public static readonly string[] PhoneRoles = ["DRVR", "TRKOWNER"];
/// <summary>Company dispatchers (role DISP) are found by a login that contains {SCAC}DSP.</summary>
public const string DispatcherRole = "DISP";
/// <summary>
/// ColdFusion <c>Replace(msg, 'VAR1', value)</c> for VAR1..VARn: each placeholder is replaced once (first occurrence), in order.
/// </summary>
public static string Fill(string template, params object?[] values)
{
var text = template;
for (var i = 0; i < values.Length; i++)
{
var key = "VAR" + (i + 1).ToString(CultureInfo.InvariantCulture);
var at = text.IndexOf(key, StringComparison.Ordinal);
if (at >= 0) text = string.Concat(text.AsSpan(0, at), Text(values[i]), text.AsSpan(at + key.Length));
}
return text;
}
/// <summary>Value as the legacy string concatenation would show it (dates as dd/MM/yyyy, no culture dependence).</summary>
public static string Text(object? v) => v switch
{
null => string.Empty,
string s => s,
DateTime d => d.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
IFormattable f => f.ToString(null, CultureInfo.InvariantCulture),
_ => v.ToString() ?? string.Empty
};
/// <summary>SMS_toUsersWithNoMobileApplication: 0791234567 -> +962791234567 (mid(phone, 2, 15)); a number without a leading 0 only gets the prefix.</summary>
public static string SmsNumber(string phone)
{
var p = phone.Trim();
return p.StartsWith('0') ? "+962" + (p.Length > 16 ? p[1..16] : p[1..]) : "+962" + p;
}
/// <summary>The message plus the app download link, as SMS_toUsersWithNoMobileApplication builds it.</summary>
public static string SmsText(string template, string appLink) => template + "، للتفاصيل الرجاء الضغط على الرابط" + appLink;
/// <summary>Items of NOTIFICATIONARY as string maps (the callers pass dictionaries of mixed value types).</summary>
public static IReadOnlyList<IReadOnlyDictionary<string, string>> Entries(IReadOnlyDictionary<string, object?> payload)
{
var list = new List<IReadOnlyDictionary<string, string>>();
if (!payload.TryGetValue("NOTIFICATIONARY", out var raw) || raw is not IEnumerable items || raw is string) return list;
foreach (var item in items)
{
var map = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
if (item is IEnumerable<KeyValuePair<string, object?>> pairs)
foreach (var (k, v) in pairs) map[k] = Text(v);
list.Add(map);
}
return list;
}
public static string Get(IReadOnlyDictionary<string, string> map, string key) => map.TryGetValue(key, out var v) ? v : string.Empty;
public static string Get(IReadOnlyDictionary<string, object?> map, string key) => map.TryGetValue(key, out var v) ? Text(v) : string.Empty;
}