refactor(SendMailHandler): remove TextToHtml

This commit is contained in:
tekh 2025-09-15 14:23:48 +02:00
parent b5fec7bb46
commit 991943d6bd
4 changed files with 28 additions and 29 deletions

View File

@ -1,14 +1,30 @@
using OtpNet; using OtpNet;
namespace EnvelopeGenerator.Application.Common.Extensions namespace EnvelopeGenerator.Application.Common.Extensions;
/// <summary>
///
/// </summary>
public static class StringExtension
{ {
public static class StringExtension /// <summary>
{ ///
/// </summary>
/// <param name="totp"></param>
/// <param name="secret"></param>
/// <returns></returns>
public static bool IsValidTotp(this string totp, string secret) public static bool IsValidTotp(this string totp, string secret)
{ {
var secret_bytes = Base32Encoding.ToBytes(secret); var secret_bytes = Base32Encoding.ToBytes(secret);
var secret_totp = new Totp(secret_bytes); var secret_totp = new Totp(secret_bytes);
return secret_totp.VerifyTotp(totp, out _, VerificationWindow.RfcSpecifiedNetworkDelay); return secret_totp.VerifyTotp(totp, out _, VerificationWindow.RfcSpecifiedNetworkDelay);
} }
}
/// <summary>
///
/// </summary>
/// <param name="seperator"></param>
/// <param name="values"></param>
/// <returns></returns>
public static string Join(this IEnumerable<string> values, string seperator) => string.Join(seperator, values);
} }

View File

@ -111,7 +111,7 @@ public abstract class SendMailHandler<TNotification> : INotificationHandler<TNot
var emailOut = new EmailOut var emailOut = new EmailOut
{ {
EmailAddress = notification.EmailAddress, EmailAddress = notification.EmailAddress,
EmailBody = TextToHtml(temp.Body), EmailBody = temp.Body,
EmailSubj = temp.Subject, EmailSubj = temp.Subject,
AddedWhen = DateTime.UtcNow, AddedWhen = DateTime.UtcNow,
AddedWho = DispatcherParams.AddedWho, AddedWho = DispatcherParams.AddedWho,
@ -132,22 +132,4 @@ public abstract class SendMailHandler<TNotification> : INotificationHandler<TNot
text = text.Replace(ph.Key, ph.Value); text = text.Replace(ph.Key, ph.Value);
return text; return text;
} }
private static string TextToHtml(string input)
{
if (string.IsNullOrEmpty(input)) return "";
// HTML encoding special characters
string encoded = System.Net.WebUtility.HtmlEncode(input);
// Convert tabs to &nbsp; (4 non-breaking spaces)
encoded = encoded.Replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;");
// Convert line breaks to <br />
encoded = encoded.Replace("\r\n", "<br />"); // Windows
encoded = encoded.Replace("\r", "<br />"); // Mac old
encoded = encoded.Replace("\n", "<br />"); // Unix/Linux
return encoded;
}
} }

View File

@ -18,7 +18,7 @@ public class DocSignedNotificationTests : TestBase
services.AddTransient<SendSignedMailHandler>(); services.AddTransient<SendSignedMailHandler>();
// overwrite EmailOutRepository // overwrite EmailOutRepository
services.AddDbRepository(opt => opt.RegisterEntity<Fake.EGDbContext2Prod, EmailOut>(ctx => ctx.EMailOuts)) services.AddDbRepository(opt => opt.RegisterEntity<Fake.EGDbContext2Prod, EmailOut>(ctx => ctx.EMailOuts));
} }
public override async Task Setup() public override async Task Setup()

View File

@ -20,6 +20,7 @@ using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using QuestPDF.Fluent; using QuestPDF.Fluent;
using QuestPDF.Infrastructure; using QuestPDF.Infrastructure;
using EnvelopeGenerator.Application.Common.Extensions;
namespace EnvelopeGenerator.Tests.Application; namespace EnvelopeGenerator.Tests.Application;
@ -174,7 +175,7 @@ public static class Extensions
public static CreateEnvelopeCommand CreateEnvelopeCommand(this Faker fake, int userId) => new() public static CreateEnvelopeCommand CreateEnvelopeCommand(this Faker fake, int userId) => new()
{ {
Message = fake.Lorem.Paragraph(fake.Random.Number(2, 5)), Message = fake.Lorem.Paragraph(fake.Random.Number(2, 5)),
Title = fake.Lorem.Paragraph(fake.Random.Number(1, 2)), Title = fake.Lorem.Words(fake.Random.Number(3, 4)).Join(" "),
UserId = userId, UserId = userId,
UseSQLExecutor = false UseSQLExecutor = false
}; };