Skip to content

Commit

Permalink
Breaking a large method into smaller, more focused private methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmed-Ghanam committed Nov 28, 2024
1 parent dfb5241 commit 52b12b3
Showing 1 changed file with 91 additions and 27 deletions.
118 changes: 91 additions & 27 deletions src/Altinn.Notifications.Core/Services/KeywordsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,33 @@ public KeywordsService(IRegisterClient registerClient)
}

/// <inheritdoc/>
/// <summary>
/// Checks whether the specified string contains the placeholder keyword <c>$recipientName$</c>.
/// </summary>
/// <param name="value">The string to check.</param>
/// <returns><c>true</c> if the specified string contains the placeholder keyword <c>$recipientName$</c>; otherwise, <c>false</c>.</returns>
public bool ContainsRecipientNamePlaceholder(string? value)
{
return !string.IsNullOrWhiteSpace(value) && value.Contains(_recipientNamePlaceholder);
}

/// <inheritdoc/>
/// <summary>
/// Checks whether the specified string contains the placeholder keyword <c>$recipientNumber$</c>.
/// </summary>
/// <param name="value">The string to check.</param>
/// <returns><c>true</c> if the specified string contains the placeholder keyword <c>$recipientNumber$</c>; otherwise, <c>false</c>.</returns>
public bool ContainsRecipientNumberPlaceholder(string? value)
{
return !string.IsNullOrWhiteSpace(value) && value.Contains(_recipientNumberPlaceholder);
}

/// <inheritdoc/>
/// <summary>
/// Replaces placeholder keywords in an <see cref="SmsRecipient"/> with actual values.
/// </summary>
/// <param name="smsRecipient">The <see cref="SmsRecipient"/> to process.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the <see cref="SmsRecipient"/> with the placeholder keywords replaced by actual values.</returns>
public async Task<SmsRecipient> ReplaceKeywordsAsync(SmsRecipient smsRecipient)
{
ArgumentNullException.ThrowIfNull(smsRecipient);
Expand All @@ -47,6 +62,11 @@ public async Task<SmsRecipient> ReplaceKeywordsAsync(SmsRecipient smsRecipient)
}

/// <inheritdoc/>
/// <summary>
/// Replaces placeholder keywords in an <see cref="EmailRecipient"/> with actual values.
/// </summary>
/// <param name="emailRecipient">The <see cref="EmailRecipient"/> to process.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the <see cref="EmailRecipient"/> with the placeholder keywords replaced by actual values.</returns>
public async Task<EmailRecipient> ReplaceKeywordsAsync(EmailRecipient emailRecipient)
{
ArgumentNullException.ThrowIfNull(emailRecipient);
Expand All @@ -67,47 +87,91 @@ public async Task<EmailRecipient> ReplaceKeywordsAsync(EmailRecipient emailRecip
/// <param name="nationalIdentityNumberGetter">A function to get the national identity number of the recipient.</param>
/// <param name="organizationNumberGetter">A function to get the organization number of the recipient.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the processed recipient.</returns>
private async Task<T> ReplaceKeywordsAsync<T>(T recipient, Func<T, string?> getBody, Action<T, string?> setBody, Func<T, string?> nationalIdentityNumberGetter, Func<T, string?> organizationNumberGetter)
private async Task<T> ReplaceKeywordsAsync<T>(
T recipient,
Func<T, string?> getBody,
Action<T, string?> setBody,
Func<T, string?> nationalIdentityNumberGetter,
Func<T, string?> organizationNumberGetter)
{
if (ContainsRecipientNamePlaceholder(getBody(recipient)))
{
var nationalIdentityNumber = nationalIdentityNumberGetter(recipient);
if (!string.IsNullOrWhiteSpace(nationalIdentityNumber))
{
var partyDetails = await _registerClient.GetPartyDetailsForPersons([nationalIdentityNumber]);
if (partyDetails != null && partyDetails.Count > 0)
{
setBody(recipient, getBody(recipient)?.Replace(_recipientNamePlaceholder, partyDetails[0]?.Name ?? string.Empty));
}
}

var organizationNumber = organizationNumberGetter(recipient);
if (!string.IsNullOrWhiteSpace(organizationNumber))
{
var partyDetails = await _registerClient.GetPartyDetailsForOrganizations([organizationNumber]);
if (partyDetails != null && partyDetails.Count > 0)
{
setBody(recipient, getBody(recipient)?.Replace(_recipientNamePlaceholder, partyDetails[0]?.Name ?? string.Empty));
}
}
await ReplaceRecipientNamePlaceholderAsync(recipient, getBody, setBody, nationalIdentityNumberGetter, organizationNumberGetter);
}

if (ContainsRecipientNumberPlaceholder(getBody(recipient)))
{
var nationalIdentityNumber = nationalIdentityNumberGetter(recipient);
if (!string.IsNullOrWhiteSpace(nationalIdentityNumber))
ReplaceRecipientNumberPlaceholder(recipient, getBody, setBody, nationalIdentityNumberGetter, organizationNumberGetter);
}

return recipient;
}

/// <summary>
/// Replaces the recipient name placeholder with the actual recipient name.
/// </summary>
/// <typeparam name="T">The type of the recipient.</typeparam>
/// <param name="recipient">The recipient to process.</param>
/// <param name="getBody">A function to get the body of the recipient.</param>
/// <param name="setBody">A function to set the body of the recipient.</param>
/// <param name="nationalIdentityNumberGetter">A function to get the national identity number of the recipient.</param>
/// <param name="organizationNumberGetter">A function to get the organization number of the recipient.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
private async Task ReplaceRecipientNamePlaceholderAsync<T>(
T recipient,
Func<T, string?> getBody,
Action<T, string?> setBody,
Func<T, string?> nationalIdentityNumberGetter,
Func<T, string?> organizationNumberGetter)
{
var nationalIdentityNumber = nationalIdentityNumberGetter(recipient);
if (!string.IsNullOrWhiteSpace(nationalIdentityNumber))
{
var partyDetails = await _registerClient.GetPartyDetailsForPersons(new List<string> { nationalIdentityNumber });
if (partyDetails != null && partyDetails.Count > 0)
{
setBody(recipient, getBody(recipient)?.Replace(_recipientNumberPlaceholder, nationalIdentityNumber));
setBody(recipient, getBody(recipient)?.Replace(_recipientNamePlaceholder, partyDetails[0]?.Name ?? string.Empty));
}
}

var organizationNumber = organizationNumberGetter(recipient);
if (!string.IsNullOrWhiteSpace(organizationNumber))
var organizationNumber = organizationNumberGetter(recipient);
if (!string.IsNullOrWhiteSpace(organizationNumber))
{
var partyDetails = await _registerClient.GetPartyDetailsForOrganizations(new List<string> { organizationNumber });
if (partyDetails != null && partyDetails.Count > 0)
{
setBody(recipient, getBody(recipient)?.Replace(_recipientNumberPlaceholder, organizationNumber));
setBody(recipient, getBody(recipient)?.Replace(_recipientNamePlaceholder, partyDetails[0]?.Name ?? string.Empty));
}
}
}

return recipient;
/// <summary>
/// Replaces the recipient number placeholder with the actual recipient number.
/// </summary>
/// <typeparam name="T">The type of the recipient.</typeparam>
/// <param name="recipient">The recipient to process.</param>
/// <param name="getBody">A function to get the body of the recipient.</param>
/// <param name="setBody">A function to set the body of the recipient.</param>
/// <param name="nationalIdentityNumberGetter">A function to get the national identity number of the recipient.</param>
/// <param name="organizationNumberGetter">A function to get the organization number of the recipient.</param>
private void ReplaceRecipientNumberPlaceholder<T>(
T recipient,
Func<T, string?> getBody,
Action<T, string?> setBody,
Func<T, string?> nationalIdentityNumberGetter,
Func<T, string?> organizationNumberGetter)
{
var nationalIdentityNumber = nationalIdentityNumberGetter(recipient);
if (!string.IsNullOrWhiteSpace(nationalIdentityNumber))
{
setBody(recipient, getBody(recipient)?.Replace(_recipientNumberPlaceholder, nationalIdentityNumber));
}

var organizationNumber = organizationNumberGetter(recipient);
if (!string.IsNullOrWhiteSpace(organizationNumber))
{
setBody(recipient, getBody(recipient)?.Replace(_recipientNumberPlaceholder, organizationNumber));
}
}
}
}

0 comments on commit 52b12b3

Please sign in to comment.