using AutoMapper; using DigitalData.Core.Application; using EnvelopeGenerator.Domain.Entities; using EnvelopeGenerator.Application.Interfaces.Repositories; using EnvelopeGenerator.Application.Dto.EnvelopeHistory; using EnvelopeGenerator.Application.Dto.Receiver; using EnvelopeGenerator.Application.Interfaces.Services; using DigitalData.Core.Abstraction.Application.DTO; using EnvelopeGenerator.Domain.Constants; namespace EnvelopeGenerator.Application.Services; /// /// /// [Obsolete("Use MediatR")] public class EnvelopeHistoryService : CRUDService, IEnvelopeHistoryService { /// /// /// /// /// public EnvelopeHistoryService(IEnvelopeHistoryRepository repository, IMapper mapper) : base(repository, mapper) { } /// /// / /// /// /// /// /// public async Task CountAsync(int? envelopeId = null, string? userReference = null, EnvelopeStatus? status = null) => await _repository.CountAsync(envelopeId: envelopeId, userReference: userReference, status: status); /// /// /// /// /// /// /// public async Task HasStatus(EnvelopeStatus status, int envelopeId, string userReference) => await _repository.CountAsync( envelopeId: envelopeId, userReference: userReference, status: status) > 0; /// /// /// /// /// /// public async Task AccessCodeAlreadyRequested(int envelopeId, string userReference) => await _repository.CountAsync( envelopeId: envelopeId, userReference:userReference, status: EnvelopeStatus.AccessCodeRequested) > 0; /// /// /// /// /// /// public async Task IsSigned(int envelopeId, string userReference) => await _repository.CountAsync( envelopeId: envelopeId, userReference: userReference, status: EnvelopeStatus.DocumentSigned) > 0; /// /// Checks if the specified envelope has been rejected. /// Note: If any document within the envelope is rejected, the entire envelope will be considered rejected. /// /// The ID of the envelope to check. /// Optional user reference associated with the envelope. /// A task that represents the asynchronous operation. The task result contains a boolean value indicating whether the envelope is rejected. public async Task IsRejected(int envelopeId, string? userReference = null) { return await _repository.CountAsync( envelopeId: envelopeId, userReference: userReference, status: EnvelopeStatus.DocumentRejected) > 0; } /// /// /// /// /// /// /// /// /// /// public async Task> ReadAsync(int? envelopeId = null, string? userReference = null, ReferenceType? referenceType = null, EnvelopeStatus? status = null, bool withSender = false, bool withReceiver = false) { var histDTOs = _mapper.Map>( await _repository.ReadAsync( envelopeId: envelopeId, userReference: userReference, status: status, withSender: withSender, withReceiver: withReceiver)); 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: EnvelopeStatus.DocumentRejected, withReceiver:true); //TODO: use IQueryable in repository to incerease the performance /// /// /// /// /// public async Task> ReadRejectingReceivers(int envelopeId) { var envelopes = await ReadRejectedAsync(envelopeId); return envelopes is null ? Enumerable.Empty() : envelopes .Where(eh => eh?.Receiver != null) .Select(eh => eh.Receiver!); } /// /// /// /// /// /// /// /// public async Task> RecordAsync(int envelopeId, string userReference, EnvelopeStatus status, string? comment = null) => await CreateAsync(new () { EnvelopeId = envelopeId, UserReference = userReference, Status = (int) status, ActionDate = DateTime.Now, Comment = comment }) .ThenAsync( Success: dto => Result.Success(dto.Id), Fail: (mssg, ntc) => Result.Fail().Message(mssg).Notice(ntc) ); }