Files
EnvelopeGenerator/EnvelopeGenerator.Server/EnvelopeGenerator.Server/BackgroundServices/EnvelopeFinalizationWorker.cs
TekH 98ef8005f0 Refactor FinalizeEnvelopeCommand and handler logic
Simplified the `FinalizeEnvelopeCommand` by removing unused properties (`BurnedAllowed` and `SaveDocResult`) and streamlining its structure. Enhanced the `FinalizeEnvelopeCommandHandler` with additional dependencies (`ILogger` and `IRepository<History>`) for logging and history access.

Improved envelope query to include related entities (`Documents.Elements`, `Histories`, and `User`) for comprehensive data processing. Refined validation logic to ensure all receivers have signed the document and added detailed error messages for better user feedback.

Updated document processing to save the signed document (`docResult`) directly to the repository. Enhanced notification handling with improved content, error handling, and logging for both receivers and the sender.

Removed redundant code and updated `EnvelopeFinalizationWorker` to align with the new command structure. These changes improve maintainability, robustness, and clarity of the envelope finalization process.
2026-11-26 17:08:28 +01:00

73 lines
2.6 KiB
C#

using EnvelopeGenerator.Application.PdfSigning;
using EnvelopeGenerator.Domain.Constants;
using EnvelopeGenerator.Infrastructure;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace EnvelopeGenerator.Server.BackgroundServices;
/// <summary>
///
/// </summary>
/// <param name="scopeFactory"></param>
/// <param name="logger"></param>
public sealed class EnvelopeFinalizationWorker(
IServiceScopeFactory scopeFactory,
ILogger<EnvelopeFinalizationWorker> logger) : BackgroundService
{
private static readonly TimeSpan PollInterval = TimeSpan.FromMinutes(1);
/// <summary>
///
/// </summary>
/// <param name="stoppingToken"></param>
/// <returns></returns>
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
try
{
using var scope = scopeFactory.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<EGDbContext>();
var sender = scope.ServiceProvider.GetRequiredService<ISender>();
var envelopeIds = await dbContext.Envelopes
.AsNoTracking()
.Where(e => e.Status == EnvelopeStatus.EnvelopeCompletelySigned && e.DocResult == null)
.Select(e => e.Id)
.ToListAsync(stoppingToken);
foreach(var envelopeId in envelopeIds)
{
try
{
await sender.Send(new FinalizeEnvelopeCommand()
{
Id = envelopeId
}, stoppingToken);
}
catch(Exception ex)
{
// TODO: integrate rabbit mq and handle exception (such as delete)
logger.LogError(ex, "Unexpected exception happened during finalization of envelope. Envelope ID: {EnvelopeId}", envelopeId);
}
}
// continue here
logger.LogDebug("EnvelopeFinalizationWorker found {Count} signed envelope(s) without DOC_RESULT.", envelopeIds.Count);
}
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
{
break;
}
catch (Exception ex)
{
logger.LogError(ex, "EnvelopeFinalizationWorker failed while reading envelopes.");
}
await Task.Delay(PollInterval, stoppingToken);
}
}
}