Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: Phone number is optional when unblocking the provider #1246

Merged
merged 5 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}

Loading