public async Task RunUpdate()
        {
            _logger.Info("Retrieving last DataLock Event Id from repository");
            var lastId = await _dataLockRepository.GetLastDataLockEventId();

            while (true)
            {
                _logger.Info($"Retrieving page of data from Payment Events Service since Event Id {lastId}");
                var stopwatch = Stopwatch.StartNew();
                var page      = (await _paymentEventsSerivce.GetDataLockEvents(lastId)).ToList();
                stopwatch.Stop();
                _logger.Info($"Response took {stopwatch.ElapsedMilliseconds}ms");

                if (!page.Any())
                {
                    _logger.Info("No data returned; exiting");
                    break;
                }

                _logger.Info($"{page.Count} records returned in page");

                foreach (var dataLockStatus in page)
                {
                    _logger.Info($"Read datalock Apprenticeship {dataLockStatus.ApprenticeshipId} " +
                                 $"Event Id {dataLockStatus.DataLockEventId} Status {dataLockStatus.ErrorCode} and EventStatus: {dataLockStatus.EventStatus}");

                    var datalockSuccess = dataLockStatus.ErrorCode == DataLockErrorCode.None;

                    if (!datalockSuccess)
                    {
                        ApplyErrorCodeWhiteList(dataLockStatus);
                    }

                    var is1617 = dataLockStatus.GetDateFromPriceEpisodeIdentifier() < _1718AcademicYearStartDate;
                    if (is1617)
                    {
                        _logger.Info($"Data lock Event Id {dataLockStatus.DataLockEventId} pertains to 16/17 academic year and will be ignored");
                    }

                    if ((datalockSuccess || dataLockStatus.ErrorCode != DataLockErrorCode.None) && !is1617)
                    {
                        var apprenticeship = await _apprenticeshipRepository.GetApprenticeship(dataLockStatus.ApprenticeshipId);

                        //temporarily ignore dlock7 & 9 combos until payments R14 fixes properly
                        if (dataLockStatus.ErrorCode.HasFlag(DataLockErrorCode.Dlock07) && dataLockStatus.IlrEffectiveFromDate < apprenticeship.StartDate)
                        {
                            _logger.Info($"Ignoring datalock for Apprenticeship #{dataLockStatus.ApprenticeshipId} Dlock07 with Effective Date before Start Date. Event Id {dataLockStatus.DataLockEventId}");
                        }
                        else
                        {
                            _logger.Info($"Updating Apprenticeship {dataLockStatus.ApprenticeshipId} " +
                                         $"Event Id {dataLockStatus.DataLockEventId} Status {dataLockStatus.ErrorCode}");

                            AutoResolveDataLockIfApprenticeshipStoppedAndBackdated(apprenticeship, dataLockStatus);

                            try
                            {
                                await _dataLockRepository.UpdateDataLockStatus(dataLockStatus);

                                await _filterAcademicYearRolloverDataLocks.Filter(dataLockStatus.ApprenticeshipId);
                            }
                            catch (RepositoryConstraintException ex) when(_config.IgnoreDataLockStatusConstraintErrors)
                            {
                                _logger.Warn(ex, $"Exception in DataLock updater");
                            }

                            if (datalockSuccess)
                            {
                                await _apprenticeshipRepository.SetHasHadDataLockSuccess(dataLockStatus.ApprenticeshipId);

                                var pendingUpdate = await
                                                    _apprenticeshipUpdateRepository.GetPendingApprenticeshipUpdate(dataLockStatus.ApprenticeshipId);

                                if (pendingUpdate != null && (pendingUpdate.Cost != null || pendingUpdate.TrainingCode != null))
                                {
                                    await _apprenticeshipUpdateRepository.ExpireApprenticeshipUpdate(pendingUpdate.Id);

                                    _logger.Info($"Pending ApprenticeshipUpdate {pendingUpdate.Id} expired due to successful data lock event {dataLockStatus.DataLockEventId}");
                                }
                            }
                        }
                    }

                    lastId = dataLockStatus.DataLockEventId;
                }
            }
        }
Exemplo n.º 2
0
        public async Task RunUpdate()
        {
            _logger.Info("Retrieving last DataLock Event Id from repository");
            var lastId = await _dataLockRepository.GetLastDataLockEventId();

            while (true)
            {
                _logger.Info($"Retrieving page of data from Payment Events Service since Event Id {lastId}");
                var stopwatch = Stopwatch.StartNew();
                var page      = (await _paymentEventsSerivce.GetDataLockEvents(lastId)).ToList();
                stopwatch.Stop();
                _logger.Info($"Response took {stopwatch.ElapsedMilliseconds}ms");

                if (!page.Any())
                {
                    _logger.Info("No data returned; exiting");
                    break;
                }

                _logger.Info($"{page.Count} records returned in page");

                foreach (var dataLockStatus in page)
                {
                    _logger.Info($"Read datalock Apprenticeship {dataLockStatus.ApprenticeshipId} " +
                                 $"Event Id {dataLockStatus.DataLockEventId} Status {dataLockStatus.ErrorCode} and EventStatus: {dataLockStatus.EventStatus}");

                    var datalockSuccess = dataLockStatus.ErrorCode == DataLockErrorCode.None;

                    if (!datalockSuccess)
                    {
                        ApplyErrorCodeWhiteList(dataLockStatus);
                    }

                    if (datalockSuccess || dataLockStatus.ErrorCode != DataLockErrorCode.None)
                    {
                        _logger.Info($"Updating Apprenticeship {dataLockStatus.ApprenticeshipId} " +
                                     $"Event Id {dataLockStatus.DataLockEventId} Status {dataLockStatus.ErrorCode}");

                        try
                        {
                            await _dataLockRepository.UpdateDataLockStatus(dataLockStatus);

                            await _filterAcademicYearRolloverDataLocks.Filter(dataLockStatus.ApprenticeshipId);
                        }
                        catch (RepositoryConstraintException ex) when(_config.IgnoreDataLockStatusConstraintErrors)
                        {
                            _logger.Warn(ex, $"Exception in DataLock updater");
                        }

                        if (datalockSuccess)
                        {
                            var apprenticeship = await _apprenticeshipRepository.GetApprenticeship(dataLockStatus.ApprenticeshipId);

                            if (!apprenticeship.HasHadDataLockSuccess)
                            {
                                await _apprenticeshipRepository.SetHasHadDataLockSuccess(apprenticeship.Id);
                            }

                            var pendingUpdate = await
                                                _apprenticeshipUpdateRepository.GetPendingApprenticeshipUpdate(dataLockStatus.ApprenticeshipId);

                            if (pendingUpdate != null && (pendingUpdate.Cost != null || pendingUpdate.TrainingCode != null))
                            {
                                await _apprenticeshipUpdateRepository.ExpireApprenticeshipUpdate(pendingUpdate.Id);

                                _logger.Info($"Pending ApprenticeshipUpdate {pendingUpdate.Id} expired due to successful data lock event {dataLockStatus.DataLockEventId}");
                            }
                        }
                    }

                    lastId = dataLockStatus.DataLockEventId;
                }
            }
        }