public static List <InvoiceCRMConatactModel> GetCustomersFromService(List <string> memberIds, string CountryCode) { var contacts = new List <InvoiceCRMConatactModel>(); var proxy = ServiceClientProvider.GetCRMContactServiceProxy(); try { var getCrmContactForMultipleDistributorsRequest = new GetCrmContactForMultipleDistributorsRequest { DetailLevel = (ContactListDetailLevel)Enum.Parse(typeof(ContactListDetailLevel), "2"), SearchDescriptor = new ContactSearchFilterDescriptor { Filters = CreateContactFilters(), }, DistributorIds = memberIds, ShowMultipleAddress = true, Take = 1000, }; var circuitBreaker = CircuitBreakerFactory.GetFactory().GetCircuitBreaker <GetCrmContactsForMultipleDistributorsResponse>(); var response = circuitBreaker.Execute(() => proxy.GetContacts(new GetContactsRequest(getCrmContactForMultipleDistributorsRequest)).GetContactsResult as GetCrmContactsForMultipleDistributorsResponse); if (response == null) { LoggerHelper.Warn($"Get customer for ds {string.Join(",", memberIds)} failed: response is null."); return(null); } if (response.Contacts != null) { contacts = PopulateCustomerList(response.Contacts.ToList(), CountryCode); } return(contacts); } catch (Exception ex) { LoggerHelper.Error(string.Format("GetContacts error : {0}", ex.Message)); } finally { proxy.Close(); } return(contacts); }
private static SaveUpdateResponseModel Update(InvoiceCRMConatactModel contact, string memberId) { var proxy = ServiceClientProvider.GetCRMContactServiceProxy(); try { var hasAddressChanges = (contact.Address != null && contact.Address.Id > 0); var hasPhoneChanges = (contact.PhoneDetail != null && contact.PhoneDetail.Id > 0) || (contact.PhoneDetail != null && !string.IsNullOrEmpty(contact.PhoneDetail.PhoneNumber)); var hasEmailChanges = (contact.EmailDetail != null && contact.EmailDetail.Id > 0) || (contact.EmailDetail != null && !string.IsNullOrEmpty(contact.EmailDetail.EmailAddress)); var updatingField = new List <string>(); updatingField.Add("FirstName"); updatingField.Add("LastName"); if (hasAddressChanges) { updatingField.Add("Addresses"); } if (hasPhoneChanges) { updatingField.Add("Phones"); } if (hasEmailChanges) { updatingField.Add("EmailAddress"); } var crmContactUpdateRequest = new CrmContactUpdateRequest { Fields = updatingField, Value = new CrmContact { EmailAddresses = hasEmailChanges ? new List <EmailAddress_V01>() { new ContactEmailAddress { IsPrimary = true, EmailId = contact.EmailDetail.Id.ToString(), Address = contact.EmailDetail.EmailAddress } } : null, Addresses = hasAddressChanges ? new List <Address_V03>() { new ContactAddress { AddressId = contact.Address.Id <= 0 ? null : contact.Address.Id.ToString(), City = string.IsNullOrEmpty(contact.Address.City)?string.Empty:contact.Address.City, Country = string.IsNullOrEmpty(contact.Address.Country)?string.Empty:contact.Address.Country, Line1 = string.IsNullOrEmpty(contact.Address.Address1)?string.Empty:contact.Address.Address1, Line2 = string.IsNullOrEmpty(contact.Address.Address2)?string.Empty:contact.Address.Address2, StateProvinceTerritory = string.IsNullOrEmpty(contact.Address.State)?string.Empty:contact.Address.State, PostalCode = string.IsNullOrEmpty(contact.Address.PostalCode)?string.Empty:contact.Address.PostalCode, TypeOfAddress = AddressType.ShipTo, } } : null, Name = new Name_V01 { First = contact.FirstName, Last = contact.LastName }, Phones = hasPhoneChanges ? new List <CrmContactPhone>() { new CrmContactPhone { IsPrimary = true, Number = contact.PhoneDetail.PhoneNumber, PhoneType = CrmContactPhoneType.Mobile, Id = contact.PhoneDetail.Id.ToString() == "0" ? null : contact.PhoneDetail.Id.ToString() } } : null, ContactType = CrmContactType.Customer, OwnerMemberId = memberId, ContactId = contact.ContactId.ToString(), IsDuplicateNameCheckRequired = false, FollowupAppointments = new List <FollowupAppointment>(), } }; var circuitBreaker = CircuitBreakerFactory.GetFactory().GetCircuitBreaker <UpdateContactResponse>(); var response = circuitBreaker.Execute(() => proxy.UpdateContact(new UpdateContactRequest(crmContactUpdateRequest)).UpdateContactResult); if (response == null) { LoggerHelper.Warn($"Create customer for ds {memberId} failed: response is null."); return(null); } return(new SaveUpdateResponseModel { Data = contact }); } catch (Exception ex) { LoggerHelper.Warn("ContactSaver Update : " + ex.StackTrace.ToString()); throw; } finally { proxy.Close(); } }
private static SaveUpdateResponseModel Create(InvoiceCRMConatactModel contact, string memberId) { var proxy = ServiceClientProvider.GetCRMContactServiceProxy(); try { var createCrmContactRequest = new CreateCrmContactRequest(); createCrmContactRequest.OwnerId = memberId; createCrmContactRequest.IsContactInformationRequired = true; CrmContact crmContact = new CrmContact(); crmContact.ContactId = null; crmContact.ContactType = CrmContactType.Customer; crmContact.ContactPriority = ContactPriorityType.None; crmContact.ContactSource = ContactSourceType.ECommerce; crmContact.ContactStatus = ContactStatusType.None; crmContact.OwnerMemberId = memberId; crmContact.IsDuplicateNameCheckRequired = contact.IsDuplicateCheckRequire; crmContact.EmailAddresses = contact.EmailDetail != null && string.IsNullOrEmpty(contact.EmailDetail.EmailAddress) ? null : new List <EmailAddress_V01> { new ContactEmailAddress { EmailId = string.Empty, Address = contact.EmailDetail.EmailAddress, IsPrimary = true } }; crmContact.FollowupAppointments = new List <FollowupAppointment>() ; crmContact.Addresses = contact.Address == null ? null : new List <Address_V03>() { new ContactAddress { AddressId = null, Line1 = contact.Address.Address1, City = contact.Address.City, Line2 = contact.Address.Address2, StateProvinceTerritory = contact.Address.State, PostalCode = contact.Address.PostalCode, Country = contact.Address.Country, IsPrimary = true, TypeOfAddress = AddressType.ShipTo } }; crmContact.Name = new Name_V01 { First = contact.FirstName, Last = contact.LastName, }; crmContact.Phones = contact.PhoneDetail == null || string.IsNullOrEmpty(contact.PhoneDetail.PhoneNumber) ? null : new List <CrmContactPhone>() { new CrmContactPhone { Id = string.Empty, IsPrimary = true, Number = contact.PhoneDetail.PhoneNumber, PhoneType = CrmContactPhoneType.Mobile } }; createCrmContactRequest.Value = crmContact; var circuitBreaker = CircuitBreakerFactory.GetFactory().GetCircuitBreaker <CreateCrmContactResponse>(); var response = circuitBreaker.Execute(() => proxy.CreateContact(new CreateContactRequest(createCrmContactRequest)).CreateContactResult as CreateCrmContactResponse); if (response == null) { LoggerHelper.Warn($"Create customer for ds {memberId} failed: response is null."); return(null); } return(createContactResponseModel(response, contact)); } catch (Exception ex) { LoggerHelper.Info($"ContactSaver:Create Failed. message: {ex.Message}, exception: {ex}"); SaveUpdateResponseModel errorModel = new SaveUpdateResponseModel(); return(errorModel); } finally { proxy.Close(); } }