protected override void RunScenario()

in sdk/SdkSamples/Agreements/ImportCustomersAgreement.cs [30:104]


        protected override void RunScenario()
        {
            var startTime = DateTime.UtcNow;
            var errorFilePath = $"{DateTime.UtcNow:yyyyMMddTHHmmss}.txt";
            var count = 0;

            var partnerOperations = this.Context.UserPartnerOperations;

            // Prefetch necessary partner agreement metadata
            var agreementDetails = partnerOperations.AgreementDetails.ByAgreementType("*")?.Get()?.Items.OrderBy(x => x.VersionRank).ToDictionary(ad => ad.AgreementType);
            if (agreementDetails == null || !agreementDetails.Any())
            {
                this.Context.ConsoleHelper.WriteColored("No Agreement metadata available.", ConsoleColor.DarkRed);
                return;
            }

            this.Context.ConsoleHelper.WriteColored($"{Environment.NewLine}Use GetAllCustomersAgreements scenario's output csv file format to import agreements.", ConsoleColor.DarkGray);
            var csvFilePath = this.ObtainCustomersAgreementCsvFileName();
            var customerAgreements = this.ParseCustomerAgreements(csvFilePath, errorFilePath);

            // Perform basic validations to check for duplicate entries (note: there can be multiple entries per customer tenant ID, just not duplicates).
            if (customerAgreements.Where(x => x.Valid).GroupBy(x => x.CustomerTenantId).Any(c => c.Count() > c.Select(ca => ca.Agreement.Type).Distinct(StringComparer.OrdinalIgnoreCase).Count()))
            {
                this.Context.ConsoleHelper.WriteColored("File contains duplicate / contradicting agreement data. Please fix and retry.", ConsoleColor.DarkRed);
                return;
            }

            // Process each line
            foreach (var customerAgreement in customerAgreements)
            {
                this.Context.ConsoleHelper.WriteObject($"Processing #{++count} {customerAgreement.Source}");

                if (!customerAgreement.Valid)
                {
                    File.AppendAllText(errorFilePath,
                        $"{customerAgreement.Source},Insufficient data.{Environment.NewLine}");
                    continue;
                }

                try
                {
                    // Fetch Agreements (of a specific type) for the customer to check if an update is necessary.
                    var customerAgreementOperations = partnerOperations.Customers.ById(customerAgreement.CustomerTenantId).Agreements;
                    var agreements = customerAgreementOperations.ByAgreementType(customerAgreement.Agreement.Type).Get();
                    if (agreements.TotalCount == 0 ||
                        DoesAgreementNeedUpdate(agreements.Items.First(), customerAgreement.Agreement))
                    {
                        var agreementDetail = agreementDetails[customerAgreement.Agreement.Type];

                        // Populate other required agreement details
                        customerAgreement.Agreement.AgreementLink = agreementDetail.AgreementLink;
                        customerAgreement.Agreement.TemplateId = agreementDetail.TemplateId;
                        customerAgreement.Agreement.Type = agreementDetail.AgreementType;

                        // Try to add the agreement
                        customerAgreementOperations.Create(customerAgreement.Agreement);
                        File.AppendAllText(errorFilePath,
                            $"{customerAgreement.Source},Agreement data updated successfully.{Environment.NewLine}");
                    }
                    else
                    {
                        File.AppendAllText(errorFilePath,
                            $"{customerAgreement.Source},Skipped as there is no change in agreement data.{Environment.NewLine}");
                    }
                }
                catch (Exception ex)
                {
                    File.AppendAllText(errorFilePath,
                        $"{customerAgreement.Source},Processing failed with error {ex.Message}{Environment.NewLine}");
                }
            }

            this.Context.ConsoleHelper.WriteObject($"Total Customers: {count} processed in {DateTime.UtcNow - startTime}.");
            this.Context.ConsoleHelper.WriteObject($"{errorFilePath}", "Processed information output file");
        }