Beispiel #1
0
        public Guid AddAddressToCustomer([FromUri] Guid customerId, [FromBody] Address address)
        {
            CustomerContact contact    = CustomerContext.Current.GetContactById(customerId);
            CustomerAddress newAddress = CustomerAddress.CreateForApplication(AppContext.Current.ApplicationId);

            // Get data from contact
            if (string.IsNullOrEmpty(address.FirstName))
            {
                address.FirstName = contact.FirstName;
            }
            if (string.IsNullOrEmpty(address.LastName))
            {
                address.LastName = contact.LastName;
            }
            if (string.IsNullOrEmpty(address.Email))
            {
                address.Email = contact.Email;
            }

            address.Populate(newAddress);
            contact.AddContactAddress(newAddress);
            contact.SaveChanges();

            return((Guid)newAddress.AddressId);
        }
        private CustomerAddress CreateOrUpdateCustomerAddress(CustomerContact contact, AddressModel addressModel)
        {
            var customerAddress = GetAddress(contact, addressModel.AddressId);
            var isNew           = customerAddress == null;
            IEnumerable <PrimaryKeyId> existingId = contact.ContactAddresses.Select(a => a.AddressId).ToList();

            if (isNew)
            {
                customerAddress = CustomerAddress.CreateInstance();
            }

            MapToAddress(addressModel, customerAddress);

            if (isNew)
            {
                contact.AddContactAddress(customerAddress);
            }
            else
            {
                contact.UpdateContactAddress(customerAddress);
            }

            contact.SaveChanges();
            if (isNew)
            {
                customerAddress.AddressId = contact.ContactAddresses
                                            .Where(a => !existingId.Contains(a.AddressId))
                                            .Select(a => a.AddressId)
                                            .Single();
                addressModel.AddressId = customerAddress.Name;
            }

            return(customerAddress);
        }
Beispiel #3
0
        public static void CreateContact(CustomerContact customerContact, Guid userId, Contact contact)
        {
            customerContact.PrimaryKeyId       = new PrimaryKeyId(userId);
            customerContact.FirstName          = contact.FirstName;
            customerContact.LastName           = contact.LastName;
            customerContact.Email              = contact.Email;
            customerContact.UserId             = "String:" + contact.Email; // The UserId needs to be set in the format "String:{email}". Else a duplicate CustomerContact will be created later on.
            customerContact.RegistrationSource = contact.RegistrationSource;

            if (contact.Addresses != null)
            {
                foreach (var address in contact.Addresses)
                {
                    customerContact.AddContactAddress(address.ConvertToCustomerAddress(CustomerAddress.CreateInstance()));
                }
            }

            // The contact, or more likely its related addresses, must be saved to the database before we can set the preferred
            // shipping and billing addresses. Using an address id before its saved will throw an exception because its value
            // will still be null.
            customerContact.SaveChanges();

            // Once the contact has been saved we can look for any existing addresses.
            CustomerAddress defaultAddress = customerContact.ContactAddresses.FirstOrDefault();

            if (defaultAddress != null)
            {
                // If an addresses was found, it will be used as default for shipping and billing.
                customerContact.PreferredShippingAddress = defaultAddress;
                customerContact.PreferredBillingAddress  = defaultAddress;

                // Save the address preferences also.
                customerContact.SaveChanges();
            }
        }
Beispiel #4
0
        private CustomerContact CreateCustomerContact(ApplicationUser user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            CustomerContact contact = CustomerContact.CreateInstance();

            // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
            // Send an email with this link
            // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
            // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
            // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

            if (!String.IsNullOrEmpty(user.FirstName) || !String.IsNullOrEmpty(user.LastName))
            {
                contact.FullName = String.Format("{0} {1}", user.FirstName, user.LastName);
            }

            contact.PrimaryKeyId       = new Mediachase.BusinessFoundation.Data.PrimaryKeyId(new Guid(user.Id));
            contact.FirstName          = user.FirstName;
            contact.LastName           = user.LastName;
            contact.Email              = user.Email;
            contact.UserId             = "String:" + user.Email; // The UserId needs to be set in the format "String:{email}". Else a duplicate CustomerContact will be created later on.
            contact.RegistrationSource = user.RegistrationSource;

            if (user.Addresses != null)
            {
                foreach (var address in user.Addresses)
                {
                    contact.AddContactAddress(address);
                }
            }

            // The contact, or more likely its related addresses, must be saved to the database before we can set the preferred
            // shipping and billing addresses. Using an address id before its saved will throw an exception because its value
            // will still be null.
            contact.SaveChanges();

            // Once the contact has been saved we can look for any existing addresses.
            CustomerAddress defaultAddress = contact.ContactAddresses.FirstOrDefault();

            if (defaultAddress != null)
            {
                // If an addresses was found, it will be used as default for shipping and billing.
                contact.PreferredShippingAddress = defaultAddress;
                contact.PreferredBillingAddress  = defaultAddress;

                // Save the address preferences also.
                contact.SaveChanges();
            }


            return(contact);
        }
Beispiel #5
0
        public CustomerContact CreateCustomerContact(SiteUser user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            CustomerContact contact = _customerContext.GetContactByUsername(user.UserName);

            if (contact == null)
            {
                contact = CustomerContact.CreateInstance();
                contact.PrimaryKeyId = new PrimaryKeyId(new Guid(user.Id));
                contact.UserId       = "String:" + user.Email; // The UserId needs to be set in the format "String:{email}". Else a duplicate CustomerContact will be created later on.
            }

            // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
            // Send an email with this link
            // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
            // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
            // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
            if (!String.IsNullOrEmpty(user.FirstName) || !String.IsNullOrEmpty(user.LastName))
            {
                contact.FullName = $"{user.FirstName} {user.LastName}";
            }

            contact.FirstName          = user.FirstName;
            contact.LastName           = user.LastName;
            contact.Email              = user.Email;
            contact.RegistrationSource = user.RegistrationSource;

            if (user.Addresses != null)
            {
                foreach (var address in user.Addresses)
                {
                    contact.AddContactAddress(address);
                }
            }

            // The contact, or more likely its related addresses, must be saved to the database before we can set the preferred
            // shipping and billing addresses. Using an address id before its saved will throw an exception because its value
            // will still be null.
            contact.SaveChanges();

            SetPreferredAddresses(contact);

            return(contact);
        }
        public void Save(Address model)
        {
            CustomerAddress address = _currentContact.ContactAddresses.FirstOrDefault(a => a.AddressId.ToString() == model.Id);

            PrimaryKeyId addressId;

            if (address == null)
            {
                address = CustomerAddress.CreateForApplication(AppContext.Current.ApplicationId);

                _currentContact.AddContactAddress(address);
                _currentContact.SaveChanges();
                addressId = address.AddressId;
            }
            else
            {
                addressId = address.AddressId;
            }

            address.Name        = string.Format("{0}, {1} {2}", model.StreetAddress, model.City, model.ZipCode);
            address.FirstName   = model.FirstName;
            address.LastName    = model.LastName;
            address.Line1       = model.StreetAddress;
            address.PostalCode  = model.ZipCode;
            address.City        = model.City;
            address.CountryCode = model.CountryCode;
            if (string.IsNullOrEmpty(address.Name))
            {
                address.Name = Guid.NewGuid().ToString();
            }

            _currentContact.SaveChanges();

            if (model.IsPreferredBillingAddress)
            {
                _currentContact.PreferredBillingAddressId = addressId;
            }

            if (model.IsPreferredShippingAddress)
            {
                _currentContact.PreferredShippingAddressId = addressId;
            }


            BusinessManager.Update(address);
            _currentContact.SaveChanges();
        }
Beispiel #7
0
        public void AddAddress(string email, Address address)
        {
            var             userId  = Customer.CreateUserId(email);
            CustomerContact contact = _customerContext.GetContactByUserId(userId);

            if (contact == null)
            {
                return;
            }

            var customerContact = address.ToCustomerAddress();

            contact.AddContactAddress(customerContact);
            contact.SaveChanges();
            contact.PreferredShippingAddress = customerContact;
            contact.SaveChanges();
            return;
        }
        public virtual void MapAddress(
            CustomerContact currentContact,
            CustomerAddressTypeEnum addressType,
            VippsAddress vippsAddress,
            string phoneNumber)
        {
            if (currentContact == null)
            {
                throw new ArgumentNullException(nameof(currentContact));
            }
            if (vippsAddress == null)
            {
                throw new ArgumentNullException(nameof(vippsAddress));
            }
            // Vipps addresses don't have an ID
            // They can be identified by Vipps address type
            var address =
                currentContact.ContactAddresses.FindVippsAddress(vippsAddress.AddressType);
            var isNewAddress = address == null;

            if (isNewAddress)
            {
                address             = CustomerAddress.CreateInstance();
                address.AddressType = addressType;
            }

            // Maps fields onto customer address:
            // Vipps address type, street, city, postalcode, countrycode
            MapVippsAddressFields(address, vippsAddress);
            if (!string.IsNullOrWhiteSpace(phoneNumber))
            {
                address.DaytimePhoneNumber = address.EveningPhoneNumber = phoneNumber;
            }

            if (isNewAddress)
            {
                currentContact.AddContactAddress(address);
            }
            else
            {
                currentContact.UpdateContactAddress(address);
            }
        }
Beispiel #9
0
        private static void MapAddressesFromCustomerToContact(CustomerPoco customer, CustomerContact contact)
        {
            foreach (var importedAddress in customer.Addresses)
            {
                var address = CustomerAddress.CreateInstance();

                address.Name        = importedAddress.Name;
                address.City        = importedAddress.City;
                address.CountryCode = importedAddress.CountryCode;
                address.CountryName = importedAddress.CountryName;
                address.FirstName   = customer.FirstName;
                address.LastName    = customer.LastName;
                address.Line1       = importedAddress.Line1;
                address.RegionCode  = importedAddress.RegionCode;
                address.RegionName  = importedAddress.RegionName;
                address.AddressType = CustomerAddressTypeEnum.Public | CustomerAddressTypeEnum.Shipping | CustomerAddressTypeEnum.Billing;

                contact.AddContactAddress(address);
            }
        }
Beispiel #10
0
        public static bool UpdateContactAddresses(string userId, CustomerContact currentContact, IEnumerable <UserAddressModel> userAddresses)
        {
            var updatedAddresses = userAddresses?.Where(x => x.DefaultShipping || x.DefaultBilling);

            if (updatedAddresses == null)
            {
                currentContact.SaveChanges();
                return(true);
            }

            foreach (var updatedAddress in updatedAddresses)
            {
                var currentAddress = currentContact.ContactAddresses
                                     .FirstOrDefault(x => x.AddressId.ToString() == updatedAddress.Id);
                CustomerAddress address;
                if (currentAddress == null)
                {
                    address = CreateCustomerAddress(updatedAddress, new Guid(userId));
                    currentContact.AddContactAddress(address);
                }
                else
                {
                    UpdateContactAddressData(currentAddress, updatedAddress);
                    address = currentAddress;
                    currentContact.UpdateContactAddress(address);
                }

                if (updatedAddress.DefaultBilling)
                {
                    currentContact.PreferredBillingAddress = address;
                }

                if (updatedAddress.DefaultShipping)
                {
                    currentContact.PreferredShippingAddress = address;
                }
            }

            currentContact.SaveChanges();
            return(true);
        }
Beispiel #11
0
        /// <summary>
        /// Creates a <see cref="CustomerAddress"/> for each existing address in <paramref name="customer"/> and adds it to <paramref name="contact"/>.
        /// </summary>
        /// <param name="customer">The <see cref="CustomerTemplate"/> holding the details about the addresses to be created.</param>
        /// <param name="contact">The <see cref="CustomerContact"/> that will be given any created address.</param>
        private static void MapAddressesFromCustomerToContact(CustomerTemplate customer, CustomerContact contact)
        {
            foreach (var importedAddress in customer.Addresses)
            {
                var address = CustomerAddress.CreateInstance();

                address.Name         = "Default address";
                address.PrimaryKeyId = new PrimaryKeyId(importedAddress.AddressId);
                address.City         = importedAddress.City;
                address.CountryCode  = importedAddress.CountryCode;
                address.CountryName  = importedAddress.CountryName;
                address.FirstName    = customer.FirstName;
                address.LastName     = customer.LastName;
                address.Line1        = importedAddress.Line1;
                address.RegionCode   = importedAddress.RegionCode;
                address.RegionName   = importedAddress.RegionName;
                address.State        = importedAddress.State;
                address.AddressType  = CustomerAddressTypeEnum.Public | CustomerAddressTypeEnum.Shipping | CustomerAddressTypeEnum.Billing;
                address.Email        = customer.Email;

                contact.AddContactAddress(address);
            }
        }