Refactor EnvelopeMailService to use MediatR ISender
Replaces IAuthenticator with ISender in EnvelopeMailService, updates the constructor accordingly, and removes unused dependencies. Improves code readability and formatting, cleans up unused usings and redundant code, and aligns with the intended MediatR-based architecture. No functional changes to email sending logic.
This commit is contained in:
@@ -4,7 +4,6 @@ using DigitalData.EmailProfilerDispatcher.Abstraction.DTOs.EmailOut;
|
|||||||
using DigitalData.EmailProfilerDispatcher.Abstraction.Services;
|
using DigitalData.EmailProfilerDispatcher.Abstraction.Services;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using Newtonsoft.Json;
|
|
||||||
using DigitalData.Core.Abstraction.Application.DTO;
|
using DigitalData.Core.Abstraction.Application.DTO;
|
||||||
using EnvelopeGenerator.Domain.Constants;
|
using EnvelopeGenerator.Domain.Constants;
|
||||||
using EnvelopeGenerator.Application.Common.Configurations;
|
using EnvelopeGenerator.Application.Common.Configurations;
|
||||||
@@ -12,6 +11,7 @@ using EnvelopeGenerator.Application.Common.Dto.EnvelopeReceiver;
|
|||||||
using EnvelopeGenerator.Application.Common.Dto.EnvelopeReceiverReadOnly;
|
using EnvelopeGenerator.Application.Common.Dto.EnvelopeReceiverReadOnly;
|
||||||
using EnvelopeGenerator.Application.Common.Extensions;
|
using EnvelopeGenerator.Application.Common.Extensions;
|
||||||
using EnvelopeGenerator.Application.Common.Interfaces.Services;
|
using EnvelopeGenerator.Application.Common.Interfaces.Services;
|
||||||
|
using MediatR;
|
||||||
|
|
||||||
namespace EnvelopeGenerator.Application.Services;
|
namespace EnvelopeGenerator.Application.Services;
|
||||||
|
|
||||||
@@ -21,170 +21,170 @@ namespace EnvelopeGenerator.Application.Services;
|
|||||||
[Obsolete("Use MediatR")]
|
[Obsolete("Use MediatR")]
|
||||||
public class EnvelopeMailService : EmailOutService, IEnvelopeMailService
|
public class EnvelopeMailService : EmailOutService, IEnvelopeMailService
|
||||||
{
|
{
|
||||||
private readonly IEmailTemplateService _tempService;
|
private readonly IEmailTemplateService _tempService;
|
||||||
private readonly IEnvelopeReceiverService _envRcvService;
|
private readonly IEnvelopeReceiverService _envRcvService;
|
||||||
private readonly DispatcherParams _dConfig;
|
private readonly DispatcherParams _dConfig;
|
||||||
private readonly IConfigService _configService;
|
private readonly IConfigService _configService;
|
||||||
private readonly Dictionary<string, string> _placeholders;
|
private readonly Dictionary<string, string> _placeholders;
|
||||||
private readonly IAuthenticator _authenticator;
|
private readonly ISender _sender;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="repository"></param>
|
/// <param name="repository"></param>
|
||||||
/// <param name="mapper"></param>
|
/// <param name="mapper"></param>
|
||||||
/// <param name="tempService"></param>
|
/// <param name="tempService"></param>
|
||||||
/// <param name="envelopeReceiverService"></param>
|
/// <param name="envelopeReceiverService"></param>
|
||||||
/// <param name="dispatcherConfigOptions"></param>
|
/// <param name="dispatcherConfigOptions"></param>
|
||||||
/// <param name="configService"></param>
|
/// <param name="configService"></param>
|
||||||
/// <param name="mailConfig"></param>
|
/// <param name="mailConfig"></param>
|
||||||
/// <param name="authenticator"></param>
|
/// <param name="sender"></param>
|
||||||
public EnvelopeMailService(IEmailOutRepository repository, IMapper mapper, IEmailTemplateService tempService, IEnvelopeReceiverService envelopeReceiverService, IOptions<DispatcherParams> dispatcherConfigOptions, IConfigService configService, IOptions<MailParams> mailConfig, IAuthenticator authenticator) : base(repository, mapper)
|
public EnvelopeMailService(IEmailOutRepository repository, IMapper mapper, IEmailTemplateService tempService, IEnvelopeReceiverService envelopeReceiverService, IOptions<DispatcherParams> dispatcherConfigOptions, IConfigService configService, IOptions<MailParams> mailConfig, ISender sender) : base(repository, mapper)
|
||||||
{
|
{
|
||||||
_tempService = tempService;
|
_tempService = tempService;
|
||||||
_envRcvService = envelopeReceiverService;
|
_envRcvService = envelopeReceiverService;
|
||||||
_dConfig = dispatcherConfigOptions.Value;
|
_dConfig = dispatcherConfigOptions.Value;
|
||||||
_configService = configService;
|
_configService = configService;
|
||||||
_placeholders = new Dictionary<string, string>(mailConfig.Value.Placeholders);
|
_placeholders = new Dictionary<string, string>(mailConfig.Value.Placeholders);
|
||||||
_authenticator = authenticator;
|
_sender = sender;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<Dictionary<string, string>> CreatePlaceholders(string? accessCode = null, EnvelopeReceiverDto? envelopeReceiverDto = null)
|
private async Task<Dictionary<string, string>> CreatePlaceholders(string? accessCode = null, EnvelopeReceiverDto? envelopeReceiverDto = null)
|
||||||
{
|
|
||||||
if (accessCode is not null)
|
|
||||||
_placeholders["[DOCUMENT_ACCESS_CODE]"] = accessCode;
|
|
||||||
|
|
||||||
if(envelopeReceiverDto?.Envelope is not null && envelopeReceiverDto.Receiver is not null)
|
|
||||||
{
|
{
|
||||||
var erId = (envelopeReceiverDto.Envelope.Uuid, envelopeReceiverDto.Receiver.Signature).ToEnvelopeKey();
|
if (accessCode is not null)
|
||||||
var sigHost = await _configService.ReadDefaultSignatureHost();
|
_placeholders["[DOCUMENT_ACCESS_CODE]"] = accessCode;
|
||||||
var linkToDoc = $"{sigHost}/EnvelopeKey/{erId}";
|
|
||||||
_placeholders["[LINK_TO_DOCUMENT]"] = linkToDoc;
|
if (envelopeReceiverDto?.Envelope is not null && envelopeReceiverDto.Receiver is not null)
|
||||||
_placeholders["[LINK_TO_DOCUMENT_TEXT]"] = linkToDoc[..Math.Min(40, linkToDoc.Length)] + "..";
|
{
|
||||||
|
var erId = (envelopeReceiverDto.Envelope.Uuid, envelopeReceiverDto.Receiver.Signature).ToEnvelopeKey();
|
||||||
|
var sigHost = await _configService.ReadDefaultSignatureHost();
|
||||||
|
var linkToDoc = $"{sigHost}/EnvelopeKey/{erId}";
|
||||||
|
_placeholders["[LINK_TO_DOCUMENT]"] = linkToDoc;
|
||||||
|
_placeholders["[LINK_TO_DOCUMENT_TEXT]"] = linkToDoc[..Math.Min(40, linkToDoc.Length)] + "..";
|
||||||
|
}
|
||||||
|
|
||||||
|
return _placeholders;
|
||||||
}
|
}
|
||||||
|
|
||||||
return _placeholders;
|
private async Task<Dictionary<string, string>> CreatePlaceholders(EnvelopeReceiverReadOnlyDto? readOnlyDto = null)
|
||||||
}
|
|
||||||
|
|
||||||
private async Task<Dictionary<string, string>> CreatePlaceholders(EnvelopeReceiverReadOnlyDto? readOnlyDto = null)
|
|
||||||
{
|
|
||||||
if (readOnlyDto?.Envelope is not null && readOnlyDto.Receiver is not null)
|
|
||||||
{
|
|
||||||
_placeholders["[NAME_RECEIVER]"] = await _envRcvService.ReadLastUsedReceiverNameByMailAsync(readOnlyDto.AddedWho).ThenAsync(res => res, (msg, ntc) => string.Empty) ?? string.Empty;
|
|
||||||
var erReadOnlyId = (readOnlyDto.Id).ToEnvelopeKey();
|
|
||||||
var sigHost = await _configService.ReadDefaultSignatureHost();
|
|
||||||
var linkToDoc = $"{sigHost}/EnvelopeKey/{erReadOnlyId}";
|
|
||||||
_placeholders["[LINK_TO_DOCUMENT]"] = linkToDoc;
|
|
||||||
_placeholders["[LINK_TO_DOCUMENT_TEXT]"] = linkToDoc[..Math.Min(40, linkToDoc.Length)] + "..";
|
|
||||||
}
|
|
||||||
|
|
||||||
return _placeholders;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
///
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="dto"></param>
|
|
||||||
/// <param name="tempType"></param>
|
|
||||||
/// <param name="optionalPlaceholders"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public async Task<DataResult<int>> SendAsync(EnvelopeReceiverDto dto, EmailTemplateType tempType, Dictionary<string, object>? optionalPlaceholders = null)
|
|
||||||
{
|
|
||||||
var tempSerResult = await _tempService.ReadByNameAsync(tempType);
|
|
||||||
if (tempSerResult.IsFailed)
|
|
||||||
return tempSerResult.ToFail<int>().Notice(LogLevel.Error, DigitalData.Core.Abstraction.Application.DTO.Flag.DataIntegrityIssue, $"The email cannot send because '{tempType}' template cannot found.");
|
|
||||||
var temp = tempSerResult.Data;
|
|
||||||
|
|
||||||
var mail = new EmailOutCreateDto()
|
|
||||||
{
|
{
|
||||||
EmailAddress = dto.Receiver!.EmailAddress,
|
if (readOnlyDto?.Envelope is not null && readOnlyDto.Receiver is not null)
|
||||||
EmailSubj = temp.Subject,
|
{
|
||||||
EmailBody = temp.Body,
|
_placeholders["[NAME_RECEIVER]"] = await _envRcvService.ReadLastUsedReceiverNameByMailAsync(readOnlyDto.AddedWho).ThenAsync(res => res, (msg, ntc) => string.Empty) ?? string.Empty;
|
||||||
//email_type = envelope_status,
|
var erReadOnlyId = (readOnlyDto.Id).ToEnvelopeKey();
|
||||||
//message = envelope_message,
|
var sigHost = await _configService.ReadDefaultSignatureHost();
|
||||||
ReferenceId = dto.EnvelopeId, //REFERENCE_ID = ENVELOPE_ID
|
var linkToDoc = $"{sigHost}/EnvelopeKey/{erReadOnlyId}";
|
||||||
ReferenceString = dto!.Envelope!.Uuid, //REFERENCE_STRING = ENVELOPE_UUID
|
_placeholders["[LINK_TO_DOCUMENT]"] = linkToDoc;
|
||||||
//receiver_name = receiver.name,
|
_placeholders["[LINK_TO_DOCUMENT_TEXT]"] = linkToDoc[..Math.Min(40, linkToDoc.Length)] + "..";
|
||||||
//receiver_access_code = receiver.access_code,
|
}
|
||||||
//sender_adress = envelope.user.email,
|
|
||||||
//sender_name = envelope.user.full_name,
|
|
||||||
//envelope_title = envelope.title,
|
|
||||||
ReminderTypeId = _dConfig.ReminderTypeId,
|
|
||||||
SendingProfile = _dConfig.SendingProfile,
|
|
||||||
EntityId = null,
|
|
||||||
WfId = (int) EnvelopeStatus.MessageAccessCodeSent,
|
|
||||||
WfReference = null,
|
|
||||||
AddedWho = _dConfig.AddedWho,
|
|
||||||
EmailAttmt1 = _dConfig.EmailAttmt1
|
|
||||||
};
|
|
||||||
|
|
||||||
//get acccess code
|
return _placeholders;
|
||||||
var acResult = await _envRcvService.ReadAccessCodeByIdAsync(envelopeId: dto.EnvelopeId, receiverId: dto.ReceiverId);
|
}
|
||||||
if (acResult.IsFailed)
|
|
||||||
return acResult.ToFail<int>().Notice(LogLevel.Error, "Therefore, access code cannot be sent");
|
|
||||||
var accessCode = acResult.Data;
|
|
||||||
|
|
||||||
var placeholders = await CreatePlaceholders(accessCode: accessCode, envelopeReceiverDto: dto);
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dto"></param>
|
||||||
|
/// <param name="tempType"></param>
|
||||||
|
/// <param name="optionalPlaceholders"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<DataResult<int>> SendAsync(EnvelopeReceiverDto dto, EmailTemplateType tempType, Dictionary<string, object>? optionalPlaceholders = null)
|
||||||
|
{
|
||||||
|
var tempSerResult = await _tempService.ReadByNameAsync(tempType);
|
||||||
|
if (tempSerResult.IsFailed)
|
||||||
|
return tempSerResult.ToFail<int>().Notice(LogLevel.Error, DigitalData.Core.Abstraction.Application.DTO.Flag.DataIntegrityIssue, $"The email cannot send because '{tempType}' template cannot found.");
|
||||||
|
var temp = tempSerResult.Data;
|
||||||
|
|
||||||
// Add optional place holders.
|
var mail = new EmailOutCreateDto()
|
||||||
if (optionalPlaceholders is not null)
|
{
|
||||||
foreach (var oph in optionalPlaceholders)
|
EmailAddress = dto.Receiver!.EmailAddress,
|
||||||
placeholders[oph.Key] = oph.Value.ToString() ?? "NULL";
|
EmailSubj = temp.Subject,
|
||||||
|
EmailBody = temp.Body,
|
||||||
|
//email_type = envelope_status,
|
||||||
|
//message = envelope_message,
|
||||||
|
ReferenceId = dto.EnvelopeId, //REFERENCE_ID = ENVELOPE_ID
|
||||||
|
ReferenceString = dto!.Envelope!.Uuid, //REFERENCE_STRING = ENVELOPE_UUID
|
||||||
|
//receiver_name = receiver.name,
|
||||||
|
//receiver_access_code = receiver.access_code,
|
||||||
|
//sender_adress = envelope.user.email,
|
||||||
|
//sender_name = envelope.user.full_name,
|
||||||
|
//envelope_title = envelope.title,
|
||||||
|
ReminderTypeId = _dConfig.ReminderTypeId,
|
||||||
|
SendingProfile = _dConfig.SendingProfile,
|
||||||
|
EntityId = null,
|
||||||
|
WfId = (int)EnvelopeStatus.MessageAccessCodeSent,
|
||||||
|
WfReference = null,
|
||||||
|
AddedWho = _dConfig.AddedWho,
|
||||||
|
EmailAttmt1 = _dConfig.EmailAttmt1
|
||||||
|
};
|
||||||
|
|
||||||
//TODO: remove the requirement to add the models using reflections
|
//get acccess code
|
||||||
return await CreateWithTemplateAsync(createDto: mail,placeholders: placeholders,
|
var acResult = await _envRcvService.ReadAccessCodeByIdAsync(envelopeId: dto.EnvelopeId, receiverId: dto.ReceiverId);
|
||||||
dto, dto.Envelope.User!, dto.Envelope);
|
if (acResult.IsFailed)
|
||||||
}
|
return acResult.ToFail<int>().Notice(LogLevel.Error, "Therefore, access code cannot be sent");
|
||||||
|
var accessCode = acResult.Data;
|
||||||
|
|
||||||
/// <summary>
|
var placeholders = await CreatePlaceholders(accessCode: accessCode, envelopeReceiverDto: dto);
|
||||||
///
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="dto"></param>
|
|
||||||
/// <param name="optionalPlaceholders"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public async Task<DataResult<int>> SendAsync(EnvelopeReceiverReadOnlyDto dto, Dictionary<string, object>? optionalPlaceholders = null)
|
|
||||||
{
|
|
||||||
var tempSerResult = await _tempService.ReadByNameAsync(EmailTemplateType.DocumentShared);
|
|
||||||
if (tempSerResult.IsFailed)
|
|
||||||
return tempSerResult.ToFail<int>().Notice(LogLevel.Error, Flag.DataIntegrityIssue, $"The email cannot send because '{EmailTemplateType.DocumentShared}' template cannot found.");
|
|
||||||
var temp = tempSerResult.Data;
|
|
||||||
|
|
||||||
var mail = new EmailOutCreateDto()
|
// Add optional place holders.
|
||||||
{
|
if (optionalPlaceholders is not null)
|
||||||
EmailAddress = dto.ReceiverMail,
|
foreach (var oph in optionalPlaceholders)
|
||||||
EmailSubj = temp.Subject,
|
placeholders[oph.Key] = oph.Value.ToString() ?? "NULL";
|
||||||
EmailBody = temp.Body,
|
|
||||||
//TODO: remove int casting when all
|
|
||||||
ReferenceId = (int) dto.EnvelopeId, //REFERENCE_ID = ENVELOPE_ID
|
|
||||||
ReferenceString = dto.Envelope!.Uuid, //REFERENCE_STRING = ENVELOPE_UUID
|
|
||||||
//receiver_name = receiver.name,
|
|
||||||
//receiver_access_code = receiver.access_code,
|
|
||||||
//sender_adress = envelope.user.email,
|
|
||||||
//sender_name = envelope.user.full_name,
|
|
||||||
//envelope_title = envelope.title,
|
|
||||||
ReminderTypeId = _dConfig.ReminderTypeId,
|
|
||||||
SendingProfile = _dConfig.SendingProfile,
|
|
||||||
EntityId = null,
|
|
||||||
WfId = (int)EnvelopeStatus.EnvelopeShared,
|
|
||||||
WfReference = null,
|
|
||||||
AddedWho = _dConfig.AddedWho,
|
|
||||||
EmailAttmt1 = _dConfig.EmailAttmt1
|
|
||||||
};
|
|
||||||
|
|
||||||
var placeholders = await CreatePlaceholders(readOnlyDto: dto);
|
//TODO: remove the requirement to add the models using reflections
|
||||||
|
return await CreateWithTemplateAsync(createDto: mail, placeholders: placeholders,
|
||||||
|
dto, dto.Envelope.User!, dto.Envelope);
|
||||||
|
}
|
||||||
|
|
||||||
// Add optional place holders.
|
/// <summary>
|
||||||
if (optionalPlaceholders is not null)
|
///
|
||||||
foreach (var oph in optionalPlaceholders)
|
/// </summary>
|
||||||
placeholders[oph.Key] = oph.Value.ToString() ?? "NULL";
|
/// <param name="dto"></param>
|
||||||
|
/// <param name="optionalPlaceholders"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<DataResult<int>> SendAsync(EnvelopeReceiverReadOnlyDto dto, Dictionary<string, object>? optionalPlaceholders = null)
|
||||||
|
{
|
||||||
|
var tempSerResult = await _tempService.ReadByNameAsync(EmailTemplateType.DocumentShared);
|
||||||
|
if (tempSerResult.IsFailed)
|
||||||
|
return tempSerResult.ToFail<int>().Notice(LogLevel.Error, Flag.DataIntegrityIssue, $"The email cannot send because '{EmailTemplateType.DocumentShared}' template cannot found.");
|
||||||
|
var temp = tempSerResult.Data;
|
||||||
|
|
||||||
return await CreateWithTemplateAsync(createDto: mail, placeholders: placeholders, dto.Envelope);
|
var mail = new EmailOutCreateDto()
|
||||||
}
|
{
|
||||||
|
EmailAddress = dto.ReceiverMail,
|
||||||
|
EmailSubj = temp.Subject,
|
||||||
|
EmailBody = temp.Body,
|
||||||
|
//TODO: remove int casting when all
|
||||||
|
ReferenceId = (int)dto.EnvelopeId, //REFERENCE_ID = ENVELOPE_ID
|
||||||
|
ReferenceString = dto.Envelope!.Uuid, //REFERENCE_STRING = ENVELOPE_UUID
|
||||||
|
//receiver_name = receiver.name,
|
||||||
|
//receiver_access_code = receiver.access_code,
|
||||||
|
//sender_adress = envelope.user.email,
|
||||||
|
//sender_name = envelope.user.full_name,
|
||||||
|
//envelope_title = envelope.title,
|
||||||
|
ReminderTypeId = _dConfig.ReminderTypeId,
|
||||||
|
SendingProfile = _dConfig.SendingProfile,
|
||||||
|
EntityId = null,
|
||||||
|
WfId = (int)EnvelopeStatus.EnvelopeShared,
|
||||||
|
WfReference = null,
|
||||||
|
AddedWho = _dConfig.AddedWho,
|
||||||
|
EmailAttmt1 = _dConfig.EmailAttmt1
|
||||||
|
};
|
||||||
|
|
||||||
|
var placeholders = await CreatePlaceholders(readOnlyDto: dto);
|
||||||
|
|
||||||
|
// Add optional place holders.
|
||||||
|
if (optionalPlaceholders is not null)
|
||||||
|
foreach (var oph in optionalPlaceholders)
|
||||||
|
placeholders[oph.Key] = oph.Value.ToString() ?? "NULL";
|
||||||
|
|
||||||
|
return await CreateWithTemplateAsync(createDto: mail, placeholders: placeholders, dto.Envelope);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="dto"></param>
|
/// <param name="dto"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<DataResult<int>> SendAccessCodeAsync(EnvelopeReceiverDto dto) => await SendAsync(dto: dto, tempType: EmailTemplateType.DocumentAccessCodeReceived);
|
public async Task<DataResult<int>> SendAccessCodeAsync(EnvelopeReceiverDto dto) => await SendAsync(dto: dto, tempType: EmailTemplateType.DocumentAccessCodeReceived);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user