in src/Feature/Account/code/Pipelines/Customers/UpdateParties.cs [40:101]
public override void Process(Commerce.Pipelines.ServicePipelineArgs args)
{
Assert.ArgumentNotNull(args, "args");
Assert.ArgumentCondition(args.Request is UpdatePartiesRequest, "args.Request", "args.Request is UpdatePartiesRequest");
Assert.ArgumentCondition(args.Result is CustomerResult, "args.Result", "args.Result is CustomerResult");
var request = (UpdatePartiesRequest)args.Request;
var result = (CustomerResult)args.Result;
Profile customerProfile = null;
var response = this.GetCommerceUserProfile(request.CommerceCustomer.ExternalId, ref customerProfile);
if (!response.Success)
{
result.Success = false;
response.SystemMessages.ToList().ForEach(m => result.SystemMessages.Add(m));
return;
}
string preferredAddress = customerProfile["GeneralInfo.preferred_address"].Value as string;
var profileValue = customerProfile["GeneralInfo.address_list"].Value as object[];
if (profileValue != null)
{
var e = profileValue.Select(i => i.ToString());
var addressList = new ProfilePropertyListCollection<string>(e);
foreach (var partyToUpdate in request.Parties)
{
Assert.IsTrue(partyToUpdate is CommerceParty, "partyToUpdate is CommerceParty");
var foundId = addressList.Where(x => x.Equals(partyToUpdate.ExternalId, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
if (foundId != null)
{
Profile commerceAddress = null;
response = this.GetCommerceAddressProfile(foundId, ref commerceAddress);
if (!response.Success)
{
result.Success = false;
response.SystemMessages.ToList().ForEach(m => result.SystemMessages.Add(m));
return;
}
// Check if the IsPrimary address flag has been flipped.
if (((CommerceParty)partyToUpdate).IsPrimary)
{
customerProfile["GeneralInfo.preferred_address"].Value = partyToUpdate.ExternalId;
customerProfile.Update();
}
else if (!string.IsNullOrWhiteSpace(preferredAddress) && preferredAddress.Equals(partyToUpdate.ExternalId, StringComparison.OrdinalIgnoreCase))
{
customerProfile["GeneralInfo.preferred_address"].Value = System.DBNull.Value;
customerProfile.Update();
}
var translateToEntityRequest = new TranslateEntityToCommerceAddressProfileRequest((CommerceParty)partyToUpdate, commerceAddress);
PipelineUtility.RunCommerceConnectPipeline<TranslateEntityToCommerceAddressProfileRequest, CommerceResult>(CommerceServerStorefrontConstants.PipelineNames.TranslateEntityToCommerceAddressProfile, translateToEntityRequest);
commerceAddress.Update();
}
}
}
}