Skip to content

Instantly share code, notes, and snippets.

@Mozu-CS
Created December 8, 2015 19:24
Show Gist options
  • Save Mozu-CS/50c9955918013168e5f5 to your computer and use it in GitHub Desktop.
Save Mozu-CS/50c9955918013168e5f5 to your computer and use it in GitHub Desktop.
A method organizing other calls to import customers into Mozu
private System.Collections.Concurrent.ConcurrentDictionary<string,string> ImportCustomers(IEnumerable<Mozu.Api.Contracts.Customer.CustomerAccountAndAuthInfo> accountAuthCollection)
{
var accountResource = new Mozu.Api.Resources.Commerce.Customer.CustomerAccountResource(_apiContext);
var contactResource = new Mozu.Api.Resources.Commerce.Customer.Accounts.CustomerContactResource(_apiContext);
var accountExternalIds = new System.Collections.Concurrent.ConcurrentDictionary<string, string>();
Parallel.ForEach(accountAuthCollection, new System.Threading.Tasks.ParallelOptions() { MaxDegreeOfParallelism = 1 }, accountAuthInfo =>
{
var filter = string.Format("ExternalId eq '{0}'", accountAuthInfo.Account.ExternalId);
var importedResultStatus = string.Empty;
Mozu.Api.Contracts.Customer.CustomerAccount existingAcct = null;
var existingAcctCollection = accountResource.GetAccountsAsync(filter: filter).Result;
if (existingAcctCollection.Items.Count == 0)
{
try
{
existingAcct = accountResource.AddAccountAsync(accountAuthInfo.Account, "Id,ExternalId").Result;
importedResultStatus = string.Format("Added-New-Account-Id:{0}", existingAcct.Id);
var existingAcctAuthTicket = accountResource.AddLoginToExistingCustomerAsync(new Mozu.Api.Contracts.Customer.CustomerLoginInfo()
{
EmailAddress = accountAuthInfo.Account.EmailAddress,
IsImport = accountAuthInfo.IsImport,
Password = accountAuthInfo.Password,
Username = accountAuthInfo.Account.UserName,
}, existingAcct.Id).Result;
foreach (var contact in accountAuthInfo.Account.Contacts)
{
var existingContact = contactResource.AddAccountContactAsync(contact, existingAcct.Id, "Id").Result;
}
}
catch (Exception ex)
{
importedResultStatus = "Error-Encountered";
}
accountExternalIds.TryAdd(existingAcct.ExternalId, importedResultStatus);
}
});
return accountExternalIds;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment