Beispiel #1
0
// Updates the balance
// And saves the account
func (serv *AdjustServ) Run(dbSession *r.Session, accountIn models.Account) (accountOut models.Account, transactionOut models.Transaction, err error) {

	interest, err := new(accounts.CalculateInterestToPayServ).Run(accountIn)
	if err != nil {
		log.Println(err)
		return
	}

	if interest > 0 {

		// create a transaction
		transaction := &models.Transaction{
			AccountId: accountIn.Id,
			Credit:    interest,
			Kind:      "interest",
		}
		transaction.Credit = interest
		transaction.Kind = "interest"

		createTransactionServ := &transactions.CreateServ{}
		transactionOut, err = createTransactionServ.Run(dbSession, *transaction)
		if err != nil {
			log.Println(err)
			return
		}

		accountIn.CurrentBalance += interest
		accountIn.LastInterestPaid = time.Now()

		updateAccountServ := &accounts.UpdateServ{}
		accountOut, err = updateAccountServ.Run(dbSession, accountIn)
		if err != nil {
			log.Println(err)
			return
		}
	} else {
		accountOut = accountIn
	}

	return
}
Beispiel #2
0
		Expect(err).NotTo(BeNil())
	})

	It("fails if the transaction is already saved", func() {
		transaction.Id = "aaaa"
		_, err := service.Run(dbSession, *transaction)
		Expect(err).NotTo(BeNil())
	})

	It("fails if no credit or debit provided", func() {
		transaction.Credit = 0
		transaction.Debit = 0
		_, err := service.Run(dbSession, *transaction)
		Expect(err).NotTo(BeNil())
	})

	It("fails if the account doesn't have enough balance", func() {
		transaction.Debit = 150
		_, err := service.Run(dbSession, *transaction)
		Expect(err).NotTo(BeNil())
	})

	It("fails if the account interests has not been updated", func() {
		account.LastInterestPaid = time.Now().AddDate(0, 0, -2) // two days
		_, err := updateAccountServ.Run(dbSession, account)
		Expect(err).To(BeNil())
		_, err = service.Run(dbSession, *transaction)
		Expect(err).NotTo(BeNil())
	})
})
Beispiel #3
0
		}
		rate, err = getRateServ.Run(account)
	})

	It("saved the test account", func() {
		Expect(account.Id).NotTo(BeEmpty())
	})

	It("returns the account", func() {
		accountOut, _, err := service.Run(dbSession, account)
		Expect(err).To(BeNil())
		Expect(accountOut.Id).NotTo(BeEmpty())
	})

	It("does nothing if duration is less than one day", func() {
		account.LastInterestPaid = time.Now().Add(-time.Duration(time.Hour * 10))
		accountOut, transaction, _ := service.Run(dbSession, account)
		Expect(accountOut.CurrentBalance).To(Equal(100.0))
		Expect(transaction.Id).To(BeEmpty())
	})

	It("updates the current balance", func() {
		accountOut, _, _ := service.Run(dbSession, account)
		Expect(accountOut.CurrentBalance).To(Equal(100 + rate))
	})

	It("updates the last interest paid", func() {
		accountOut, _, _ := service.Run(dbSession, account)
		Expect(accountOut.LastInterestPaid).To(matchers.BeWithin(time.Now()))
	})