Skip to content

Commit

Permalink
Fix sonar warnings for Admin for AB#16872.
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianMaki committed Jan 4, 2025
1 parent cee4da2 commit 727ecb1
Show file tree
Hide file tree
Showing 36 changed files with 224 additions and 239 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace HealthGateway.Admin.Client.Components.Communications;

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Fluxor;
using Fluxor.Blazor.Web.Components;
Expand Down Expand Up @@ -47,21 +48,18 @@ public partial class BroadcastDialog : FluxorComponent
BroadcastActionType.ExternalLink,
];

[SuppressMessage("Style", "IDE0072:Populate switch", Justification = "Team decision")]
private Func<string, string?> ValidateActionUrl =>
urlString =>
{
switch (this.Broadcast.ActionType)
return this.Broadcast.ActionType switch
{
case BroadcastActionType.InternalLink:
case BroadcastActionType.ExternalLink:
return urlString.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase)
&& Uri.TryCreate(this.ActionUrlString, UriKind.Absolute, out Uri? _)
? null
: "URL is invalid";
case BroadcastActionType.None:
default:
return urlString.Length == 0 ? null : "Selected Action Type does not support Action URL";
}
BroadcastActionType.InternalLink or BroadcastActionType.ExternalLink => urlString.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase)
&& Uri.TryCreate(this.ActionUrlString, UriKind.Absolute, out Uri? _)
? null
: "URL is invalid",
_ => urlString.Length == 0 ? null : "Selected Action Type does not support Action URL",
};
};

[CascadingParameter]
Expand Down Expand Up @@ -149,14 +147,10 @@ private void RetrieveFormValues()
? null
: (this.ExpiryDate.Value + this.ExpiryTime.Value).ToUniversalTime();

if (this.ActionUrlString.Length > 0 && Uri.TryCreate(this.ActionUrlString, UriKind.Absolute, out Uri? result))
{
this.Broadcast.ActionUrl = result;
}
else
{
this.Broadcast.ActionUrl = null;
}
this.Broadcast.ActionUrl = this.ActionUrlString.Length > 0 &&
Uri.TryCreate(this.ActionUrlString, UriKind.Absolute, out Uri? result)
? result
: null;
}

private void HandleClickCancel()
Expand Down
10 changes: 5 additions & 5 deletions Apps/Admin/Client/Components/Delegation/DelegateTable.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace HealthGateway.Admin.Client.Components.Delegation
{
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Fluxor;
using Fluxor.Blazor.Web.Components;
Expand Down Expand Up @@ -49,6 +50,7 @@ public partial class DelegateTable : FluxorComponent

private IEnumerable<DelegateRow> Rows => this.Data.Select(d => new DelegateRow(d));

[SuppressMessage("Style", "IDE0072:Populate switch", Justification = "Team decision")]
private static Color GetStatusColor(DelegationStatus status)
{
return status switch
Expand All @@ -73,11 +75,9 @@ public DelegateRow(ExtendedDelegateInfo model)
this.DateOfBirth = model.Birthdate;
this.PersonalHealthNumber = model.PersonalHealthNumber;
this.Address = AddressUtility.GetAddressAsSingleLine(model.PhysicalAddress ?? model.PostalAddress);
this.DelegationStatus = model.DelegationStatus switch
{
DelegationStatus.Unknown => DelegationStatus.Allowed,
_ => model.DelegationStatus,
};
this.DelegationStatus = model.DelegationStatus == DelegationStatus.Unknown
? DelegationStatus.Allowed
: model.DelegationStatus;
this.ToBeRemoved = model.StagedDelegationStatus == DelegationStatus.Disallowed;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public partial class AddressConfirmationDialog<TErrorAction, TSuccessAction> : F

private IMask? PostalCodeMask { get; set; }

[SuppressMessage("Style", "IDE0072:Populate switch", Justification = "Team decision")]
private string Country
{
get => this.country;
Expand Down Expand Up @@ -188,6 +189,16 @@ private static string GetCountryNameFromInput(string input)
return string.Empty;
}

private static string? ValidateCanadianPostalCode(string postalCode)
{
return !AddressUtility.PostalCodeRegex().IsMatch(postalCode) ? "Incomplete postal code" : null;
}

private static string? ValidateUsPostalCode(string postalCode)
{
return !AddressUtility.ZipCodeRegex().IsMatch(postalCode) ? "Incomplete zip code" : null;
}

private void PopulateInputs(Address address)
{
this.AddressLines = string.Join(Environment.NewLine, address.StreetLines);
Expand All @@ -211,14 +222,16 @@ private void PopulateInputs(Address address)

private string GetCountryToOutput()
{
if (this.SelectedCountryCode == CountryCode.CA && this.OutputCanadaAsEmptyString)
{
return string.Empty;
}
string? canadaOutput = this.SelectedCountryCode == CountryCode.CA && this.OutputCanadaAsEmptyString
? string.Empty
: null;

return this.OutputCountryCodeFormat ? this.SelectedCountryCode.ToString() : AddressUtility.GetCountryName(this.SelectedCountryCode);
return canadaOutput ?? (this.OutputCountryCodeFormat
? this.SelectedCountryCode.ToString()
: AddressUtility.GetCountryName(this.SelectedCountryCode));
}

[SuppressMessage("Style", "IDE0072:Populate switch", Justification = "Team decision")]
private Address GetAddressModel()
{
return new()
Expand All @@ -245,22 +258,17 @@ private void HandleCountryChanged(string value)
this.OtherState = string.Empty;
}

[SuppressMessage("Style", "IDE0072:Populate switch", Justification = "Team decision")]
private string? ValidatePostalCode(string postalCode)
{
if (string.IsNullOrWhiteSpace(postalCode))
{
return "Required";
}

switch (this.SelectedCountryCode)
{
case CountryCode.CA:
return !AddressUtility.PostalCodeRegex().IsMatch(postalCode) ? "Incomplete postal code" : null;
case CountryCode.US:
return !AddressUtility.ZipCodeRegex().IsMatch(postalCode) ? "Incomplete zip code" : null;
default:
return null;
}
return string.IsNullOrWhiteSpace(postalCode)
? "Required"
: this.SelectedCountryCode switch
{
CountryCode.CA => ValidateCanadianPostalCode(postalCode),
CountryCode.US => ValidateUsPostalCode(postalCode),
_ => null,
};
}

private void HandleActionSuccess(TSuccessAction successAction)
Expand Down
16 changes: 6 additions & 10 deletions Apps/Admin/Client/Pages/AgentAccessPage.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,13 @@ public partial class AgentAccessPage : FluxorComponent

private static Func<string, string?> ValidateQueryParameter => parameter =>
{
if (string.IsNullOrWhiteSpace(parameter))
{
return "Search parameter is required";
}

if (StringManipulator.StripWhitespace(parameter).Length < 3)
{
return "Query must contain at least 3 characters";
}
string? lengthValidationResult = StringManipulator.StripWhitespace(parameter).Length < 3
? "Query must contain at least 3 characters"
: null;

return null;
return string.IsNullOrWhiteSpace(parameter)
? "Search parameter is required"
: lengthValidationResult;
};

[Inject]
Expand Down
16 changes: 6 additions & 10 deletions Apps/Admin/Client/Pages/BetaAccessPage.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,13 @@ public partial class BetaAccessPage : FluxorComponent
{
private static Func<string, string?> ValidateQueryParameter => parameter =>
{
if (string.IsNullOrWhiteSpace(parameter))
{
return "Email is required";
}

if (!NaiveEmailValidator.IsValid(StringManipulator.StripWhitespace(parameter)))
{
return "Invalid email format";
}
string? emailValidationResult = !NaiveEmailValidator.IsValid(StringManipulator.StripWhitespace(parameter))
? "Invalid email format"
: null;

return null;
return string.IsNullOrWhiteSpace(parameter)
? "Email is required"
: emailValidationResult;
};

[Inject]
Expand Down
31 changes: 13 additions & 18 deletions Apps/Admin/Client/Pages/DashboardPage.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,24 +134,19 @@ private int UniqueDays
}
}

private IEnumerable<DailyDataRow> TableData
{
get
{
return this.DailyUsageCounts.UserRegistrations.Select(x => new DailyDataRow { Date = x.Key, UserRegistrations = x.Value })
.Concat(this.DailyUsageCounts.UserLogins.Select(x => new DailyDataRow { Date = x.Key, UserLogins = x.Value }))
.Concat(this.DailyUsageCounts.DependentRegistrations.Select(x => new DailyDataRow { Date = x.Key, DependentRegistrations = x.Value }))
.GroupBy(x => x.Date)
.Select(
g => new DailyDataRow
{
Date = g.Key,
UserRegistrations = g.Sum(x => x.UserRegistrations),
UserLogins = g.Sum(x => x.UserLogins),
DependentRegistrations = g.Sum(x => x.DependentRegistrations),
});
}
}
private IEnumerable<DailyDataRow> TableData =>
this.DailyUsageCounts.UserRegistrations.Select(x => new DailyDataRow { Date = x.Key, UserRegistrations = x.Value })
.Concat(this.DailyUsageCounts.UserLogins.Select(x => new DailyDataRow { Date = x.Key, UserLogins = x.Value }))
.Concat(this.DailyUsageCounts.DependentRegistrations.Select(x => new DailyDataRow { Date = x.Key, DependentRegistrations = x.Value }))
.GroupBy(x => x.Date)
.Select(
g => new DailyDataRow
{
Date = g.Key,
UserRegistrations = g.Sum(x => x.UserRegistrations),
UserLogins = g.Sum(x => x.UserLogins),
DependentRegistrations = g.Sum(x => x.DependentRegistrations),
});

/// <inheritdoc/>
protected override void OnInitialized()
Expand Down
16 changes: 6 additions & 10 deletions Apps/Admin/Client/Pages/DelegationPage.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,13 @@ public partial class DelegationPage : FluxorComponent

private static Func<string, string?> ValidateQueryParameter => parameter =>
{
if (string.IsNullOrWhiteSpace(parameter))
{
return "PHN is required";
}

if (!PhnValidator.Validate(StringManipulator.StripWhitespace(parameter)).IsValid)
{
return "Invalid PHN";
}
string? phnValidationResult = !PhnValidator.Validate(StringManipulator.StripWhitespace(parameter)).IsValid
? "Invalid PHN"
: null;

return null;
return string.IsNullOrWhiteSpace(parameter)
? "PHN is required"
: phnValidationResult;
};

[Inject]
Expand Down
46 changes: 28 additions & 18 deletions Apps/Admin/Client/Pages/SupportPage.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace HealthGateway.Admin.Client.Pages
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;
using Fluxor;
Expand Down Expand Up @@ -109,24 +110,8 @@ private PatientQueryType SelectedQueryType

private Func<string, string?> ValidateQueryParameter => parameter =>
{
if (string.IsNullOrWhiteSpace(parameter))
{
return "Search parameter is required";
}

if (this.PhnOrDependentSelected && !PhnValidator.Validate(StringManipulator.StripWhitespace(parameter)).IsValid)
{
return "Invalid PHN";
}

return this.SelectedQueryType switch
{
PatientQueryType.Email or PatientQueryType.Sms when StringManipulator.StripWhitespace(parameter).Length < 5
=> "Email/SMS must be minimum 5 characters",
PatientQueryType.Sms when !StringManipulator.IsPositiveNumeric(parameter)
=> "SMS must contain digits only",
_ => null,
};
string? result = ValidateParameterInput(parameter) ?? ValidatePhn(parameter, this.PhnOrDependentSelected);
return result ?? ValidateQueryType(parameter, this.SelectedQueryType);
};

private AuthenticationState? AuthenticationState { get; set; }
Expand Down Expand Up @@ -167,6 +152,31 @@ protected override async ValueTask DisposeAsyncCore(bool disposing)
await base.DisposeAsyncCore(disposing);
}

private static string? ValidateParameterInput(string parameter)
{
return string.IsNullOrWhiteSpace(parameter) ? "Search parameter is required" : null;
}

private static string? ValidatePhn(string parameter, bool phnOrDependentSelected)
{
return phnOrDependentSelected && !PhnValidator.Validate(StringManipulator.StripWhitespace(parameter)).IsValid
? "Invalid PHN"
: null;
}

[SuppressMessage("Style", "IDE0072:Populate switch", Justification = "Team decision")]
private static string? ValidateQueryType(string parameter, PatientQueryType selectedQueryType)
{
return selectedQueryType switch
{
PatientQueryType.Email or PatientQueryType.Sms when StringManipulator.StripWhitespace(parameter).Length < 5
=> "Email/SMS must be minimum 5 characters",
PatientQueryType.Sms when !StringManipulator.IsPositiveNumeric(parameter)
=> "SMS must contain digits only",
_ => null,
};
}

private void Clear()
{
this.Dispatcher.Dispatch(new PatientSupportActions.ResetStateAction());
Expand Down
18 changes: 7 additions & 11 deletions Apps/Admin/Client/Services/DateConversionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,19 @@ public DateTime ConvertFromUtc(DateTime utcDateTime)
/// <inheritdoc/>
public DateTime? ConvertFromUtc(DateTime? utcDateTime, bool returnNowIfNull = false)
{
if (utcDateTime != null)
{
return this.ConvertFromUtc(utcDateTime.Value);
}

if (returnNowIfNull)
{
return this.ConvertFromUtc(DateTime.UtcNow);
}

return null;
DateTime? dateTimeToConvert = GetDateTimeToConvert(utcDateTime, returnNowIfNull);
return dateTimeToConvert.HasValue ? this.ConvertFromUtc(dateTimeToConvert.Value) : null;
}

/// <inheritdoc/>
public string ConvertToShortFormatFromUtc(DateTime? utcDateTime, string fallbackString = "-")
{
return utcDateTime == null ? fallbackString : DateFormatter.ToShortDateAndTime(this.ConvertFromUtc(utcDateTime.Value));
}

private static DateTime? GetDateTimeToConvert(DateTime? utcDateTime, bool returnNowIfNull)
{
return utcDateTime ?? (returnNowIfNull ? DateTime.UtcNow : null);
}
}
}
2 changes: 2 additions & 0 deletions Apps/Admin/Client/Utils/BreakpointUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// -------------------------------------------------------------------------
namespace HealthGateway.Admin.Client.Utils
{
using System.Diagnostics.CodeAnalysis;
using MudBlazor;

/// <summary>
Expand Down Expand Up @@ -59,6 +60,7 @@ public static class BreakpointUtility
/// A string containing the override code for the breakpoint or null if the breakpoint has no associated override
/// code.
/// </returns>
[SuppressMessage("Style", "IDE0072:Populate switch", Justification = "Team decision")]
public static string? GetOverrideCode(this Breakpoint breakpoint)
{
return breakpoint switch
Expand Down
Loading

0 comments on commit 727ecb1

Please sign in to comment.