/// <inheritdoc/>
        public async Task <UpdatedContractStatusResponse> ConfirmApprovalAsync(UpdateConfirmApprovalRequest request)
        {
            _logger.LogInformation($"[{nameof(ConfirmApprovalAsync)}] called with contract number: {request.ContractNumber} and contract version {request.ContractVersion}.");

            var contract = await _repository.GetByContractNumberAndVersionWithIncludesAsync(request.ContractNumber, request.ContractVersion, ContractDataEntityInclude.Datas);

            ContractStatus newContractStatus = ContractStatus.Approved;

            _contractValidator.Validate(contract, request);
            _contractValidator.ValidateStatusChange(contract, newContractStatus);

            var updatedContractStatusResponse = new UpdatedContractStatusResponse
            {
                Id              = contract.Id,
                ContractNumber  = contract.ContractNumber,
                ContractVersion = contract.ContractVersion,
                Ukprn           = contract.Ukprn,
                Status          = (ContractStatus)contract.Status,
                Action          = ActionType.ContractConfirmApproval
            };

            contract.Status = (int)newContractStatus;

            await _contractDocumentService.UpsertOriginalContractXmlAsync(contract, request);

            await _repository.UpdateContractAsync(contract);

            updatedContractStatusResponse.NewStatus = (ContractStatus)contract.Status;
            await _auditService.TrySendAuditAsync(GetAudit(updatedContractStatusResponse));

            return(updatedContractStatusResponse);
        }
        public async Task UpsertOriginalContractXmlAsync_ExpectedResult_Test()
        {
            //Arrange
            var contractDocumentService = BuildContractDocumentService();
            var request = new UpdateConfirmApprovalRequest()
            {
                ContractNumber = "Main-0002", ContractVersion = 2, FileName = BlobHelper.BlobName
            };
            var contract = new DataModels.Contract()
            {
                Id = 7,
                ContractVersion = 2,
                ContractNumber  = "Main-0002",
                ContractData    = new DataModels.ContractData()
                {
                    Id = 7,
                    OriginalContractXml = "<contract>sample.xml.data</contract>"
                }
            };

            //Act
            await contractDocumentService.UpsertOriginalContractXmlAsync(contract, request);

            //Assert
            contractDocumentService.Should().NotBeNull();
            contract.ContractData.OriginalContractXml.Should().Be(BlobHelper.BlobSampleContent);
        }
        public async Task UpdateContractConfirmApproval_WithDefaultParameters_Returns404Response()
        {
            // Arrange
            var content = new UpdateConfirmApprovalRequest()
            {
                ContractNumber  = "Levy-0099",
                ContractVersion = 1,
                FileName        = _blobName
            };

            // Act
            var response = await _testClient.PatchAsync(_confirmApproval, GetStringContent(content));

            // Assert
            response.StatusCode.Should().Be(HttpStatusCode.NotFound);
        }
        public async Task UpdateContractConfirmApprovalAsync_ReturnsExpectedResult_Test()
        {
            //Arrange
            SetMapperHelper();
            string       baseUrl        = $"https://localhost:5001";
            const string contractNumber = "Main-0001";
            const string title          = "Test Title";

            var working = new DataModels.Contract {
                Id = 1, Title = title, ContractNumber = contractNumber, ContractVersion = 1, Ukprn = 12345678, Status = (int)ContractStatus.ApprovedWaitingConfirmation
            };

            var request = new UpdateConfirmApprovalRequest()
            {
                ContractNumber = "Main-0001", ContractVersion = 1, FileName = BlobHelper.BlobName
            };

            ILoggerAdapter <ContractService>    logger     = new LoggerAdapter <ContractService>(new Logger <ContractService>(new LoggerFactory()));
            ILoggerAdapter <ContractRepository> loggerRepo = new LoggerAdapter <ContractRepository>(new Logger <ContractRepository>(new LoggerFactory()));

            MockAuditService();

            var inMemPdsDbContext = HelperExtensions.GetInMemoryPdsDbContext();
            var repo         = new Repository <DataModels.Contract>(inMemPdsDbContext);
            var work         = new SingleUnitOfWorkForRepositories(inMemPdsDbContext);
            var contractRepo = new ContractRepository(repo, work, loggerRepo);
            var uriService   = new UriService(baseUrl);
            var asposeDocumentManagementContractService = GetDocumentService();
            var contractValidationService = GetContractValidationService();
            var mediator = BuildMediator();
            var contractDocumentService = BuildContractDocumentService();
            var service = new ContractService(contractRepo, _mapper, uriService, logger, _mockAuditService.Object, _semaphoreOnEntity, asposeDocumentManagementContractService, contractValidationService, mediator, contractDocumentService);

            await repo.AddAsync(working);

            await work.CommitAsync();

            //Act
            var beforeUpdate = await contractRepo.GetByContractNumberAndVersionWithIncludesAsync(request.ContractNumber, request.ContractVersion, ContractDataEntityInclude.Datas);

            // assigning to a new variable before this is an in memory db so the
            // LastEmailReminderSent was being populated.
            var actualBeforeUpdate = new DataModels.Contract()
            {
                Id              = beforeUpdate.Id,
                Title           = beforeUpdate.Title,
                ContractNumber  = beforeUpdate.ContractNumber,
                ContractVersion = beforeUpdate.ContractVersion,
                Ukprn           = beforeUpdate.Ukprn,
                Status          = beforeUpdate.Status
            };

            var contract = await service.ConfirmApprovalAsync(request);

            var afterUpdate = await contractRepo.GetByContractNumberAndVersionWithIncludesAsync(request.ContractNumber, request.ContractVersion, ContractDataEntityInclude.Datas);

            //Assert
            contract.Should().NotBeNull();
            actualBeforeUpdate.Status.Should().Be((int)ContractStatus.ApprovedWaitingConfirmation);
            afterUpdate.Status.Should().Be((int)ContractStatus.Approved);
            afterUpdate.ContractData.OriginalContractXml.Should().Be(BlobHelper.BlobSampleContent);
        }