Skip to content
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
@@ -1,18 +1,19 @@
namespace MessagingService.BusinessLogic.Services.EmailServices.Smtp2Go
{
using Models;
using Newtonsoft.Json;
using Service.Services.Email.Smtp2Go;
using Shared.General;
using Shared.Logger;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Models;
using Newtonsoft.Json;
using Service.Services.Email.Smtp2Go;
using Shared.General;
using Shared.Logger;

/// <summary>
///
Expand All @@ -32,10 +33,6 @@

#region Constructors

/// <summary>
/// Initializes a new instance of the <see cref="Smtp2GoProxy" /> class.
/// </summary>
/// <param name="httpClient">The HTTP client.</param>
public Smtp2GoProxy(HttpClient httpClient)
{
this.HttpClient = httpClient;
Expand All @@ -45,31 +42,18 @@

#region Methods

/// <summary>
/// Sends the email.
/// </summary>
/// <param name="messageId">The message identifier.</param>
/// <param name="fromAddress">From address.</param>
/// <param name="toAddresses">To addresses.</param>
/// <param name="subject">The subject.</param>
/// <param name="body">The body.</param>
/// <param name="isHtml">if set to <c>true</c> [is HTML].</param>
/// <param name="attachments">The attachments.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
public async Task<EmailServiceProxyResponse> SendEmail(Guid messageId,
String fromAddress,
List<String> toAddresses,
String subject,
String body,
Boolean isHtml,
List<EmailAttachment> attachments,
CancellationToken cancellationToken)
{
CancellationToken cancellationToken) {
EmailServiceProxyResponse response = null;

// Translate the request message
Smtp2GoSendEmailRequest apiRequest = new Smtp2GoSendEmailRequest
Smtp2GoSendEmailRequest apiRequest = new()
{
ApiKey = ConfigurationReader.GetValue("SMTP2GoAPIKey"),
HTMLBody = isHtml ? body : string.Empty,
Expand Down Expand Up @@ -100,19 +84,17 @@

Logger.LogDebug($"Request Message Sent to Email Provider [SMTP2Go] {requestSerialised}");

StringContent content = new StringContent(requestSerialised, Encoding.UTF8, "application/json");
StringContent content = new(requestSerialised, Encoding.UTF8, "application/json");

String requestUri = $"{ConfigurationReader.GetValue("SMTP2GoBaseAddress")}email/send";
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri);
requestMessage.Content = content;

HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(requestMessage, cancellationToken);

Smtp2GoSendEmailResponse apiResponse = new Smtp2GoSendEmailResponse(){
Data = new Smtp2GoSendEmailResponseData()
};
Smtp2GoSendEmailResponse apiResponse = new() { Data = new Smtp2GoSendEmailResponseData() };

Check warning on line 95 in MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoProxy.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoProxy.cs#L95

Remove this useless assignment to local variable 'apiResponse'.
if (httpResponse.IsSuccessStatusCode){
apiResponse = JsonConvert.DeserializeObject<Smtp2GoSendEmailResponse>(await httpResponse.Content.ReadAsStringAsync());
apiResponse = JsonConvert.DeserializeObject<Smtp2GoSendEmailResponse>(await httpResponse.Content.ReadAsStringAsync(cancellationToken));
}
else{
apiResponse = new Smtp2GoSendEmailResponse();
Expand All @@ -134,12 +116,7 @@
};
return response;
}

/// <summary>
/// Converts the type of the file.
/// </summary>
/// <param name="emailAttachmentFileType">Type of the email attachment file.</param>
/// <returns></returns>

private String ConvertFileType(FileType emailAttachmentFileType)
{
switch(emailAttachmentFileType)
Expand All @@ -151,14 +128,6 @@
}
}

/// <summary>
/// Gets the message status.
/// </summary>
/// <param name="providerReference">The provider reference.</param>
/// <param name="startDate">The start date.</param>
/// <param name="endDate">The end date.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
public async Task<MessageStatusResponse> GetMessageStatus(String providerReference,
DateTime startDate,
DateTime endDate,
Expand Down Expand Up @@ -208,11 +177,6 @@
return response;
}

/// <summary>
/// Translates the message status.
/// </summary>
/// <param name="status">The status.</param>
/// <returns></returns>
private MessageStatus TranslateMessageStatus(String status)
{
MessageStatus result;
Expand Down
16 changes: 10 additions & 6 deletions MessagingService.BusinessLogic/Services/MessagingDomainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,30 +120,34 @@
Boolean isHtml,
List<EmailAttachment> attachments,
CancellationToken cancellationToken) {
Result result = await ApplyEmailUpdates(async (EmailAggregate emailAggregate) => {
Result result = await ApplyEmailUpdates(async (EmailAggregate emailAggregate) =>

Check notice on line 123 in MessagingService.BusinessLogic/Services/MessagingDomainService.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

MessagingService.BusinessLogic/Services/MessagingDomainService.cs#L123

Remove the type specification; it is redundant.
{
// Check if this message has been sent before
if (emailAggregate.GetMessageStatus() != EmailMessageAggregate.MessageStatus.NotSet) {
if (emailAggregate.GetMessageStatus() != EmailMessageAggregate.MessageStatus.NotSet)
{
return Result.Success();
}

// send message to provider (record event)
emailAggregate.SendRequestToProvider(fromAddress, toAddresses, subject, body, isHtml, attachments);
emailAggregate.SendRequestToProvider(fromAddress, toAddresses, subject, body, isHtml, attachments);

// Make call to Email provider here
EmailServiceProxyResponse emailResponse = await this.EmailServiceProxy.SendEmail(messageId, fromAddress,
toAddresses, subject, body, isHtml, attachments, cancellationToken);

if (emailResponse.ApiCallSuccessful) {
if (emailResponse.ApiCallSuccessful)
{
// response message from provider (record event)
emailAggregate.ReceiveResponseFromProvider(emailResponse.RequestIdentifier,
emailResponse.EmailIdentifier);
}
else {
else
{
emailAggregate.ReceiveBadResponseFromProvider(emailResponse.Error, emailResponse.ErrorCode);
}

return Result.Success();
}, messageId, cancellationToken,false);
}, messageId, cancellationToken, false);

if (result.IsFailed)
return result;
Expand Down Expand Up @@ -235,7 +239,7 @@

public async Task<Result> UpdateMessageStatus(EmailCommands.UpdateMessageStatusCommand command,
CancellationToken cancellationToken) {
Result result = await ApplyEmailUpdates(async (EmailAggregate emailAggregate) => {

Check warning on line 242 in MessagingService.BusinessLogic/Services/MessagingDomainService.cs

View workflow job for this annotation

GitHub Actions / Build and Test Pull Requests

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
switch (command.Status) {
case EmailServices.MessageStatus.Bounced:
emailAggregate.MarkMessageAsBounced(command.Description, command.Timestamp);
Expand Down
Loading
Loading