Skip to content

Commit a326921

Browse files
committed
chore: add envelope.Month method for monthly calculations
1 parent b5fde8f commit a326921

File tree

4 files changed

+36
-36
lines changed

4 files changed

+36
-36
lines changed

internal/controllers/envelope.go

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/envelope-zero/backend/internal/models"
88
"github.com/gin-gonic/gin"
9-
"github.com/rs/zerolog/log"
109
)
1110

1211
// RegisterEnvelopeRoutes registers the routes for envelopes with
@@ -82,28 +81,8 @@ func GetEnvelope(c *gin.Context) {
8281

8382
// If a month is requested, return only month specfic data
8483
if !month.Month.IsZero() {
85-
spent, err := envelope.Spent(month.Month)
86-
if err != nil {
87-
FetchErrorHandler(c, err)
88-
return
89-
}
90-
91-
var allocation models.Allocation
92-
models.DB.First(&allocation, &models.Allocation{
93-
Month: uint8(month.Month.UTC().Month()),
94-
Year: uint(month.Month.UTC().Year()),
95-
})
96-
97-
balance := allocation.Amount.Add(spent)
98-
log.Info().Str("spent", spent.String()).Str("allocation", allocation.Amount.String()).Str("balance", balance.String()).Msg("GetEnvelope")
99-
10084
c.JSON(http.StatusOK, gin.H{
101-
"data": map[string]interface{}{
102-
"allocation": allocation.Amount,
103-
"spent": spent,
104-
"balance": balance,
105-
"month": month.Month,
106-
},
85+
"data": envelope.Month(month.Month),
10786
})
10887
return
10988
}

internal/controllers/envelope_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,7 @@ type EnvelopeDetailResponse struct {
2424

2525
type EnvelopeMonthResponse struct {
2626
test.APIResponse
27-
Data struct {
28-
Month time.Time `json:"month"`
29-
Spent decimal.Decimal `json:"spent"`
30-
Balance decimal.Decimal `json:"balance"`
31-
Allocation decimal.Decimal `json:"allocation"`
32-
}
27+
Data models.EnvelopeMonth
3328
}
3429

3530
func TestGetEnvelopes(t *testing.T) {

internal/models/envelope.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,16 @@ type Envelope struct {
1616
Note string `json:"note,omitempty"`
1717
}
1818

19+
// EnvelopeMonth contains data about an Envelope for a specific month.
20+
type EnvelopeMonth struct {
21+
Month time.Time `json:"month"`
22+
Spent decimal.Decimal `json:"spent"`
23+
Balance decimal.Decimal `json:"balance"`
24+
Allocation decimal.Decimal `json:"allocation"`
25+
}
26+
1927
// Spent returns the amount spent for the month the time.Time instance is in.
20-
func (e Envelope) Spent(t time.Time) (decimal.Decimal, error) {
28+
func (e Envelope) Spent(t time.Time) decimal.Decimal {
2129
// All transactions where the Envelope ID matches and that have an external account as source and an internal account as destination
2230
incoming, _ := RawTransactions(
2331
fmt.Sprintf("SELECT transactions.* FROM transactions, accounts AS source_accounts, accounts AS destination_accounts WHERE transactions.source_account_id = source_accounts.id AND source_accounts.external AND transactions.destination_account_id = destination_accounts.id AND NOT destination_accounts.external AND transactions.envelope_id = %v", e.ID),
@@ -44,5 +52,25 @@ func (e Envelope) Spent(t time.Time) (decimal.Decimal, error) {
4452
}
4553
}
4654

47-
return incomingSum.Sub(outgoingSum), nil
55+
return incomingSum.Sub(outgoingSum)
56+
}
57+
58+
// Month calculates the month specific values for an envelope and returns an EnvelopeMonth for them.
59+
func (e Envelope) Month(t time.Time) EnvelopeMonth {
60+
spent := e.Spent(t)
61+
62+
var allocation Allocation
63+
DB.First(&allocation, &Allocation{
64+
Month: uint8(t.UTC().Month()),
65+
Year: uint(t.UTC().Year()),
66+
})
67+
68+
balance := allocation.Amount.Add(spent)
69+
70+
return EnvelopeMonth{
71+
Month: t,
72+
Spent: spent,
73+
Balance: balance,
74+
Allocation: allocation.Amount,
75+
}
4876
}

internal/models/envelope_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,9 @@ func TestEnvelopeMonthSum(t *testing.T) {
4545
}
4646
models.DB.Create(&transactionIn)
4747

48-
envelopeSpent, err := envelope.Spent(time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC))
49-
assert.Nil(t, err)
50-
assert.True(t, envelopeSpent.Equal(spent.Neg()), "Month calculation for 2022-01 is wrong: should be %v, but is %v", spent.Neg(), envelopeSpent)
48+
envelopeMonth := envelope.Month(time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC))
49+
assert.True(t, envelopeMonth.Spent.Equal(spent.Neg()), "Month calculation for 2022-01 is wrong: should be %v, but is %v", spent.Neg(), envelopeMonth.Spent)
5150

52-
envelopeSpent, err = envelope.Spent(time.Date(2022, 2, 1, 0, 0, 0, 0, time.UTC))
53-
assert.Nil(t, err)
54-
assert.True(t, envelopeSpent.Equal(spent.Neg()), "Month calculation for 2022-02 is wrong: should be %v, but is %v", spent, envelopeSpent)
51+
envelopeMonth = envelope.Month(time.Date(2022, 2, 1, 0, 0, 0, 0, time.UTC))
52+
assert.True(t, envelopeMonth.Spent.Equal(spent.Neg()), "Month calculation for 2022-02 is wrong: should be %v, but is %v", spent, envelopeMonth.Spent)
5553
}

0 commit comments

Comments
 (0)