diff --git a/pkg/models/tasks.go b/pkg/models/tasks.go index 69ce2eb99..4f9e45f1a 100644 --- a/pkg/models/tasks.go +++ b/pkg/models/tasks.go @@ -929,6 +929,22 @@ func (t *Task) Update(s *xorm.Session, a web.Auth) (err error) { // Make sure we have a bucket var bucket *Bucket + if t.Done { + bucket = &Bucket{} + exists, err := s. + Where("list_id = ? and is_done_bucket = ?", t.ListID, true). + Get(bucket) + if err != nil { + return err + } + if !exists { + bucket = nil + } + if bucket != nil { + t.BucketID = bucket.ID + } + } + if t.BucketID == 0 || (t.ListID != 0 && ot.ListID != t.ListID) { bucket, err = getDefaultBucket(s, t.ListID) if err != nil { diff --git a/pkg/models/tasks_test.go b/pkg/models/tasks_test.go index 20af79e7d..67a5db283 100644 --- a/pkg/models/tasks_test.go +++ b/pkg/models/tasks_test.go @@ -260,6 +260,28 @@ func TestTask_Update(t *testing.T) { assert.Equal(t, int64(4), task.BucketID) // bucket 4 is the default bucket on list 2 assert.True(t, task.Done) // bucket 4 is the done bucket, so the task should be marked as done as well }) + t.Run("marking a task as done should move it to the done bucket", func(t *testing.T) { + db.LoadAndAssertFixtures(t) + s := db.NewSession() + defer s.Close() + + task := &Task{ + ID: 1, + Done: true, + } + err := task.Update(s, u) + assert.NoError(t, err) + err = s.Commit() + assert.NoError(t, err) + assert.True(t, task.Done) + assert.Equal(t, int64(3), task.BucketID) + + db.AssertExists(t, "tasks", map[string]interface{}{ + "id": 1, + "done": true, + "bucket_id": 3, + }, false) + }) } func TestTask_Delete(t *testing.T) {