This commit is contained in:
@@ -2,6 +2,8 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"spend-sparrow/internal/db"
|
||||
"spend-sparrow/internal/types"
|
||||
"time"
|
||||
|
||||
@@ -12,50 +14,72 @@ type Dashboard struct {
|
||||
db *sqlx.DB
|
||||
}
|
||||
|
||||
func NewDashboard(db *sqlx.DB) TreasureChest {
|
||||
return TreasureChestImpl{
|
||||
func NewDashboard(db *sqlx.DB) *Dashboard {
|
||||
return &Dashboard{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (s Dashboard) Summary(ctx context.Context, user *types.User, month time.Time) (*types.DashboardSummary, error) {
|
||||
func (s Dashboard) Summary(ctx context.Context, user *types.User, month time.Time) (*types.DashboardMonthlySummary, error) {
|
||||
if user == nil {
|
||||
return nil, ErrUnauthorized
|
||||
}
|
||||
|
||||
var summary types.DashboardSummary
|
||||
var summary types.DashboardMonthlySummary
|
||||
|
||||
s.db.SelectContext(ctx, &summary.Expenses, `
|
||||
SELECT
|
||||
var value *int64
|
||||
err := s.db.GetContext(ctx, &value, `
|
||||
SELECT SUM(value)
|
||||
FROM "transaction"
|
||||
WHERE user_id = $1
|
||||
AND value < 0
|
||||
AND account_id IS NOT NULL
|
||||
AND error IS NULL
|
||||
AND date_trunc('month', date) = date_trund('month', $2)`,
|
||||
user.Id, month)
|
||||
|
||||
s.db.SelectContext(ctx, &summary.Income, `
|
||||
SELECT
|
||||
FROM "transaction"
|
||||
WHERE user_id = $1
|
||||
AND value > 0
|
||||
AND account_id IS NOT NULL
|
||||
AND treasure_chest_id IS NULL
|
||||
AND error IS NULL
|
||||
AND date_trunc('month', date) = date_trund('month', $2)`,
|
||||
user.Id, month)
|
||||
|
||||
s.db.SelectContext(ctx, &summary.Savings, `
|
||||
SELECT
|
||||
FROM "transaction"
|
||||
WHERE user_id = $1
|
||||
AND value > 0
|
||||
AND treasure_chest_id IS NOT NULL
|
||||
AND account_id IS NULL
|
||||
AND error IS NULL
|
||||
AND date_trunc('month', date) = date_trund('month', $2)`,
|
||||
AND date(timestamp, 'start of month') = date($2, 'start of month')`,
|
||||
user.Id, month)
|
||||
err = db.TransformAndLogDbError("dashboard", nil, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if value != nil {
|
||||
summary.Savings = *value
|
||||
}
|
||||
|
||||
err = s.db.GetContext(ctx, &value, `
|
||||
SELECT SUM(value)
|
||||
FROM "transaction"
|
||||
WHERE user_id = $1
|
||||
AND account_id IS NOT NULL
|
||||
AND treasure_chest_id IS NULL
|
||||
AND error IS NULL
|
||||
AND date(timestamp, 'start of month') = date($2, 'start of month')`,
|
||||
user.Id, month)
|
||||
err = db.TransformAndLogDbError("dashboard", nil, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if value != nil {
|
||||
summary.Income = *value
|
||||
}
|
||||
|
||||
// err = s.db.GetContext(ctx, &summary.Expenses, `
|
||||
// SELECT SUM(value)
|
||||
// FROM "transaction"
|
||||
// WHERE user_id = $1
|
||||
// AND account_id IS NOT NULL
|
||||
// AND treasure_chest_id IS NOT NULL
|
||||
// AND error IS NULL
|
||||
// AND date(timestamp, 'start of month') = date($2, 'start of month')`,
|
||||
// user.Id, month)
|
||||
// err = db.TransformAndLogDbError("dashboard", nil, err)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
summary.Total = summary.Income - summary.Expenses
|
||||
summary.Month = month
|
||||
|
||||
slog.Info("Dashboard summary", "summary", summary)
|
||||
|
||||
return &summary, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user