Fixed a bug where adding assignees or reminders via an update would re-create them and not respect already inserted ones, leaving a lot of garbage

This commit is contained in:
kolaente 2019-11-03 23:08:26 +01:00
parent 9be5ab248c
commit c203d73b33
Signed by untrusted user: konrad
GPG Key ID: F40E70337AB24C9B
2 changed files with 30 additions and 3 deletions

View File

@ -43,7 +43,7 @@ type TaskAssigneeWithUser struct {
}
func getRawTaskAssigneesForTasks(taskIDs []int64) (taskAssignees []*TaskAssigneeWithUser, err error) {
taskAssignees = []*TaskAssigneeWithUser{nil}
taskAssignees = []*TaskAssigneeWithUser{}
err = x.Table("task_assignees").
Select("task_id, users.*").
In("task_id", taskIDs).
@ -55,6 +55,17 @@ func getRawTaskAssigneesForTasks(taskIDs []int64) (taskAssignees []*TaskAssignee
// Create or update a bunch of task assignees
func (t *Task) updateTaskAssignees(assignees []*User) (err error) {
// Load the current assignees
currentAssignees, err := getRawTaskAssigneesForTasks([]int64{t.ID})
if err != nil {
return err
}
t.Assignees = make([]*User, 0, len(currentAssignees))
for _, assignee := range currentAssignees {
t.Assignees = append(t.Assignees, &assignee.User)
}
// If we don't have any new assignees, delete everything right away. Saves us some hassle.
if len(assignees) == 0 && len(t.Assignees) > 0 {
_, err = x.Where("task_id = ?", t.ID).

View File

@ -369,6 +369,12 @@ func GetTasksByUIDs(uids []string) (tasks []*Task, err error) {
return
}
func getRemindersForTasks(taskIDs []int64) (reminders []*TaskReminder, err error) {
reminders = []*TaskReminder{}
err = x.Table("task_reminders").In("task_id", taskIDs).Find(&reminders)
return
}
// This function takes a map with pointers and returns a slice with pointers to tasks
// It adds more stuff like assignees/labels/etc to a bunch of tasks
func addMoreInfoToTasks(taskMap map[int64]*Task) (tasks []*Task, err error) {
@ -456,8 +462,7 @@ func addMoreInfoToTasks(taskMap map[int64]*Task) (tasks []*Task, err error) {
}
// Get all reminders and put them in a map to have it easier later
reminders := []*TaskReminder{}
err = x.Table("task_reminders").In("task_id", taskIDs).Find(&reminders)
reminders, err := getRemindersForTasks(taskIDs)
if err != nil {
return
}
@ -730,6 +735,17 @@ func updateDone(oldTask *Task, newTask *Task) {
// The parameter is a slice with unix dates which holds the new reminders.
func (t *Task) updateReminders(reminders []int64) (err error) {
// Load the current reminders
taskReminders, err := getRemindersForTasks([]int64{t.ID})
if err != nil {
return err
}
t.RemindersUnix = make([]int64, 0, len(taskReminders))
for _, reminder := range taskReminders {
t.RemindersUnix = append(t.RemindersUnix, reminder.ReminderUnix)
}
// If we're removing everything, delete all reminders right away
if len(reminders) == 0 && len(t.RemindersUnix) > 0 {
_, err = x.Where("task_id = ?", t.ID).