public void GetAccountsByCustomerSomeAccountsFound()
        {
            IAccountRepository accountRepository = new AccountRepository(NhibernateHelper.SessionFactory);
            Repository repository = new Repository(NhibernateHelper.SessionFactory);

            Customer thirdParty1 = new Customer { Code = "tjdsklfs", Email = "*****@*****.**", LastName = "roux", FirstName = "Olivier", Password = "******", PasswordSalt = "sss" };
            Customer thirdParty2 = new Customer { Code = "topsecret", Email = "*****@*****.**", LastName = "roux2", FirstName = "Olivier", Password = "******", PasswordSalt = "sss" };

            Account account1 = new Account { Balance = 201, BalanceDate = DateTime.Now, Number = "dsf1", Iban="12354"};
            Account account2 = new Account { Balance = 202, BalanceDate = DateTime.Now, Number = "dsf2", Iban="12435"};

            Role role = new Role{Id=1};
            thirdParty1.RelatedAccounts.Add(account1, role);
            thirdParty1.RelatedAccounts.Add(account2, role);

            using (NhibernateHelper.SessionFactory.GetCurrentSession().BeginTransaction())
            {
                repository.Save(thirdParty1);
                repository.Save(thirdParty2);
                repository.Save(account1);
                repository.Save(account2);

                repository.Flush();

                IList<Account> accounts = accountRepository.GetAccountsByCustomer(thirdParty1.Id);
                Assert.AreEqual(2, accounts.Count);
            }
        }
Beispiel #2
0
        public static Account Create(
            int value_i,
            decimal value_d,
            DateTime? value_null,
            string value_s,
            IList<Operation> value_iList,
            int value_i1,
            bool value_b,
            string value_s1,
            string value_s2,
            IList<TagDepenses> value_iList1_,
            string value_s3,
            DateTime? value_null1
        )
        {
            //return DataHelper.GetAccounts().FirstOrDefault(x => x.Id == value_i);
            //PexAssume.IsNotNullOrEmpty<Operation>(value_iList);

            Account account = new Account();
            account.Id = value_i;
            account.Balance = value_d;
            account.BalanceDate = value_null;
            account.Number = value_s;
            account.Operations = DataHelper.GetOperations();
            account.NbOfDaysOverdraft = value_i1;
            account.AuthorizeOverdraft = value_b;
            account.Iban = value_s1;
            account.Currency = value_s2;
            account.TagDepenses = value_iList1_;
            ((BankingProduct)account).Name = value_s3;
            ((BankingProduct)account).CreationDate = value_null1;
            return account;
        }
        public void Balance_CustomerOK()
        {
            //arrange
            Account account = new Account();
            Account account2 = new Account();
            Customer customer = new Customer();

            customer.RelatedAccounts.Add(account, new Role());
            customer.RelatedAccounts.Add(account2, new Role());

            IRepository repository = MockRepository.GenerateMock<IRepository>();
            ICustomerRepository customerRepository = MockRepository.GenerateMock<ICustomerRepository>();
            IAccountRepository accountRepository = MockRepository.GenerateMock<IAccountRepository>();
            IDtoCreator<Customer, CustomerDto> custCreator = new CustomerDtoCreator();

            IAccountServices accountServices = MockRepository.GenerateMock<IAccountServices>(); //(repository, accountRepository,customerRepository);
            accountServices.Expect(x => x.Balance(account2)).Return(-10);
            accountServices.Expect(x => x.Balance(account)).Return(-20);

            //act
            CustomerServices services = new CustomerServices(customerRepository, repository, accountServices, custCreator);
            decimal balance = services.CustomerBalance(customer);

            //assert
            Assert.AreEqual(balance, -30);
            accountServices.VerifyAllExpectations();
        }
Beispiel #4
0
        public void CreateAccount(String accountName, int customerID, int roleID)
        {
            Customer customer = _repository.Get<Customer>(customerID);
            if (customer == null)
            {
                throw new AccountServicesException("No customer found for the ID: " + customerID);
            }

            Role role = _repository.Get<Role>(roleID);
            if (role == null)
            {
                throw new AccountServicesException("No role found for the ID: " + roleID);
            }

            Account account = new Account();
            account.Name = accountName;
            customer.RelatedAccounts.Add(account, role);
            account.RelatedCustomers.Add(customer, role);

            using (TransactionScope scope = new TransactionScope())
            {
                _repository.Save<Account>(account);
                _repository.SaveOrUpdate<Customer>(customer);
                scope.Complete();
            }
        }
Beispiel #5
0
 public Decimal Balance(Account account)
 {
     if (account.Operations != null)
     {
         return account.Operations.Sum(x => x.SignedAmount);
     }
     return 0;
 }
Beispiel #6
0
 public Permission GetCustomerRights(Account account, Customer customer)
 {
     if (customer.RelatedAccounts.ContainsKey(account))
     {
         Role role = customer.RelatedAccounts[account];
         return role.Permission;
     }
     return 0;
 }
Beispiel #7
0
 public Permission GetAdvisorRights(Account account, Advisor advisor)
 {
     foreach (Customer customer in account.RelatedCustomers.Keys)
     {
         if(advisor.Customers.Contains(customer)){
             return advisor.Role.Permission;
         }
     }
     return 0;
 }
        public List<TagDepenses> AggregateAccountTags(Account account)
        {
            //excluding null tags - this should never happen
            //while when new transactions is created, the tag is set to a new empty tag.

            var grouped = account.Operations.Where(x => x.SignedAmount < 0 && x.Tag!=null).GroupBy(x => x.Tag);
            var dict = grouped.ToDictionary(x => x.Key, y => y.Sum(x => x.SignedAmount));
            List<TagDepenses> depenses = dict.Select(x => new TagDepenses() { Tag = x.Key, Depenses = Math.Abs(x.Value) }).ToList();
            return depenses;
        }
        public decimal Balance(Account account)
        {
            SIRepository repository = new SIRepository();
            SIAccountRepository accountRepository = new SIAccountRepository();
            SICustomerRepository customerRepository = new SICustomerRepository();
            IDtoCreator<Account, AccountDto> accountCreator = new AccountDtoCreator();

            //act
            var accountServices = new AccountServices(repository, accountRepository, customerRepository, accountCreator);
            var result = accountServices.Balance(account);
            return result;
        }
        public void CategorizePayments(Account account)
        {
            //TODO:get the owner of the account,get his personal tags
            //get his personal rules
            //classify his spendings

            TextClassifier classifier = new TextClassifier();

            var operations = account.Operations;
            foreach (var operation in operations)
            {
                var tag = classifier.Categorize(operation.OppositeDescription);
                operation.Tag = tag;
                _repository.SaveOrUpdate(operation);
            }
        }
        public void makeTransfer_DebitAccountNotFound()
        {
            //arrange
            IOperationRepository operationRepository = MockRepository.GenerateStub<IOperationRepository>();
            IRepository repository = MockRepository.GenerateStub<IRepository>();
            IDtoCreator<Operation, OperationDto> operationCreator = MockRepository.GenerateStub<IDtoCreator<Operation, OperationDto>>();

            Account creditAccount = new Account() { Id = 1 };
            Account debitAccount = new Account() { Id = 2 };

            repository.Expect(x=>x.Get<Account>(creditAccount.Id)).Return(creditAccount);

            //act
            OperationServices services = new OperationServices(operationRepository, repository,operationCreator );
            services.MakeTransfer(debitAccount.Id, creditAccount.Id, 0, null);
        }
        public void Balance_AccountOK()
        {
            //arrange
            Account account = new Account();
            account.Operations = new List<Operation>();
            account.Operations.Add(new Operation { Account = account, Amount = 10, Direction = Direction.Credit });
            account.Operations.Add(new Operation { Account = account, Amount = 20, Direction = Direction.Debit });
            account.Operations.Add(new Operation { Account = account, Amount = 0, Direction = Direction.Credit });

            IRepository repository = MockRepository.GenerateStub<IRepository>();
            ICustomerRepository customerRepository = MockRepository.GenerateStub<ICustomerRepository>();
            IAccountRepository accountRepository = MockRepository.GenerateStub<IAccountRepository>();
            IDtoCreator<Account, AccountDto> accountCreator = new AccountDtoCreator();

            //act
            AccountServices services = new AccountServices(repository, accountRepository, customerRepository, accountCreator);
            decimal balance = services.Balance(account);

            //assert
            Assert.AreEqual(balance, -10);
        }
        public void GetAccountByCustomerNoAccountFound()
        {
            IAccountRepository accountRepository = new AccountRepository(NhibernateHelper.SessionFactory);
            Repository repository = new Repository(NhibernateHelper.SessionFactory);

            Customer thirdParty1 = new Customer { Code = "tjdsklfs", Email = "*****@*****.**", LastName = "Roux", FirstName = "Olivier", Password = "******", PasswordSalt = "sss" };
            Customer thirdParty2 = new Customer { Code = "topsecret", Email = "*****@*****.**", LastName = "Roux2", FirstName = "Olivier", Password = "******", PasswordSalt = "sss" };
            Account account1 = new Account { Balance = 201, BalanceDate = DateTime.Now, Number = "dsf1", Iban="12349340943"};

            using (NhibernateHelper.SessionFactory.GetCurrentSession().BeginTransaction())
            {
                repository.Save(thirdParty1);
                repository.Save(thirdParty2);
                repository.Save(account1);

                repository.Flush();

                IList<Account> accounts = accountRepository.GetAccountsByCustomer(thirdParty2.Id);
                Assert.AreEqual(0, accounts.Count);
            }
        }
        public string Transfer(
            Account debitAccount,
            Account creditAccount,
            decimal amount,
            string motif
        )
        {
            MGuid.NewGuid = () => new Guid("64d80a10-4f21-4acb-9d3d-0332e68c4394");

            PexAssume.IsNotNull(debitAccount);
            PexAssume.IsNotNull(creditAccount);
            PexAssume.IsTrue(creditAccount != debitAccount);
            var repository = new SIRepository();
            var operationRepository = new SIOperationRepository();
            var operationCreator = new OperationDtoCreator();

            //act
            var operationServices = new OperationServices(operationRepository, repository, operationCreator);
            operationServices.Transfer(debitAccount, creditAccount, amount, motif);

            string result = operationServices.Transfer(debitAccount, creditAccount, amount, motif);
            PexAssert.IsNotNullOrEmpty(result);
            return result;
        }
Beispiel #15
0
        public String Transfer(Account debitAccount, Account creditAccount, decimal amount, string motif)
        {
            if (!debitAccount.AuthorizeOverdraft && debitAccount.Balance < amount)
            {
                throw new OperationServicesException(StringResources.ErrorNotEnoughtMoney);
            }

            String transactionCode = Guid.NewGuid().ToString();
            Operation debitOperation = new Operation() {
                Account = debitAccount, Direction = Direction.Debit, Amount = amount, Motif = motif, Date=DateTime.Now,
                TransactionCode=transactionCode, Currency = debitAccount.Currency};
            Operation creditOperation = new Operation() {
                Account = creditAccount, Direction = Direction.Credit, Amount = amount, Motif = motif, Date=DateTime.Now,TransactionCode=transactionCode, Currency = creditAccount.Currency};

            debitAccount.Operations.Add(debitOperation);

            //for the debit payment, the account number is set as the description of the payment
            //as well as the motif of the payment
            debitOperation.Description = String.Format("TRANSFER {0} {1}", creditAccount.Iban, motif);
            creditOperation.Description = String.Format("RECEIVED PAYMENT {0} {1}", debitAccount.Iban, motif);

            creditAccount.Operations.Add(creditOperation);
            debitAccount.Balance -= amount;
            creditAccount.Balance += amount;

            using (TransactionScope scope = new TransactionScope())
            {
                _repository.Save(debitOperation);
                _repository.Save(creditOperation);
                _repository.Save(debitAccount);
                _repository.Save(creditAccount);

                scope.Complete();
            }
            return transactionCode;
        }
Beispiel #16
0
        public Permission GetUserRights(Account account, UserIdentity user)
        {
            if (user.Type == "Advisor")
            {
                Advisor advisor = _repository.Get<Advisor>(user.Id);
                if (advisor != null)
                {
                    return GetAdvisorRights(account, advisor);
                }
                return Permission.No;

            }
            else if (user.Type == "Customer")
            {
                Customer customer = _repository.Get<Customer>(user.Id);
                if (customer != null)
                {
                    return GetCustomerRights(account, customer);
                }
                return Permission.No;
            }
            else
            {
                throw new UserServicesException("Not expected type of user");
            }
        }
Beispiel #17
0
        public String TransferToExternal(Account debitAccount, String creditAccountIban, decimal amount, String motif)
        {
            String transactionCode = Guid.NewGuid().ToString();
            Operation debitOperation = new Operation() {
                Account = debitAccount,
                Direction = Direction.Debit,
                Amount = amount,
                Motif = motif,
                Date = DateTime.Now,
                TransactionCode = transactionCode,
                OppositeIban = creditAccountIban,
                Currency = debitAccount.Currency,
                Description = String.Format("TRANSFER {0} {1}",creditAccountIban,motif)
            };
            debitAccount.Operations.Add(debitOperation);

            //update of the balance - also of the evolution of the balance
            debitAccount.Balance -= amount;

            using (TransactionScope scope = new TransactionScope())
            {

                _repository.Save(debitOperation);
                _repository.Save(debitAccount);

                scope.Complete();
            }

            return transactionCode;
            //Here in real scenario I would send the transaction to CBS
        }
Beispiel #18
0
 public decimal Balance(Account account)
 {
     Contract.Requires<AccountServicesException>(account != null);
     return default(decimal);
 }
Beispiel #19
0
        //adds first 80 transactions with the date originated from the CSV file (to have some historic information)
        //adds next 40 transactions and chooses random date from last 20 days - to have some actual information
        private static void SelectForAccount(IRepository repository, Account account, Random rnd, IOrderedEnumerable<Operation> randomTransactions)
        {
            randomTransactions.Take(80).ForEach(x => { x.Account = account; repository.Save(x); });

            randomTransactions.Skip(80).Take(40).ForEach(x =>
            {
                x.Account = account;
                x.Date = DateTime.Now.Subtract(new TimeSpan(rnd.Next(20), 0, 0, 0));
                repository.Save(x);
            });
        }
 /// <summary>
 /// Computes balance evolution of account - completely from beginning, if there was an evolution, erases the content
 /// </summary>
 /// <param name="account"></param>
 public void ComputeBalancePoints(Account account)
 {
     if (account.Operations.Count > 0)
     {
         account.BalancePoints = new List<BalancePoint>();
         CreateBalancePointsForOperations(account.Operations, 0, account);
     }
 }
        public void UpdateBalancePoints(Account account)
        {
            if (account.BalancePoints.Count > 0)
            {
                var last = account.BalancePoints.OrderBy(x => x.Date).Last();

                //have to compare the dates! not datetimes
                var newOperations = account.Operations.Where(x => x.Date.Date.CompareTo(last.Date) > 0);
                var previousDate = last.Balance;
                CreateBalancePointsForOperations(newOperations, previousDate,account);
            }
            account.Balance = account.BalancePoints.Last().Balance;
        }
        public String Transfer(Account debitAccount, Account creditAccount, decimal amount, string motif)
        {
            Contract.Requires<OperationServicesException>(amount != null);
            Contract.Requires<ArgumentNullException>(debitAccount != null);
            Contract.Requires<ArgumentNullException>(creditAccount != null);
            Contract.Requires<OperationServicesException>(debitAccount != creditAccount);
            Contract.Requires<OperationServicesException>(amount > 0);

            return default(String);
        }
        public void GetClientAccounts_ClientNotNull()
        {
            //arange
            IRepository repository = MockRepository.GenerateStub<IRepository>();
            IAccountRepository accountRepository = MockRepository.GenerateMock<IAccountRepository>();
            ICustomerRepository thirdPartyRepository = MockRepository.GenerateStub<ICustomerRepository>();
            IDtoCreator<Account, AccountDto> accountCreator = new AccountDtoCreator();

            Customer customer = new Customer { Id = 3 };
            IList<Account> accounts = new List<Account>();
            Account account = new Account() { Id = 1 };
            accounts.Add(account);

            accountRepository.Expect(x => x.GetAccountsByCustomer(customer.Id)).Return(accounts);

            //act
            AccountServices services = new AccountServices(repository, accountRepository, thirdPartyRepository,accountCreator);
            IList<AccountDto> retrievedAccounts = services.GetCustomerAccounts(customer.Id);

            //assert
            Assert.AreEqual(1, retrievedAccounts.Count);
            Assert.AreEqual(account.Id, retrievedAccounts[0].Id);
            accountRepository.VerifyAllExpectations();
        }
        public void makeTransfer_NotEnoughMoneyDebitAccount()
        {
            IOperationRepository operationRepository = MockRepository.GenerateStub<IOperationRepository>();
            IRepository repository = MockRepository.GenerateMock<IRepository>();
            IDtoCreator<Operation, OperationDto> operationCreator = MockRepository.GenerateStub<IDtoCreator<Operation, OperationDto>>();

            Account debitAccount = new Account() { Id = 1, Balance = 100 };
            Account creditAccount = new Account() { Id = 2, Balance = 300 };

            repository.Expect(x=>x.Get<Account>(debitAccount.Id)).Return(debitAccount);
            repository.Expect(x=>x.Get<Account>(creditAccount.Id)).Return(creditAccount);

            OperationServices services = new OperationServices(operationRepository, repository, operationCreator);
            services.MakeTransfer(debitAccount.Id, creditAccount.Id, 200, "");

            repository.VerifyAllExpectations();
        }
        public void GetIndividualCustomerByCode_Found()
        {
            ICustomerRepository customerRepository = new CustomerRepository(NhibernateHelper.SessionFactory);
            Repository repository = new Repository(NhibernateHelper.SessionFactory);

            Customer customer = new Customer { Code = "tjdsklfs", Email = "*****@*****.**", FirstName = "Sim", LastName = "Lehericey", Password = "******", PasswordSalt = "sss" };
            Account account1 = new Account { Balance = 201, BalanceDate = DateTime.Now, Number = "dsf1", Iban="1234"};

            Customer retrievedCustomer;

            using (NhibernateHelper.SessionFactory.GetCurrentSession().BeginTransaction())
            {
                repository.Save(customer);
                repository.Save(account1);
                repository.Flush();

                retrievedCustomer = customerRepository.GetCustomerByCode(customer.Code);
                Assert.IsNotNull(retrievedCustomer);
            }
        }
        /// <summary>
        /// Helping method, iterates through operations and sums the values to compute the account evolution
        /// </summary>
        /// <param name="operations"></param>
        /// <param name="lastBalance"></param>
        /// <param name="account"></param>
        private void CreateBalancePointsForOperations(IEnumerable<Operation> operations, Decimal lastBalance,Account account)
        {
            var parDate = operations
                    .OrderBy(x => x.Date.Date)
                    .GroupBy(x => x.Date.Date)
                    .ToDictionary(x => x.Key, x => x.Sum(y => y.SignedAmount));

            using (TransactionScope scope = new TransactionScope())
            {

                foreach (var item in parDate)
                {
                    BalancePoint point = new BalancePoint { Date = item.Key, Balance = lastBalance + item.Value, Account = account }; // item.Value.Sum(x => x.SignedAmount) };
                    lastBalance = point.Balance;
                    _repository.Save(point);
                    account.BalancePoints.Add(point);
                }
                account.Balance = lastBalance;
                scope.Complete();
            }
        }
 public string TransferToExternal(Account debitAccount, String creditAccountIban, decimal amount, string motif)
 {
     Contract.Requires<OperationServicesException>(amount > 0);
     Contract.Requires<OperationServicesException>(debitAccount != null);
     Contract.Requires<OperationServicesException>(!string.IsNullOrEmpty(creditAccountIban));
     return default(String);
 }
 private List<TagDepenses> AggregateAccountTags(Account account)
 {
     var grouped = account.Operations.Where(x => x.SignedAmount < 0).GroupBy(x => x.Tag);
     var dict = grouped.ToDictionary(x => x.Key, y => y.Sum(x => x.SignedAmount));
     List<TagDepenses> depenses = dict.Select(x => new TagDepenses() { Tag = x.Key, Depenses = x.Value }).ToList();
     return depenses;
 }
        /// <summary>
        /// Method which computes the Balance points of an account
        /// //TODO: Modify this to work only over certain time period
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        private Dictionary<DateTime, BalancePoint> ComputeBalancePoints(Account account)
        {
            Dictionary<DateTime, BalancePoint> result = new Dictionary<DateTime, BalancePoint>();

            if (account.Operations.Count > 0)
            {

                var parDate = account.Operations
                    .OrderBy(x => x.Date.Date)
                    .GroupBy(x => x.Date.Date)
                    .ToDictionary(x => x.Key, x => x.ToList());

                decimal previousDate = 0;
                for (DateTime date = parDate.Keys.First(); date.CompareTo(DateTime.Now) <= 0; date = date.AddDays(1))
                {
                    BalancePoint point = null;
                    if (parDate.ContainsKey(date))
                    {
                        List<Operation> operationsToAdd = parDate[date];
                        point = new BalancePoint() { Date = date.Date, Balance = previousDate + operationsToAdd.Sum(x => x.SignedAmount) };
                    }
                    else
                    {
                        point = new BalancePoint() { Date = date, Balance = previousDate };
                    }
                    result.Add(date.Date, point);
                    previousDate = result[date].Balance;
                    _repository.SaveOrUpdate(point);
                }
            }
            return result;
        }
        public void makeTransfer_Ok()
        {
            //arrange
            IOperationRepository operationRepository = MockRepository.GenerateStub<IOperationRepository>();
            IRepository repository = MockRepository.GenerateMock<IRepository>();
            IDtoCreator<Operation, OperationDto> operationCreator = MockRepository.GenerateStub<IDtoCreator<Operation, OperationDto>>();

            Account debitAccount = new Account() { Id = 1, Balance = 200 };
            Account creditAccount = new Account() { Id = 2, Balance = 300 };

            repository.Expect(x => x.Get<Account>(debitAccount.Id)).Return(debitAccount);
            repository.Expect(x => x.Get<Account>(creditAccount.Id)).Return(creditAccount);
            repository.Expect(x => x.Save(debitAccount));
            repository.Expect(x => x.Save(creditAccount));
            repository.Expect(x => x.Save(new Operation())).IgnoreArguments();
            repository.Expect(x => x.Save(new Operation())).IgnoreArguments();

            //act
            OperationServices services = new OperationServices(operationRepository, repository, operationCreator);
            services.MakeTransfer(debitAccount.Id, creditAccount.Id, 200, "transfer test");

            //assert
            Assert.AreEqual(0, debitAccount.Balance);
            Assert.AreEqual(500, creditAccount.Balance);

            Assert.AreEqual(1, debitAccount.Operations.Count);

            Operation debitOperation = debitAccount.Operations[0];
            Assert.AreEqual(debitAccount.Id, debitOperation.Account.Id);
            Assert.AreEqual(200, debitOperation.Amount);
            Assert.AreEqual(Direction.Debit, debitOperation.Direction);
            Assert.AreEqual("transfer test", debitOperation.Motif);

            Operation creditOperation = creditAccount.Operations[0];
            Assert.AreEqual(creditAccount.Id, creditOperation.Account.Id);
            Assert.AreEqual(200, creditOperation.Amount);
            Assert.AreEqual(Direction.Credit, creditOperation.Direction);
            Assert.AreEqual("transfer test", creditOperation.Motif);

            repository.VerifyAllExpectations();
        }