Replaces obsolete DataTable-based logic in ReportCreator with async repository queries for EnvelopeReport entities. Refactors ReportItem to use explicit header and detail fields, removing legacy Envelope references. Updates report designer bindings to match new ReportItem properties. Improves exception handling and overall type safety.
73 lines
2.7 KiB
C#
73 lines
2.7 KiB
C#
using DevExpress.Xpo;
|
|
using DigitalData.Core.Abstraction.Application.Repository;
|
|
using EnvelopeGenerator.Domain.Constants;
|
|
using EnvelopeGenerator.Domain.Entities;
|
|
using EnvelopeGenerator.ServiceHost.Exceptions;
|
|
using EnvelopeGenerator.ServiceHost.Extensions;
|
|
|
|
namespace EnvelopeGenerator.ServiceHost.Jobs.FinalizeDocument;
|
|
|
|
public class ReportCreator(ILogger<ReportCreator> Logger, IRepository<EnvelopeReport> reportRepo)
|
|
{
|
|
public async Task<byte[]> CreateReportAsync(Envelope envelope, CancellationToken cancel = default)
|
|
{
|
|
try
|
|
{
|
|
Logger.LogDebug("Loading report data..");
|
|
var reports = await reportRepo.Where(r => r.EnvelopeId == envelope.Id).ToListAsync(cancel);
|
|
|
|
if (reports.Count == 0)
|
|
{
|
|
throw new CreateReportException("No report data found!");
|
|
}
|
|
|
|
var items = reports
|
|
.Select(r => ToReportItem(r, envelope))
|
|
.OrderByDescending(r => r.ItemDate)
|
|
.ToList();
|
|
|
|
var buffer = DoCreateReport(items);
|
|
Logger.LogDebug("Report created!");
|
|
|
|
return buffer;
|
|
}
|
|
catch (Exception ex) when (ex is not CreateReportException)
|
|
{
|
|
Logger.LogError(ex);
|
|
throw new CreateReportException("Could not prepare report data!", ex);
|
|
}
|
|
}
|
|
|
|
private byte[] DoCreateReport(List<ReportItem> reportItems)
|
|
{
|
|
var source = new ReportSource { Items = reportItems };
|
|
var report = new rptEnvelopeHistory { DataSource = source, DataMember = "Items" };
|
|
|
|
Logger.LogDebug("Creating report in memory..");
|
|
report.CreateDocument();
|
|
|
|
Logger.LogDebug("Exporting report to stream..");
|
|
using var stream = new MemoryStream();
|
|
report.ExportToPdf(stream);
|
|
|
|
Logger.LogDebug("Writing report to buffer..");
|
|
return stream.ToArray();
|
|
}
|
|
|
|
private static ReportItem ToReportItem(EnvelopeReport report, Envelope envelope)
|
|
{
|
|
return new ReportItem
|
|
{
|
|
CreatorFullName = envelope.User is not null
|
|
? $"{envelope.User.Prename} {envelope.User.Name}".Trim()
|
|
: string.Empty,
|
|
CreatorEmailAddress = envelope.User?.Email ?? string.Empty,
|
|
EnvelopeTitle = report.HeadTitle ?? string.Empty,
|
|
EnvelopeUuid = report.HeadUuid ?? string.Empty,
|
|
EnvelopeCertificationType = envelope.CertificationType is int certType ? ((CertificationType)certType).ToString() : "null",
|
|
ItemStatus = (EnvelopeStatus)report.PosStatus,
|
|
ItemUserReference = report.PosWho ?? string.Empty,
|
|
ItemDate = report.PosWhen ?? DateTime.MinValue
|
|
};
|
|
}
|
|
} |