/// <summary> /// Get current contact info /// </summary> /// <returns></returns> public static ContactCookieModel GetCurrentContact() { var currentContact = StateManager.GetCookie <ContactCookieModel>(EzCMSContants.CurrentContactInformation); /* * This is the first time user come to site and has no cookie. We will * - Create new cookie * - Create an anonymous contact for user */ if (currentContact == null) { //Create new anonymous contact var annonymousContactRepository = HostContainer.GetInstance <IRepository <AnonymousContact> >(); var annonymousContact = new AnonymousContact { CookieKey = PasswordUtilities.GenerateUniqueKey(), IpAddress = HttpContext.Current.GetUserAgentInformationFromRequest().IpAddress }; annonymousContactRepository.Insert(annonymousContact); //Setup contact currentContact = new ContactCookieModel { AnonymousContactId = annonymousContact.Id, CookieKey = annonymousContact.CookieKey, IpAddress = annonymousContact.IpAddress }; //Set current contact to cookie StateManager.SetCookie(EzCMSContants.CurrentContactInformation, currentContact, DateTime.UtcNow.AddYears(1)); } return(currentContact); }
/// <summary> /// Save contact info /// </summary> /// <param name="model"></param> public static void SetCurrentContact(ContactCookieModel model) { var annonymousContactRepository = HostContainer.GetInstance <IRepository <AnonymousContact> >(); var anonymousContact = annonymousContactRepository.FetchFirst(c => c.CookieKey.Equals(model.CookieKey)); /* * If we cannot find the anonymous contact of user, maybe because the data is removed, user log to different account or user remove cookie. We will * - Create an anonymous contact for user */ if (anonymousContact == null) { anonymousContact = new AnonymousContact { ContactId = model.ContactId, CookieKey = model.CookieKey, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, Phone = model.Phone, Address = model.Address, IpAddress = HttpContext.Current.GetUserAgentInformationFromRequest().IpAddress }; annonymousContactRepository.Insert(anonymousContact); model = new ContactCookieModel(anonymousContact); } else { if (model.ContactId.HasValue) { /* * If the contact ID in database has no model, then maybe user just have an account so we will link the anonymous contact to current contact */ if (!anonymousContact.ContactId.HasValue) { anonymousContact.ContactId = model.ContactId; anonymousContact.Email = model.Email; anonymousContact.FirstName = model.FirstName; anonymousContact.LastName = model.LastName; anonymousContact.Phone = model.Phone; anonymousContact.IpAddress = model.IpAddress; anonymousContact.Address = model.Address; annonymousContactRepository.Update(anonymousContact); model = new ContactCookieModel(anonymousContact); } else { /* * If the contact ID in database has model but different with current contact id, then user may change the account. So we will * - Create new anonymous for current user */ if (model.ContactId.Value != anonymousContact.ContactId.Value) { anonymousContact = new AnonymousContact { ContactId = model.ContactId, CookieKey = PasswordUtilities.GenerateUniqueKey(), Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, Phone = model.Phone, Address = model.Address, IpAddress = HttpContext.Current.GetUserAgentInformationFromRequest().IpAddress }; annonymousContactRepository.Insert(anonymousContact); model = new ContactCookieModel(anonymousContact); } else { /* * If the contact ID in database has model and equal with current contact id, then we will check if there are any contact information updates or not. * - If yes, then update anonymous contact information */ if (!anonymousContact.FirstName.AreEqual(model.FirstName) || !anonymousContact.LastName.AreEqual(model.LastName) || !anonymousContact.Email.AreEqual(model.Email) || !anonymousContact.Phone.AreEqual(model.Phone) || !anonymousContact.IpAddress.AreEqual(model.IpAddress) || !anonymousContact.Address.AreEqual(model.Address)) { anonymousContact.FirstName = model.FirstName; anonymousContact.LastName = model.LastName; anonymousContact.Email = model.Email; anonymousContact.Phone = model.Phone; anonymousContact.IpAddress = model.IpAddress; anonymousContact.Address = model.Address; annonymousContactRepository.Update(anonymousContact); model = new ContactCookieModel(anonymousContact); } } } } else { if (!string.IsNullOrEmpty(anonymousContact.FirstName) && !string.IsNullOrEmpty(anonymousContact.LastName) && (!string.IsNullOrEmpty(anonymousContact.Email) || !string.IsNullOrEmpty(anonymousContact.Phone))) { var contactService = HostContainer.GetInstance <IContactService>(); var contact = contactService.CreateContactIfNotExists(new Contact { FirstName = anonymousContact.FirstName, LastName = anonymousContact.LastName, Email = anonymousContact.Email, PreferredPhoneNumber = anonymousContact.Phone, AddressLine1 = anonymousContact.Address, }); anonymousContact.ContactId = contact.Id; model = new ContactCookieModel(anonymousContact); } } } StateManager.SetCookie(EzCMSContants.CurrentContactInformation, model, DateTime.UtcNow.AddYears(1)); }