Skip to content

Commit

Permalink
fix: quick allocation now respects the budget (#983)
Browse files Browse the repository at this point in the history
  • Loading branch information
morremeyer authored Mar 1, 2024
1 parent 9014f2f commit 40b0f17
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkg/controllers/v4/month.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func DeleteAllocations(c *gin.Context) {
// @Param mode body BudgetAllocationMode true "Budget"
// @Router /v4/months [post]
func SetAllocations(c *gin.Context) {
month, _, err := parseMonthQuery(c)
month, budget, err := parseMonthQuery(c)
if err != nil {
c.JSON(status(err), httpError{
Error: err.Error(),
Expand Down Expand Up @@ -325,9 +325,12 @@ func SetAllocations(c *gin.Context) {
// Get all envelope IDs and allocation amounts where there is no allocation
// for the request month, but one for the last month
err = models.DB.
Joins("JOIN categories ON categories.id = envelopes.category_id ").
Joins("JOIN budgets ON budgets.id = categories.budget_id").
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).
Select("envelopes.id, month_configs.allocation").
Table("envelopes").
Where("budgets.id = ?", budget.ID).
Find(&envelopesAmount).
Error
if err != nil {
Expand Down
16 changes: 16 additions & 0 deletions pkg/controllers/v4/month_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ func (suite *TestSuiteStandard) TestMonthsDeleteFail() {

func (suite *TestSuiteStandard) TestMonthsAllocateBudgeted() {
budget := createTestBudget(suite.T(), v4.BudgetEditable{})

category := createTestCategory(suite.T(), v4.CategoryEditable{BudgetID: budget.Data.ID})
envelope1 := createTestEnvelope(suite.T(), v4.EnvelopeEditable{CategoryID: category.Data.ID})
envelope2 := createTestEnvelope(suite.T(), v4.EnvelopeEditable{CategoryID: category.Data.ID})
Expand All @@ -173,6 +174,12 @@ func (suite *TestSuiteStandard) TestMonthsAllocateBudgeted() {
e2Amount := decimal.NewFromFloat(40)
eArchivedAmount := decimal.NewFromFloat(50)

// Unaffected budget
budgetUnaffected := createTestBudget(suite.T(), v4.BudgetEditable{Name: "Nothing should happen with this"})
categoryUnaffected := createTestCategory(suite.T(), v4.CategoryEditable{BudgetID: budgetUnaffected.Data.ID})
envelopeUnaffected := createTestEnvelope(suite.T(), v4.EnvelopeEditable{CategoryID: categoryUnaffected.Data.ID})

// Months
january := types.NewMonth(2022, 1)
february := january.AddDate(0, 1)

Expand All @@ -185,6 +192,7 @@ func (suite *TestSuiteStandard) TestMonthsAllocateBudgeted() {
{envelope1.Data.ID, january, e1Amount},
{envelope2.Data.ID, january, e2Amount},
{archivedEnvelope.Data.ID, january, eArchivedAmount},
{envelopeUnaffected.Data.ID, january, decimal.NewFromFloat(100)},
}

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

// Quick allocations skip archived envelopes, so this should be zero
suite.Assert().True(archivedEnvelopeMonth.Data.Allocation.IsZero(), "Expected: 0, got %s, Request ID: %s", archivedEnvelopeMonth.Data.Allocation, recorder.Header().Get("x-request-id"))

// Verify that nothing happened for the unaffected budget
// Regression test for https://github.com/envelope-zero/backend/issues/972
recorder = test.Request(suite.T(), http.MethodGet, strings.Replace(envelopeUnaffected.Data.Links.Month, "YYYY-MM", february.String(), 1), "")
test.AssertHTTPStatus(suite.T(), &recorder, http.StatusOK)
var envelopeUnaffectedMonth v4.MonthConfigResponse
test.DecodeResponse(suite.T(), &recorder, &envelopeUnaffectedMonth)
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"))
}

func (suite *TestSuiteStandard) TestMonthsAllocateSpend() {
Expand Down

0 comments on commit 40b0f17

Please sign in to comment.