private WhiteListVerResult ExtractResultForCompanyFromResponse(bool verifyBankAccount, EntryListResponse content, InputCompany companyToVerify)
        {
            WhiteListVerResult tempWhiteListVerResult = new WhiteListVerResult()
            {
                Nip = companyToVerify.NIP,
                VerificationDate = DateTime.Now.ToString()
            };

            if (companyToVerify.BankAccountNumber is null || companyToVerify.FormatErrors.Contains(InputCompanyFormatError.BankAccountFormatError))
            {
                tempWhiteListVerResult.GivenAccountNumber = string.Empty;
            }
        private Dictionary <string, WhiteListVerResult> VerifyChunkOfCompanies(List <InputCompany> chunkOfCompaies, string nipsInString, DateTime dateOfRequest, bool verifyBankAccount)
        {
            EntryListResponse content = null;
            Dictionary <string, WhiteListVerResult> result = new Dictionary <string, WhiteListVerResult>();
            WhiteListVerResult tempWhiteListVerResult      = null;

            content = _whiteListClient.VerifyCompanies(nipsInString, dateOfRequest).GetAwaiter().GetResult();

            foreach (var companyToVerify in chunkOfCompaies)
            {
                tempWhiteListVerResult = ExtractResultForCompanyFromResponse(verifyBankAccount, content, companyToVerify);
                result.Add(companyToVerify.ID, tempWhiteListVerResult);
            }

            return(result);
        }
        /// <summary>
        /// Verifies the given list of companies. The list of companies is divided into chunks are verified in chunks
        /// to speed up the process.
        /// API restricts to have max. 10 requests of max. 30 nips
        /// see more: https://www.gov.pl/web/kas/api-wykazu-podatnikow-vat
        /// </summary>
        /// <param name="inputCompaniesToVerify"></param>
        /// <param name="verifyBankAccount">If the bank account shall be verified if exists on the list of allowed/correct bank accounts</param>
        /// <returns><code>null</code> when there are no companies to verify</returns>
        /// <exception cref="ArgumentNullException">When input companies are null.</exception>
        public Dictionary <string, WhiteListVerResult> VerifyCompanies(List <InputCompany> inputCompaniesToVerify, bool verifyBankAccount, bool verifyForInvoiceDate)
        {
            if (inputCompaniesToVerify == null)
            {
                throw new ArgumentNullException("inputCompaniesToVerify", "comapnies to verify are null.");
            }

            if (inputCompaniesToVerify.Count == 0)
            {
                return(new Dictionary <string, WhiteListVerResult>());
            }

            Dictionary <string, WhiteListVerResult> result = new Dictionary <string, WhiteListVerResult>();

            List <InputCompany> companiesToVerify = inputCompaniesToVerify.Where(c => !string.IsNullOrEmpty(c.NIP)).ToList();
            List <InputCompany> nipEmptyCompanies = inputCompaniesToVerify.Where(iC => string.IsNullOrEmpty(iC.NIP)).ToList();

            try
            {
                if (!verifyForInvoiceDate)
                {
                    for (int i = 0; i <= companiesToVerify.Count / _maxNumOfNipsPerOneRequest && i < _maxNumOfRequestsPerDay; i++)
                    {
                        var    chunkOfCompaies = companiesToVerify.Skip(i * _maxNumOfNipsPerOneRequest).Take(_maxNumOfNipsPerOneRequest).ToList();
                        string nipsInString    = GetNIPsInOneString(chunkOfCompaies);
                        Dictionary <string, WhiteListVerResult> chunkVerification = VerifyChunkOfCompanies(chunkOfCompaies, nipsInString, DateTime.Now, verifyBankAccount);
                        foreach (var verResult in chunkVerification)
                        {
                            result.Add(verResult.Key, verResult.Value);
                            _numOfNipsAskedInTheAppRun++;
                        }
                        _numOfRequestsInTheAppRun++;
                    }
                }
                else
                {
                    var groupsOfComapnies = companiesToVerify.GroupBy(c => c.InvoiceDate, c => c);
                    foreach (var groupOfComanies in groupsOfComapnies)
                    {
                        for (int i = 0; i <= groupOfComanies.Count() / _maxNumOfNipsPerOneRequest; i++)
                        {
                            var chunkOfCompaies = groupOfComanies.Skip(i * _maxNumOfNipsPerOneRequest).Take(_maxNumOfNipsPerOneRequest).ToList();
                            if (chunkOfCompaies.Count == 0)
                            {
                                continue;
                            }
                            string nipsInString = GetNIPsInOneString(chunkOfCompaies);
                            Dictionary <string, WhiteListVerResult> chunkVerification = VerifyChunkOfCompanies(chunkOfCompaies, nipsInString, chunkOfCompaies[0].InvoiceDate, verifyBankAccount);
                            foreach (var verResult in chunkVerification)
                            {
                                result.Add(verResult.Key, verResult.Value);
                                _numOfNipsAskedInTheAppRun++;
                            }
                            _numOfRequestsInTheAppRun++;
                        }
                    }
                }

                if (nipEmptyCompanies != null)
                {
                    foreach (var nipEmptyCompany in nipEmptyCompanies)
                    {
                        WhiteListVerResult resultC = new WhiteListVerResult();
                        resultC.ConfirmationResponseString = string.Empty;
                        resultC.FullName                     = string.Empty;
                        resultC.FullResidenceAddress         = string.Empty;
                        resultC.FullWorkingAddress           = string.Empty;
                        resultC.GivenAccountNumber           = nipEmptyCompany.BankAccountNumber;
                        resultC.IsActiveVATPayer             = false;
                        resultC.IsGivenAccountNumOnWhiteList = false;
                        resultC.Nip = string.Empty;
                        resultC.VerificationDate   = DateTime.Now.ToString();
                        resultC.VerificationStatus = WhiteListVerResultStatus.ErrorNIPEmpty;
                        result.Add(nipEmptyCompany.ID, resultC);
                    }
                }
            }
            catch (System.Exception e)
            {
                throw e;
            }
            return(result);
        }