Skip to content

Commit 00ff02f

Browse files
authored
fix: unarchiving an envelope now also unarchives its parent category (#726)
Resolves #687.
1 parent 9cadf93 commit 00ff02f

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

pkg/models/database.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ func Migrate(db *gorm.DB) error {
1919
/*
2020
* Workaround for https://github.com/go-gorm/gorm/issues/5968
2121
* Remove with 3.0.0
22+
*
23+
* With this, remove the e.UUID != uuid.Nil check in the Envelope.BeforeUpdate method
2224
*/
2325
// Account
2426
db.Unscoped().Model(&Account{}).Select("OnBudget").Where("accounts.on_budget IS NULL").Update("OnBudget", false),
@@ -36,10 +38,10 @@ func Migrate(db *gorm.DB) error {
3638
// Delete allocations with an amount of 0
3739
db.Unscoped().Model(&Allocation{}).Where("amount IS '0'").Delete(&Allocation{}),
3840
}
39-
for _, query := range queries {
41+
for i, query := range queries {
4042
err = query.Error
4143
if err != nil {
42-
return fmt.Errorf("error during DB migration: %w", err)
44+
return fmt.Errorf("error during DB migration for migration %d: %w", i+1, err)
4345
}
4446
}
4547

pkg/models/envelope.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,29 @@ func (e Envelope) Self() string {
3636
return "Envelope"
3737
}
3838

39+
// BeforeUpdate verifies the state of the envelope before
40+
// committing an update to the database.
41+
func (e *Envelope) BeforeUpdate(tx *gorm.DB) (err error) {
42+
// If the archival state is updated from archived to unarchived and the category is
43+
// archived, unarchive the category, too.
44+
//
45+
// This checks for the envelope's ID to not be nil since there is a call during migration
46+
// where it is nil. Remove this with v3.0.0 when these migrations are removed, too.
47+
if e.ID != uuid.Nil && !e.Hidden {
48+
var category Category
49+
err = tx.First(&category, e.CategoryID).Error
50+
if err != nil {
51+
return
52+
}
53+
54+
if category.Hidden {
55+
tx.Model(&category).Updates(map[string]any{"hidden": false})
56+
}
57+
}
58+
59+
return
60+
}
61+
3962
func (e *Envelope) AfterFind(tx *gorm.DB) (err error) {
4063
e.links(tx)
4164
return
@@ -45,6 +68,7 @@ func (e *Envelope) AfterFind(tx *gorm.DB) (err error) {
4568
// query the resource directly after creating or updating it.
4669
func (e *Envelope) AfterSave(tx *gorm.DB) (err error) {
4770
e.links(tx)
71+
4872
return
4973
}
5074

pkg/models/envelope_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,27 @@ func (suite *TestSuiteStandard) TestEnvelopeMonthBalance() {
267267
})
268268
}
269269
}
270+
271+
// TestEnvelopeUnarchiveUnarchivesCategory tests that when an envelope is unarchived, but its parent category
272+
// is archived, the parent category is unarchived, too.
273+
func (suite *TestSuiteStandard) TestEnvelopeUnarchiveUnarchivesCategory() {
274+
budget := suite.createTestBudget(models.BudgetCreate{})
275+
category := suite.createTestCategory(models.CategoryCreate{
276+
BudgetID: budget.ID,
277+
Hidden: true,
278+
})
279+
280+
envelope := suite.createTestEnvelope(models.EnvelopeCreate{
281+
CategoryID: category.ID,
282+
Name: "TestEnvelopeUnarchiveUnarchivesCategory",
283+
Hidden: true,
284+
})
285+
286+
// Unarchive the envelope
287+
envelope.Hidden = false
288+
suite.db.Save(&envelope)
289+
290+
// Reload the category
291+
suite.db.First(&category, category.ID)
292+
assert.False(suite.T(), category.Hidden, "Category should be unarchived when child envelope is unarchived")
293+
}

0 commit comments

Comments
 (0)