private async Task <Batch> ProcessFile(PrintFileInfo fileInfo)
        {
            var receipt = JsonConvert.DeserializeObject <PrintReceipt>(fileInfo.FileContent);

            if (receipt?.Batch == null || receipt.Batch.BatchDate == DateTime.MinValue)
            {
                fileInfo.InvalidFileContent = fileInfo.FileContent;
                throw new FileFormatValidationException($"Could not process print response file [{fileInfo.FileName}] due to invalid file format");
            }

            var batch = await _batchService.Get(receipt.Batch.BatchNumber);

            if (batch == null)
            {
                fileInfo.InvalidFileContent = fileInfo.FileContent;
                throw new FileFormatValidationException($"Could not process print response file [{fileInfo.FileName}] due to non matching Batch Number [{receipt.Batch.BatchNumber}]");
            }

            batch.BatchCreated         = receipt.Batch.BatchDate;
            batch.NumberOfCoverLetters = receipt.Batch.PostalContactCount;
            batch.NumberOfCertificates = receipt.Batch.TotalCertificateCount;
            batch.PrintedDate          = receipt.Batch.ProcessedDate;
            batch.DateOfResponse       = DateTime.UtcNow;
            batch.Status       = CertificateStatus.Printed;
            batch.Certificates = await _batchService.GetCertificatesForBatchNumber(batch.BatchNumber);

            return(batch);
        }
Beispiel #2
0
        public async Task <List <CertificatePrintStatusUpdateMessage> > Execute()
        {
            var printStatusUpdateMessages = new List <CertificatePrintStatusUpdateMessage>();

            _logger.Log(LogLevel.Information, "PrintDeliveryNotificationCommand - Started");

            var fileNames = await _externalFileTransferClient.GetFileNames(_options.Directory, FilePatternRegEx, false);

            if (!fileNames.Any())
            {
                _logger.LogInformation("No certificate delivery notifications from the printer are available to process");
                return(null);
            }

            var sortedFileNames = fileNames.ToList().SortByDateTimePattern(DateTimePatternRegEx, DateTimePatternFormat);

            foreach (var fileName in sortedFileNames)
            {
                try
                {
                    var fileContents = await _externalFileTransferClient.DownloadFile($"{_options.Directory}/{fileName}");

                    var fileInfo = new PrintFileInfo(fileContents, fileName);

                    try
                    {
                        var messages = ProcessDeliveryNotifications(fileInfo);

                        if (fileInfo.ValidationMessages.Count > 0)
                        {
                            _logger.LogError($"The delivery notification file [{fileInfo.FileName}] contained invalid entries, an error file has been created");
                            await CreateErrorFile(fileInfo, _options.Directory, _options.ErrorDirectory);
                        }

                        await ArchiveFile(fileContents, fileName, _options.Directory, _options.ArchiveDirectory);

                        printStatusUpdateMessages.AddRange(messages);
                    }
                    catch (FileFormatValidationException ex)
                    {
                        fileInfo.ValidationMessages.Add(ex.Message);
                        await CreateErrorFile(fileInfo, _options.Directory, _options.ErrorDirectory);

                        throw;
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, $"Could not process delivery notification file [{fileName}]");
                }
            }

            return(printStatusUpdateMessages);
        }
        public async Task <List <CertificatePrintStatusUpdateMessage> > Execute()
        {
            List <CertificatePrintStatusUpdateMessage> printStatusUpdateMessages = new List <CertificatePrintStatusUpdateMessage>();

            _logger.Log(LogLevel.Information, "PrintResponseCommand - Started");

            var fileNames = await _externalFileTransferClient.GetFileNames(_options.Directory, FilePatternRegEx, false);

            if (!fileNames.Any())
            {
                _logger.Log(LogLevel.Information, "There are no certificate print responses from the printer to process");
                return(null);
            }

            var sortedFileNames = fileNames.ToList().SortByDateTimePattern(DateTimePatternRegEx, DateTimePatternFormat);

            foreach (var fileName in sortedFileNames)
            {
                var fileContents = await _externalFileTransferClient.DownloadFile($"{_options.Directory}/{fileName}");

                var fileInfo = new PrintFileInfo(fileContents, fileName);

                try
                {
                    try
                    {
                        var batch = await ProcessFile(fileInfo);

                        var messages = await _batchService.Update(batch);

                        await ArchiveFile(fileContents, fileName, _options.Directory, _options.ArchiveDirectory);

                        printStatusUpdateMessages.AddRange(messages);
                    }
                    catch (FileFormatValidationException ex)
                    {
                        fileInfo.ValidationMessages.Add(ex.Message);
                        await CreateErrorFile(fileInfo, _options.Directory, _options.ErrorDirectory);

                        throw;
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, $"Could not process print response file [{fileName}]");
                }
            }

            return(printStatusUpdateMessages);
        }
        protected async Task CreateErrorFile(PrintFileInfo fileInfo, string downloadDirectoryName, string errorDirectoryName)
        {
            var errorFileName = fileInfo.FileName;

            var exists = await _internalFileTransferClient.FileExists($"{errorDirectoryName}/{fileInfo.FileName}");

            if (exists.GetValueOrDefault(false))
            {
                errorFileName = errorFileName.Replace(".json", $"_{DateTime.UtcNow:ddMMyyHHmmss}.json");
            }

            var errorFileContents = JsonConvert.SerializeObject(fileInfo);

            await _internalFileTransferClient.UploadFile(errorFileContents, $"{errorDirectoryName}/{errorFileName}");
        }
Beispiel #5
0
        private List <CertificatePrintStatusUpdateMessage> ProcessDeliveryNotifications(PrintFileInfo fileInfo)
        {
            var receipt = JsonConvert.DeserializeObject <DeliveryReceipt>(fileInfo.FileContent);

            if (receipt?.DeliveryNotifications == null)
            {
                fileInfo.InvalidFileContent = fileInfo.FileContent;
                throw new FileFormatValidationException($"Could not process delivery notification file [{fileInfo.FileName}] due to invalid file format");
            }

            return(receipt.DeliveryNotifications.Select(n => new CertificatePrintStatusUpdateMessage
            {
                CertificateReference = n.CertificateNumber,
                BatchNumber = n.BatchID,
                Status = n.Status,
                StatusAt = n.StatusChangeDate,
                ReasonForChange = n.Reason
            }).ToList());
        }