/// <summary>
        /// Updates the PreApproved Customers configuration.
        /// </summary>
        /// <param name="preApprovedCustomers">The new list of PreApproved Customers.</param>
        /// <returns>The updated PreApprovedCustomers configuration.</returns>
        public async Task <PreApprovedCustomersViewModel> UpdateAsync(PreApprovedCustomersViewModel preApprovedCustomers)
        {
            preApprovedCustomers.AssertNotNull(nameof(preApprovedCustomers));

            PreApprovedCustomersList customerList = new PreApprovedCustomersList();

            if (preApprovedCustomers.IsEveryCustomerPreApproved)
            {
                string[] ids = new string[] { Guid.Empty.ToString() };
                customerList.CustomerIds = ids.ToList();
            }
            else
            {
                customerList.CustomerIds = preApprovedCustomers.CustomerIds;
            }

            var preApprovedCustomersBlob = await this.GetPreApprovedCustomersBlob();

            await preApprovedCustomersBlob.UploadTextAsync(JsonConvert.SerializeObject(customerList));

            // invalidate the cache, we do not update it to avoid race condition between web instances
            await this.ApplicationDomain.CachingService.ClearAsync(PreApprovedCustomersRepository.PreApprovedCustomersCacheKey);

            return(await this.RetrieveCustomerDetailsAsync());
        }
        public async Task <PreApprovedCustomersViewModel> UpdatePreApprovedCustomersConfiguration(PreApprovedCustomersViewModel preApprovedCustomers)
        {
            // Save to repository.
            PreApprovedCustomersViewModel updatedCustomers = await ApplicationDomain.Instance.PreApprovedCustomersRepository.UpdateAsync(preApprovedCustomers).ConfigureAwait(false);

            return(updatedCustomers);
        }
        /// <summary>
        /// Retrieves the PreApproved Customers for rendering in UX.
        /// </summary>
        /// <returns>The PreApproved Customers.</returns>
        public async Task <PreApprovedCustomersViewModel> RetrieveCustomerDetailsAsync()
        {
            // retrieve the list of customers from Partner Center.
            IAggregatePartner sdkClient    = ApplicationDomain.Instance.PartnerCenterClient;
            List <Customer>   allCustomers = new List <Customer>();

            // create a customer enumerator which will aid us in traversing the customer pages
            Enumerators.IResourceCollectionEnumerator <PartnerCenter.Models.SeekBasedResourceCollection <Customer> > customersEnumerator = sdkClient.Enumerators.Customers.Create(sdkClient.Customers.Query(QueryFactory.Instance.BuildIndexedQuery(100)));
            while (customersEnumerator.HasValue)
            {
                foreach (Customer c in customersEnumerator.Current.Items)
                {
                    allCustomers.Add(c);
                }

                customersEnumerator.Next();
            }

            // if all customers are preapproved then every customer's IsPreApproved is true.
            bool allCustomersPreApproved = false;
            PreApprovedCustomersList currentPreApprovedCustomers = await RetrieveAsync().ConfigureAwait(false);

            if (currentPreApprovedCustomers.CustomerIds != null)
            {
                // Find if the all customers approved entry is present.
                allCustomersPreApproved = currentPreApprovedCustomers.CustomerIds.Any(cid => cid == Guid.Empty.ToString());
            }

            // populate portal customer list.
            List <PortalCustomer> preApprovedCustomerDetails = (from customer in allCustomers
                                                                select new PortalCustomer()
            {
                TenantId = customer.Id,
                CompanyName = customer.CompanyProfile.CompanyName,
                Domain = customer.CompanyProfile.Domain,
                IsPreApproved = false
            }).ToList();

            // identify the customers who are preapproved and update them.
            if (!allCustomersPreApproved && (currentPreApprovedCustomers.CustomerIds != null))
            {
                foreach (string customerId in currentPreApprovedCustomers.CustomerIds)
                {
                    try
                    {
                        // can raise an exception if a customer has been removed from PartnerCenter although preapproved in the portal.
                        preApprovedCustomerDetails.FirstOrDefault(customer => customer.TenantId == customerId).IsPreApproved = true;
                    }
                    catch (NullReferenceException)
                    {
                        // This has been intentionally left empty.
                    }
                }
            }

            PreApprovedCustomersViewModel viewModel = new PreApprovedCustomersViewModel
            {
                IsEveryCustomerPreApproved = allCustomersPreApproved,
                Items = preApprovedCustomerDetails.OrderBy(customer => customer.CompanyName)
            };

            if (!allCustomersPreApproved && currentPreApprovedCustomers.CustomerIds != null)
            {
                viewModel.CustomerIds.AddRange(currentPreApprovedCustomers.CustomerIds.ToList());
            }

            return(viewModel);
        }