fix double task with same id issue

This commit is contained in:
Erik Winter 2021-08-20 07:38:57 +02:00
parent a4b0f89901
commit 1448ea627b
2 changed files with 43 additions and 3 deletions

View File

@ -44,6 +44,22 @@ func (inbox *Inbox) Process() (*InboxResult, error) {
return &InboxResult{}, fmt.Errorf("%w: %v", ErrInboxProcess, err) return &InboxResult{}, fmt.Errorf("%w: %v", ErrInboxProcess, err)
} }
// deduplicate
taskKeys := map[string]*task.Task{}
for _, newT := range tasks {
existingT, ok := taskKeys[newT.Id]
switch {
case !ok:
taskKeys[newT.Id] = newT
case newT.Version >= existingT.Version:
taskKeys[newT.Id] = newT
}
}
tasks = []*task.Task{}
for _, t := range taskKeys {
tasks = append(tasks, t)
}
// split them // split them
doneTasks, updateTasks := []*task.Task{}, []*task.Task{} doneTasks, updateTasks := []*task.Task{}, []*task.Task{}
for _, t := range tasks { for _, t := range tasks {

View File

@ -35,15 +35,15 @@ func TestInboxProcess(t *testing.T) {
}, },
{ {
Subject: "to recurring", Subject: "to recurring",
Body: "recur: 2021-05-14, daily\nid: xxx-xxx\nversion: 1", Body: "recur: 2021-05-14, daily\nid: xxx-xxx-a\nversion: 1",
}, },
{ {
Subject: "to planned", Subject: "to planned",
Body: "due: 2021-05-14\nid: xxx-xxx\nversion: 1", Body: "due: 2021-05-14\nid: xxx-xxx-b\nversion: 1",
}, },
{ {
Subject: "to unplanned", Subject: "to unplanned",
Body: "id: xxx-xxx\nversion: 1", Body: "id: xxx-xxx-c\nversion: 1",
}, },
}, },
}, },
@ -110,6 +110,30 @@ func TestInboxProcess(t *testing.T) {
task.FOLDER_UNPLANNED: {}, task.FOLDER_UNPLANNED: {},
}, },
}, },
{
name: "deduplicate",
messages: map[string][]*mstore.Message{
task.FOLDER_INBOX: {
{
Subject: "version 2",
Body: "id: xxx-xxx\nversion: 1\n",
},
{
Subject: "version 2b",
Body: "id: xxx-xxx\nversion: 1\n",
},
},
task.FOLDER_UNPLANNED: {{
Subject: "the task",
Body: "id: xxx-xxx\nversion: 1\n",
}},
},
expCount: 1,
expMsgs: map[string][]*mstore.Message{
task.FOLDER_INBOX: {},
task.FOLDER_UNPLANNED: {{Subject: "version 2b"}},
},
},
} { } {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
mstorer, err := mstore.NewMemory([]string{ mstorer, err := mstore.NewMemory([]string{