예제 #1
0
        public async Task <RegistrationResponse> UserRegistration(RegisterUserModelDto modelDto)
        {
            RegistrationResponse resp;

            if (modelDto == null)
            {
                throw new ArgumentNullException(nameof(modelDto));
            }

            var isEmailTaken = await CheckUserExists(e => e.EmailAddress.Equals(modelDto.EmailAddress));

            if (isEmailTaken != null)
            {
                // if email is already taken
                resp = new RegistrationResponse
                {
                    IsEmailTaken = true,
                    IsSucceeded  = false,
                };
                return(resp);
            }

            //transaction
            using (var _savieTransaction = _userProfileDbContext.Database.BeginTransaction())
            {
                //create password encrypt
                CreatePasswordEncrypt(modelDto.Pasword, out byte[] passwordHash, out byte[] passwordSalt);

                var user = new UserModel
                {
                    Firstname    = modelDto.Firstname,
                    Lastname     = modelDto.Lastname,
                    EmailAddress = modelDto.EmailAddress,
                    PhoneNo      = modelDto.PhoneNo,
                    Password     = modelDto.Pasword,
                    ReferredBy   = modelDto.ReferredBy,
                    PaswordHash  = passwordHash,
                    PasswordSalt = passwordSalt,
                    DateCreated  = DateTime.UtcNow,
                    IsActivated  = false,
                    IsActive     = false,
                };
                await _userProfileDbContext.AddAsync(user);

                await _userProfileDbContext.SaveChangesAsync();

                if (user.Id > 0)
                {
                    //if user registration succeeds and email not taken
                    var rnd  = new Random();
                    var rnd1 = rnd.Next(10, 99);
                    var rnd2 = rnd.Next(100, 999);
                    var rnd3 = rnd.Next(1000, 9999);
                    var code = rnd3 + user.EmailAddress.Substring(3, 5).ToUpper() + rnd2 + rnd1;
                    var name = user.Firstname;

                    if (user.EmailAddress != null && user.PhoneNo != null)
                    {
                        var activationCode = new UserActivationModel
                        {
                            ActivationCode      = code,
                            SentOn              = DateTime.UtcNow,
                            ExpiresAt           = DateTime.UtcNow.AddMinutes(5),//expires after 5 Minutes
                            ActivatedOn         = null,
                            ComfirmedActivation = false,
                            UserModelId         = user.Id,
                        };
                        //save the activation code

                        await _userProfileDbContext.UserActivationModel.AddAsync(activationCode);

                        await _userProfileDbContext.SaveChangesAsync();
                    }
                    _savieTransaction.Commit();

                    // await Task.Run(() => _notification.SendActivationCodeBySMS(name, code, user.PhoneNo));
                    await Task.Run(() => _notification.SendActivationCodeByMail(name, code, user.EmailAddress));

                    resp = new RegistrationResponse
                    {
                        IsSucceeded  = true,
                        IsEmailTaken = false,
                    };

                    return(resp);
                }
            }

            // if user registraion fails
            return(null);
        }