528 lines
15 KiB
Go
528 lines
15 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"math"
|
|
"spend-sparrow/internal/db"
|
|
"spend-sparrow/internal/types"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jmoiron/sqlx"
|
|
)
|
|
|
|
type TransactionRecurring interface {
|
|
Add(ctx context.Context, user *types.User, transactionRecurring types.TransactionRecurringInput) (*types.TransactionRecurring, error)
|
|
Update(ctx context.Context, user *types.User, transactionRecurring types.TransactionRecurringInput) (*types.TransactionRecurring, error)
|
|
GetAll(ctx context.Context, user *types.User) ([]*types.TransactionRecurring, error)
|
|
GetAllByAccount(ctx context.Context, user *types.User, accountId string) ([]*types.TransactionRecurring, error)
|
|
GetAllByTreasureChest(ctx context.Context, user *types.User, treasureChestId string) ([]*types.TransactionRecurring, error)
|
|
Delete(ctx context.Context, user *types.User, id string) error
|
|
|
|
GenerateTransactions(ctx context.Context, user *types.User) error
|
|
}
|
|
|
|
type TransactionRecurringImpl struct {
|
|
db *sqlx.DB
|
|
clock Clock
|
|
random Random
|
|
transaction Transaction
|
|
}
|
|
|
|
func NewTransactionRecurring(db *sqlx.DB, random Random, clock Clock, transaction Transaction) TransactionRecurring {
|
|
return TransactionRecurringImpl{
|
|
db: db,
|
|
clock: clock,
|
|
random: random,
|
|
transaction: transaction,
|
|
}
|
|
}
|
|
|
|
func (s TransactionRecurringImpl) Add(ctx context.Context,
|
|
user *types.User,
|
|
transactionRecurringInput types.TransactionRecurringInput,
|
|
) (*types.TransactionRecurring, error) {
|
|
if user == nil {
|
|
return nil, ErrUnauthorized
|
|
}
|
|
|
|
tx, err := s.db.BeginTxx(ctx, nil)
|
|
err = db.TransformAndLogDbError("transactionRecurring Add", nil, err)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() {
|
|
_ = tx.Rollback()
|
|
}()
|
|
|
|
transactionRecurring, err := s.validateAndEnrichTransactionRecurring(ctx, tx, nil, user.Id, transactionRecurringInput)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
r, err := tx.NamedExecContext(ctx, `
|
|
INSERT INTO "transaction_recurring" (id, user_id, interval_months,
|
|
next_execution, party, description, account_id, treasure_chest_id, value, created_at, created_by)
|
|
VALUES (:id, :user_id, :interval_months,
|
|
:next_execution, :party, :description, :account_id, :treasure_chest_id, :value, :created_at, :created_by)`,
|
|
transactionRecurring)
|
|
err = db.TransformAndLogDbError("transactionRecurring Insert", r, err)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = tx.Commit()
|
|
err = db.TransformAndLogDbError("transactionRecurring Add", nil, err)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return transactionRecurring, nil
|
|
}
|
|
|
|
func (s TransactionRecurringImpl) Update(ctx context.Context,
|
|
user *types.User,
|
|
input types.TransactionRecurringInput,
|
|
) (*types.TransactionRecurring, error) {
|
|
if user == nil {
|
|
return nil, ErrUnauthorized
|
|
}
|
|
uuid, err := uuid.Parse(input.Id)
|
|
if err != nil {
|
|
slog.Error("transactionRecurring update", "err", err)
|
|
return nil, fmt.Errorf("could not parse Id: %w", ErrBadRequest)
|
|
}
|
|
|
|
tx, err := s.db.BeginTxx(ctx, nil)
|
|
err = db.TransformAndLogDbError("transactionRecurring Update", nil, err)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() {
|
|
_ = tx.Rollback()
|
|
}()
|
|
|
|
transactionRecurring := &types.TransactionRecurring{}
|
|
err = tx.GetContext(ctx, transactionRecurring, `SELECT * FROM transaction_recurring WHERE user_id = ? AND id = ?`, user.Id, uuid)
|
|
err = db.TransformAndLogDbError("transactionRecurring Update", nil, err)
|
|
if err != nil {
|
|
if errors.Is(err, db.ErrNotFound) {
|
|
return nil, fmt.Errorf("transactionRecurring %v not found: %w", input.Id, ErrBadRequest)
|
|
}
|
|
return nil, types.ErrInternal
|
|
}
|
|
|
|
transactionRecurring, err = s.validateAndEnrichTransactionRecurring(ctx, tx, transactionRecurring, user.Id, input)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
r, err := tx.NamedExecContext(ctx, `
|
|
UPDATE transaction_recurring
|
|
SET
|
|
interval_months = :interval_months,
|
|
next_execution = :next_execution,
|
|
party = :party,
|
|
description = :description,
|
|
account_id = :account_id,
|
|
treasure_chest_id = :treasure_chest_id,
|
|
value = :value,
|
|
updated_at = :updated_at,
|
|
updated_by = :updated_by
|
|
WHERE id = :id
|
|
AND user_id = :user_id`, transactionRecurring)
|
|
err = db.TransformAndLogDbError("transactionRecurring Update", r, err)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = tx.Commit()
|
|
err = db.TransformAndLogDbError("transactionRecurring Update", nil, err)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return transactionRecurring, nil
|
|
}
|
|
|
|
func (s TransactionRecurringImpl) GetAll(ctx context.Context, user *types.User) ([]*types.TransactionRecurring, error) {
|
|
if user == nil {
|
|
return nil, ErrUnauthorized
|
|
}
|
|
|
|
transactionRecurrings := make([]*types.TransactionRecurring, 0)
|
|
err := s.db.SelectContext(ctx, &transactionRecurrings, `
|
|
SELECT *
|
|
FROM transaction_recurring
|
|
WHERE user_id = ?
|
|
ORDER BY created_at DESC`,
|
|
user.Id)
|
|
err = db.TransformAndLogDbError("transactionRecurring GetAll", nil, err)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return transactionRecurrings, nil
|
|
}
|
|
|
|
func (s TransactionRecurringImpl) GetAllByAccount(ctx context.Context, user *types.User, accountId string) ([]*types.TransactionRecurring, error) {
|
|
if user == nil {
|
|
return nil, ErrUnauthorized
|
|
}
|
|
|
|
accountUuid, err := uuid.Parse(accountId)
|
|
if err != nil {
|
|
slog.Error("transactionRecurring GetAllByAccount", "err", err)
|
|
return nil, fmt.Errorf("could not parse accountId: %w", ErrBadRequest)
|
|
}
|
|
|
|
tx, err := s.db.BeginTxx(ctx, nil)
|
|
err = db.TransformAndLogDbError("transactionRecurring GetAllByAccount", nil, err)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() {
|
|
_ = tx.Rollback()
|
|
}()
|
|
|
|
var rowCount int
|
|
err = tx.GetContext(ctx, &rowCount, `SELECT COUNT(*) FROM account WHERE id = ? AND user_id = ?`, accountUuid, user.Id)
|
|
err = db.TransformAndLogDbError("transactionRecurring GetAllByAccount", nil, err)
|
|
if err != nil {
|
|
if errors.Is(err, db.ErrNotFound) {
|
|
return nil, fmt.Errorf("account %v not found: %w", accountId, ErrBadRequest)
|
|
}
|
|
return nil, types.ErrInternal
|
|
}
|
|
|
|
transactionRecurrings := make([]*types.TransactionRecurring, 0)
|
|
err = tx.SelectContext(ctx, &transactionRecurrings, `
|
|
SELECT *
|
|
FROM transaction_recurring
|
|
WHERE user_id = ?
|
|
AND account_id = ?
|
|
ORDER BY created_at DESC`,
|
|
user.Id, accountUuid)
|
|
err = db.TransformAndLogDbError("transactionRecurring GetAll", nil, err)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = tx.Commit()
|
|
err = db.TransformAndLogDbError("transactionRecurring GetAllByAccount", nil, err)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return transactionRecurrings, nil
|
|
}
|
|
|
|
func (s TransactionRecurringImpl) GetAllByTreasureChest(ctx context.Context,
|
|
user *types.User,
|
|
treasureChestId string,
|
|
) ([]*types.TransactionRecurring, error) {
|
|
if user == nil {
|
|
return nil, ErrUnauthorized
|
|
}
|
|
|
|
treasureChestUuid, err := uuid.Parse(treasureChestId)
|
|
if err != nil {
|
|
slog.Error("transactionRecurring GetAllByTreasureChest", "err", err)
|
|
return nil, fmt.Errorf("could not parse treasureChestId: %w", ErrBadRequest)
|
|
}
|
|
|
|
tx, err := s.db.BeginTxx(ctx, nil)
|
|
err = db.TransformAndLogDbError("transactionRecurring GetAllByTreasureChest", nil, err)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() {
|
|
_ = tx.Rollback()
|
|
}()
|
|
|
|
var rowCount int
|
|
err = tx.GetContext(ctx, &rowCount, `SELECT COUNT(*) FROM treasure_chest WHERE id = ? AND user_id = ?`, treasureChestId, user.Id)
|
|
err = db.TransformAndLogDbError("transactionRecurring GetAllByTreasureChest", nil, err)
|
|
if err != nil {
|
|
if errors.Is(err, db.ErrNotFound) {
|
|
return nil, fmt.Errorf("treasurechest %v not found: %w", treasureChestId, ErrBadRequest)
|
|
}
|
|
return nil, types.ErrInternal
|
|
}
|
|
|
|
transactionRecurrings := make([]*types.TransactionRecurring, 0)
|
|
err = tx.SelectContext(ctx, &transactionRecurrings, `
|
|
SELECT *
|
|
FROM transaction_recurring
|
|
WHERE user_id = ?
|
|
AND treasure_chest_id = ?
|
|
ORDER BY created_at DESC`,
|
|
user.Id, treasureChestUuid)
|
|
err = db.TransformAndLogDbError("transactionRecurring GetAll", nil, err)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = tx.Commit()
|
|
err = db.TransformAndLogDbError("transactionRecurring GetAllByTreasureChest", nil, err)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return transactionRecurrings, nil
|
|
}
|
|
|
|
func (s TransactionRecurringImpl) Delete(ctx context.Context, user *types.User, id string) error {
|
|
if user == nil {
|
|
return ErrUnauthorized
|
|
}
|
|
uuid, err := uuid.Parse(id)
|
|
if err != nil {
|
|
slog.Error("transactionRecurring delete", "err", err)
|
|
return fmt.Errorf("could not parse Id: %w", ErrBadRequest)
|
|
}
|
|
|
|
tx, err := s.db.BeginTxx(ctx, nil)
|
|
err = db.TransformAndLogDbError("transactionRecurring Delete", nil, err)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
defer func() {
|
|
_ = tx.Rollback()
|
|
}()
|
|
|
|
var transactionRecurring types.TransactionRecurring
|
|
err = tx.GetContext(ctx, &transactionRecurring, `SELECT * FROM transaction_recurring WHERE user_id = ? AND id = ?`, user.Id, uuid)
|
|
err = db.TransformAndLogDbError("transactionRecurring Delete", nil, err)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
r, err := tx.ExecContext(ctx, "DELETE FROM transaction_recurring WHERE id = ? AND user_id = ?", uuid, user.Id)
|
|
err = db.TransformAndLogDbError("transactionRecurring Delete", r, err)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = tx.Commit()
|
|
err = db.TransformAndLogDbError("transactionRecurring Delete", nil, err)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s TransactionRecurringImpl) GenerateTransactions(ctx context.Context, user *types.User) error {
|
|
if user == nil {
|
|
return ErrUnauthorized
|
|
}
|
|
now := s.clock.Now()
|
|
|
|
tx, err := s.db.BeginTxx(ctx, nil)
|
|
err = db.TransformAndLogDbError("transactionRecurring GenerateTransactions", nil, err)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() {
|
|
_ = tx.Rollback()
|
|
}()
|
|
|
|
recurringTransactions := make([]*types.TransactionRecurring, 0)
|
|
err = tx.SelectContext(ctx, &recurringTransactions, `
|
|
SELECT * FROM transaction_recurring WHERE user_id = ? AND next_execution <= ?`,
|
|
user.Id, now)
|
|
err = db.TransformAndLogDbError("transactionRecurring GenerateTransactions", nil, err)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, transactionRecurring := range recurringTransactions {
|
|
transaction := types.Transaction{
|
|
Timestamp: *transactionRecurring.NextExecution,
|
|
Party: transactionRecurring.Party,
|
|
Description: transactionRecurring.Description,
|
|
|
|
TreasureChestId: transactionRecurring.TreasureChestId,
|
|
Value: transactionRecurring.Value,
|
|
}
|
|
|
|
_, err = s.transaction.Add(ctx, tx, user, transaction)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
nextExecution := transactionRecurring.NextExecution.AddDate(0, int(transactionRecurring.IntervalMonths), 0)
|
|
r, err := tx.ExecContext(ctx, `UPDATE transaction_recurring SET next_execution = ? WHERE id = ? AND user_id = ?`,
|
|
nextExecution, transactionRecurring.Id, user.Id)
|
|
err = db.TransformAndLogDbError("transactionRecurring GenerateTransactions", r, err)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
err = tx.Commit()
|
|
err = db.TransformAndLogDbError("transactionRecurring GenerateTransactions", nil, err)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s TransactionRecurringImpl) validateAndEnrichTransactionRecurring(
|
|
ctx context.Context,
|
|
tx *sqlx.Tx,
|
|
oldTransactionRecurring *types.TransactionRecurring,
|
|
userId uuid.UUID,
|
|
input types.TransactionRecurringInput,
|
|
) (*types.TransactionRecurring, error) {
|
|
var (
|
|
id uuid.UUID
|
|
accountUuid *uuid.UUID
|
|
treasureChestUuid *uuid.UUID
|
|
createdAt time.Time
|
|
createdBy uuid.UUID
|
|
updatedAt *time.Time
|
|
updatedBy uuid.UUID
|
|
intervalMonths int64
|
|
|
|
err error
|
|
rowCount int
|
|
)
|
|
|
|
if oldTransactionRecurring == nil {
|
|
id, err = s.random.UUID()
|
|
if err != nil {
|
|
return nil, types.ErrInternal
|
|
}
|
|
createdAt = s.clock.Now()
|
|
createdBy = userId
|
|
} else {
|
|
id = oldTransactionRecurring.Id
|
|
createdAt = oldTransactionRecurring.CreatedAt
|
|
createdBy = oldTransactionRecurring.CreatedBy
|
|
time := s.clock.Now()
|
|
updatedAt = &time
|
|
updatedBy = userId
|
|
}
|
|
|
|
hasAccount := false
|
|
hasTreasureChest := false
|
|
if input.AccountId != "" {
|
|
temp, err := uuid.Parse(input.AccountId)
|
|
if err != nil {
|
|
slog.Error("transactionRecurring validate", "err", err)
|
|
return nil, fmt.Errorf("could not parse accountId: %w", ErrBadRequest)
|
|
}
|
|
accountUuid = &temp
|
|
err = tx.GetContext(ctx, &rowCount, `SELECT COUNT(*) FROM account WHERE id = ? AND user_id = ?`, accountUuid, userId)
|
|
err = db.TransformAndLogDbError("transactionRecurring validate", nil, err)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if rowCount == 0 {
|
|
slog.Error("transactionRecurring validate", "err", err)
|
|
return nil, fmt.Errorf("account not found: %w", ErrBadRequest)
|
|
}
|
|
|
|
hasAccount = true
|
|
}
|
|
|
|
if input.TreasureChestId != "" {
|
|
temp, err := uuid.Parse(input.TreasureChestId)
|
|
if err != nil {
|
|
slog.Error("transactionRecurring validate", "err", err)
|
|
return nil, fmt.Errorf("could not parse treasureChestId: %w", ErrBadRequest)
|
|
}
|
|
treasureChestUuid = &temp
|
|
var treasureChest types.TreasureChest
|
|
err = tx.GetContext(ctx, &treasureChest, `SELECT * FROM treasure_chest WHERE id = ? AND user_id = ?`, treasureChestUuid, userId)
|
|
err = db.TransformAndLogDbError("transactionRecurring validate", nil, err)
|
|
if err != nil {
|
|
if errors.Is(err, db.ErrNotFound) {
|
|
return nil, fmt.Errorf("treasure chest not found: %w", ErrBadRequest)
|
|
}
|
|
return nil, err
|
|
}
|
|
if treasureChest.ParentId == nil {
|
|
return nil, fmt.Errorf("treasure chest is a group: %w", ErrBadRequest)
|
|
}
|
|
hasTreasureChest = true
|
|
}
|
|
|
|
if !hasAccount && !hasTreasureChest {
|
|
slog.Error("transactionRecurring validate", "err", err)
|
|
return nil, fmt.Errorf("either account or treasure chest is required: %w", ErrBadRequest)
|
|
}
|
|
if hasAccount && hasTreasureChest {
|
|
slog.Error("transactionRecurring validate", "err", err)
|
|
return nil, fmt.Errorf("either account or treasure chest is required, not both: %w", ErrBadRequest)
|
|
}
|
|
|
|
valueFloat, err := strconv.ParseFloat(input.Value, 64)
|
|
if err != nil {
|
|
slog.Error("transactionRecurring validate", "err", err)
|
|
return nil, fmt.Errorf("could not parse value: %w", ErrBadRequest)
|
|
}
|
|
value := int64(math.Round(valueFloat * DECIMALS_MULTIPLIER))
|
|
|
|
if input.Party != "" {
|
|
err = validateString(input.Party, "party")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if input.Description != "" {
|
|
err = validateString(input.Description, "description")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
intervalMonths, err = strconv.ParseInt(input.IntervalMonths, 10, 0)
|
|
if err != nil {
|
|
slog.Error("transactionRecurring validate", "err", err)
|
|
return nil, fmt.Errorf("could not parse intervalMonths: %w", ErrBadRequest)
|
|
}
|
|
if intervalMonths < 1 {
|
|
slog.Error("transactionRecurring validate", "err", err)
|
|
return nil, fmt.Errorf("intervalMonths needs to be greater than 0: %w", ErrBadRequest)
|
|
}
|
|
var nextExecution *time.Time = nil
|
|
if input.NextExecution != "" {
|
|
t, err := time.Parse("2006-01-02", input.NextExecution)
|
|
if err != nil {
|
|
slog.Error("transaction validate", "err", err)
|
|
return nil, fmt.Errorf("could not parse timestamp: %w", ErrBadRequest)
|
|
}
|
|
|
|
t = time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())
|
|
nextExecution = &t
|
|
}
|
|
|
|
transactionRecurring := types.TransactionRecurring{
|
|
Id: id,
|
|
UserId: userId,
|
|
|
|
IntervalMonths: intervalMonths,
|
|
NextExecution: nextExecution,
|
|
|
|
Party: input.Party,
|
|
Description: input.Description,
|
|
|
|
AccountId: accountUuid,
|
|
TreasureChestId: treasureChestUuid,
|
|
Value: value,
|
|
|
|
CreatedAt: createdAt,
|
|
CreatedBy: createdBy,
|
|
UpdatedAt: updatedAt,
|
|
UpdatedBy: &updatedBy,
|
|
}
|
|
|
|
return &transactionRecurring, nil
|
|
}
|