Esempio n. 1
0
File: adjust.go Progetto: sporto/kic
// 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
}
Esempio n. 2
0
File: create.go Progetto: sporto/kic
func (serv *CreateServ) Run(dbSession *r.Session, accountIn models.Account) (accountOut models.Account, err error) {

	if accountIn.Id != "" {
		err = errors.New("Account already has an id")
		return
	}

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

	response, err := r.Table("accounts").Insert(accountIn).RunWrite(dbSession)
	if err != nil {
		return
	}

	id := response.GeneratedKeys[0]

	accountOut, err = new(GetServ).Run(dbSession, id)

	return
}
Esempio n. 3
0
File: update.go Progetto: sporto/kic
func (serv *UpdateServ) Run(dbSession *r.Session, accountIn models.Account) (accountOut models.Account, err error) {

	if accountIn.Id == "" {
		err = errors.New("Invalid id")
		return
	}

	accountIn.UpdatedAt = time.Now()

	_, err = r.Table("accounts").Get(accountIn.Id).Update(accountIn).RunRow(dbSession)
	if err != nil {
		return
	}

	accountOut, err = new(GetServ).Run(dbSession, accountIn.Id)

	return
}
Esempio n. 4
0
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/sporto/kic/api/models"
	"github.com/sporto/kic/api/services/accounts"
	"github.com/sporto/kic/api/services/transactions"
	"time"
	// "os"
	"log"
)

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

	var (
		createAccountServ accounts.CreateServ
		updateAccountServ accounts.UpdateServ
		service           transactions.CreateServ
		account           models.Account
		transaction       *models.Transaction
		err               error
	)

	BeforeEach(func() {
		// log.Println("---- BeforeEach -------")
		// log.Println("dbSession", dbSession)
		accountIn := *&models.Account{CurrentBalance: 100, LastInterestPaid: time.Now()}
		account, err = createAccountServ.Run(dbSession, accountIn)
		// log.Println("Account created ", account)
		if err != nil {
			log.Println("Account not created because ", err)
		}
		transaction = &models.Transaction{AccountId: account.Id, Debit: 50, Credit: 100}
	})
Esempio n. 5
0
	"fmt"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/sporto/kic/api/lib/matchers"
	"github.com/sporto/kic/api/models"
	"github.com/sporto/kic/api/services/account_balances"
	"github.com/sporto/kic/api/services/accounts"
	"time"
)

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

	var (
		service     account_balances.AdjustServ
		createServ  accounts.CreateServ
		getRateServ accounts.GetInterestRateServ
		account     models.Account
		err         error
		rate        float64
	)

	BeforeEach(func() {
		// create an empty accountIn for testing
		accountIn := &models.Account{
			CurrentBalance:   100,
			LastInterestPaid: time.Now().AddDate(-1, 0, 0),
		}
		// create the account and get a ref
		account, err = createServ.Run(dbSession, *accountIn)
		if err != nil {
			fmt.Println(err)
		}
Esempio n. 6
0
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("UpdateServ", func() {

	var (
		service    accounts.UpdateServ
		createServ accounts.CreateServ
		account    models.Account
		err        error
	)

	BeforeEach(func() {
		// create an account for testing
		accountIn := &models.Account{}
		account, err = createServ.Run(dbSession, *accountIn)

		if err != nil {
			fmt.Println(err)
		}
		// fmt.Println("Account saved", accountIn.Id)
	})

	It("Saved the account", func() {
Esempio n. 7
0
	. "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())
	})

	It("Gets the account", func() {
		account, err := service.Run(dbSession, accountId)
		Expect(err).To(BeNil())