25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
3.3KB

  1. using Campaign_Tracker.Server.LegacyData;
  2. namespace Campaign_Tracker.Server.ExtensionData;
  3. /// <summary>
  4. /// Validates a <see cref="LegacyLinkReference"/> by resolving it through the
  5. /// anti-corruption layer (<see cref="ILegacyDataAccess"/>).
  6. /// Each link type maps to a unique primary-key lookup, guaranteeing no ambiguity (AC #2, AC #3).
  7. /// </summary>
  8. public sealed class LegacyLinkValidator : ILegacyLinkValidator
  9. {
  10. private readonly ILegacyDataAccess _legacyData;
  11. public LegacyLinkValidator(ILegacyDataAccess legacyData)
  12. {
  13. _legacyData = legacyData;
  14. }
  15. public async Task<LegacyLinkValidationResult> ValidateAsync(
  16. LegacyLinkReference reference,
  17. CancellationToken cancellationToken = default)
  18. {
  19. return reference.Type switch
  20. {
  21. LegacyLinkType.JurisdictionJCode => await ValidateJurisdictionAsync(reference.Value, cancellationToken),
  22. LegacyLinkType.KitId => await ValidateKitAsync(reference.Value, cancellationToken),
  23. LegacyLinkType.ContactId => await ValidateContactAsync(reference.Value, cancellationToken),
  24. _ => LegacyLinkValidationResult.Failure($"Unknown legacy link type '{reference.Type}'."),
  25. };
  26. }
  27. private async Task<LegacyLinkValidationResult> ValidateJurisdictionAsync(
  28. string jCode, CancellationToken cancellationToken)
  29. {
  30. if (string.IsNullOrWhiteSpace(jCode))
  31. return LegacyLinkValidationResult.Failure("JCode is required and cannot be blank.");
  32. var record = await _legacyData.GetJurisdictionAsync(jCode, cancellationToken);
  33. return record is null
  34. ? LegacyLinkValidationResult.Failure(
  35. $"No legacy jurisdiction found for JCode '{jCode}'. Verify the identifier and try again.")
  36. : LegacyLinkValidationResult.Success();
  37. }
  38. private async Task<LegacyLinkValidationResult> ValidateKitAsync(
  39. string rawId, CancellationToken cancellationToken)
  40. {
  41. if (!int.TryParse(rawId, System.Globalization.NumberStyles.Integer,
  42. System.Globalization.CultureInfo.InvariantCulture, out var id))
  43. return LegacyLinkValidationResult.Failure(
  44. $"Kit identifier '{rawId}' is not a valid integer ID.");
  45. var record = await _legacyData.GetKitByIdAsync(id, cancellationToken);
  46. return record is null
  47. ? LegacyLinkValidationResult.Failure(
  48. $"No legacy kit found for ID {id}. Verify the identifier and try again.")
  49. : LegacyLinkValidationResult.Success();
  50. }
  51. private async Task<LegacyLinkValidationResult> ValidateContactAsync(
  52. string rawId, CancellationToken cancellationToken)
  53. {
  54. if (!int.TryParse(rawId, System.Globalization.NumberStyles.Integer,
  55. System.Globalization.CultureInfo.InvariantCulture, out var id))
  56. return LegacyLinkValidationResult.Failure(
  57. $"Contact identifier '{rawId}' is not a valid integer ID.");
  58. var record = await _legacyData.GetContactByIdAsync(id, cancellationToken);
  59. return record is null
  60. ? LegacyLinkValidationResult.Failure(
  61. $"No legacy contact found for ID {id}. Verify the identifier and try again.")
  62. : LegacyLinkValidationResult.Success();
  63. }
  64. }

Powered by TurnKey Linux.