/// <summary>
        /// Converts the audit records to a list of customer details.
        /// </summary>
        /// <param name="client">Provides the ability to interface with Partner Center.</param>
        /// <param name="records">A list of audit records from Partner Center.</param>
        /// <param name="details">A list of existing customer details.</param>
        /// <param name="additionalInfo">Additional information to be added to the converted customer details.</param>
        /// <returns>
        /// A list of customer details that incorporates the changes reflected by the audit records.
        /// </returns>
        public async static Task <List <CustomerDetail> > ConvertAsync(
            IPartnerServiceClient client,
            List <AuditRecord> records,
            List <CustomerDetail> details,
            Dictionary <string, string> additionalInfo)
        {
            Customer                  resource;
            CustomerDetail            control;
            IEnumerable <AuditRecord> filtered;
            List <CustomerDetail>     results;

            try
            {
                // Filter the audit records, so that only records for successful operations are considered.
                filtered = records
                           .Where(r => r.OperationStatus == OperationStatus.Succeeded && !string.IsNullOrEmpty(r.CustomerId))
                           .OrderBy(r => r.OperationDate);

                results = new List <CustomerDetail>(details);

                foreach (AuditRecord record in filtered)
                {
                    control = results.SingleOrDefault(r => r.Id.Equals(record.CustomerId, StringComparison.InvariantCultureIgnoreCase));

                    /*
                     * If the control variable is null and the operation type value is not equal
                     * to AddCustomer, then that means we have a customer that accepted the reseller
                     * relationship. Currently there is not an audit record for when a customer
                     * accepts the reseller relationship.
                     */

                    if (control == null && record.OperationType != OperationType.AddCustomer)
                    {
                        resource = await client.Customers[record.CustomerId].GetAsync().ConfigureAwait(false);

                        results.Add(
                            ResourceConverter.Convert <Customer, CustomerDetail>(
                                resource,
                                additionalInfo));
                    }
                    else if (control != null)
                    {
                        control.RemovedFromPartnerCenter = false;
                    }

                    if (record.OperationType == OperationType.AddCustomer)
                    {
                        resource = Convert <Customer>(record);

                        results.Add(
                            ResourceConverter.Convert <Customer, CustomerDetail>(
                                resource,
                                additionalInfo));
                    }
                    else if (record.OperationType == OperationType.RemovePartnerCustomerRelationship)
                    {
                        control.RemovedFromPartnerCenter = true;
                    }
                    else if (record.OperationType == OperationType.UpdateCustomerBillingProfile)
                    {
                        control.BillingProfile = Convert <CustomerBillingProfile>(record);
                    }
                }

                return(results);
            }
            finally
            {
                control  = null;
                filtered = null;
                resource = null;
            }
        }
        /// <summary>
        /// Converts the audit records to a list of subscription details.
        /// </summary>
        /// <param name="client">Provides the ability to interface with Partner Center.</param>
        /// <param name="records">A list of audit records from Partner Center.</param>
        /// <param name="details">A list of existing subscription details.</param>
        /// <param name="customer">The details for the customer that owns the subscription.</param>
        /// <returns>
        /// A list of subscription details that incorporates the changes reflected by the audit records.
        /// </returns>
        public static async Task <List <SubscriptionDetail> > ConvertAsync(
            IPartnerServiceClient client,
            IEnumerable <AuditRecord> auditRecords,
            List <SubscriptionDetail> details,
            CustomerDetail customer)
        {
            IEnumerable <AuditRecord> filteredRecords;
            List <SubscriptionDetail> fromOrders;
            List <SubscriptionDetail> results;
            SubscriptionDetail        control;
            Subscription resource;

            try
            {
                // Extract a list of audit records that are scope to the defined resource type and were successful.
                filteredRecords = auditRecords
                                  .Where(r => (r.ResourceType == ResourceType.Subscription || r.ResourceType == ResourceType.Order) &&
                                         r.OperationStatus == OperationStatus.Succeeded)
                                  .OrderBy(r => r.OperationDate);

                results = new List <SubscriptionDetail>(details);

                foreach (AuditRecord record in filteredRecords)
                {
                    if (record.ResourceType == ResourceType.Order)
                    {
                        fromOrders = await ConvertAsync(
                            client,
                            customer,
                            Convert <Order>(record)).ConfigureAwait(false);

                        if (fromOrders != null)
                        {
                            results.AddRange(fromOrders);
                        }
                    }
                    else if (record.ResourceType == ResourceType.Subscription)
                    {
                        resource = Convert <Subscription>(record);
                        control  = results.SingleOrDefault(r => r.Id.Equals(resource.Id, StringComparison.InvariantCultureIgnoreCase));

                        if (control != null)
                        {
                            results.Remove(control);
                        }

                        results.Add(
                            ResourceConverter.Convert <Subscription, SubscriptionDetail>(
                                resource,
                                new Dictionary <string, string> {
                            { "TenantId", customer.Id }
                        }));
                    }
                }

                return(results);
            }
            finally
            {
                control         = null;
                filteredRecords = null;
                fromOrders      = null;
                resource        = null;
            }
        }