Skip to content

Commit 40b0f17

Browse files
authored
fix: quick allocation now respects the budget (#983)
1 parent 9014f2f commit 40b0f17

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

pkg/controllers/v4/month.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ func DeleteAllocations(c *gin.Context) {
287287
// @Param mode body BudgetAllocationMode true "Budget"
288288
// @Router /v4/months [post]
289289
func SetAllocations(c *gin.Context) {
290-
month, _, err := parseMonthQuery(c)
290+
month, budget, err := parseMonthQuery(c)
291291
if err != nil {
292292
c.JSON(status(err), httpError{
293293
Error: err.Error(),
@@ -325,9 +325,12 @@ func SetAllocations(c *gin.Context) {
325325
// Get all envelope IDs and allocation amounts where there is no allocation
326326
// for the request month, but one for the last month
327327
err = models.DB.
328+
Joins("JOIN categories ON categories.id = envelopes.category_id ").
329+
Joins("JOIN budgets ON budgets.id = categories.budget_id").
328330
Joins("JOIN month_configs ON month_configs.envelope_id = envelopes.id AND envelopes.archived IS FALSE AND month_configs.month = ? AND NOT EXISTS(?)", pastMonth, queryCurrentMonth).
329331
Select("envelopes.id, month_configs.allocation").
330332
Table("envelopes").
333+
Where("budgets.id = ?", budget.ID).
331334
Find(&envelopesAmount).
332335
Error
333336
if err != nil {

pkg/controllers/v4/month_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ func (suite *TestSuiteStandard) TestMonthsDeleteFail() {
164164

165165
func (suite *TestSuiteStandard) TestMonthsAllocateBudgeted() {
166166
budget := createTestBudget(suite.T(), v4.BudgetEditable{})
167+
167168
category := createTestCategory(suite.T(), v4.CategoryEditable{BudgetID: budget.Data.ID})
168169
envelope1 := createTestEnvelope(suite.T(), v4.EnvelopeEditable{CategoryID: category.Data.ID})
169170
envelope2 := createTestEnvelope(suite.T(), v4.EnvelopeEditable{CategoryID: category.Data.ID})
@@ -173,6 +174,12 @@ func (suite *TestSuiteStandard) TestMonthsAllocateBudgeted() {
173174
e2Amount := decimal.NewFromFloat(40)
174175
eArchivedAmount := decimal.NewFromFloat(50)
175176

177+
// Unaffected budget
178+
budgetUnaffected := createTestBudget(suite.T(), v4.BudgetEditable{Name: "Nothing should happen with this"})
179+
categoryUnaffected := createTestCategory(suite.T(), v4.CategoryEditable{BudgetID: budgetUnaffected.Data.ID})
180+
envelopeUnaffected := createTestEnvelope(suite.T(), v4.EnvelopeEditable{CategoryID: categoryUnaffected.Data.ID})
181+
182+
// Months
176183
january := types.NewMonth(2022, 1)
177184
february := january.AddDate(0, 1)
178185

@@ -185,6 +192,7 @@ func (suite *TestSuiteStandard) TestMonthsAllocateBudgeted() {
185192
{envelope1.Data.ID, january, e1Amount},
186193
{envelope2.Data.ID, january, e2Amount},
187194
{archivedEnvelope.Data.ID, january, eArchivedAmount},
195+
{envelopeUnaffected.Data.ID, january, decimal.NewFromFloat(100)},
188196
}
189197

190198
for _, allocation := range allocations {
@@ -219,6 +227,14 @@ func (suite *TestSuiteStandard) TestMonthsAllocateBudgeted() {
219227

220228
// Quick allocations skip archived envelopes, so this should be zero
221229
suite.Assert().True(archivedEnvelopeMonth.Data.Allocation.IsZero(), "Expected: 0, got %s, Request ID: %s", archivedEnvelopeMonth.Data.Allocation, recorder.Header().Get("x-request-id"))
230+
231+
// Verify that nothing happened for the unaffected budget
232+
// Regression test for https://github.com/envelope-zero/backend/issues/972
233+
recorder = test.Request(suite.T(), http.MethodGet, strings.Replace(envelopeUnaffected.Data.Links.Month, "YYYY-MM", february.String(), 1), "")
234+
test.AssertHTTPStatus(suite.T(), &recorder, http.StatusOK)
235+
var envelopeUnaffectedMonth v4.MonthConfigResponse
236+
test.DecodeResponse(suite.T(), &recorder, &envelopeUnaffectedMonth)
237+
suite.Assert().True(envelopeUnaffectedMonth.Data.Allocation.Equal(decimal.Zero), "Expected: %s, got %s, Request ID: %s", decimal.Zero, envelopeUnaffectedMonth.Data.Allocation, recorder.Header().Get("x-request-id"))
222238
}
223239

224240
func (suite *TestSuiteStandard) TestMonthsAllocateSpend() {

0 commit comments

Comments
 (0)