<commit_before>﻿using SFA.DAS.EAS.Domain.Data.Repositories;
using SFA.DAS.EAS.Domain.Interfaces;
using SFA.DAS.EAS.Domain.Models.Payments;
using SFA.DAS.NLog.Logger;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SFA.DAS.EAS.DbMaintenance.WebJob.Jobs
{
    public class PaymentIntegrityCheckerJob : IJob
    {
        private readonly IPaymentService _paymentService;
        private readonly IEmployerAccountRepository _employerAccountRepository;
        private readonly IDasLevyRepository _levyRepository;
        private readonly ILog _logger;

        public PaymentIntegrityCheckerJob(
            IPaymentService paymentService,
            IEmployerAccountRepository employerAccountRepository,
            IDasLevyRepository levyRepository,
            ILog logger)
        {
            _paymentService = paymentService;
            _employerAccountRepository = employerAccountRepository;
            _levyRepository = levyRepository;
            _logger = logger;
        }

        public async Task Run()
        {
            var accounts = await _employerAccountRepository.GetAllAccounts();

            var periodEnds = await _levyRepository.GetAllPeriodEnds();

            foreach (var periodEnd in periodEnds)
            {
                foreach (var account in accounts)
                {
                    var expectedPayments =
                        (IEnumerable<Payment>)await _paymentService.GetAccountPayments(periodEnd.Id,
                            account.Id);

                    var actualPayments =
                        (await _levyRepository.GetAccountPaymentsByPeriodEnd(account.Id, periodEnd.Id))
                        .ToArray();


                    var expectedPaymentsTotal = expectedPayments?.Sum(x => x.Amount) ?? 0;
                    var actualPaymentsTotal = actualPayments.Sum(x => x.Amount);

                    var paymentMissing = actualPaymentsTotal != expectedPaymentsTotal;

                    //If the totals add up we check to see if all the payments are correct
                    //just in case a duplicate or incorrect record has the same amount as a correct one
                    if (!paymentMissing)
                    {
                        paymentMissing = expectedPayments?.Except(actualPayments).Any() ?? false;<commit_msg>Refactored code based on code review<commit_after>﻿using SFA.DAS.EAS.Domain.Data.Repositories;
using SFA.DAS.EAS.Domain.Interfaces;
using SFA.DAS.NLog.Logger;
using System.Linq;
using System.Threading.Tasks;

namespace SFA.DAS.EAS.DbMaintenance.WebJob.Jobs
{
    public class PaymentIntegrityCheckerJob : IJob
    {
        private readonly IPaymentService _paymentService;
        private readonly IEmployerAccountRepository _employerAccountRepository;
        private readonly IDasLevyRepository _levyRepository;
        private readonly ILog _logger;

        public PaymentIntegrityCheckerJob(
            IPaymentService paymentService,
            IEmployerAccountRepository employerAccountRepository,
            IDasLevyRepository levyRepository,
            ILog logger)
        {
            _paymentService = paymentService;
            _employerAccountRepository = employerAccountRepository;
            _levyRepository = levyRepository;
            _logger = logger;
        }

        public async Task Run()
        {
            var accounts = await _employerAccountRepository.GetAllAccounts();

            var periodEnds = await _levyRepository.GetAllPeriodEnds();

            foreach (var periodEnd in periodEnds)
            {
                foreach (var account in accounts)
                {
                    var expectedPayments = await _paymentService.GetAccountPayments(periodEnd.Id, account.Id);

                    var actualPayments =
                        (await _levyRepository.GetAccountPaymentsByPeriodEnd(account.Id, periodEnd.Id))
                        .ToArray();


                    var expectedPaymentsTotal = expectedPayments.Sum(x => x.Amount);
                    var actualPaymentsTotal = actualPayments.Sum(x => x.Amount);

                    var paymentMissing = actualPaymentsTotal != expectedPaymentsTotal;

                    //If the totals add up we check to see if all the payments are correct
                    //just in case a duplicate or incorrect record has the same amount as a correct one
                    if (!paymentMissing)
                    {
                        paymentMissing = expectedPayments.Except(actualPayments).Any();
{'fraction_non_alphanumeric': 0.059802847754654984, 'fraction_numerical': 0.0002190580503833516, 'mean_word_length': 2.058271935699933, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1220520', 'n_tokens_mistral': 1131, 'n_tokens_neox': 1078, 'n_words': 270}