Exemple #1
0
        public ICreateAccountResponse CreateAccount(ICreateAccountRequest request)
        {
            var result = new CreateAccountResponse();

            result.InvalidPassword = request.Password.Length <RegistrationConfiguration.MinimumPasswordLength || request.Password.Length> RegistrationConfiguration.MaximumPasswordLength;
            var acct = new Account
            {
                Active               = true,
                CreatedOnUtc         = DateTime.UtcNow,
                Encrypted            = RegistrationConfiguration.EncryptPassword,
                Id                   = Guid.NewGuid(),
                Username             = request.Username,
                Password             = request.Password,
                RecoveryEmailAddress = request.RecoveryEmailAddress
            };


            result.UsernameExists = _accountRepository.Table.Any(
                x => x.Username.Equals(acct.Username, StringComparison.InvariantCultureIgnoreCase));


            result.InavlidUsername = acct.Username.Length <RegistrationConfiguration.MinimumUsernameLength || acct.Username.Length> RegistrationConfiguration.MaximumUsernameLength;

            result.InvalidRecoveryEmailAddress = String.IsNullOrEmpty(acct.RecoveryEmailAddress) ||
                                                 acct.RecoveryEmailAddress.Length >= 200;

            if (!result.UsernameExists & !result.InavlidUsername & !result.InvalidPassword & !result.InvalidRecoveryEmailAddress)
            {
                if (RegistrationConfiguration.EncryptPassword)
                {
                    acct.Password  = request.Password.EncryptText();
                    acct.Encrypted = true;
                }
                _accountRepository.Insert(acct);
                result.Success = true;
                result.Account = acct;
            }
            return(result);
        }
Exemple #2
0
        public async Task <ICreateAccountResponse> CreateAccount(ICreateAccountRequest createAccountRequest, IEmployee employee)
        {
            ICustomer customer;

            if (createAccountRequest.CustomerId != 0)
            {
                customer = _customerModule.GetCustomer(createAccountRequest.CustomerId);
                if (customer == null)
                {
                    throw new DataIntegrityViolation($"Customer {createAccountRequest.CustomerId} doesn't exist");
                }
            }
            else if (createAccountRequest.Customer != null)
            {
                customer = _customerModule.CreateCustomer(new CustomerInfo {
                    Name = createAccountRequest.Customer.Name
                });
                _customerModule.StageChanges();
            }
            else
            {
                throw new DataIntegrityViolation($"No customer specified");
            }

            var accountApp = _accountModule.CreateAccountApplication(employee);

            accountApp.Nrb = createAccountRequest.Nrb;

            try
            {
                accountApp.AddApplicant(customer, CustomerProductRole.Owner);
            }
            catch (DomainException)
            {
                _customerModule.RejectChanges();
                _accountModule.RejectChanges();
                throw;
            }

            accountApp.ApplicantsAdded();
            _customerModule.SaveChanges();
            _accountModule.SaveChanges();

            string additionalInfo;

            try
            {
                additionalInfo = await _environmentAdapter.GetApplicationAdditionalInfo(accountApp, "Set debit");
            }
            catch (Exception)
            {
                _accountModule.SaveChanges();
                _customerModule.SaveChanges();
                return(new CreateAccountResponse {
                    Application = accountApp
                });
            }

            var account = _accountModule.CreateAccount(new AccountInfo {
                Debit = decimal.Parse(additionalInfo), Nrb = createAccountRequest.Nrb
            }, accountApp.Applicants.First().Id);

            accountApp.AdditionalDataCollected(account);
            _accountModule.StageChanges();

            try
            {
                await _environmentAdapter.CreateAccountInTransactionSystem(account);

                if (string.IsNullOrEmpty(account.Nrb))
                {
                    _accountModule.RejectChanges();
                    return(new CreateAccountResponse {
                        Application = accountApp
                    });
                }
                else
                {
                    accountApp.AccountCreated();
                    _accountModule.SaveChanges();
                    return(new CreateAccountResponse {
                        Account = account, Application = accountApp
                    });
                }
            }
            catch (Exception)
            {
                _accountModule.RejectChanges();
                return(new CreateAccountResponse {
                    Application = accountApp
                });
            }
        }