Make sure creating a new task and updating an existing one use the same logic for setting the bucket

This commit is contained in:
kolaente 2021-03-21 22:49:41 +01:00
parent 6d78ee7ce1
commit 41eee00aed
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 59 additions and 72 deletions

View File

@ -720,7 +720,6 @@ func checkBucketAndTaskBelongToSameList(fullTask *Task, bucket *Bucket) (err err
// Checks if adding a new task would exceed the bucket limit
func checkBucketLimit(s *xorm.Session, t *Task, bucket *Bucket) (err error) {
// Check the limit
if bucket.Limit > 0 {
taskCount, err := s.
Where("bucket_id = ?", bucket.ID).
@ -735,6 +734,62 @@ func checkBucketLimit(s *xorm.Session, t *Task, bucket *Bucket) (err error) {
return nil
}
// Contains all the task logic to figure out what bucket to use for this task.
func setTaskBucket(s *xorm.Session, task *Task, originalTask *Task, doCheckBucketLimit bool) (err error) {
// Make sure we have a bucket
var bucket *Bucket
if task.Done {
bucket = &Bucket{}
exists, err := s.
Where("list_id = ? and is_done_bucket = ?", task.ListID, true).
Get(bucket)
if err != nil {
return err
}
if !exists {
bucket = nil
}
if bucket != nil {
task.BucketID = bucket.ID
}
}
if task.BucketID == 0 || (originalTask != nil && task.ListID != 0 && originalTask.ListID != task.ListID) {
bucket, err = getDefaultBucket(s, task.ListID)
if err != nil {
return err
}
task.BucketID = bucket.ID
}
if bucket == nil {
bucket, err = getBucketByID(s, task.BucketID)
if err != nil {
return err
}
}
// If there is a bucket set, make sure they belong to the same list as the task
err = checkBucketAndTaskBelongToSameList(task, bucket)
if err != nil {
return
}
// Check the bucket limit
// Only check the bucket limit if the task is being moved between buckets, allow reordering the task within a bucket
if doCheckBucketLimit {
if err := checkBucketLimit(s, task, bucket); err != nil {
return err
}
}
if bucket.IsDoneBucket {
task.Done = true
}
return nil
}
// Create is the implementation to create a list task
// @Summary Create a task
// @Description Inserts a task into a list.
@ -786,33 +841,11 @@ func createTask(s *xorm.Session, t *Task, a web.Auth, updateAssignees bool) (err
}
// Get the default bucket and move the task there
var bucket *Bucket
if t.BucketID == 0 {
bucket, err = getDefaultBucket(s, t.ListID)
if err != nil {
return err
}
t.BucketID = bucket.ID
}
if bucket == nil {
bucket, err = getBucketByID(s, t.BucketID)
if err != nil {
return err
}
}
// If there is a bucket set, make sure they belong to the same list as the task
err = checkBucketAndTaskBelongToSameList(t, bucket)
err = setTaskBucket(s, t, nil, true)
if err != nil {
return
}
// Bucket Limit
if err := checkBucketLimit(s, t, bucket); err != nil {
return err
}
// Get the index for this task
latestTask := &Task{}
_, err = s.Where("list_id = ?", t.ListID).OrderBy("id desc").Get(latestTask)
@ -927,43 +960,9 @@ func (t *Task) Update(s *xorm.Session, a web.Auth) (err error) {
"is_favorite",
}
// 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 {
return err
}
t.BucketID = bucket.ID
}
if bucket == nil {
bucket, err = getBucketByID(s, t.BucketID)
if err != nil {
return err
}
}
// If there is a bucket set, make sure they belong to the same list as the task
err = checkBucketAndTaskBelongToSameList(t, bucket)
err = setTaskBucket(s, t, &ot, t.BucketID != ot.BucketID)
if err != nil {
return
return err
}
// If the task is being moved between lists, make sure to move the bucket + index as well
@ -978,18 +977,6 @@ func (t *Task) Update(s *xorm.Session, a web.Auth) (err error) {
colsToUpdate = append(colsToUpdate, "index")
}
// Check the bucket limit
// Only check the bucket limit if the task is being moved between buckets, allow reordering the task within a bucket
if t.BucketID != ot.BucketID {
if err := checkBucketLimit(s, t, bucket); err != nil {
return err
}
}
if bucket.IsDoneBucket {
t.Done = true
}
// Update the labels
//
// Maybe FIXME: