Esempio n. 1
0
        private async Task UploadCertificateDetailsToPinter()
        {
            try
            {
                _aggregateLogger.LogInfo("Print Process Function Started");

                var scheduleRun = await _assessorServiceApi.GetSchedule(ScheduleType.PrintRun);

                if (scheduleRun == null)
                {
                    _aggregateLogger.LogInfo("Print Function not scheduled to run at this time.");
                    return;
                }

                var batchLogResponse = await _assessorServiceApi.GetCurrentBatchLog();

                var batchNumber  = batchLogResponse.BatchNumber + 1;
                var certificates = (await _assessorServiceApi.GetCertificatesToBePrinted()).ToList()
                                   .Sanitise(_aggregateLogger);

                if (certificates.Count == 0)
                {
                    _aggregateLogger.LogInfo("No certificates to process");
                }
                else
                {
                    var certificateFileName =
                        $"IFA-Certificate-{DateTime.UtcNow.UtcToTimeZoneTime():MMyy}-{batchNumber.ToString().PadLeft(3, '0')}.json";
                    var excelFileName = $"IFA-Certificate-{DateTime.UtcNow.UtcToTimeZoneTime()}-{batchNumber.ToString().PadLeft(3, '0')}.xlsx";

                    var batchLogRequest = new CreateBatchLogRequest
                    {
                        BatchNumber         = batchNumber,
                        FileUploadStartTime = DateTime.UtcNow,
                        Period               = DateTime.UtcNow.UtcToTimeZoneTime().ToString("MMyy"),
                        BatchCreated         = DateTime.UtcNow,
                        CertificatesFileName = certificateFileName
                    };

                    var configuration = _configurationWrapper.GetConfiguration();

                    if (configuration.Sftp.UseJson)
                    {
                        _printingJsonCreator.Create(batchNumber, certificates, certificateFileName);
                        await _notificationService.Send(batchNumber, certificates, certificateFileName);
                    }
                    else
                    {
                        _printingSpreadsheetCreator.Create(batchNumber, certificates);
                        await _notificationService.Send(batchNumber, certificates, excelFileName);
                    }

                    batchLogRequest.FileUploadEndTime    = DateTime.UtcNow;
                    batchLogRequest.NumberOfCertificates = certificates.Count;
                    batchLogRequest.NumberOfCoverLetters = 0;
                    batchLogRequest.ScheduledDate        = batchLogResponse.ScheduledDate;

                    await _fileTransferClient.LogUploadDirectory();

                    await _assessorServiceApi.CreateBatchLog(batchLogRequest);

                    await _assessorServiceApi.ChangeStatusToPrinted(batchNumber, certificates);
                }
                await _assessorServiceApi.CompleteSchedule(scheduleRun.Id);
            }
            catch (Exception e)
            {
                _aggregateLogger.LogError("Function Errored", e);
                throw;
            }
        }