feat: make default bucket configurable
continuous-integration/drone/pr Build is passing Details

This commit is contained in:
kolaente 2023-09-03 15:17:17 +02:00
parent bbbb45d224
commit 60bd5c8a79
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
5 changed files with 49 additions and 37 deletions

View File

@ -38,6 +38,7 @@
title: testbucket6 title: testbucket6
project_id: 6 project_id: 6
created_by_id: 1 created_by_id: 1
position: 1
created: 2020-04-18 21:13:52 created: 2020-04-18 21:13:52
updated: 2020-04-18 21:13:52 updated: 2020-04-18 21:13:52
- id: 7 - id: 7
@ -135,6 +136,7 @@
title: testbucket22 title: testbucket22
project_id: 6 project_id: 6
created_by_id: 1 created_by_id: 1
position: 2
created: 2020-04-18 21:13:52 created: 2020-04-18 21:13:52
updated: 2020-04-18 21:13:52 updated: 2020-04-18 21:13:52
- id: 23 - id: 23

View File

@ -52,6 +52,7 @@
identifier: test6 identifier: test6
owner_id: 6 owner_id: 6
position: 6 position: 6
default_bucket_id: 22
updated: 2018-12-02 15:13:12 updated: 2018-12-02 15:13:12
created: 2018-12-01 15:13:12 created: 2018-12-01 15:13:12
- -

View File

@ -78,28 +78,21 @@ func getBucketByID(s *xorm.Session, id int64) (b *Bucket, err error) {
return return
} }
func getDefaultBucket(s *xorm.Session, projectID int64) (bucket *Bucket, err error) { func getDefaultBucketID(s *xorm.Session, project *Project) (bucketID int64, err error) {
bucket = &Bucket{} if project.DefaultBucketID != 0 {
return project.DefaultBucketID, nil
}
bucket := &Bucket{}
_, err = s. _, err = s.
Where("project_id = ?", projectID). Where("project_id = ?", project.ID).
OrderBy("position asc"). OrderBy("position asc").
Get(bucket) Get(bucket)
return
}
func getDoneBucketForProject(s *xorm.Session, projectID int64) (bucket *Bucket, err error) {
bucket = &Bucket{}
exists, err := s.
Where("id = (select done_bucket_id from projects where id = ?)", projectID).
Get(bucket)
if err != nil { if err != nil {
return nil, err return 0, err
}
if !exists {
bucket = nil
} }
return return bucket.ID, nil
} }
// ReadAll returns all buckets with their tasks for a certain project // ReadAll returns all buckets with their tasks for a certain project
@ -330,15 +323,19 @@ func (b *Bucket) Delete(s *xorm.Session, _ web.Auth) (err error) {
} }
// Get the default bucket // Get the default bucket
defaultBucket, err := getDefaultBucket(s, b.ProjectID) p, err := GetProjectSimpleByID(s, b.ProjectID)
if err != nil { if err != nil {
return return
} }
defaultBucketID, err := getDefaultBucketID(s, p)
if err != nil {
return err
}
// Remove all associations of tasks to that bucket // Remove all associations of tasks to that bucket
_, err = s. _, err = s.
Where("bucket_id = ?", b.ID). Where("bucket_id = ?", b.ID).
Cols("bucket_id"). Cols("bucket_id").
Update(&Task{BucketID: defaultBucket.ID}) Update(&Task{BucketID: defaultBucketID})
return return
} }

View File

@ -629,17 +629,18 @@ func checkBucketLimit(s *xorm.Session, t *Task, bucket *Bucket) (err error) {
} }
// Contains all the task logic to figure out what bucket to use for this task. // 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) (targetBucket *Bucket, err error) { func setTaskBucket(s *xorm.Session, task *Task, originalTask *Task, doCheckBucketLimit bool, project *Project) (targetBucket *Bucket, err error) {
// Make sure we have a bucket
var bucket *Bucket if project == nil {
if task.Done && originalTask != nil && !originalTask.Done { project, err = GetProjectSimpleByID(s, task.ProjectID)
bucket, err := getDoneBucketForProject(s, task.ProjectID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if bucket != nil { }
task.BucketID = bucket.ID
} var bucket *Bucket
if task.Done && originalTask != nil && !originalTask.Done {
task.BucketID = project.DoneBucketID
} }
if task.BucketID == 0 && originalTask != nil && originalTask.BucketID != 0 { if task.BucketID == 0 && originalTask != nil && originalTask.BucketID != 0 {
@ -648,11 +649,10 @@ func setTaskBucket(s *xorm.Session, task *Task, originalTask *Task, doCheckBucke
// Either no bucket was provided or the task was moved between projects // Either no bucket was provided or the task was moved between projects
if task.BucketID == 0 || (originalTask != nil && task.ProjectID != 0 && originalTask.ProjectID != task.ProjectID) { if task.BucketID == 0 || (originalTask != nil && task.ProjectID != 0 && originalTask.ProjectID != task.ProjectID) {
bucket, err = getDefaultBucket(s, task.ProjectID) task.BucketID, err = getDefaultBucketID(s, project)
if err != nil { if err != nil {
return return
} }
task.BucketID = bucket.ID
} }
if bucket == nil { if bucket == nil {
@ -676,11 +676,6 @@ func setTaskBucket(s *xorm.Session, task *Task, originalTask *Task, doCheckBucke
} }
} }
project, err := GetProjectSimpleByID(s, task.ProjectID)
if err != nil {
return nil, err
}
if bucket.ID == project.DoneBucketID && originalTask != nil && !originalTask.Done { if bucket.ID == project.DoneBucketID && originalTask != nil && !originalTask.Done {
task.Done = true task.Done = true
} }
@ -737,7 +732,7 @@ func createTask(s *xorm.Session, t *Task, a web.Auth, updateAssignees bool) (err
} }
// Check if the project exists // Check if the project exists
l, err := GetProjectSimpleByID(s, t.ProjectID) p, err := GetProjectSimpleByID(s, t.ProjectID)
if err != nil { if err != nil {
return err return err
} }
@ -754,7 +749,7 @@ func createTask(s *xorm.Session, t *Task, a web.Auth, updateAssignees bool) (err
} }
// Get the default bucket and move the task there // Get the default bucket and move the task there
_, err = setTaskBucket(s, t, nil, true) _, err = setTaskBucket(s, t, nil, true, nil)
if err != nil { if err != nil {
return return
} }
@ -786,7 +781,7 @@ func createTask(s *xorm.Session, t *Task, a web.Auth, updateAssignees bool) (err
return err return err
} }
t.setIdentifier(l) t.setIdentifier(p)
if t.IsFavorite { if t.IsFavorite {
if err := addToFavorites(s, t.ID, createdBy, FavoriteKindTask); err != nil { if err := addToFavorites(s, t.ID, createdBy, FavoriteKindTask); err != nil {
@ -843,7 +838,7 @@ func (t *Task) Update(s *xorm.Session, a web.Auth) (err error) {
// Old task has the stored reminders // Old task has the stored reminders
ot.Reminders = reminders ot.Reminders = reminders
targetBucket, err := setTaskBucket(s, t, &ot, t.BucketID != 0 && t.BucketID != ot.BucketID) targetBucket, err := setTaskBucket(s, t, &ot, t.BucketID != 0 && t.BucketID != ot.BucketID, nil)
if err != nil { if err != nil {
return err return err
} }

View File

@ -170,6 +170,23 @@ func TestTask_Create(t *testing.T) {
assert.Error(t, err) assert.Error(t, err)
assert.True(t, IsErrBucketLimitExceeded(err)) assert.True(t, IsErrBucketLimitExceeded(err))
}) })
t.Run("default bucket different", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
task := &Task{
Title: "Lorem",
Description: "Lorem Ipsum Dolor",
ProjectID: 6,
}
err := task.Create(s, usr)
assert.NoError(t, err)
db.AssertExists(t, "tasks", map[string]interface{}{
"id": task.ID,
"bucket_id": 22, // default bucket of project 6 but with a position of 2
}, false)
})
} }
func TestTask_Update(t *testing.T) { func TestTask_Update(t *testing.T) {