Assignees optimizations (#47)
This commit is contained in:
@ -143,73 +143,3 @@ func updateDone(oldTask *ListTask, newTask *ListTask) {
|
||||
newTask.Done = false
|
||||
}
|
||||
}
|
||||
|
||||
// Create a bunch of task assignees
|
||||
func (t *ListTask) updateTaskAssignees(assignees []*User) (err error) {
|
||||
|
||||
// Get old assignees to delete
|
||||
var found bool
|
||||
var assigneesToDelete []int64
|
||||
for _, oldAssignee := range t.Assignees {
|
||||
found = false
|
||||
for _, newAssignee := range assignees {
|
||||
if newAssignee.ID == oldAssignee.ID {
|
||||
found = true // If a new assignee is already in the list with old assignees
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Put all assignees which are only on the old list to the trash
|
||||
if !found {
|
||||
assigneesToDelete = append(assigneesToDelete, oldAssignee.ID)
|
||||
}
|
||||
}
|
||||
|
||||
// Delete all assignees not passed
|
||||
if len(assigneesToDelete) > 0 {
|
||||
_, err = x.In("user_id", assigneesToDelete).
|
||||
And("task_id = ?", t.ID).
|
||||
Delete(ListTaskAssginee{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Get the list to perform later checks
|
||||
list := List{ID: t.ListID}
|
||||
err = list.ReadOne()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Loop through our users and add them
|
||||
AddNewAssignee:
|
||||
for _, u := range assignees {
|
||||
// Check if the user is already assigned and assign him only if not
|
||||
for _, oldAssignee := range t.Assignees {
|
||||
if oldAssignee.ID == u.ID {
|
||||
// continue outer loop
|
||||
continue AddNewAssignee
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the user exists and has access to the list
|
||||
newAssignee, err := GetUserByID(u.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !list.CanRead(&newAssignee) {
|
||||
return ErrUserDoesNotHaveAccessToList{list.ID, u.ID}
|
||||
}
|
||||
|
||||
_, err = x.Insert(ListTaskAssginee{
|
||||
TaskID: t.ID,
|
||||
UserID: u.ID,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user