Skip to content

Commit d82074f

Browse files
authored
feat: add month spent sum (#474)
1 parent 3d5361b commit d82074f

File tree

6 files changed

+21
-0
lines changed

6 files changed

+21
-0
lines changed

api/docs.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3579,6 +3579,11 @@ const docTemplate = `{
35793579
"description": "The name of the Budget",
35803580
"type": "string",
35813581
"example": "Zero budget"
3582+
},
3583+
"spent": {
3584+
"description": "The amount of money spent in this month",
3585+
"type": "number",
3586+
"example": 133.7
35823587
}
35833588
}
35843589
},

api/swagger.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3567,6 +3567,11 @@
35673567
"description": "The name of the Budget",
35683568
"type": "string",
35693569
"example": "Zero budget"
3570+
},
3571+
"spent": {
3572+
"description": "The amount of money spent in this month",
3573+
"type": "number",
3574+
"example": 133.7
35703575
}
35713576
}
35723577
},

api/swagger.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,10 @@ definitions:
651651
description: The name of the Budget
652652
example: Zero budget
653653
type: string
654+
spent:
655+
description: The amount of money spent in this month
656+
example: 133.7
657+
type: number
654658
type: object
655659
models.MonthConfigCreate:
656660
properties:

pkg/controllers/month.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ func (co Controller) GetMonth(c *gin.Context) {
159159

160160
// Update the month's balance
161161
month.Balance = month.Balance.Add(envelopeMonth.Balance)
162+
month.Spent = month.Spent.Add(envelopeMonth.Spent)
162163

163164
// Set the allocation link. If there is no allocation, we send the collection endpoint.
164165
// With this, any client will be able to see that the "Budgeted" amount is 0 and therefore

pkg/controllers/month_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ func (suite *TestSuiteStandard) TestMonth() {
9898
Month: time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC),
9999
Income: decimal.NewFromFloat(0),
100100
Balance: decimal.NewFromFloat(10.99),
101+
Spent: decimal.NewFromFloat(10),
101102
Categories: []models.CategoryEnvelopes{
102103
{
103104
Name: category.Data.Name,
@@ -126,6 +127,7 @@ func (suite *TestSuiteStandard) TestMonth() {
126127
Month: time.Date(2022, 2, 1, 0, 0, 0, 0, time.UTC),
127128
Income: decimal.NewFromFloat(0),
128129
Balance: decimal.NewFromFloat(53.11),
130+
Spent: decimal.NewFromFloat(5),
129131
Categories: []models.CategoryEnvelopes{
130132
{
131133
Name: category.Data.Name,
@@ -154,6 +156,7 @@ func (suite *TestSuiteStandard) TestMonth() {
154156
Month: time.Date(2022, 3, 1, 0, 0, 0, 0, time.UTC),
155157
Income: decimal.NewFromFloat(1500),
156158
Balance: decimal.NewFromFloat(69.28),
159+
Spent: decimal.NewFromFloat(15),
157160
Categories: []models.CategoryEnvelopes{
158161
{
159162
Name: category.Data.Name,
@@ -197,6 +200,8 @@ func (suite *TestSuiteStandard) TestMonth() {
197200
suite.Assert().FailNow("Response envelope length does not match!", "Envelope list does not have exactly 1 item, it has %d, Request ID: %s", len(month.Data.Categories[0].Envelopes), r.Header().Get("x-request-id"))
198201
}
199202

203+
suite.Assert().True(month.Data.Spent.Equal(tt.response.Data.Spent), "Month spent is wrong. Should be %v, but is %v: %#v", tt.response.Data.Spent, month.Data.Spent, month.Data)
204+
200205
expected := tt.response.Data.Categories[0].Envelopes[0]
201206
envelope := month.Data.Categories[0].Envelopes[0]
202207
suite.Assert().True(envelope.Spent.Equal(expected.Spent), "Monthly spent calculation for %v is wrong: should be %v, but is %v: %#v", month.Data.Month, expected.Spent, envelope.Spent, month.Data)

pkg/models/month.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ type Month struct {
2121
Income decimal.Decimal `json:"income" example:"2317.34"` // The total income for the month (sum of all incoming transactions without an Envelope)
2222
Available decimal.Decimal `json:"available" example:"217.34"` // The amount available to budget
2323
Balance decimal.Decimal `json:"balance" example:"5231.37"` // The sum of all envelope balances
24+
Spent decimal.Decimal `json:"spent" example:"133.70"` // The amount of money spent in this month
2425
Categories []CategoryEnvelopes `json:"categories"` // A list of envelope month calculations grouped by category
2526
}

0 commit comments

Comments
 (0)