Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Commit

Permalink
Fixed SMS sending. (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
Utar94 authored May 7, 2024
1 parent 189461b commit ecac279
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private async Task<SignInCommandResult> HandleCredentialsAsync(Credentials crede
};
SentMessages sentMessages = user == null
? await _messageService.SendAsync(Templates.AccountAuthentication, email, locale, variables, cancellationToken)
: await _messageService.SendAsync(Templates.AccountAuthentication, user, locale, variables, cancellationToken);
: await _messageService.SendAsync(Templates.AccountAuthentication, user, ContactType.Email, locale, variables, cancellationToken);
SentMessage sentMessage = sentMessages.ToSentMessage(email);
return SignInCommandResult.AuthenticationLinkSent(sentMessage);
}
Expand Down Expand Up @@ -122,7 +122,7 @@ private async Task<SignInCommandResult> SendMultiFactorAuthenticationMessageAsyn
["OneTimePassword"] = oneTimePassword.Password
};
string template = Templates.GetMultiFactorAuthentication(contactType);
SentMessages sentMessages = await _messageService.SendAsync(template, user, locale, variables, cancellationToken);
SentMessages sentMessages = await _messageService.SendAsync(template, user, contactType, locale, variables, cancellationToken);
SentMessage sentMessage = sentMessages.ToSentMessage(contact);
return SignInCommandResult.RequireOneTimePasswordValidation(oneTimePassword, sentMessage);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Logitar.Portal.Contracts.Messages;
using Logitar.Master.Contracts.Accounts;
using Logitar.Portal.Contracts.Messages;
using Logitar.Portal.Contracts.Users;

namespace Logitar.Master.Application.Accounts;
Expand All @@ -7,5 +8,5 @@ public interface IMessageService
{
Task<SentMessages> SendAsync(string template, Email email, string? locale = null, Dictionary<string, string>? variables = null, CancellationToken cancellationToken = default);
Task<SentMessages> SendAsync(string template, Phone phone, string? locale = null, Dictionary<string, string>? variables = null, CancellationToken cancellationToken = default);
Task<SentMessages> SendAsync(string template, User user, string? locale = null, Dictionary<string, string>? variables = null, CancellationToken cancellationToken = default);
Task<SentMessages> SendAsync(string template, User user, ContactType contactType, string? locale = null, Dictionary<string, string>? variables = null, CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
using Logitar.Master.Application.Accounts;
using Logitar.Master.Contracts.Accounts;
using Logitar.Portal.Contracts;
using Logitar.Portal.Contracts.Messages;
using Logitar.Portal.Contracts.Users;
using Microsoft.Extensions.Configuration;

namespace Logitar.Master.Infrastructure.IdentityServices;

internal class MessageService : IMessageService
{
private readonly IMessageClient _messageClient;
private readonly Guid _phoneSenderId;

public MessageService(IMessageClient messageClient)
public MessageService(IConfiguration configuration, IMessageClient messageClient)
{
_messageClient = messageClient;
_phoneSenderId = configuration.GetSection("Portal").GetValue<Guid?>("PhoneSenderId")
?? throw new ArgumentException("The configuration key 'Portal:PhoneSenderId' is required.", nameof(configuration));
}

public async Task<SentMessages> SendAsync(string template, Email email, string? locale, Dictionary<string, string>? variables, CancellationToken cancellationToken)
Expand All @@ -21,7 +26,7 @@ public async Task<SentMessages> SendAsync(string template, Email email, string?
Type = RecipientType.To,
Address = email.Address
};
return await SendAsync(template, recipient, locale, variables, cancellationToken);
return await SendAsync(template, recipient, ContactType.Email, locale, variables, cancellationToken);
}

public async Task<SentMessages> SendAsync(string template, Phone phone, string? locale, Dictionary<string, string>? variables, CancellationToken cancellationToken)
Expand All @@ -31,25 +36,29 @@ public async Task<SentMessages> SendAsync(string template, Phone phone, string?
Type = RecipientType.To,
PhoneNumber = phone.E164Formatted
};
return await SendAsync(template, recipient, locale, variables, cancellationToken);
return await SendAsync(template, recipient, ContactType.Phone, locale, variables, cancellationToken);
}

public async Task<SentMessages> SendAsync(string template, User user, string? locale, Dictionary<string, string>? variables, CancellationToken cancellationToken)
public async Task<SentMessages> SendAsync(string template, User user, ContactType contactType, string? locale, Dictionary<string, string>? variables, CancellationToken cancellationToken)
{
RecipientPayload recipient = new()
{
Type = RecipientType.To,
UserId = user.Id
};
return await SendAsync(template, recipient, locale, variables, cancellationToken);
return await SendAsync(template, recipient, contactType, locale, variables, cancellationToken);
}

private async Task<SentMessages> SendAsync(string template, RecipientPayload recipient, string? locale, IEnumerable<KeyValuePair<string, string>>? variables, CancellationToken cancellationToken)
private async Task<SentMessages> SendAsync(string template, RecipientPayload recipient, ContactType contactType, string? locale, IEnumerable<KeyValuePair<string, string>>? variables, CancellationToken cancellationToken)
{
SendMessagePayload payload = new(template)
{
Locale = locale
};
if (contactType == ContactType.Phone)
{
payload.SenderId = _phoneSenderId;
}
payload.Recipients.Add(recipient);
if (variables != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public async Task It_should_send_a_Multi_Factor_Authentication_email_message()
OneTimePasswordService.Setup(x => x.CreateAsync(user, "MultiFactorAuthentication", CancellationToken)).ReturnsAsync(oneTimePassword);

SentMessages sentMessages = new([Guid.NewGuid()]);
MessageService.Setup(x => x.SendAsync("MultiFactorAuthenticationEmail", user, payload.Locale,
MessageService.Setup(x => x.SendAsync("MultiFactorAuthenticationEmail", user, ContactType.Email, payload.Locale,
It.Is<Dictionary<string, string>>(v => v.Count == 1 && v["OneTimePassword"] == oneTimePassword.Password), CancellationToken)
).ReturnsAsync(sentMessages);

Expand Down Expand Up @@ -262,7 +262,7 @@ public async Task It_should_send_a_Multi_Factor_Authentication_Sms_message()
OneTimePasswordService.Setup(x => x.CreateAsync(user, "MultiFactorAuthentication", CancellationToken)).ReturnsAsync(oneTimePassword);

SentMessages sentMessages = new([Guid.NewGuid()]);
MessageService.Setup(x => x.SendAsync("MultiFactorAuthenticationPhone", user, payload.Locale,
MessageService.Setup(x => x.SendAsync("MultiFactorAuthenticationPhone", user, ContactType.Phone, payload.Locale,
It.Is<Dictionary<string, string>>(v => v.Count == 1 && v["OneTimePassword"] == oneTimePassword.Password), CancellationToken)
).ReturnsAsync(sentMessages);

Expand Down Expand Up @@ -320,7 +320,7 @@ public async Task It_should_send_an_authentication_link_when_the_user_does_not_h
};

SentMessages sentMessages = new([Guid.NewGuid()]);
MessageService.Setup(x => x.SendAsync("AccountAuthentication", user, payload.Locale,
MessageService.Setup(x => x.SendAsync("AccountAuthentication", user, ContactType.Email, payload.Locale,
It.Is<Dictionary<string, string>>(v => v.Count == 1 && v["Token"] == createdToken.Token), CancellationToken)
).ReturnsAsync(sentMessages);

Expand Down

0 comments on commit ecac279

Please sign in to comment.