using System.Collections; using System.Globalization; namespace NTP.Application.Grains.Permits; /// Pure rules of the mobile notifications (legacy MobileNotifications.cfc), kept apart so they can be tested. public static class NotificationRules { /// Template types (NOTIFICATIONMESSAGETEMPLATE.TYPE) used by the grains screens. 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"; /// Driver and truck-owner app accounts are found by the truck's phone number. public static readonly string[] PhoneRoles = ["DRVR", "TRKOWNER"]; /// Company dispatchers (role DISP) are found by a login that contains {SCAC}DSP. public const string DispatcherRole = "DISP"; /// /// ColdFusion Replace(msg, 'VAR1', value) for VAR1..VARn: each placeholder is replaced once (first occurrence), in order. /// 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; } /// Value as the legacy string concatenation would show it (dates as dd/MM/yyyy, no culture dependence). 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 }; /// SMS_toUsersWithNoMobileApplication: 0791234567 -> +962791234567 (mid(phone, 2, 15)); a number without a leading 0 only gets the prefix. 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; } /// The message plus the app download link, as SMS_toUsersWithNoMobileApplication builds it. public static string SmsText(string template, string appLink) => template + "، للتفاصيل الرجاء الضغط على الرابط" + appLink; /// Items of NOTIFICATIONARY as string maps (the callers pass dictionaries of mixed value types). public static IReadOnlyList> Entries(IReadOnlyDictionary payload) { var list = new List>(); 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(StringComparer.OrdinalIgnoreCase); if (item is IEnumerable> pairs) foreach (var (k, v) in pairs) map[k] = Text(v); list.Add(map); } return list; } public static string Get(IReadOnlyDictionary map, string key) => map.TryGetValue(key, out var v) ? v : string.Empty; public static string Get(IReadOnlyDictionary map, string key) => map.TryGetValue(key, out var v) ? Text(v) : string.Empty; }