internal static CustomerProfileEntity Create(ICustomerProfile customerProfile)
 {
     return(new CustomerProfileEntity
     {
         CustomerId = customerProfile.CustomerId,
         Email = customerProfile.Email,
         LowerCasedEmail = customerProfile.Email.ToLower(),
         FirstName = customerProfile.FirstName,
         LastName = customerProfile.LastName,
         PhoneNumber = customerProfile.PhoneNumber,
         ShortPhoneNumber = customerProfile.ShortPhoneNumber,
         CountryPhoneCodeId = customerProfile.CountryPhoneCodeId,
         CountryOfNationalityId = customerProfile.CountryOfNationalityId,
         TierId = customerProfile.TierId,
         Registered = DateTime.UtcNow,
         IsEmailVerified = customerProfile.IsEmailVerified,
         IsPhoneVerified = customerProfile.IsPhoneVerified,
         LoginProviders = new List <LoginProviderEntity>(customerProfile.LoginProviders
                                                         .Select(x => new LoginProviderEntity
         {
             LoginProvider = x
         })),
         Status = customerProfile.Status,
     });
 }
        public async Task <CustomerProfileErrorCodes> CreateIfNotExistsAsync(ICustomerProfile customerProfile)
        {
            var isSocialAccountProfile =
                customerProfile.LoginProviders.Any(p => p != LoginProvider.Standard);

            if (isSocialAccountProfile)
            {
                customerProfile.IsEmailVerified = true;
            }

            if (customerProfile.CountryOfNationalityId.HasValue)
            {
                var countryResult =
                    await _dictionariesClient.Salesforce.GetCountryOfResidenceByIdAsync(
                        customerProfile.CountryOfNationalityId.Value);

                if (countryResult == null)
                {
                    return(CustomerProfileErrorCodes.InvalidCountryOfNationalityId);
                }
            }

            var creationResult = await _customerProfileRepository.CreateIfNotExistAsync(customerProfile);

            switch (creationResult)
            {
            case CustomerProfileErrorCodes.CustomerProfileAlreadyExistsWithDifferentProvider:
                _log.Warning("Customer Profile already exists but with different login provider",
                             context: customerProfile.CustomerId);
                return(creationResult);

            case CustomerProfileErrorCodes.CustomerProfileAlreadyExists:
                _log.Warning("Customer Profile already exists", context: customerProfile.CustomerId);
                return(creationResult);
            }

            _log.Info("Customer profile is created", context: customerProfile.CustomerId);

            if (isSocialAccountProfile)
            {
                Task.Run(async() =>
                {
                    try
                    {
                        await _emailVerifiedPublisher.PublishAsync(new EmailVerifiedEvent
                        {
                            CustomerId = customerProfile.CustomerId,
                            TimeStamp  = DateTime.UtcNow
                        });
                    }
                    catch (Exception e)
                    {
                        _log.Error(e);
                    }
                });
            }

            return(creationResult);
        }
        public async Task <CustomerProfileErrorCodes> CreateIfNotExistAsync(ICustomerProfile customerProfile)
        {
            using (var context = _contextFactory.CreateDataContext())
            {
                var lowerCasedEmail = _encryptionService.EncryptValue(customerProfile.Email.ToLower());

                //Note: Here we pull only customers that are Active (Not Archived/Deleted) and if there are none we create a new profile.
                //      We might want to give option for Profile Restore instead of creating new one if the person had a profile previously.
                var existentCustomerProfile = await context.CustomerProfiles
                                              .Include(x => x.LoginProviders)
                                              .IgnoreQueryFilters()
                                              .FirstOrDefaultAsync(c =>
                                                                   c.CustomerId == customerProfile.CustomerId ||
                                                                   c.Email == customerProfile.Email ||
                                                                   c.LowerCasedEmail == lowerCasedEmail);

                if (existentCustomerProfile != null)
                {
                    var registrationProvider = customerProfile.LoginProviders.First();
                    var hasDifferentProvider = existentCustomerProfile.LoginProviders
                                               .Any(x => x.LoginProvider != registrationProvider);

                    return(hasDifferentProvider ?
                           CustomerProfileErrorCodes.CustomerProfileAlreadyExistsWithDifferentProvider :
                           CustomerProfileErrorCodes.CustomerProfileAlreadyExists);
                }

                var entity = CustomerProfileEntity.Create(customerProfile);

                entity = _encryptionService.Encrypt(entity);

                context.CustomerProfiles.Add(entity);

                await context.SaveChangesAsync();

                return(CustomerProfileErrorCodes.None);
            }
        }
 public CustomProfileController(ICustomerProfile UserProfile)
 {
     this.userProfile = UserProfile;
 }