diff --git a/EnvelopeGenerator.Application/Contracts/IEnvelopeHistoryService.cs b/EnvelopeGenerator.Application/Contracts/IEnvelopeHistoryService.cs index cd8bb1fb..d67d8828 100644 --- a/EnvelopeGenerator.Application/Contracts/IEnvelopeHistoryService.cs +++ b/EnvelopeGenerator.Application/Contracts/IEnvelopeHistoryService.cs @@ -15,7 +15,7 @@ namespace EnvelopeGenerator.Application.Contracts Task IsSigned(int envelopeId, string userReference); - Task> ReadAsync(int? envelopeId = null, string? userReference = null, int? status = null); + Task> ReadAsync(int? envelopeId = null, string? userReference = null, ReferenceType? referenceType = null, int? status = null); Task> ReadRejectedAsync(int envelopeId, string? userReference = null); diff --git a/EnvelopeGenerator.Application/DIExtensions.cs b/EnvelopeGenerator.Application/DIExtensions.cs new file mode 100644 index 00000000..79b33c45 --- /dev/null +++ b/EnvelopeGenerator.Application/DIExtensions.cs @@ -0,0 +1,14 @@ +using EnvelopeGenerator.Application.Contracts; +using EnvelopeGenerator.Application.Services; +using Microsoft.Extensions.DependencyInjection; +using static EnvelopeGenerator.Common.Constants; + +namespace EnvelopeGenerator.Application +{ + public static class DIExtensions + { + public static IServiceCollection AddHistoryService(this IServiceCollection services, Func? classifier = null) => services + .AddSingleton(classifier ?? EnvelopeHistoryService.DefaultClassifier) + .AddScoped(); + } +} \ No newline at end of file diff --git a/EnvelopeGenerator.Application/DTOs/EnvelopeHistory/EnvelopeHistoryDto.cs b/EnvelopeGenerator.Application/DTOs/EnvelopeHistory/EnvelopeHistoryDto.cs index e98ca6db..1667efa6 100644 --- a/EnvelopeGenerator.Application/DTOs/EnvelopeHistory/EnvelopeHistoryDto.cs +++ b/EnvelopeGenerator.Application/DTOs/EnvelopeHistory/EnvelopeHistoryDto.cs @@ -1,4 +1,7 @@ -namespace EnvelopeGenerator.Application.DTOs.EnvelopeHistory +using DigitalData.UserManager.Application.DTOs.User; +using static EnvelopeGenerator.Common.Constants; + +namespace EnvelopeGenerator.Application.DTOs.EnvelopeHistory { public record EnvelopeHistoryDto( long Id, @@ -7,6 +10,8 @@ int Status, DateTime AddedWhen, DateTime? ActionDate, + UserCreateDto? Sender, ReceiverDto? Receiver, + ReferenceType ReferenceType, string? Comment = null); } \ No newline at end of file diff --git a/EnvelopeGenerator.Application/Services/EnvelopeHistoryService.cs b/EnvelopeGenerator.Application/Services/EnvelopeHistoryService.cs index 30509af7..879570cc 100644 --- a/EnvelopeGenerator.Application/Services/EnvelopeHistoryService.cs +++ b/EnvelopeGenerator.Application/Services/EnvelopeHistoryService.cs @@ -50,11 +50,15 @@ namespace EnvelopeGenerator.Application.Services status: (int)EnvelopeStatus.DocumentRejected) > 0; } - public async Task> ReadAsync(int? envelopeId = null, string? userReference = null, int? status = null) => _mapper.MapOrThrow>( - await _repository.ReadAsync( - envelopeId: envelopeId, - userReference: userReference, - status: status)); + public async Task> ReadAsync(int? envelopeId = null, string? userReference = null, ReferenceType? referenceType = null, int? status = null) + { + var histDTOs = _mapper.MapOrThrow>( + await _repository.ReadAsync( + envelopeId: envelopeId, + userReference: userReference, + status: status)); + return referenceType is null ? histDTOs : histDTOs.Where(h => h.ReferenceType == referenceType); + } public async Task> ReadRejectedAsync(int envelopeId, string? userReference = null) => await ReadAsync(envelopeId: envelopeId, userReference: userReference, status: (int)EnvelopeStatus.DocumentRejected); diff --git a/EnvelopeGenerator.Common/Constants.vb b/EnvelopeGenerator.Common/Constants.vb index bf1cf589..9b8ce7ed 100644 --- a/EnvelopeGenerator.Common/Constants.vb +++ b/EnvelopeGenerator.Common/Constants.vb @@ -26,6 +26,13 @@ MessageCompletionSent = 3005 End Enum + Public Enum ReferenceType + Receiver + Sender + System + Unknown + End Enum + Public Enum ElementStatus Created = 0 End Enum diff --git a/EnvelopeGenerator.Domain/Entities/EnvelopeHistory.cs b/EnvelopeGenerator.Domain/Entities/EnvelopeHistory.cs index 0595d379..5c0f0cd1 100644 --- a/EnvelopeGenerator.Domain/Entities/EnvelopeHistory.cs +++ b/EnvelopeGenerator.Domain/Entities/EnvelopeHistory.cs @@ -1,5 +1,7 @@ -using System.ComponentModel.DataAnnotations; +using DigitalData.UserManager.Domain.Entities; +using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using static EnvelopeGenerator.Common.Constants; namespace EnvelopeGenerator.Domain.Entities { @@ -34,10 +36,18 @@ namespace EnvelopeGenerator.Domain.Entities [Column("COMMENT", TypeName = "nvarchar(max)")] public string? Comment { get; set; } - //[ForeignKey("UserReference")] - //public virtual DigitalData.UserManager.Domain.Entities.User? Sender { get; set; } + [ForeignKey("UserReference")] + public virtual User? Sender { get; set; } [ForeignKey("UserReference")] public virtual Receiver? Receiver { get; set; } + + [NotMapped] + public ReferenceType ReferenceType => (Status / 3) switch + { + 1 => ReferenceType.Sender, + 2 or 3 => ReferenceType.Receiver, + _ => ReferenceType.Unknown, + }; } } \ No newline at end of file diff --git a/EnvelopeGenerator.Infrastructure/Contracts/IEnvelopeHistoryRepository.cs b/EnvelopeGenerator.Infrastructure/Contracts/IEnvelopeHistoryRepository.cs index efe9a74a..47109f1e 100644 --- a/EnvelopeGenerator.Infrastructure/Contracts/IEnvelopeHistoryRepository.cs +++ b/EnvelopeGenerator.Infrastructure/Contracts/IEnvelopeHistoryRepository.cs @@ -7,6 +7,6 @@ namespace EnvelopeGenerator.Infrastructure.Contracts { Task CountAsync(int? envelopeId = null, string? userReference = null, int? status = null); - Task> ReadAsync(int? envelopeId = null, string? userReference = null, int? status = null, bool withReceiver = true); + Task> ReadAsync(int? envelopeId = null, string? userReference = null, int? status = null, bool withSender = false, bool withReceiver = false); } } \ No newline at end of file diff --git a/EnvelopeGenerator.Infrastructure/EGDbContext.cs b/EnvelopeGenerator.Infrastructure/EGDbContext.cs index 895af99f..4028b4c2 100644 --- a/EnvelopeGenerator.Infrastructure/EGDbContext.cs +++ b/EnvelopeGenerator.Infrastructure/EGDbContext.cs @@ -54,6 +54,12 @@ namespace DigitalData.UserManager.Infrastructure.Repositories .HasForeignKey(eh => eh.UserReference) .HasPrincipalKey(e => e.EmailAddress); + modelBuilder.Entity() + .HasOne(eh => eh.Sender) + .WithMany() + .HasForeignKey(eh => eh.UserReference) + .HasPrincipalKey(e => e.Email); + // Configure entities to handle database triggers modelBuilder.Entity().ToTable(tb => tb.HasTrigger("TBSIG_ENVELOPE_HISTORY_AFT_INS")); modelBuilder.Entity().ToTable(tb => tb.HasTrigger("TBSIG_ENVELOPE_HISTORY_AFT_INS")); diff --git a/EnvelopeGenerator.Infrastructure/Repositories/EnvelopeHistoryRepository.cs b/EnvelopeGenerator.Infrastructure/Repositories/EnvelopeHistoryRepository.cs index a6db9fc5..2c3c64c3 100644 --- a/EnvelopeGenerator.Infrastructure/Repositories/EnvelopeHistoryRepository.cs +++ b/EnvelopeGenerator.Infrastructure/Repositories/EnvelopeHistoryRepository.cs @@ -12,7 +12,7 @@ namespace EnvelopeGenerator.Infrastructure.Repositories { } - private IQueryable By(int? envelopeId = null, string? userReference = null, int? status = null, bool withReceiver = false) + private IQueryable By(int? envelopeId = null, string? userReference = null, int? status = null, bool withSender = false, bool withReceiver = false) { var query = _dbSet.AsQueryable(); @@ -25,7 +25,10 @@ namespace EnvelopeGenerator.Infrastructure.Repositories if (status is not null) query = query.Where(eh => eh.Status == status); - if(withReceiver) + if (withSender) + query = query.Include(eh => eh.Sender); + + if (withReceiver) query = query.Include(eh => eh.Receiver); return query; @@ -36,10 +39,12 @@ namespace EnvelopeGenerator.Infrastructure.Repositories userReference: userReference, status: status).CountAsync(); - public async Task> ReadAsync(int? envelopeId = null, string? userReference = null, int? status = null, bool withReceiver = true) => await By( + public async Task> ReadAsync(int? envelopeId = null, string? userReference = null, int? status = null, bool withSender = false, bool withReceiver = false) + => await By( envelopeId: envelopeId, userReference: userReference, status: status, + withSender: withSender, withReceiver: withReceiver).ToListAsync(); } } \ No newline at end of file diff --git a/EnvelopeGenerator.Web/Program.cs b/EnvelopeGenerator.Web/Program.cs index bb73c019..6751660e 100644 --- a/EnvelopeGenerator.Web/Program.cs +++ b/EnvelopeGenerator.Web/Program.cs @@ -100,11 +100,11 @@ try builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); - builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); + builder.Services.AddHistoryService(); //Auto mapping profiles builder.Services.AddAutoMapper(typeof(BasicDtoMappingProfile).Assembly);