示例#1
0
文件: create_test.go 项目: sporto/kic
	///////////////////////////////////////////////////////////

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

	It("saves the transaction", func() {
		transaction, err := service.Run(dbSession, *transaction)
		Expect(err).To(BeNil())
		Expect(transaction.Id).NotTo(BeEmpty())
	})

	It("updates the account balance", func() {
		_, err := service.Run(dbSession, *transaction)
		Expect(err).To(BeNil())
		getAccountServ := new(accounts.GetServ)
		account, err = getAccountServ.Run(dbSession, account.Id)
		Expect(account.CurrentBalance).To(Equal(150.0))
	})

	It("has the current balance", func() {
		transaction, _ := service.Run(dbSession, *transaction)
		Expect(transaction.Balance).To(Equal(150.0))
	})

	It("fails when no account id provided", func() {
		transaction.AccountId = ""
		_, err := service.Run(dbSession, *transaction)
		Expect(err).NotTo(BeNil())
	})
示例#2
0
文件: create.go 项目: sporto/kic
func (serv *CreateServ) Run(dbSession *r.Session, transactionIn models.Transaction) (transactionOut models.Transaction, err error) {

	// fail if transaction is already saved
	if transactionIn.Id != "" {
		err = errors.New("Transaction Id must be nil")
		return
	}

	// fail if not account id provided
	if transactionIn.AccountId == "" {
		err = errors.New("Account Id cannot be nil")
		return
	}

	// fail if no credit or debit provided
	if transactionIn.Credit <= 0 && transactionIn.Debit <= 0 {
		err = errors.New("Credit or Debit must be provided")
		return
	}

	// check that the account exist
	getServ := new(accounts.GetServ)
	account, err := getServ.Run(dbSession, transactionIn.AccountId)
	if err != nil {
		return
	}

	// check that the transaction is valid e.g. enough balance
	if transactionIn.Debit > account.CurrentBalance {
		err = errors.New("Not enough balance")
		return
	}

	// check that interest has been p   aid
	// unless kind is interest
	if transactionIn.Kind != KindInterest {

		dur := time.Now().Sub(account.LastInterestPaid)
		if dur.Hours() > 24 {
			err = errors.New("Interest not updated")
			return
		}
	}

	transactionIn.CreatedAt = time.Now()
	transactionIn.UpdatedAt = time.Now()

	// update the current account balance
	account.CurrentBalance += transactionIn.Credit
	account.CurrentBalance -= transactionIn.Debit

	// also update the balance in the transaction
	transactionIn.Balance = account.CurrentBalance

	// save the transaction
	response, err := r.Table("transactions").Insert(transactionIn).RunWrite(dbSession)
	if err != nil {
		return
	}

	id := response.GeneratedKeys[0]

	// get the transaction out
	transactionOut, err = new(GetServ).Run(dbSession, id)
	if err != nil {
		return
	}

	updateAccountServ := new(accounts.UpdateServ)
	_, err = updateAccountServ.Run(dbSession, account)
	if err != nil {
		return
	}

	return
}
示例#3
0
文件: get_test.go 项目: sporto/kic
package accounts_test

import (
	"fmt"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/sporto/kic/api/models"
	"github.com/sporto/kic/api/services/accounts"
)

var _ = Describe("GetServ", func() {

	var (
		service           accounts.GetServ
		accountId         string
		createAccountServ accounts.CreateServ
	)

	BeforeEach(func() {
		accountIn := new(models.Account)
		accountIn.Name = "X"
		accountOut, err := createAccountServ.Run(dbSession, *accountIn)
		if err != nil {
			fmt.Println("Account not created")
		}
		accountId = accountOut.Id
	})

	It("Saved the account", func() {
		Expect(accountId).NotTo(BeEmpty())
	})