Revert "Refactor envelope handling to use ISQLExecutor"

This reverts commit 4cabaf31917c8860411349c0e5fc1c62e7c5c384.
This commit is contained in:
Developer 02 2025-05-06 09:45:38 +02:00
parent b0eb1b6389
commit b609253893
6 changed files with 103 additions and 46 deletions

View File

@ -9,6 +9,36 @@ namespace EnvelopeGenerator.Application.Contracts.SQLExecutor;
/// </summary>
public static class Extension
{
private static DynamicParameters CreateParmas(int userId, string title = "", string message = "", bool tfaEnabled = false)
{
var parameters = new DynamicParameters();
parameters.Add("@UserId", userId);
parameters.Add("@Title", title);
parameters.Add("@TfaEnabled", tfaEnabled ? 1 : 0);
parameters.Add("@Message", message);
return parameters;
}
/// <summary>
///
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="executor"></param>
/// <param name="userId"></param>
/// <param name="title"></param>
/// <param name="message"></param>
/// <param name="tfaEnabled"></param>
/// <param name="cancellation"></param>
/// <returns></returns>
public static async Task<TEntity?> CreateEnvelopeAsync<TEntity>(this ISQLExecutor executor, int userId, string title = "", string message = "", bool tfaEnabled = false, CancellationToken cancellation = default)
where TEntity : class
{
var parameters = CreateParmas(userId, title, message, tfaEnabled);
var envelopes = await executor.Execute<TEntity, EnvelopeCreateReadSQL>(parameters, cancellation);
return envelopes.FirstOrDefault();
}
/// <summary>
///
/// </summary>
@ -19,14 +49,10 @@ public static class Extension
/// <param name="tfaEnabled"></param>
/// <param name="cancellation"></param>
/// <returns></returns>
public static async Task<Envelope?> CreateEnvelopeAsync(this ISQLExecutor executor, int userId, string title = "", string message = "", bool tfaEnabled = false, CancellationToken cancellation = default)
public static async Task<Envelope?> CreateEnvelopeAsync(this IEnvelopeExecutor executor, int userId, string title = "", string message = "", bool tfaEnabled = false, CancellationToken cancellation = default)
{
var parameters = new DynamicParameters();
parameters.Add("@UserId", userId);
parameters.Add("@Title", title);
parameters.Add("@TfaEnabled", tfaEnabled ? 1 : 0);
parameters.Add("@Message", message);
var envelopes = await executor.Execute<Envelope, EnvelopeCreateReadSQL>(parameters, cancellation);
return envelopes.FirstOrDefault();
var parameters = CreateParmas(userId, title, message, tfaEnabled);
return await executor.CreateEnvelopeAsync(parameters, cancellation: cancellation);
}
}

View File

@ -0,0 +1,19 @@
using Dapper;
using EnvelopeGenerator.Domain.Entities;
namespace EnvelopeGenerator.Application.Contracts.SQLExecutor;
/// <summary>
///
/// </summary>
public interface IEnvelopeExecutor : ISQLExecutor
{
/// <summary>
///
/// </summary>
/// <param name="parameters"></param>
/// <param name="addUser"></param>
/// <param name="cancellation"></param>
/// <returns></returns>
Task<Envelope?> CreateEnvelopeAsync(DynamicParameters parameters, bool addUser = true, CancellationToken cancellation = default);
}

View File

@ -9,18 +9,18 @@ namespace EnvelopeGenerator.Application.Envelopes.Commands;
/// </summary>
public class CreateEnvelopeCommandHandler : IRequestHandler<CreateEnvelopeCommand, CreateEnvelopeResponse?>
{
private readonly ISQLExecutor _executor;
private readonly IEnvelopeExecutor _envelopeExecutor;
private readonly IMapper _mapper;
/// <summary>
///
/// </summary>
/// <param name="executor"></param>
/// <param name="envelopeExecutor"></param>
/// <param name="mapper"></param>
public CreateEnvelopeCommandHandler(ISQLExecutor executor, IMapper mapper)
public CreateEnvelopeCommandHandler(IEnvelopeExecutor envelopeExecutor, IMapper mapper)
{
_executor = executor;
_envelopeExecutor = envelopeExecutor;
_mapper = mapper;
}
@ -34,7 +34,7 @@ public class CreateEnvelopeCommandHandler : IRequestHandler<CreateEnvelopeComman
{
int userId = request.UserId ?? throw new InvalidOperationException("UserId cannot be null when creating an envelope.");
var envelope = await _executor.CreateEnvelopeAsync(userId, request.Title, request.Message, request.TFAEnabled, cancellationToken);
var envelope = await _envelopeExecutor.CreateEnvelopeAsync(userId, request.Title, request.Message, request.TFAEnabled, cancellationToken);
return _mapper.Map<CreateEnvelopeResponse>(envelope);
}

View File

@ -1,33 +0,0 @@
using EnvelopeGenerator.Application.Contracts.SQLExecutor;
using EnvelopeGenerator.Domain.Entities;
namespace EnvelopeGenerator.Application.SQL;
/// <summary>
///
/// </summary>
public class EnvelopeReceiverCreateReadSQL : ISQL<Envelope>
{
/// <summary>
///
/// </summary>
public string Raw => @"
USE [DD_ECM]
GO
DECLARE @OUT_RECEIVER_ID int
DECLARE @ENV_UID varchar(36) = @ENV_UID
EXEC [dbo].[PRSIG_API_CREATE_RECEIVER]
@ENV_UID = @ENV_UID,
@EMAIL_ADRESS = @EMAIL_ADRESS ,
@SALUTATION = @SALUTATION,
@PHONE = @PHONE,
@OUT_RECEIVER_ID = @OUT_RECEIVER_ID OUTPUT
SELECT TOP(1) *
FROM TBSIG_ENVELOPE_RECEIVER
WHERE [GUID] = @OUT_RECEIVER_ID;
";
}

View File

@ -82,6 +82,8 @@ public static class DIExtensions
SetDapperTypeMap<DocumentReceiverElement>();
SetDapperTypeMap<DocumentStatus>();
services.AddScoped<IEnvelopeExecutor, EnvelopeExecutor>();
if (sqlExecutorConfiguration is not null || sqlExecutorConfigureOptions is not null)
services.AddSQLExecutor(sqlExecutorConfiguration, sqlExecutorConfigureOptions);

View File

@ -0,0 +1,43 @@
using Dapper;
using DigitalData.UserManager.Application.Contracts.Repositories;
using DigitalData.UserManager.Infrastructure.Repositories;
using EnvelopeGenerator.Application.Contracts.SQLExecutor;
using EnvelopeGenerator.Application.SQL;
using EnvelopeGenerator.Domain.Entities;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace EnvelopeGenerator.Infrastructure.Executor;
public class EnvelopeExecutor : SQLExecutor, IEnvelopeExecutor
{
private readonly IUserRepository _userRepository;
public EnvelopeExecutor(ILogger<EnvelopeExecutor> logger, IServiceProvider provider, IOptions<SQLExecutorParams> sqlExecutorParamsOptions, IUserRepository userRepository) : base(provider, sqlExecutorParamsOptions)
{
_userRepository = userRepository;
}
public async Task<Envelope?> CreateEnvelopeAsync(DynamicParameters parameters, bool addUser = true, CancellationToken cancellation = default)
{
using var connection = new SqlConnection(Params.ConnectionString);
var sql = Provider.GetRequiredService<EnvelopeCreateReadSQL>();
await connection.OpenAsync(cancellation);
var envelopes = await connection.QueryAsync<Envelope>(sql.Raw, parameters);
var envelope = envelopes.FirstOrDefault();
if (envelope is null)
return null;
// Add User
if (addUser)
{
var user = await _userRepository.ReadByIdAsync(envelope.UserId);
envelope.User = user;
}
return envelope;
}
}