using DevExpress.Xpo; using DigitalData.Core.Abstraction.Application.Repository; using EnvelopeGenerator.Domain.Constants; using EnvelopeGenerator.Domain.Entities; using EnvelopeGenerator.ServiceHost.Exceptions; namespace EnvelopeGenerator.ServiceHost.Jobs.FinalizeDocument; public class ReportCreator(IRepository reportRepo) { public async Task CreateReportAsync(Envelope envelope, CancellationToken cancel = default) { var reports = await reportRepo.Where(r => r.EnvelopeId == envelope.Id).ToListAsync(cancel); if (reports.Count == 0) throw new CreateReportException("Could not prepare report data! No report data found!"); var items = reports .Select(r => ToReportItem(r, envelope)) .OrderByDescending(r => r.ItemDate) .ToList(); var source = new ReportSource { Items = items }; var report = new rptEnvelopeHistory { DataSource = source, DataMember = "Items" }; report.CreateDocument(); using var stream = new MemoryStream(); report.ExportToPdf(stream); 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 }; } }