service

package
v0.0.0-...-d62e8b7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2024 License: GPL-3.0 Imports: 15 Imported by: 1

Documentation

Index

Constants

View Source
const (
	IDIndex      = 0
	IDHeader     = "Id"
	DateIndex    = 1
	DateHeader   = "Date"
	AmountIndex  = 2
	AmountHeader = "Transaction"
)

Variables

View Source
var ErrApplyingTransactions = errors.New("error applying transactions")
View Source
var ErrParsingAmount = errors.New("can't convert transaction amount to decimal")
View Source
var ErrParsingDate = errors.New("can't convert transaction date")
View Source
var ErrParsingID = errors.New("can't convert transaction id")
View Source
var ErrReadingTransactions = errors.New("error parsing transactions csv")

Functions

This section is empty.

Types

type EmailService

type EmailService struct {
	EmailSender adapters.EmailSender
	Template    *template.Template
}

func (EmailService) Send

func (emailService EmailService) Send(
	user *models.User,
	transactionsBalance decimal.Decimal,
	transactionsPerMonth []TransactionsPerMonth,
	avgDebit, avgCredit decimal.Decimal,
) error

send formats the data received by parameters into the Stori mail and sends it by email

type IEmailService

type IEmailService interface {
	// Send formats the data received by parameters into the Stori mail and sends it by email
	Send(
		user *models.User,
		transactionBalance decimal.Decimal,
		transactionsPerMonth []TransactionsPerMonth,
		avgDebit, avgCredit decimal.Decimal,
	) error
}

type ITransactionService

type ITransactionService interface {
	// Apply creates the transactions received by parameter and applies its total balance to the user's balance
	Apply(email string, transactions []models.Transaction, transactionsBalance decimal.Decimal) (*models.User, error)
}

type Service

type Service struct {
	TransactionsReader TransactionsReader
	EmailService       IEmailService
	TransactionService ITransactionService
}

func (Service) CalculateAverageDebitAndCredit

func (service Service) CalculateAverageDebitAndCredit(transactions []models.Transaction) (decimal.Decimal, decimal.Decimal)

CalculateAverageDebitAndCredit calculates the average debit and average credit from a list of transactions

func (Service) CalculateTotalBalance

func (service Service) CalculateTotalBalance(transactions []models.Transaction) decimal.Decimal

CalculateTotalBalance calculates the total balance from a list of transactions as the sum of all transactions

func (Service) CalculateTransactionsPerMonth

func (service Service) CalculateTransactionsPerMonth(transactions []models.Transaction) []TransactionsPerMonth

CalculateTransactionsPerMonth calculates the amount of transactions for each month present in the list of transactions. The returned list is in ascending order by month.

func (Service) Process

func (service Service) Process(csvFileName, destinationEmail string) error

Process read csv file called csvFileName, calculates total balance, number of transactions grouped by month and average credit and debit and send this information by email to destinationEmail

type TransactionService

type TransactionService struct {
	DB                    *gorm.DB
	UserRepository        repository.IUserRepository
	TransactionRepository repository.ITransactionRepository
}

func (TransactionService) Apply

func (service TransactionService) Apply(
	email string,
	transactions []models.Transaction,
	transactionsBalance decimal.Decimal,
) (*models.User, error)

Apply creates the transactions received by parameter and applies its total balance to the user's balance

type TransactionsPerMonth

type TransactionsPerMonth struct {
	Month  time.Time
	Amount int
}

type TransactionsReader

type TransactionsReader struct {
	LocalCSVReader adapters.CSVReader
	S3CSVReader    adapters.CSVReader
}

func (TransactionsReader) Read

func (reader TransactionsReader) Read(csvFileName string) ([]models.Transaction, error)

Read reads a CSV file that contains a list of transactions.

If the csvFileName starts with 's3://', it reads the csv from s3. Otherwise, it reads it from local folder.

Returns the list of transactions of the CSV file or ErrReadingTransactions if an error is produced

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL