fix(tasks): recalculate position of all tasks in a list or bucket when it would hit 0
continuous-integration/drone/push Build is passing Details

This commit is contained in:
kolaente 2023-02-23 16:00:36 +01:00
parent 6908167fb2
commit 1efa1696bf
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 68 additions and 1 deletions

View File

@ -140,7 +140,7 @@ type TaskWithComments struct {
}
// TableName returns the table name for listtasks
func (Task) TableName() string {
func (*Task) TableName() string {
return "tasks"
}
@ -1207,6 +1207,21 @@ func (t *Task) Update(s *xorm.Session, a web.Auth) (err error) {
if err != nil {
return err
}
// Update all positions if the newly saved position is < 0.1
if ot.Position < 0.1 {
err = recalculateTaskPositions(s, t.ListID)
if err != nil {
return err
}
}
if ot.KanbanPosition < 0.1 {
err = recalculateTaskKanbanPositions(s, t.BucketID)
if err != nil {
return err
}
}
// Get the task updated timestamp in a new struct - if we'd just try to put it into t which we already have, it
// would still contain the old updated date.
nt := &Task{}
@ -1215,6 +1230,8 @@ func (t *Task) Update(s *xorm.Session, a web.Auth) (err error) {
return err
}
t.Updated = nt.Updated
t.Position = nt.Position
t.KanbanPosition = nt.KanbanPosition
doer, _ := user.GetFromAuth(a)
err = events.Dispatch(&TaskUpdatedEvent{
@ -1228,6 +1245,56 @@ func (t *Task) Update(s *xorm.Session, a web.Auth) (err error) {
return updateListLastUpdated(s, &List{ID: t.ListID})
}
func recalculateTaskKanbanPositions(s *xorm.Session, bucketID int64) (err error) {
allTasks := []*Task{}
err = s.Where("bucket_id = ?", bucketID).Find(&allTasks)
if err != nil {
return
}
maxPosition := math.Pow(2, 32)
for i, task := range allTasks {
currentPosition := maxPosition / float64(len(allTasks)) * (float64(i + 1))
_, err = s.Cols("kanban_position").
Where("id = ?", task.ID).
Update(&Task{KanbanPosition: currentPosition})
if err != nil {
return
}
}
return
}
func recalculateTaskPositions(s *xorm.Session, listID int64) (err error) {
allTasks := []*Task{}
err = s.Where("list_id = ?", listID).Find(&allTasks)
if err != nil {
return
}
maxPosition := math.Pow(2, 32)
for i, task := range allTasks {
currentPosition := maxPosition / float64(len(allTasks)) * (float64(i + 1))
_, err = s.Cols("position").
Where("id = ?", task.ID).
Update(&Task{Position: currentPosition})
if err != nil {
return
}
}
return
}
func addOneMonthToDate(d time.Time) time.Time {
return time.Date(d.Year(), d.Month()+1, d.Day(), d.Hour(), d.Minute(), d.Second(), d.Nanosecond(), config.GetTimeZone())
}