Esempio n. 1
0
        public async Task WriteRecordsForYearAsync <T>(string filePath, int year, Func <Task <List <T> > > fillRecordsAsync)
        {
            var path      = Path.GetDirectoryName(filePath);
            var fileName  = Path.GetFileNameWithoutExtension(filePath);
            var extension = Path.GetExtension(filePath);
            var prefix    = fileName.BeforeFirst("_");

            var records = await fillRecordsAsync();

            filePath = $"{prefix}_{year}-{(year + 1).ToTwoDigitYear()}{extension}";
            if (!string.IsNullOrWhiteSpace(path))
            {
                filePath = Path.Combine(path, filePath);
            }

            await Extensions.SaveCSVAsync(_SharedBusinessLogic.FileRepository, records, filePath);
        }
Esempio n. 2
0
        public async Task WriteRecordsPerYearAsync <T>(string filePath, Func <int, Task <List <T> > > fillRecordsAsync)
        {
            var path      = Path.GetDirectoryName(filePath);
            var fileName  = Path.GetFileNameWithoutExtension(filePath);
            var extension = Path.GetExtension(filePath);
            var prefix    = fileName.BeforeFirst("_");
            var datePart  = fileName.AfterLast("_", includeWhenNoSeparator: false);

            var endYear   = _snapshotDateHelper.GetSnapshotDate(SectorTypes.Private).Year;
            var startYear = _SharedBusinessLogic.SharedOptions.FirstReportingYear;

            if (!string.IsNullOrWhiteSpace(datePart))
            {
                var start = datePart.BeforeFirst("-").ToInt32().ToFourDigitYear();
                if (start > startYear && start <= endYear)
                {
                    startYear = start;
                }

                endYear = startYear;
            }

            //Make sure start and end are in correct order
            if (startYear > endYear)
            {
                (startYear, endYear) = (endYear, startYear);
            }

            for (var year = endYear; year >= startYear; year--)
            {
                var records = await fillRecordsAsync(year);

                filePath = $"{prefix}_{year}-{(year + 1).ToTwoDigitYear()}{extension}";
                if (!string.IsNullOrWhiteSpace(path))
                {
                    filePath = Path.Combine(path, filePath);
                }

                await Extensions.SaveCSVAsync(_SharedBusinessLogic.FileRepository, records, filePath);
            }
        }
Esempio n. 3
0
        //Update GPG download file
        public async Task UpdateDownloadFilesAsync(ILogger log, string userEmail = null, bool force = false)
        {
            if (RunningJobs.Contains(nameof(UpdateDownloadFiles)))
            {
                return;
            }

            try
            {
                var returnYears = _SharedBusinessLogic.DataRepository.GetAll <Return>()
                                  .Where(r => r.Status == ReturnStatuses.Submitted)
                                  .Select(r => r.AccountingDate.Year)
                                  .Distinct()
                                  .ToList();

                //Get the downloads location
                var downloadsLocation = _SharedBusinessLogic.SharedOptions.DownloadsLocation;

                //Ensure we have a directory
                if (!await _SharedBusinessLogic.FileRepository.GetDirectoryExistsAsync(downloadsLocation))
                {
                    await _SharedBusinessLogic.FileRepository.CreateDirectoryAsync(downloadsLocation);
                }

                foreach (var year in returnYears)
                {
                    //If another server is already in process of creating a file then skip

                    var downloadFilePattern = $"GPGData_{year}-{year + 1}.csv";
                    var files = await _SharedBusinessLogic.FileRepository.GetFilesAsync(downloadsLocation,
                                                                                        downloadFilePattern);

                    var oldDownloadFilePath = files.FirstOrDefault();

                    //Skip if the file already exists and is newer than 1 hour or older than 1 year
                    if (oldDownloadFilePath != null && !force)
                    {
                        var lastWriteTime =
                            await _SharedBusinessLogic.FileRepository.GetLastWriteTimeAsync(oldDownloadFilePath);

                        if (lastWriteTime.AddHours(1) >= VirtualDateTime.Now ||
                            lastWriteTime.AddYears(2) <= VirtualDateTime.Now)
                        {
                            continue;
                        }
                    }

                    var returns = await _SharedBusinessLogic.DataRepository.GetAll <Return>().Where(r =>
                                                                                                    r.AccountingDate.Year == year &&
                                                                                                    r.Status == ReturnStatuses.Submitted &&
                                                                                                    r.Organisation.Status == OrganisationStatuses.Active)
                                  .ToListAsync();

                    returns.RemoveAll(r =>
                                      r.Organisation.OrganisationName.StartsWithI(_SharedBusinessLogic.SharedOptions.TestPrefix));

                    var downloadData = returns.ToList()
                                       .Select(r => DownloadResult.Create(r))
                                       .OrderBy(d => d.EmployerName)
                                       .ToList();

                    var newFilePath =
                        _SharedBusinessLogic.FileRepository.GetFullPath(Path.Combine(downloadsLocation,
                                                                                     $"GPGData_{year}-{year + 1}.csv"));
                    try
                    {
                        if (downloadData.Any())
                        {
                            await Extensions.SaveCSVAsync(_SharedBusinessLogic.FileRepository, downloadData,
                                                          newFilePath, oldDownloadFilePath);
                        }
                        else if (!string.IsNullOrWhiteSpace(oldDownloadFilePath))
                        {
                            await _SharedBusinessLogic.FileRepository.DeleteFileAsync(oldDownloadFilePath);
                        }
                    }
                    catch (Exception ex)
                    {
                        log.LogError(ex, ex.Message);
                    }
                }

                if (force && !string.IsNullOrWhiteSpace(userEmail))
                {
                    try
                    {
                        await _Messenger.SendMessageAsync(
                            "UpdateDownloadFiles complete",
                            userEmail,
                            "The update of the download files completed successfully.");
                    }
                    catch (Exception ex)
                    {
                        log.LogError(ex, ex.Message);
                    }
                }
            }
            finally
            {
                RunningJobs.Remove(nameof(UpdateDownloadFiles));
            }
        }