From 073d9692d72601f24a36888f2e76c1073c491918 Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Thu, 2 Oct 2025 21:11:46 +0100 Subject: [PATCH] more fixes for codacy --- .../EmailServices/Smtp2Go/Smtp2GoProxy.cs | 67 +++------- .../TheSMSWorks/TheSmsWorksProxy.cs | 34 ++---- MessagingService/Common/ClaimsHelper.cs | 114 ------------------ MessagingService/Common/Extensions.cs | 6 +- .../Controllers/EmailController.cs | 3 +- MessagingService/Controllers/SMSController.cs | 1 + 6 files changed, 34 insertions(+), 191 deletions(-) delete mode 100644 MessagingService/Common/ClaimsHelper.cs diff --git a/MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoProxy.cs b/MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoProxy.cs index 1c2a771..e5d95c0 100644 --- a/MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoProxy.cs +++ b/MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoProxy.cs @@ -50,8 +50,6 @@ public async Task SendEmail(Guid messageId, Boolean isHtml, List attachments, CancellationToken cancellationToken) { - EmailServiceProxyResponse response = null; - // Translate the request message Smtp2GoSendEmailRequest apiRequest = new() { @@ -96,17 +94,14 @@ public async Task SendEmail(Guid messageId, if (httpResponse.IsSuccessStatusCode){ apiResponse = JsonConvert.DeserializeObject(await httpResponse.Content.ReadAsStringAsync(cancellationToken)); } - else{ - apiResponse = new Smtp2GoSendEmailResponse(); - apiResponse.Data = new Smtp2GoSendEmailResponseData(); - apiResponse.Data.Error = httpResponse.StatusCode.ToString(); - apiResponse.Data.ErrorCode = ((Int32)httpResponse.StatusCode).ToString(); + else { + apiResponse = new Smtp2GoSendEmailResponse { Data = new Smtp2GoSendEmailResponseData { Error = httpResponse.StatusCode.ToString(), ErrorCode = ((Int32)httpResponse.StatusCode).ToString() } }; } Logger.LogDebug($"Response Message Received from Email Provider [SMTP2Go] {JsonConvert.SerializeObject(apiResponse)}"); // Translate the Response - response = new EmailServiceProxyResponse + return new EmailServiceProxyResponse { ApiCallSuccessful = httpResponse.IsSuccessStatusCode && String.IsNullOrEmpty(apiResponse.Data.ErrorCode), EmailIdentifier = apiResponse.Data.EmailId, @@ -114,7 +109,6 @@ public async Task SendEmail(Guid messageId, ErrorCode = apiResponse.Data.ErrorCode, RequestIdentifier = apiResponse.RequestId }; - return response; } private String ConvertFileType(FileType emailAttachmentFileType) @@ -133,8 +127,6 @@ public async Task GetMessageStatus(String providerReferen DateTime endDate, CancellationToken cancellationToken) { - MessageStatusResponse response = null; - Smtp2GoEmailSearchRequest apiRequest = new() { ApiKey = ConfigurationReader.GetValue("SMTP2GoAPIKey"), @@ -166,50 +158,31 @@ public async Task GetMessageStatus(String providerReferen Logger.LogDebug($"Response Message Received from Email Provider [SMTP2Go] {JsonConvert.SerializeObject(apiResponse)}"); // Translate the Response - response = new MessageStatusResponse + return new MessageStatusResponse { ApiStatusCode = httpResponse.StatusCode, MessageStatus = this.TranslateMessageStatus(apiResponse.Data.EmailDetails.Single().Status), ProviderStatusDescription = apiResponse.Data.EmailDetails.Single().Status, Timestamp = apiResponse.Data.EmailDetails.Single().EmailStatusDate }; - - return response; } - private MessageStatus TranslateMessageStatus(String status) - { - MessageStatus result; - switch (status) - { - case "failed": - case "deferred": - result = MessageStatus.Failed; - break; - case "hardbounce": - case "refused": - case "softbounce": - case "returned": - result = MessageStatus.Bounced; - break; - case "delivered": - case "ok": - case "sent": - result = MessageStatus.Delivered; - break; - case "rejected": - result = MessageStatus.Rejected; - break; - case "complained": - case "spam": - result = MessageStatus.Spam; - break; - default: - result = MessageStatus.Unknown; - break; - } - - return result; + private MessageStatus TranslateMessageStatus(String status) { + return status switch { + "failed" => MessageStatus.Failed, + "deferred" => MessageStatus.Failed, + "hardbounce" => MessageStatus.Bounced, + "refused" => MessageStatus.Bounced, + "softbounce" => MessageStatus.Bounced, + "returned" => MessageStatus.Bounced, + "delivered" => MessageStatus.Delivered, + "ok" => MessageStatus.Delivered, + "sent" => MessageStatus.Delivered, + "rejected" => MessageStatus.Rejected, + "complained" => MessageStatus.Spam, + "spam" => MessageStatus.Spam, + _ => MessageStatus.Unknown, + }; } #endregion diff --git a/MessagingService.BusinessLogic/Services/SMSServices/TheSMSWorks/TheSmsWorksProxy.cs b/MessagingService.BusinessLogic/Services/SMSServices/TheSMSWorks/TheSmsWorksProxy.cs index e7ed862..c1badba 100644 --- a/MessagingService.BusinessLogic/Services/SMSServices/TheSMSWorks/TheSmsWorksProxy.cs +++ b/MessagingService.BusinessLogic/Services/SMSServices/TheSMSWorks/TheSmsWorksProxy.cs @@ -129,31 +129,15 @@ public async Task GetMessageStatus(String providerReferen /// private MessageStatus TranslateMessageStatus(String status) { - MessageStatus result = MessageStatus.NotSet; - switch (status) - { - case "UNDELIVERABLE": - result = MessageStatus.Undeliverable; - break; - case "DELIVERED": - case "SENT": - result = MessageStatus.Delivered; - break; - case "REJECTED": - result = MessageStatus.Rejected; - break; - case "EXPIRED": - result = MessageStatus.Expired; - break; - case "INCOMING": - result = MessageStatus.Incoming; - break; - default: - result = MessageStatus.Rejected; - break; - } - - return result; + return status switch { + "UNDELIVERABLE" => MessageStatus.Undeliverable, + "DELIVERED"=> MessageStatus.Delivered, + "SENT" => MessageStatus.Delivered, + "REJECTED" => MessageStatus.Rejected, + "EXPIRED" => MessageStatus.Expired, + "INCOMING" => MessageStatus.Incoming, + _ => MessageStatus.Rejected + }; } /// diff --git a/MessagingService/Common/ClaimsHelper.cs b/MessagingService/Common/ClaimsHelper.cs deleted file mode 100644 index a6e13e6..0000000 --- a/MessagingService/Common/ClaimsHelper.cs +++ /dev/null @@ -1,114 +0,0 @@ -using Shared.Exceptions; -using System; -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; -using System.Linq; -using System.Security.Claims; -using System.Threading.Tasks; - -namespace MessagingService.Common -{ - [ExcludeFromCodeCoverage] - public class ClaimsHelper - { - #region Methods - - /// - /// Gets the user claims. - /// - /// The user. - /// Type of the custom claim. - /// The default value. - /// - /// No claim [{customClaimType}] found for user id [{userIdClaim.Value} - public static Claim GetUserClaim(ClaimsPrincipal user, - String customClaimType, - String defaultValue = "") - { - Claim userClaim = null; - - if (ClaimsHelper.IsPasswordToken(user)) - { - // Get the claim from the token - userClaim = user.Claims.SingleOrDefault(c => c.Type == customClaimType); - - if (userClaim == null) - { - throw new NotFoundException($"Claim type [{customClaimType}] not found"); - } - } - else - { - userClaim = new Claim(customClaimType, defaultValue); - } - - return userClaim; - } - - /// - /// Determines whether [is client token] [the specified user]. - /// - /// The user. - /// - /// true if [is client token] [the specified user]; otherwise, false. - /// - public static Boolean IsPasswordToken(ClaimsPrincipal user) - { - Boolean result = false; - - Claim userIdClaim = user.Claims.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier); - - if (userIdClaim != null) - { - result = true; - } - - return result; - } - - /// - /// Validates the route parameter. - /// - /// - /// The route parameter. - /// The user claim. - public static Boolean ValidateRouteParameter(T routeParameter, - Claim userClaim) - { - if (routeParameter.ToString() != userClaim.Value) - { - return false; - } - - return true; - } - - /// - /// Determines whether [is user roles valid] [the specified user]. - /// - /// The user. - /// The allowed roles. - /// - /// true if [is user roles valid] [the specified user]; otherwise, false. - /// - public static Boolean IsUserRolesValid(ClaimsPrincipal user, String[] allowedRoles) - { - if (IsPasswordToken(user) == false) - { - return true; - } - - foreach (String allowedRole in allowedRoles) - { - if (user.IsInRole(allowedRole) == false) - { - return false; - } - } - - return true; - } - - #endregion - } -} diff --git a/MessagingService/Common/Extensions.cs b/MessagingService/Common/Extensions.cs index dfffa7c..2e78479 100644 --- a/MessagingService/Common/Extensions.cs +++ b/MessagingService/Common/Extensions.cs @@ -51,9 +51,7 @@ public static void PreWarm(this IApplicationBuilder applicationBuilder) IConfigurationSection subscriptionConfigSection = Startup.Configuration.GetSection("AppSettings:SubscriptionConfiguration"); SubscriptionWorkersRoot subscriptionWorkersRoot = new(); subscriptionConfigSection.Bind(subscriptionWorkersRoot); - - String eventStoreConnectionString = ConfigurationReader.GetValue("EventStoreSettings", "ConnectionString"); - + IDomainEventHandlerResolver mainEventHandlerResolver = Startup.Container.GetInstance("Main"); Dictionary eventHandlerResolvers = new() { @@ -66,7 +64,7 @@ public static void PreWarm(this IApplicationBuilder applicationBuilder) String connectionString = Startup.Configuration.GetValue("EventStoreSettings:ConnectionString"); EventStoreClientSettings eventStoreConnectionSettings = EventStoreClientSettings.Create(connectionString); applicationBuilder.ConfigureSubscriptionService(subscriptionWorkersRoot, - eventStoreConnectionString, + connectionString, eventHandlerResolvers, Extensions.log, subscriptionRepositoryResolver).Wait(CancellationToken.None); diff --git a/MessagingService/Controllers/EmailController.cs b/MessagingService/Controllers/EmailController.cs index 1ae7fd1..d6712cd 100644 --- a/MessagingService/Controllers/EmailController.cs +++ b/MessagingService/Controllers/EmailController.cs @@ -1,4 +1,5 @@ -using Shared.Results; +using Shared.General; +using Shared.Results; using Shared.Results.Web; using SimpleResults; using SendEmailRequestDTO = MessagingService.DataTransferObjects.SendEmailRequest; diff --git a/MessagingService/Controllers/SMSController.cs b/MessagingService/Controllers/SMSController.cs index 66bad73..56edb02 100644 --- a/MessagingService/Controllers/SMSController.cs +++ b/MessagingService/Controllers/SMSController.cs @@ -1,4 +1,5 @@ using MessagingService.BusinessLogic.Requests; +using Shared.General; using Shared.Results; using Shared.Results.Web; using SimpleResults;