Added analytics by transactions

This commit is contained in:
2026-04-11 11:37:48 +03:00
parent b66be96033
commit 8e074db55f
8 changed files with 346 additions and 1 deletions
+14
View File
@@ -13,6 +13,7 @@ type TransactionService interface {
Create(ctx context.Context, req domain.CreateTransactionRequest) (*domain.Transaction, error)
GetByID(ctx context.Context, id int64) (*domain.Transaction, error)
List(ctx context.Context, filter domain.TransactionListFilter) ([]*domain.Transaction, error)
Analytics(ctx context.Context, filter domain.TransactionAnalyticsFilter) (domain.TransactionAnalytics, error)
Update(ctx context.Context, id int64, req domain.UpdateTransactionRequest) (*domain.Transaction, error)
Delete(ctx context.Context, id int64) error
}
@@ -31,6 +32,7 @@ var (
ErrReceiptLinkConflict = errors.New("receipt_id and detach_receipt cannot be used together")
ErrInvalidTransaction = errors.New("type and category are required")
ErrReceiptNotFound = errors.New("receipt not found")
ErrInvalidAnalytics = errors.New("type must be income or expense")
)
func (s *transactionService) Create(ctx context.Context, req domain.CreateTransactionRequest) (*domain.Transaction, error) {
@@ -85,6 +87,18 @@ func (s *transactionService) List(ctx context.Context, filter domain.Transaction
return s.repo.List(ctx, filter)
}
func (s *transactionService) Analytics(ctx context.Context, filter domain.TransactionAnalyticsFilter) (domain.TransactionAnalytics, error) {
if filter.Type != nil {
typeValue := strings.TrimSpace(*filter.Type)
if typeValue != "income" && typeValue != "expense" {
return domain.TransactionAnalytics{}, ErrInvalidAnalytics
}
filter.Type = &typeValue
}
return s.repo.Analytics(ctx, filter)
}
func (s *transactionService) Update(ctx context.Context, id int64, req domain.UpdateTransactionRequest) (*domain.Transaction, error) {
if req.ReceiptID != nil && req.DetachReceipt {
return nil, ErrReceiptLinkConflict