All checks were successful
Build and Deploy / deploy (push) Successful in 3m50s
Co-authored-by: Cursor <cursoragent@cursor.com>
53 lines
1.3 KiB
C#
53 lines
1.3 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace Oms.Application.Shared;
|
|
|
|
public class CouponApplyScope
|
|
{
|
|
[JsonPropertyName("countryCodes")]
|
|
public List<string>? CountryCodes { get; set; }
|
|
}
|
|
|
|
public static class MarketingCouponScope
|
|
{
|
|
public static bool Matches(string? applyScopeJson, string? destinationCountry)
|
|
{
|
|
var codes = ParseCountryCodes(applyScopeJson);
|
|
if (codes.Count == 0)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(destinationCountry))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var dest = destinationCountry.Trim();
|
|
return codes.Any(x => string.Equals(x, dest, StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
public static IReadOnlyList<string> ParseCountryCodes(string? applyScopeJson)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(applyScopeJson))
|
|
{
|
|
return [];
|
|
}
|
|
|
|
try
|
|
{
|
|
var scope = JsonSerializer.Deserialize<CouponApplyScope>(applyScopeJson);
|
|
return (scope?.CountryCodes ?? [])
|
|
.Where(x => !string.IsNullOrWhiteSpace(x))
|
|
.Select(x => x.Trim())
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
.ToList();
|
|
}
|
|
catch (JsonException)
|
|
{
|
|
return [];
|
|
}
|
|
}
|
|
}
|