Skip to content

Commit eb052b8

Browse files
authored
fix: add envelope links to month endpoint (#909)
1 parent ec6d952 commit eb052b8

File tree

6 files changed

+97
-64
lines changed

6 files changed

+97
-64
lines changed

api/docs.go

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8024,6 +8024,9 @@ const docTemplate = `{
80248024
"type": "string",
80258025
"example": "65392deb-5e92-4268-b114-297faad6cdce"
80268026
},
8027+
"links": {
8028+
"$ref": "#/definitions/controllers.EnvelopeV3Links"
8029+
},
80278030
"name": {
80288031
"description": "Name of the envelope",
80298032
"type": "string",
@@ -8112,24 +8115,11 @@ const docTemplate = `{
81128115
},
81138116
"links": {
81148117
"description": "Links to related resources",
8115-
"type": "object",
8116-
"properties": {
8117-
"month": {
8118-
"description": "The MonthConfig for the envelope",
8119-
"type": "string",
8120-
"example": "https://example.com/api/v3/envelopes/45b6b5b9-f746-4ae9-b77b-7688b91f8166/YYYY-MM"
8121-
},
8122-
"self": {
8123-
"description": "The envelope itself",
8124-
"type": "string",
8125-
"example": "https://example.com/api/v3/envelopes/45b6b5b9-f746-4ae9-b77b-7688b91f8166"
8126-
},
8127-
"transactions": {
8128-
"description": "The envelope's transactions",
8129-
"type": "string",
8130-
"example": "https://example.com/api/v3/transactions?envelope=45b6b5b9-f746-4ae9-b77b-7688b91f8166"
8118+
"allOf": [
8119+
{
8120+
"$ref": "#/definitions/controllers.EnvelopeV3Links"
81318121
}
8132-
}
8122+
]
81338123
},
81348124
"name": {
81358125
"description": "Name of the envelope",
@@ -8148,6 +8138,26 @@ const docTemplate = `{
81488138
}
81498139
}
81508140
},
8141+
"controllers.EnvelopeV3Links": {
8142+
"type": "object",
8143+
"properties": {
8144+
"month": {
8145+
"description": "The MonthConfig for the envelope",
8146+
"type": "string",
8147+
"example": "https://example.com/api/v3/envelopes/45b6b5b9-f746-4ae9-b77b-7688b91f8166/YYYY-MM"
8148+
},
8149+
"self": {
8150+
"description": "The envelope itself",
8151+
"type": "string",
8152+
"example": "https://example.com/api/v3/envelopes/45b6b5b9-f746-4ae9-b77b-7688b91f8166"
8153+
},
8154+
"transactions": {
8155+
"description": "The envelope's transactions",
8156+
"type": "string",
8157+
"example": "https://example.com/api/v3/transactions?envelope=45b6b5b9-f746-4ae9-b77b-7688b91f8166"
8158+
}
8159+
}
8160+
},
81518161
"controllers.GoalCreateResponseV3": {
81528162
"type": "object",
81538163
"properties": {

api/swagger.json

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8013,6 +8013,9 @@
80138013
"type": "string",
80148014
"example": "65392deb-5e92-4268-b114-297faad6cdce"
80158015
},
8016+
"links": {
8017+
"$ref": "#/definitions/controllers.EnvelopeV3Links"
8018+
},
80168019
"name": {
80178020
"description": "Name of the envelope",
80188021
"type": "string",
@@ -8101,24 +8104,11 @@
81018104
},
81028105
"links": {
81038106
"description": "Links to related resources",
8104-
"type": "object",
8105-
"properties": {
8106-
"month": {
8107-
"description": "The MonthConfig for the envelope",
8108-
"type": "string",
8109-
"example": "https://example.com/api/v3/envelopes/45b6b5b9-f746-4ae9-b77b-7688b91f8166/YYYY-MM"
8110-
},
8111-
"self": {
8112-
"description": "The envelope itself",
8113-
"type": "string",
8114-
"example": "https://example.com/api/v3/envelopes/45b6b5b9-f746-4ae9-b77b-7688b91f8166"
8115-
},
8116-
"transactions": {
8117-
"description": "The envelope's transactions",
8118-
"type": "string",
8119-
"example": "https://example.com/api/v3/transactions?envelope=45b6b5b9-f746-4ae9-b77b-7688b91f8166"
8107+
"allOf": [
8108+
{
8109+
"$ref": "#/definitions/controllers.EnvelopeV3Links"
81208110
}
8121-
}
8111+
]
81228112
},
81238113
"name": {
81248114
"description": "Name of the envelope",
@@ -8137,6 +8127,26 @@
81378127
}
81388128
}
81398129
},
8130+
"controllers.EnvelopeV3Links": {
8131+
"type": "object",
8132+
"properties": {
8133+
"month": {
8134+
"description": "The MonthConfig for the envelope",
8135+
"type": "string",
8136+
"example": "https://example.com/api/v3/envelopes/45b6b5b9-f746-4ae9-b77b-7688b91f8166/YYYY-MM"
8137+
},
8138+
"self": {
8139+
"description": "The envelope itself",
8140+
"type": "string",
8141+
"example": "https://example.com/api/v3/envelopes/45b6b5b9-f746-4ae9-b77b-7688b91f8166"
8142+
},
8143+
"transactions": {
8144+
"description": "The envelope's transactions",
8145+
"type": "string",
8146+
"example": "https://example.com/api/v3/transactions?envelope=45b6b5b9-f746-4ae9-b77b-7688b91f8166"
8147+
}
8148+
}
8149+
},
81408150
"controllers.GoalCreateResponseV3": {
81418151
"type": "object",
81428152
"properties": {

api/swagger.yaml

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,8 @@ definitions:
953953
description: UUID for the resource
954954
example: 65392deb-5e92-4268-b114-297faad6cdce
955955
type: string
956+
links:
957+
$ref: '#/definitions/controllers.EnvelopeV3Links'
956958
name:
957959
description: Name of the envelope
958960
example: Groceries
@@ -1015,21 +1017,9 @@ definitions:
10151017
example: 65392deb-5e92-4268-b114-297faad6cdce
10161018
type: string
10171019
links:
1020+
allOf:
1021+
- $ref: '#/definitions/controllers.EnvelopeV3Links'
10181022
description: Links to related resources
1019-
properties:
1020-
month:
1021-
description: The MonthConfig for the envelope
1022-
example: https://example.com/api/v3/envelopes/45b6b5b9-f746-4ae9-b77b-7688b91f8166/YYYY-MM
1023-
type: string
1024-
self:
1025-
description: The envelope itself
1026-
example: https://example.com/api/v3/envelopes/45b6b5b9-f746-4ae9-b77b-7688b91f8166
1027-
type: string
1028-
transactions:
1029-
description: The envelope's transactions
1030-
example: https://example.com/api/v3/transactions?envelope=45b6b5b9-f746-4ae9-b77b-7688b91f8166
1031-
type: string
1032-
type: object
10331023
name:
10341024
description: Name of the envelope
10351025
example: Groceries
@@ -1043,6 +1033,21 @@ definitions:
10431033
example: "2022-04-17T20:14:01.048145Z"
10441034
type: string
10451035
type: object
1036+
controllers.EnvelopeV3Links:
1037+
properties:
1038+
month:
1039+
description: The MonthConfig for the envelope
1040+
example: https://example.com/api/v3/envelopes/45b6b5b9-f746-4ae9-b77b-7688b91f8166/YYYY-MM
1041+
type: string
1042+
self:
1043+
description: The envelope itself
1044+
example: https://example.com/api/v3/envelopes/45b6b5b9-f746-4ae9-b77b-7688b91f8166
1045+
type: string
1046+
transactions:
1047+
description: The envelope's transactions
1048+
example: https://example.com/api/v3/transactions?envelope=45b6b5b9-f746-4ae9-b77b-7688b91f8166
1049+
type: string
1050+
type: object
10461051
controllers.GoalCreateResponseV3:
10471052
properties:
10481053
data:

pkg/controllers/envelope_v3.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,25 @@ func (e EnvelopeCreateV3) ToCreate() models.EnvelopeCreate {
3131
}
3232
}
3333

34-
type EnvelopeV3 struct {
35-
models.Envelope
36-
Hidden bool `json:"hidden,omitempty"` // Remove the hidden field
37-
38-
Links struct {
39-
Self string `json:"self" example:"https://example.com/api/v3/envelopes/45b6b5b9-f746-4ae9-b77b-7688b91f8166"` // The envelope itself
40-
Transactions string `json:"transactions" example:"https://example.com/api/v3/transactions?envelope=45b6b5b9-f746-4ae9-b77b-7688b91f8166"` // The envelope's transactions
41-
Month string `json:"month" example:"https://example.com/api/v3/envelopes/45b6b5b9-f746-4ae9-b77b-7688b91f8166/YYYY-MM"` // The MonthConfig for the envelope
42-
} `json:"links"` // Links to related resources
34+
type EnvelopeV3Links struct {
35+
Self string `json:"self" example:"https://example.com/api/v3/envelopes/45b6b5b9-f746-4ae9-b77b-7688b91f8166"` // The envelope itself
36+
Transactions string `json:"transactions" example:"https://example.com/api/v3/transactions?envelope=45b6b5b9-f746-4ae9-b77b-7688b91f8166"` // The envelope's transactions
37+
Month string `json:"month" example:"https://example.com/api/v3/envelopes/45b6b5b9-f746-4ae9-b77b-7688b91f8166/YYYY-MM"` // The MonthConfig for the envelope
4338
}
4439

45-
func (e *EnvelopeV3) links(c *gin.Context) {
40+
func (l *EnvelopeV3Links) links(c *gin.Context, e models.Envelope) {
4641
url := c.GetString(string(database.ContextURL))
4742
self := fmt.Sprintf("%s/v3/envelopes/%s", url, e.ID)
4843

49-
e.Links.Self = self
50-
e.Links.Transactions = fmt.Sprintf("%s/v3/transactions?envelope=%s", url, e.ID)
51-
e.Links.Month = fmt.Sprintf("%s/v3/envelopes/%s/YYYY-MM", url, e.ID)
44+
l.Self = self
45+
l.Transactions = fmt.Sprintf("%s/v3/transactions?envelope=%s", url, e.ID)
46+
l.Month = fmt.Sprintf("%s/v3/envelopes/%s/YYYY-MM", url, e.ID)
47+
}
48+
49+
type EnvelopeV3 struct {
50+
models.Envelope
51+
Links EnvelopeV3Links `json:"links"` // Links to related resources
52+
Hidden bool `json:"hidden,omitempty"` // Remove the hidden field
5253
}
5354

5455
func (co Controller) getEnvelopeV3(c *gin.Context, id uuid.UUID) (EnvelopeV3, httperrors.Error) {
@@ -61,7 +62,7 @@ func (co Controller) getEnvelopeV3(c *gin.Context, id uuid.UUID) (EnvelopeV3, ht
6162
Envelope: m,
6263
}
6364

64-
r.links(c)
65+
r.Links.links(c, m)
6566
return r, httperrors.Error{}
6667
}
6768

pkg/controllers/month_v3.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type EnvelopeMonthV3 struct {
4545
Spent decimal.Decimal `json:"spent" example:"73.12"` // The amount spent over the whole month
4646
Balance decimal.Decimal `json:"balance" example:"12.32"` // The balance at the end of the monht
4747
Allocation decimal.Decimal `json:"allocation" example:"85.44"` // The amount of money allocated
48+
Links EnvelopeV3Links `json:"links"`
4849
}
4950

5051
// RegisterMonthRoutesV3 registers the routes for months with
@@ -160,7 +161,7 @@ func (co Controller) GetMonthV3(c *gin.Context) {
160161
}
161162

162163
for _, envelope := range envelopes {
163-
envelopeMonth, err := envelopeMonthV3(co.DB, envelope, result.Month)
164+
envelopeMonth, err := envelopeMonthV3(c, co.DB, envelope, result.Month)
164165
if err != nil {
165166
e := httperrors.Parse(c, err)
166167
s := e.Error()
@@ -359,7 +360,7 @@ func (co Controller) SetAllocationsV3(c *gin.Context) {
359360
}
360361

361362
// envelopeMonthV3 calculates the month specific values for an envelope and returns an EnvelopeMonthV3 with them
362-
func envelopeMonthV3(db *gorm.DB, e models.Envelope, month types.Month) (EnvelopeMonthV3, error) {
363+
func envelopeMonthV3(c *gin.Context, db *gorm.DB, e models.Envelope, month types.Month) (EnvelopeMonthV3, error) {
363364
spent := e.Spent(db, month)
364365

365366
envelopeMonth := EnvelopeMonthV3{
@@ -388,5 +389,8 @@ func envelopeMonthV3(db *gorm.DB, e models.Envelope, month types.Month) (Envelop
388389
}
389390

390391
envelopeMonth.Allocation = allocation.Amount
392+
393+
// Set the links
394+
envelopeMonth.Links.links(c, e)
391395
return envelopeMonth, nil
392396
}

pkg/controllers/month_v3_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,9 @@ func (suite *TestSuiteStandard) TestMonthsV3() {
483483
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.Categories[0].Envelopes))
484484
}
485485

486+
// Verify the links are set correctly
487+
assert.Equal(t, envelope.Data.Links.Month, month.Categories[0].Envelopes[0].Links.Month)
488+
486489
// Category calculations
487490
expectedCategory := tt.result.Categories[0]
488491
category := month.Categories[0]

0 commit comments

Comments
 (0)