Skip to content

Commit

Permalink
Addding additional validation in rules and counterparty to support ap…
Browse files Browse the repository at this point in the history
…i changes (#341)

* Fixed the counterparty validation logic.

* Added additional validation logic to counterparty and rule destinations.
  • Loading branch information
sipsorcery authored May 24, 2024
1 parent c357c02 commit 34fde54
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 14 deletions.
6 changes: 6 additions & 0 deletions src/NoFrixion.MoneyMoov/Claims/IdentityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,16 @@ public static string WhoAmI(this ClaimsPrincipal claimsPrincipal)

public static string WhoAmI(this ClaimsIdentity claimsIdentity)
{
if(claimsIdentity == null)
{
return "Empty claims identity";
}

if (claimsIdentity.IsComplianceOfficer())
{
return "Compliance " + claimsIdentity.GetEmailAddress();
}

if (claimsIdentity.IsOperationsOfficer())
{
return "Operations " + claimsIdentity.GetEmailAddress();
Expand Down
21 changes: 21 additions & 0 deletions src/NoFrixion.MoneyMoov/Models/Account/AccountIdentifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,27 @@ public string BitcoinAddress
? $"{SortCode[..2]}-{SortCode.Substring(2, 2)}-{SortCode.Substring(4, 2)} {AccountNumber}"
: "No identifier.";

public bool IsSameDestination(AccountIdentifier other)
{
if(other == null)
{
return false;
}

if (Type != other.Type)
{
return false;
}

return Type switch
{
AccountIdentifierType.IBAN => IBAN == other.IBAN,
AccountIdentifierType.SCAN => SortCode == other.SortCode && AccountNumber == other.AccountNumber,
AccountIdentifierType.BTC => BitcoinAddress == other.BitcoinAddress,
_ => false
};
}

public virtual Dictionary<string, string> ToDictionary(string keyPrefix)
{
return new Dictionary<string, string>
Expand Down
11 changes: 8 additions & 3 deletions src/NoFrixion.MoneyMoov/Models/Rules/RuleActions/SweepAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ public Dictionary<string, string> ToDictionary(string keyPrefix)
{
var dict = new Dictionary<string, string>
{
//{ keyPrefix + nameof(Priority), Priority.ToString() },
//{ keyPrefix + nameof(ActionType), ActionType.ToString() },
{ keyPrefix + nameof(AmountToLeave), AmountToLeave.ToString() },
{ keyPrefix + nameof(MinimumAmountToRunAt), MinimumAmountToRunAt.ToString() },
{ keyPrefix + nameof(PayoutYourReference), PayoutYourReference ?? string.Empty },
Expand Down Expand Up @@ -122,7 +120,14 @@ public IEnumerable<ValidationResult> Validate(ValidationContext validationContex
{
foreach (var err in dest.Validate(validationContext))
{
yield return err;
if (err.MemberNames == null || !err.MemberNames.Any())
{
yield return new ValidationResult(err.ErrorMessage, new List<string> { nameof(Destinations) });
}
else
{
yield return err;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,6 @@ public override IEnumerable<ValidationResult> Validate(ValidationContext validat
new string[] { nameof(SweepAmount), nameof(SweepPercentage) });
}

if(Identifier== null)
{
yield return new ValidationResult($"The destination identifier details must be set for a sweep destination.",
new string[] { nameof(Identifier) });
}

if (Identifier?.Currency == CurrencyTypeEnum.None)
{
yield return new ValidationResult($"The destination identifier currency must be set.",
Expand Down
53 changes: 48 additions & 5 deletions src/NoFrixion.MoneyMoov/Models/Transaction/Counterparty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,43 @@ public class Counterparty
public string Summary
=> Name + (Identifier != null ? ", " + Identifier.Summary : string.Empty);

public bool IsAccountIDSet() => AccountID != null && AccountID != Guid.Empty;

public bool IsBeneficiaryIDSet() => BeneficiaryID != null && BeneficiaryID != Guid.Empty;

public bool IsIdentifierSet() => Identifier != null && (
!string.IsNullOrEmpty(Identifier.IBAN) ||
(!string.IsNullOrEmpty(Identifier.AccountNumber) && !string.IsNullOrEmpty(Identifier.SortCode)) ||
!string.IsNullOrEmpty(Identifier.BitcoinAddress));

private int DestinationsSetCount() =>
(IsAccountIDSet() ? 1 : 0) + (IsBeneficiaryIDSet() ? 1 : 0) + (IsIdentifierSet() ? 1 : 0);

public bool IsSameDestination(Counterparty? other)
{
if (other == null)
{
return false;
}

if (IsAccountIDSet() && other.IsAccountIDSet())
{
return AccountID == other.AccountID;
}

if (IsBeneficiaryIDSet() && other.IsBeneficiaryIDSet())
{
return BeneficiaryID == other.BeneficiaryID;
}

if (IsIdentifierSet() && other.IsIdentifierSet())
{
return Identifier!.IsSameDestination(other.Identifier);
}

return false;
}

public virtual Dictionary<string, string> ToDictionary(string keyPrefix)
{
var dict = new Dictionary<string, string>
Expand Down Expand Up @@ -100,16 +137,22 @@ public virtual string GetApprovalHash()

public virtual IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (AccountID == null && BeneficiaryID == null && Identifier == null)
if (DestinationsSetCount() == 0)
{
yield return new ValidationResult($"One of the destination options (AccountID, BeneficiaryID or Identifier) must be set for a counterparty.",
null);
}

if (DestinationsSetCount() > 1)
{
yield return new ValidationResult($"One of the desintation options (AccountID, BeneficiaryID or Identifier) must be set for a counterparty.",
new string[] { nameof(Identifier) });
yield return new ValidationResult($"Only one of the destination options (AccountID, BeneficiaryID or Identifier) can be set for a counterparty.",
null);
}

if (Identifier != null && Identifier.Type == AccountIdentifierType.Unknown)
if (IsIdentifierSet() && Identifier?.Type == AccountIdentifierType.Unknown)
{
yield return new ValidationResult($"The counterparty identifier must have either an IBAN or account number and sort code set.",
new string[] { nameof(Identifier) });
null);
}
}

Expand Down

0 comments on commit 34fde54

Please sign in to comment.