public static CustomerModel ToCustomerModel(this Customer customer) { if (customer.Addresses == null) customer.Addresses = new Dictionary<Guid, Address>(); if (customer.PersonalInformation == null) customer.PersonalInformation = new PersonalInformation(); if (customer.EmailAddress == null) customer.EmailAddress = new EmailAddress(); if (customer.LearningTopicsOfInterest == null) customer.LearningTopicsOfInterest = new HashSet<string>(); if (customer.PhoneNumbers == null) customer.PhoneNumbers = new Dictionary<Guid, PhoneNumber>(); if (customer.HyperLinks == null) customer.HyperLinks = new Dictionary<Guid, HyperLink>(); var model = new CustomerModel { Addresses = customer.Addresses.Values.ToArray(), CanSendSMSToCell = customer.ContactPreferences.CanSendSMSToCell, Comments = customer.PersonalInformation.Comments, CustomerId = customer.CustomerId, DateAddedUtc = customer.DateAddedUtc, DateRegisteredUtc = customer.DateRegisteredUtc, PreferredContactDays = customer.ContactPreferences.Days, EmailAddress = customer.EmailAddress.Address, Employer = customer.ContactInformation.Employer, FirstName = customer.ContactInformation.FirstName, Gender = customer.PersonalInformation.Gender, IsRegistered = customer.IsRegisteredForPws, LastName = customer.ContactInformation.LastName, LearningTopicsOfInterest = customer.LearningTopicsOfInterest.ToArray(), MiddleName = customer.ContactInformation.MiddleName, Occupation = customer.ContactInformation.Occupation, PhoneNumbers = customer.PhoneNumbers.Values.ToArray(), PictureLastUpdatedDateUtc = customer.Pictures.Values.Count > 0 ? (DateTime?)customer.Pictures.Values.FirstOrDefault().LastUpdatedDateUtc : null, PreferredContactFrequencies = customer.ContactPreferences.Frequencies != null ? customer.ContactPreferences.Frequencies.ToArray() : new ContactFrequency[0], PreferredContactMethods = customer.ContactPreferences.Methods != null ? customer.ContactPreferences.Methods.ToArray() : new ContactMethod[0], PreferredDeliveryMethod = customer.PreferredDeliveryMethod, PreferredDeliveryMethodDetailsIfOther = customer.PreferredDeliveryMethodDetailsIfOther, PreferredLanguage = customer.ContactPreferences.PreferredLanguage, PreferredShippingMethod = customer.PreferredShippingMethod, PreferredShoppingMethods = customer.PreferredShoppingMethod != null ? customer.PreferredShoppingMethod.ToArray() : new ShoppingMethod[0], PreferredContactTimes = customer.ContactPreferences.Time != null ? customer.ContactPreferences.Time.ToArray() : new ContactTime[0], ProfileDateUtc = customer.ProfileDateUtc, ReferredBy = customer.PersonalInformation != null ? customer.PersonalInformation.ReferredBy : null, Spouse = customer.PersonalInformation != null ? customer.PersonalInformation.Spouse : new Spouse(), Subscriptions = customer.Subscriptions.Values.ToArray(), }; model.SocialNetworks = customer.HyperLinks.Values .Select ( h => new CustomerModel.SocialNetwork { Key = h.HyperLinkKey, Type = h.HyperLinkType, Url = h.Url.ToString() } ) .ToArray(); model.Birthday = customer.SpecialOccasions.Values .Where(so => so.SpecialOccasionType == SpecialOccasionType.Birthday) .Select ( so => new CustomerModel.ImportantDate { Key = so.SpecialOccasionKey, Type = SpecialOccasionType.Birthday, Month = so.Month, Day = so.Day, Year = so.Year } ) .FirstOrDefault(); model.Anniversary = customer.SpecialOccasions.Values .Where(so => so.SpecialOccasionType == SpecialOccasionType.Anniversary) .Select ( so => new CustomerModel.ImportantDate { Key = so.SpecialOccasionKey, Type = SpecialOccasionType.Anniversary, Month = so.Month, Day = so.Day, Year = so.Year } ) .FirstOrDefault(); model.ImportantDates = customer.SpecialOccasions.Values .Where(so => so.SpecialOccasionType != SpecialOccasionType.Birthday && so.SpecialOccasionType != SpecialOccasionType.Anniversary) .Select ( so => new CustomerModel.ImportantDate { Key = so.SpecialOccasionKey, Type = so.SpecialOccasionType, Month = so.Month, Day = so.Day, Year = so.Year, Description = so.Description } ) .ToArray(); return model; }
public dynamic UpdateCustomer(Guid id, CustomerModel model) { // TODO: validate model state var customer = _clientFactory.GetCustomersQueryServiceClient().GetCustomer(model.CustomerId); var command = model.ToSaveCommand(customer); try { var sendEmail = false; // REVIEW: This logic doesnt belong here in the controller and should be moved back behind the email service. // IMPORTANT - We have to first check to see if the model email address is changing before saving the customer // Are we removing an existing address // model is null - do nothing if (!string.IsNullOrWhiteSpace(model.EmailAddress)) { // don't send email if customer has no email address or the email address has not changed if (customer.EmailAddress == null || string.IsNullOrWhiteSpace(customer.EmailAddress.Address)) { //adding a new address sendEmail = true; _logger.Info("Send Opt-in email for an existing customer with out email address."); } else if (customer.EmailAddress != null && !string.IsNullOrWhiteSpace(customer.EmailAddress.Address) && !string.IsNullOrWhiteSpace(model.EmailAddress) && !customer.EmailAddress.Address.Equals(model.EmailAddress, StringComparison.InvariantCultureIgnoreCase)) { // changing existing address // customer has address, model has address but they are different - send email sendEmail = true; _logger.Info("Send Opt-in email for an existing customer with changing email address."); } } _clientFactory.GetCommandServiceClient().Execute(command); try { if (sendEmail) _emailService.SendOptInMail(model); } catch (Exception ex) { _logger.Error(ex); } // REST style returns id and hyperlinks in the response return new { id = command.CustomerId, links = new { self = new { href = Url.Route("DefaultApi", new { id = command.CustomerId }) }, photo = new { href = Url.Route("Default", new { controller = "customerimages", action = "profile", id = command.CustomerId }) }, detail = new { href = Url.Route("Default", new { controller = "customers", action = "detail", id = command.CustomerId }) } } }; } catch (CommandException ex) { throw ApiHelpers.ServerError(ex.Message, string.Join("\r\n", ex.Errors)); } }
private CustomerModel Map(UploadedCustomer uploadedCustomer) { var model = new CustomerModel { CustomerId = uploadedCustomer.CustomerId, FirstName = uploadedCustomer.FirstName, LastName = uploadedCustomer.LastName, EmailAddress = uploadedCustomer.EmailAddress != null ? uploadedCustomer.EmailAddress.Address : null }; return model; }
public dynamic CreateCustomer(CustomerModel model) { // TODO: validate model state var command = model.ToCreateCommand(); try { _clientFactory.GetCommandServiceClient().Execute(command); model.CustomerId = command.CustomerId; try { // REVIEW: this check is redundant. The email service does all validaiton of the model // Did the user enter an email address for this customer? if (!string.IsNullOrWhiteSpace(model.EmailAddress)) { _emailService.SendOptInMail(model); _logger.Info("Send Opt-in email for new customer with email address."); _emailService.SendInviteToRegister(model); _logger.Info("Send InviteToRegister email for new customer with email address."); } } catch (Exception ex) { _logger.Error(ex); } // REST style returns id and hyperlinks in the response return new { id = command.CustomerId, links = new { self = new { href = Url.Route("DefaultApi", new { id = command.CustomerId }) }, photo = new { href = Url.Route("Default", new { controller = "customerimages", action = "profile", id = command.CustomerId }) }, detail = new { href = Url.Route("Default", new { controller = "customers", action = "detail", id = command.CustomerId }) } } }; } catch (CommandException ex) { throw ApiHelpers.ServerError(ex.Message, string.Join("\r\n", ex.Errors)); } }
public void SendOptInMail(CustomerModel model) { if (!_appSettings.GetValue<bool>("Feature.ExactTarget.DoubleOptIn")) return; if (string.IsNullOrWhiteSpace(model.EmailAddress)) return; var consultant = _consultantContext.Consultant; if (!consultant.Subscription.IsActive()) return; var culture = _appSettings.GetValue("GMF.EmailCulture"); var emailCulture = string.IsNullOrWhiteSpace(model.PreferredLanguage) ? culture : model.PreferredLanguage; var contentDomain = string.Format(_appSettings.GetValue("GMF.EmailContentDomain"), consultant.SubsidiaryCode); var contentID = string.Format(_appSettings.GetValue("GMF.OptInInviteContentId"), emailCulture); var subject = string.Empty; var sender = consultant.PrimaryEmailAddress; var consultantMoniker = !string.IsNullOrWhiteSpace(consultant.PrimaryMoniker) ? "/" + consultant.PrimaryMoniker : string.Empty; var consultantPhoneNumber = (consultant.PrimaryPhoneNumber != null) ? consultant.PrimaryPhoneNumber.Number : ""; var consultantLevel = consultant.ConsultantLevel.HasValue ? consultant.ConsultantLevel.Value : 0; var recipient = new EmailRecipient { Recipient = model.EmailAddress, Attributes = new[] { new EmailAttribute { Name = "CustomerId", Value = model.CustomerId.ToString("N") }, new EmailAttribute { Name = "ConsultantMoniker", Value = consultantMoniker }, new EmailAttribute { Name = "ConsultantFirstName", Value = consultant.FirstName }, new EmailAttribute { Name = "ConsultantLastName", Value = consultant.LastName }, new EmailAttribute { Name = "ConsultantEmailAddress", Value = sender }, new EmailAttribute { Name = "ConsultantPhoneNumber", Value = consultantPhoneNumber }, new EmailAttribute { Name = "ConsultantCareerLevel", Value = consultantLevel.ToString() }, new EmailAttribute { Name = "CustomerFirstName", Value = model.FirstName }, new EmailAttribute { Name = "CustomerLastName", Value = model.LastName }, new EmailAttribute { Name = "PWSDomain", Value = _appSettings.GetValue("GMF.PWSDomain") }, new EmailAttribute { Name = "InTouchDomain", Value = _appSettings.GetValue("GMF.InTouchDomain") }, new EmailAttribute { Name = "EmailAddress", Value = model.EmailAddress } } }; _emailService.SendPredefinedEmail(contentDomain, contentID, subject, sender, new[] { recipient }); }