Skip to content

Commit 27b08b8

Browse files
authored
fix: do not quick allocate for archived envelopes (#673)
1 parent 3ad291a commit 27b08b8

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

pkg/controllers/month.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func (co Controller) SetAllocations(c *gin.Context) {
182182
// Get all envelope IDs and allocation amounts where there is no allocation
183183
// for the request month, but one for the last month
184184
if !queryWithRetry(c, co.DB.
185-
Joins("JOIN allocations ON allocations.envelope_id = envelopes.id AND allocations.month = ? AND NOT EXISTS(?)", pastMonth, queryCurrentMonth).
185+
Joins("JOIN allocations ON allocations.envelope_id = envelopes.id AND envelopes.hidden IS FALSE AND allocations.month = ? AND NOT EXISTS(?)", pastMonth, queryCurrentMonth).
186186
Select("envelopes.id, allocations.amount").
187187
Table("envelopes").
188188
Find(&envelopesAmount)) {

pkg/controllers/month_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ func (suite *TestSuiteStandard) TestSetMonthBudgeted() {
158158
category := suite.createTestCategory(models.CategoryCreate{BudgetID: budget.Data.ID})
159159
envelope1 := suite.createTestEnvelope(models.EnvelopeCreate{CategoryID: category.Data.ID})
160160
envelope2 := suite.createTestEnvelope(models.EnvelopeCreate{CategoryID: category.Data.ID})
161+
archivedEnvelope := suite.createTestEnvelope(models.EnvelopeCreate{CategoryID: category.Data.ID, Hidden: true})
161162

162163
allocation1 := suite.createTestAllocation(models.AllocationCreate{
163164
Month: types.NewMonth(2022, 1),
@@ -171,6 +172,12 @@ func (suite *TestSuiteStandard) TestSetMonthBudgeted() {
171172
EnvelopeID: envelope2.Data.ID,
172173
})
173174

175+
_ = suite.createTestAllocation(models.AllocationCreate{
176+
Month: types.NewMonth(2022, 1),
177+
Amount: decimal.NewFromFloat(50),
178+
EnvelopeID: archivedEnvelope.Data.ID,
179+
})
180+
174181
// Update in budgeted mode allocations
175182
recorder := test.Request(suite.controller, suite.T(), http.MethodPost, strings.Replace(budget.Data.Links.MonthAllocations, "YYYY-MM", "2022-02", 1), controllers.BudgetAllocationMode{Mode: controllers.AllocateLastMonthBudget})
176183
assertHTTPStatus(suite.T(), &recorder, http.StatusNoContent)
@@ -189,6 +196,13 @@ func (suite *TestSuiteStandard) TestSetMonthBudgeted() {
189196
var envelope2Month controllers.EnvelopeMonthResponse
190197
suite.decodeResponse(&recorder, &envelope2Month)
191198
suite.Assert().True(allocation2.Data.Amount.Equal(envelope2Month.Data.Allocation), "Expected: %s, got %s, Request ID: %s", allocation2.Data.Amount, envelope2Month.Data.Allocation, recorder.Header().Get("x-request-id"))
199+
200+
// Verify the allocation for the archived envelope
201+
recorder = test.Request(suite.controller, suite.T(), http.MethodGet, strings.Replace(archivedEnvelope.Data.Links.Month, "YYYY-MM", "2022-02", 1), "")
202+
assertHTTPStatus(suite.T(), &recorder, http.StatusOK)
203+
var archivedEnvelopeMonth controllers.EnvelopeMonthResponse
204+
suite.decodeResponse(&recorder, &archivedEnvelopeMonth)
205+
suite.Assert().True(archivedEnvelopeMonth.Data.Allocation.IsZero(), "Expected: 0, got %s, Request ID: %s", archivedEnvelopeMonth.Data.Allocation, recorder.Header().Get("x-request-id"))
192206
}
193207

194208
func (suite *TestSuiteStandard) TestSetMonthSpend() {

0 commit comments

Comments
 (0)