예제 #1
0
        public Customer GetCustomer(string email)
        {
            var             userId  = Customer.CreateUserId(email);
            CustomerContact contact = _customerContext.GetContactByUserId(userId);

            if (contact == null)
            {
                return(null);
            }

            var address = contact.ContactAddresses.Select(x => x.ToAddress()).ToList();

            return(new Customer()
            {
                Email = contact.Email,
                LastName = contact.LastName,
                FirstName = contact.FirstName,
                Addresses = address
            });
        }
예제 #2
0
        private void SaveCustomer(CustomerPoco customer, PrimaryKeyId orgId)
        {
            var user = _userManager.FindByEmailAsync(customer.Email)
                       .GetAwaiter()
                       .GetResult();

            if (user == null)
            {
                user = new SiteUser
                {
                    CreationDate = DateTime.UtcNow,
                    Username     = customer.Email,
                    Email        = customer.Email,
                    FirstName    = customer.FirstName,
                    LastName     = customer.LastName,
                    IsApproved   = true
                };

                var result = _userManager.CreateAsync(user, "Episerver123!")
                             .GetAwaiter()
                             .GetResult();

                if (!result.Succeeded)
                {
                    return;
                }
            }

            foreach (var role in customer.Roles)
            {
                if (!_roleManager.RoleExistsAsync(role)
                    .GetAwaiter()
                    .GetResult())
                {
                    var createdRole = new IdentityRole(role);

                    var roleResult = _roleManager.CreateAsync(createdRole)
                                     .GetAwaiter()
                                     .GetResult();

                    if (!roleResult.Succeeded)
                    {
                        continue;
                    }
                    _userManager.AddToRoleAsync(user.Id, role)
                    .GetAwaiter()
                    .GetResult();
                }
            }

            FoundationContact foundationContact;
            var contact = CustomerContext.GetContactByUserId($"String:{customer.Email}");

            if (contact == null)
            {
                foundationContact        = FoundationContact.New();
                foundationContact.UserId = customer.Email;
                foundationContact.Email  = customer.Email;
            }
            else
            {
                foundationContact = new FoundationContact(contact);
            }

            foundationContact.FirstName            = customer.FirstName;
            foundationContact.LastName             = customer.LastName;
            foundationContact.FullName             = $"{foundationContact.FirstName} {foundationContact.LastName}";
            foundationContact.RegistrationSource   = "Imported customer";
            foundationContact.AcceptMarketingEmail = true;
            foundationContact.ConsentUpdated       = DateTime.UtcNow;
            foundationContact.UserRole             = customer.B2BRole;
            foundationContact.UserLocationId       = customer.Location;
            foundationContact.DemoUserTitle        = customer.DemoUserTitle;
            foundationContact.DemoUserDescription  = customer.DemoUserDescription;
            foundationContact.ShowInDemoUserMenu   = customer.ShowInDemoUserMenu == 0 ? 1 : customer.ShowInDemoUserMenu;
            foundationContact.DemoSortOrder        = customer.DemoSortOrder;

            if (orgId != PrimaryKeyId.Empty)
            {
                foundationContact.Contact.OwnerId = orgId;
            }
            foundationContact.SaveChanges();

            MapAddressesFromCustomerToContact(customer, foundationContact.Contact);
            MapCreditCardsFromCustomerToContact(customer.CreditCards, foundationContact.Contact);
            foundationContact.SaveChanges();
        }