Skip to content

Commit

Permalink
Update: Phone number is optional when unblocking the provider (#1246)
Browse files Browse the repository at this point in the history
* Added the RequiredIfAttribute class for property validation

* Made phone number optional for provider unblocking.

* Moved BlockPhoneNumber field from ProviderBaseDto to the ProviderDto model.

* Removed all validations in BlockPhoneNumber.

* Added BlockPhoneNumber mapping ignoring
  • Loading branch information
VitaliyYuras authored Sep 25, 2023
1 parent a4f7456 commit 7aa31d1
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,6 @@ public class ProviderBaseDto

public bool IsBlocked { get; set; }

[DataType(DataType.PhoneNumber)]
[RegularExpression(
Constants.PhoneNumberRegexModel,
ErrorMessage = Constants.PhoneErrorMessage)]
[DisplayFormat(DataFormatString = Constants.PhoneNumberFormat)]
[MaxLength(Constants.UnifiedPhoneLength)]
[Required(ErrorMessage = "PhoneNumber is required")]
public string BlockPhoneNumber { get; set; } = string.Empty;

[MaxLength(500)]
public string BlockReason { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.ComponentModel.DataAnnotations;

using OutOfSchool.WebApi.Validators;
namespace OutOfSchool.WebApi.Models.Providers;

public class ProviderBlockDto
Expand All @@ -16,7 +16,7 @@ public class ProviderBlockDto
ErrorMessage = Constants.PhoneErrorMessage)]
[DisplayFormat(DataFormatString = Constants.PhoneNumberFormat)]
[MaxLength(Constants.UnifiedPhoneLength)]
[Required(ErrorMessage = "PhoneNumber is required")]
[RequiredIf("IsBlocked", true, ErrorMessage = "PhoneNumber is required")]
public string BlockPhoneNumber { get; set; } = string.Empty;

[MaxLength(500)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public class ProviderDto : ProviderBaseDto
[Required]
[EnumDataType(typeof(OwnershipType), ErrorMessage = Constants.EnumErrorMessage)]
public OwnershipType Ownership { get; set; }

public string BlockPhoneNumber { get; set; } = string.Empty;
}
2 changes: 1 addition & 1 deletion OutOfSchool/OutOfSchool.WebApi/Services/ProviderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public async Task<ResponseDto> Block(ProviderBlockDto providerBlockDto, string t

provider.IsBlocked = providerBlockDto.IsBlocked;
provider.BlockReason = providerBlockDto.IsBlocked ? providerBlockDto.BlockReason : null;
provider.BlockPhoneNumber = providerBlockDto.IsBlocked ? providerBlockDto.BlockPhoneNumber : null;
provider.BlockPhoneNumber = providerBlockDto.IsBlocked ? providerBlockDto.BlockPhoneNumber : string.Empty;

await providerRepository.RunInTransaction(async () =>
{
Expand Down
3 changes: 2 additions & 1 deletion OutOfSchool/OutOfSchool.WebApi/Util/MappingProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ public MappingProfile()
.ForMember(dest => dest.Status, opt => opt.Ignore())
.ForMember(dest => dest.StatusReason, opt => opt.Ignore())
.ForMember(dest => dest.LicenseStatus, opt => opt.Ignore())
.ForMember(dest => dest.ProviderAdmins, opt => opt.Ignore());
.ForMember(dest => dest.ProviderAdmins, opt => opt.Ignore())
.ForMember(dest => dest.BlockPhoneNumber, opt => opt.Ignore());

CreateMap<Provider, ProviderUpdateDto>()
.ForMember(dest => dest.ActualAddress, opt => opt.MapFrom(src => src.ActualAddress))
Expand Down
38 changes: 38 additions & 0 deletions OutOfSchool/OutOfSchool.WebApi/Validators/RequiredIfAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.ComponentModel.DataAnnotations;

namespace OutOfSchool.WebApi.Validators;

public class RequiredIfAttribute : ValidationAttribute
{
private readonly string otherProperty;
private readonly bool requiredValue;

public RequiredIfAttribute(string otherProperty, bool requiredValue)
{
this.otherProperty = otherProperty;
this.requiredValue = requiredValue;
}

protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var propertyInfo = validationContext.ObjectType.GetProperty(otherProperty);

if (propertyInfo == null)
{
return new ValidationResult($"Property {otherProperty} not found.");
}

var otherPropertyValue = propertyInfo.GetValue(validationContext.ObjectInstance);

if ((otherPropertyValue is bool otherValue) && otherValue == requiredValue)
{
if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
{
return new ValidationResult(ErrorMessage);
}
}

return ValidationResult.Success;
}
}

0 comments on commit 7aa31d1

Please sign in to comment.