/// <inheritdoc />
        public Task Execute()
        {
            ExternalUserId externalUserId = this._userService
                                            .GetCurrentUser();

            return(this.GetCustomerInternal(externalUserId));
        }
Example #2
0
        public async Task <IUser> CreateUserCredentials(CustomerId customerId, ExternalUserId externalUserId)
        {
            var user = _userFactory.NewUser(customerId, externalUserId);
            await _userRepository.Add(user);

            return(user);
        }
Example #3
0
        public async Task Register_WritesOutput_InputIsValid(decimal amount)
        {
            var presenter      = new RegisterPresenter();
            var externalUserId = new ExternalUserId("github/ivanpaulovich");
            var ssn            = new SSN("8608178888");

            var sut = new Register(
                new TestUserService(_fixture.Context),
                _fixture.EntityFactory,
                _fixture.EntityFactory,
                _fixture.EntityFactory,
                presenter,
                _fixture.CustomerRepository,
                _fixture.AccountRepository,
                _fixture.UserRepository,
                _fixture.UnitOfWork);

            await sut.Execute(new RegisterInput(
                                  ssn,
                                  new PositiveMoney(amount)));

            var actual = presenter.Registers.Last();

            Assert.NotNull(actual);
            Assert.Equal(ssn, actual.Customer.SSN);
            Assert.NotEmpty(actual.Customer.Name.ToString());
            Assert.Equal(amount, actual.Account.CurrentBalance.ToDecimal());
        }
Example #4
0
        public ExternalUserId GetExternalUserId()
        {
            var    user           = this._httpContextAccessor.HttpContext.User;
            string value          = user.FindFirst(c => c.Type == "id")?.Value;
            var    externalUserId = new ExternalUserId($"{user.Identity.AuthenticationType}/{value}");

            return(externalUserId);
        }
        public ExternalUserId GetExternalUserId()
        {
            var user = _httpContextAccessor.HttpContext.User;

            var login          = user.FindFirst(c => c.Type == "urn:github:login")?.Value;
            var externalUserId = new ExternalUserId($"{user.Identity.AuthenticationType}/{login}");

            return(externalUserId);
        }
Example #6
0
        /// <summary>
        ///     Create User Credentials.
        /// </summary>
        /// <param name="customerId">CustomerId.</param>
        /// <param name="externalUserId">External User Id.</param>
        /// <returns>The User.</returns>
        public async Task <IUser> CreateUserCredentials(CustomerId customerId, ExternalUserId externalUserId)
        {
            var user = this.userFactory.NewUser(customerId, externalUserId);

            await this.userRepository.Add(user)
            .ConfigureAwait(false);

            return(user);
        }
        private async Task WithdrawInternal(AccountId accountId, PositiveMoney withdrawAmount)
        {
            ExternalUserId externalUserId = this._userService
                                            .GetCurrentUser();

            IUser user = await this._userRepository
                         .Find(externalUserId)
                         .ConfigureAwait(false);

            if (user is UserNull)
            {
                this._outputPort?.NotFound();
                return;
            }

            ICustomer customer = await this._customerRepository
                                 .Find(user.UserId)
                                 .ConfigureAwait(false);

            if (customer is CustomerNull)
            {
                this._outputPort?.NotFound();
                return;
            }

            IAccount account = await this._accountRepository
                               .Find(accountId, customer.CustomerId)
                               .ConfigureAwait(false);

            if (account is Account withdrawAccount)
            {
                PositiveMoney localCurrencyAmount =
                    await this._currencyExchange
                    .Convert(withdrawAmount, withdrawAccount.Currency)
                    .ConfigureAwait(false);

                Debit debit = this._accountFactory
                              .NewDebit(withdrawAccount, localCurrencyAmount, DateTime.Now);

                if (withdrawAccount.GetCurrentBalance().Amount - debit.Amount.Amount < 0)
                {
                    this._outputPort?.OutOfFunds();
                    return;
                }

                await this.Withdraw(withdrawAccount, debit)
                .ConfigureAwait(false);

                this._outputPort?.Ok(debit, withdrawAccount);
                return;
            }

            this._outputPort?.NotFound();
        }
        /// <inheritdoc />
        public ExternalUserId GetCurrentUser()
        {
            ClaimsPrincipal user = this._httpContextAccessor
                                   .HttpContext
                                   .User;

            string         id             = user.FindFirst(ClaimTypes.NameIdentifier)?.Value !;
            ExternalUserId externalUserId = new ExternalUserId($"{user.Identity.AuthenticationType}/{id}");

            return(externalUserId);
        }
        private async Task <IUser> GetExistingUser()
        {
            ExternalUserId externalUserId = this._userService
                                            .GetCurrentUser();

            IUser existingUser = await this._userRepository
                                 .Find(externalUserId)
                                 .ConfigureAwait(false);

            return(existingUser);
        }
        private void BuildOutput(
            ExternalUserId externalUserId,
            ICustomer customer,
            List <Boundaries.GetCustomerDetails.Account> accounts)
        {
            var output = new GetCustomerDetailsOutput(
                externalUserId,
                customer,
                accounts);

            this.outputPort.Standard(output);
        }
Example #11
0
        private void BuildOutput(
            ExternalUserId externalUserId,
            ICustomer customer,
            IAccount account)
        {
            var output = new RegisterOutput(
                externalUserId,
                customer,
                account);

            this._outputPort.Standard(output);
        }
Example #12
0
 public GetCustomerDetailsOutput(
     ExternalUserId externalUserId,
     ICustomer customer,
     List<Account> accounts)
 {
     ExternalUserId = externalUserId;
     Customer customerEntity = (Customer)customer;
     CustomerId = customerEntity.Id;
     SSN = customerEntity.SSN;
     Name = customerEntity.Name;
     Accounts = accounts;
 }
Example #13
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GetCustomerDetailsOutput"/> class.
        /// </summary>
        /// <param name="externalUserId">External User Id.</param>
        /// <param name="customer">Customer object.</param>
        /// <param name="accounts">Accounts list.</param>
        public GetCustomerDetailsOutput(
            ExternalUserId externalUserId,
            ICustomer customer,
            List <Account> accounts)
        {
            Customer customerEntity = (Customer)customer;

            this.ExternalUserId = externalUserId;
            this.CustomerId     = customerEntity.Id;
            this.SSN            = customerEntity.SSN;
            this.Name           = customerEntity.Name;
            this.Accounts       = accounts;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="Customer"/> class.
        /// </summary>
        /// <param name="externalUserId">External User Id.</param>
        /// <param name="customer">Customer object.</param>
        /// <param name="accounts">Accounts list.</param>
        public Customer(
            ExternalUserId externalUserId,
            ICustomer customer,
            List <Account> accounts)
        {
            var customerEntity = (Domain.Customers.Customer)customer;

            this.ExternalUserId = externalUserId;
            this.CustomerId     = customerEntity.Id;
            this.SSN            = customerEntity.SSN;
            this.Name           = customerEntity.Name;
            this.Accounts       = accounts;
        }
Example #15
0
        public Customer(
            ExternalUserId externalUserId,
            ICustomer customer,
            List <Account> accounts)
        {
            ExternalUserId = externalUserId;
            var customerEntity = (Domain.Customers.Customer)customer;

            CustomerId = customerEntity.Id;
            SSN        = customerEntity.SSN;
            Name       = customerEntity.Name;
            Accounts   = accounts;
        }
        public async Task <IUser> GetUser(ExternalUserId externalUserId)
        {
            User user = _context.Users
                        .Where(e => e.ExternalUserId.Equals(externalUserId))
                        .SingleOrDefault();

            if (user is null)
            {
                throw new UserNotFoundException($"The user {externalUserId} does not exist or is not processed yet.");
            }

            return(await Task.FromResult <User>(user));
        }
Example #17
0
        public async Task <IUser> GetUser(ExternalUserId externalUserId)
        {
            var user = await this._context
                       .Users
                       .Where(a => a.ExternalUserId.Equals(externalUserId))
                       .SingleOrDefaultAsync();

            if (user is null)
            {
                throw new UserNotFoundException($"The user {externalUserId} does not exist or is not processed yet.");
            }

            return(user);
        }
Example #18
0
        public async Task <IUser> GetUser(ExternalUserId externalUserId)
        {
            Domain.Security.User user = this._context.Users
                                        .Where(e => e.ExternalUserId.Equals(externalUserId))
                                        .SingleOrDefault();

            if (user is null)
            {
                throw new UserNotFoundException($"The user {externalUserId} does not exist or is not processed yet.");
            }

            return(await Task.FromResult(user)
                   .ConfigureAwait(false));
        }
Example #19
0
        public async Task <IUser> Get(ExternalUserId externalUserId)
        {
            Infrastructure.EntityFrameworkDataAccess.User user = await _context
                                                                 .Users
                                                                 .Where(a => a.ExternalUserId.Equals(externalUserId))
                                                                 .SingleOrDefaultAsync();

            if (user is null)
            {
                throw new UserNotFoundException($"The user {externalUserId} does not exist or is not processed yet.");
            }

            return(user);
        }
Example #20
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="RegisterOutput" /> class.
        /// </summary>
        /// <param name="externalUserId">External User Id.</param>
        /// <param name="customer">Customer object.</param>
        /// <param name="account">Account object.</param>
        public RegisterOutput(ExternalUserId externalUserId, ICustomer customer, IAccount account)
        {
            if (account is Domain.Accounts.Account accountEntity)
            {
                List <Transaction> transactionResults = new List <Transaction>();
                foreach (ICredit credit in accountEntity.Credits
                         .GetTransactions())
                {
                    Credit creditEntity = (Credit)credit;

                    Transaction transactionOutput = new Transaction(
                        Credit.Description,
                        creditEntity.Amount,
                        creditEntity.TransactionDate);

                    transactionResults.Add(transactionOutput);
                }

                foreach (IDebit debit in accountEntity.Debits
                         .GetTransactions())
                {
                    Debit debitEntity = (Debit)debit;

                    Transaction transactionOutput = new Transaction(
                        Debit.Description,
                        debitEntity.Amount,
                        debitEntity.TransactionDate);

                    transactionResults.Add(transactionOutput);
                }

                this.Account = new Account(
                    accountEntity.Id,
                    accountEntity.GetCurrentBalance(),
                    transactionResults);

                List <Account> accountOutputs = new List <Account>();
                accountOutputs.Add(this.Account);

                this.Customer = new Customer(externalUserId, customer, accountOutputs);
            }
            else
            {
                throw new ArgumentNullException(nameof(account));
            }
        }
Example #21
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="GetCustomerDetailsOutput" /> class.
        /// </summary>
        /// <param name="externalUserId">External User Id.</param>
        /// <param name="customer">Customer object.</param>
        /// <param name="accounts">Accounts list.</param>
        public GetCustomerDetailsOutput(
            ExternalUserId externalUserId,
            ICustomer customer,
            List <Account> accounts)
        {
            if (customer is null)
            {
                throw new ArgumentNullException(nameof(customer));
            }

            Customer customerEntity = (Customer)customer;

            this.ExternalUserId = externalUserId;
            this.CustomerId     = customerEntity.Id;
            this.SSN            = customerEntity.SSN;
            this.Name           = customerEntity.Name;
            this.Accounts       = accounts;
        }
        private async Task GetCustomerInternal(ExternalUserId externalUserId)
        {
            IUser user = await this._userRepository
                         .Find(externalUserId)
                         .ConfigureAwait(false);

            ICustomer customer = await this._customerRepository
                                 .Find(user.UserId)
                                 .ConfigureAwait(false);

            if (customer is Customer getCustomer)
            {
                this._outputPort?.Ok(getCustomer);
                return;
            }

            this._outputPort?.NotFound();
        }
Example #23
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="Customer" /> class.
        /// </summary>
        /// <param name="externalUserId">External User Id.</param>
        /// <param name="customer">Customer object.</param>
        /// <param name="accounts">Accounts list.</param>
        public Customer(
            ExternalUserId externalUserId,
            ICustomer customer,
            List <Account> accounts)
        {
            if (customer is null)
            {
                throw new ArgumentNullException(nameof(customer));
            }

            var customerEntity = (Domain.Customers.Customer)customer;

            this.ExternalUserId = externalUserId;
            this.CustomerId     = customerEntity.Id;
            this.SSN            = customerEntity.SSN;
            this.Name           = customerEntity.Name;
            this.Accounts       = accounts;
        }
        public RegisterOutput(ExternalUserId externalUserId, ICustomer customer, IAccount account)
        {
            var accountEntity = (Domain.Accounts.Account)account;

            List <Transaction> transactionResults = new List <Transaction>();

            foreach (ICredit credit in accountEntity.Credits
                     .GetTransactions())
            {
                Credit creditEntity = (Credit)credit;

                Transaction transactionOutput = new Transaction(
                    creditEntity.Description,
                    creditEntity.Amount,
                    creditEntity.TransactionDate);

                transactionResults.Add(transactionOutput);
            }

            foreach (IDebit debit in accountEntity.Debits
                     .GetTransactions())
            {
                Debit debitEntity = (Debit)debit;

                Transaction transactionOutput = new Transaction(
                    debitEntity.Description,
                    debitEntity.Amount,
                    debitEntity.TransactionDate);

                transactionResults.Add(transactionOutput);
            }

            Account = new Account(
                account.Id,
                account.GetCurrentBalance(),
                transactionResults);

            List <Account> accountOutputs = new List <Account>();

            accountOutputs.Add(Account);

            Customer = new Customer(externalUserId, customer, accountOutputs);
        }
        private async Task SignUpInternal(ExternalUserId externalUserId)
        {
            IUser findUser = await this._userRepository
                             .Find(externalUserId)
                             .ConfigureAwait(false);

            if (findUser is User existingUser)
            {
                this._outputPort?.UserAlreadyExists(existingUser);
            }
            else
            {
                User user = this._userFactory
                            .NewUser(externalUserId);

                await this.CreateUser(user)
                .ConfigureAwait(false);

                this._outputPort?.Ok(user);
            }
        }
        private async Task CloseAccountInternal(AccountId accountId)
        {
            ExternalUserId externalUserId = this._userService
                                            .GetCurrentUser();

            IUser existingUser = await this._userRepository
                                 .Find(externalUserId)
                                 .ConfigureAwait(false);

            ICustomer customer = await this._customerRepository
                                 .Find(existingUser.UserId)
                                 .ConfigureAwait(false);

            if (customer is CustomerNull)
            {
                this._outputPort?.NotFound();
                return;
            }

            IAccount account = await this._accountRepository
                               .Find(accountId, customer.CustomerId)
                               .ConfigureAwait(false);

            if (account is Account closingAccount)
            {
                if (!closingAccount.IsClosingAllowed())
                {
                    this._outputPort?.HasFunds();
                    return;
                }

                await this.Close(closingAccount)
                .ConfigureAwait(false);

                this._outputPort?.Ok(closingAccount);
                return;
            }

            this._outputPort?.NotFound();
        }
        /// <inheritdoc />
        public async Task Execute()
        {
            ExternalUserId externalUserId = this._userService
                                            .GetCurrentUser();

            IUser user = await this._userRepository
                         .Find(externalUserId)
                         .ConfigureAwait(false);

            ICustomer customer = await this._customerRepository
                                 .Find(user.UserId)
                                 .ConfigureAwait(false);

            if (customer is Customer getCustomer)
            {
                await this.GetAccountsInternal(getCustomer)
                .ConfigureAwait(false);
            }
            else
            {
                this._outputPort?.NotFound();
            }
        }
Example #28
0
        /// <inheritdoc />
        public IUser GetUser()
        {
            ClaimsPrincipal user = this._httpContextAccessor
                                   .HttpContext
                                   .User;

            string id             = user.FindFirst(c => c.Type == "id")?.Value !;
            var    externalUserId = new ExternalUserId($"{user.Identity.AuthenticationType}/{id}");
            var    username       = new Name(user.Identity.Name !);

            CustomerId?customerId = null;

            if (user.FindFirst(c => c.Type == "customerid") is { } value)
            {
                customerId = new CustomerId(new Guid(value.Value));
            }

            IUser domainUser = this._userFactory.NewUser(
                customerId,
                externalUserId,
                username);

            return(domainUser);
        }
        private async Task OpenAccountInternal(PositiveMoney amountToDeposit)
        {
            ExternalUserId externalUserId = this._userService
                                            .GetCurrentUser();

            IUser user = await this._userRepository
                         .Find(externalUserId)
                         .ConfigureAwait(false);

            if (user is UserNull)
            {
                this._outputPort?.NotFound();
                return;
            }

            ICustomer customer = await this._customerRepository
                                 .Find(user.UserId)
                                 .ConfigureAwait(false);

            if (customer is CustomerNull)
            {
                this._outputPort?.NotFound();
                return;
            }

            Account account = this._accountFactory
                              .NewAccount(customer.CustomerId, amountToDeposit.Currency);

            Credit credit = this._accountFactory
                            .NewCredit(account, amountToDeposit, DateTime.Now);

            await this.Deposit(account, credit)
            .ConfigureAwait(false);

            this._outputPort?.Ok(account);
        }
Example #30
0
 public IUser NewUser(ICustomer customer, ExternalUserId externalUserId) => new User(customer, externalUserId);