mark done in inbox
This commit is contained in:
parent
e564ba32e5
commit
1a8f72e364
|
@ -38,13 +38,30 @@ func (inbox *Inbox) Process() (*InboxResult, error) {
|
|||
|
||||
start := time.Now()
|
||||
|
||||
// find tasks to be processed
|
||||
tasks, err := inbox.taskRepo.FindAll(task.FOLDER_INBOX)
|
||||
if err != nil {
|
||||
return &InboxResult{}, fmt.Errorf("%w: %v", ErrInboxProcess, err)
|
||||
}
|
||||
|
||||
var cleanupNeeded bool
|
||||
// split them
|
||||
doneTasks, updateTasks := []*task.Task{}, []*task.Task{}
|
||||
for _, t := range tasks {
|
||||
if t.Done {
|
||||
doneTasks = append(doneTasks, t)
|
||||
continue
|
||||
}
|
||||
updateTasks = append(updateTasks, t)
|
||||
}
|
||||
|
||||
// remove
|
||||
if err := inbox.taskRepo.Remove(doneTasks); err != nil {
|
||||
return &InboxResult{}, fmt.Errorf("%w: %v", ErrInboxProcess, err)
|
||||
}
|
||||
|
||||
// update
|
||||
var cleanupNeeded bool
|
||||
for _, t := range updateTasks {
|
||||
if err := inbox.taskRepo.Update(t); err != nil {
|
||||
return &InboxResult{}, fmt.Errorf("%w: %v", ErrInboxProcess, err)
|
||||
}
|
||||
|
|
|
@ -92,6 +92,24 @@ func TestInboxProcess(t *testing.T) {
|
|||
task.FOLDER_UNPLANNED: {{Subject: "not really old version"}},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "remove done",
|
||||
messages: map[string][]*mstore.Message{
|
||||
task.FOLDER_INBOX: {{
|
||||
Subject: "is done",
|
||||
Body: "id: xxx-xxx\nversion: 1\ndone: true\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: {},
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
mstorer, err := mstore.NewMemory([]string{
|
||||
|
|
|
@ -126,3 +126,33 @@ func (rr *RemoteRepository) CleanUp() error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rr *RemoteRepository) Remove(tasks []*task.Task) error {
|
||||
tMap := map[string]*task.Task{}
|
||||
for _, t := range tasks {
|
||||
tMap[t.Id] = t
|
||||
}
|
||||
|
||||
var toBeRemoved []*mstore.Message
|
||||
for _, folder := range task.KnownFolders {
|
||||
msgs, err := rr.mstore.Messages(folder)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %v", ErrMStoreError, err)
|
||||
}
|
||||
|
||||
for _, msg := range msgs {
|
||||
id, _ := task.FieldFromBody(task.FIELD_ID, msg.Body)
|
||||
if _, ok := tMap[id]; ok {
|
||||
toBeRemoved = append(toBeRemoved, msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, msg := range toBeRemoved {
|
||||
if err := rr.mstore.Remove(msg); err != nil {
|
||||
return fmt.Errorf("%w: %v", ErrMStoreError, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -135,15 +135,10 @@ func TestRepoUpdate(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestRepoCleanUp(t *testing.T) {
|
||||
folderNew := "New"
|
||||
folderPlanned := "Planned"
|
||||
folders := []string{"INBOX", folderNew, "Recurring",
|
||||
folderPlanned, "Unplanned",
|
||||
}
|
||||
id := "id"
|
||||
subject := "subject"
|
||||
|
||||
mem, err := mstore.NewMemory(folders)
|
||||
mem, err := mstore.NewMemory(task.KnownFolders)
|
||||
test.OK(t, err)
|
||||
|
||||
for v := 1; v <= 3; v++ {
|
||||
|
@ -151,9 +146,9 @@ func TestRepoCleanUp(t *testing.T) {
|
|||
id: %s
|
||||
version: %d
|
||||
`, id, v)
|
||||
folder := folderNew
|
||||
folder := task.FOLDER_NEW
|
||||
if v%2 == 1 {
|
||||
folder = folderPlanned
|
||||
folder = task.FOLDER_PLANNED
|
||||
}
|
||||
test.OK(t, mem.Add(folder, subject, body))
|
||||
}
|
||||
|
@ -162,19 +157,43 @@ version: %d
|
|||
test.OK(t, repo.CleanUp())
|
||||
|
||||
expNew := []*mstore.Message{}
|
||||
actNew, err := mem.Messages(folderNew)
|
||||
actNew, err := mem.Messages(task.FOLDER_NEW)
|
||||
test.OK(t, err)
|
||||
test.Equals(t, expNew, actNew)
|
||||
expPlanned := []*mstore.Message{{
|
||||
Uid: 3,
|
||||
Folder: folderPlanned,
|
||||
Folder: task.FOLDER_PLANNED,
|
||||
Subject: subject,
|
||||
Body: `
|
||||
id: id
|
||||
version: 3
|
||||
`,
|
||||
}}
|
||||
actPlanned, err := mem.Messages(folderPlanned)
|
||||
actPlanned, err := mem.Messages(task.FOLDER_PLANNED)
|
||||
test.OK(t, err)
|
||||
test.Equals(t, expPlanned, actPlanned)
|
||||
}
|
||||
|
||||
func TestRepoRemove(t *testing.T) {
|
||||
mem, err := mstore.NewMemory(task.KnownFolders)
|
||||
test.OK(t, err)
|
||||
|
||||
for id := 1; id <= 3; id++ {
|
||||
test.OK(t, mem.Add(task.FOLDER_PLANNED, "action", fmt.Sprintf("id: id-%d\n", id)))
|
||||
}
|
||||
remote := storage.NewRemoteRepository(mem)
|
||||
tasks := []*task.Task{
|
||||
{Id: "id-1"},
|
||||
{Id: "id-3"},
|
||||
}
|
||||
test.OK(t, remote.Remove(tasks))
|
||||
actMsgs, err := mem.Messages(task.FOLDER_PLANNED)
|
||||
expMsgs := []*mstore.Message{{
|
||||
Uid: 2,
|
||||
Folder: task.FOLDER_PLANNED,
|
||||
Subject: "action",
|
||||
Body: "id: id-2\n",
|
||||
}}
|
||||
test.Equals(t, expMsgs, actMsgs)
|
||||
|
||||
}
|
||||
|
|
|
@ -34,6 +34,7 @@ const (
|
|||
FIELD_PROJECT = "project"
|
||||
FIELD_DUE = "due"
|
||||
FIELD_RECUR = "recur"
|
||||
FIELD_DONE = "done"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -53,6 +54,7 @@ var (
|
|||
FIELD_PROJECT,
|
||||
FIELD_DUE,
|
||||
FIELD_RECUR,
|
||||
FIELD_DONE,
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -70,6 +72,7 @@ type Task struct {
|
|||
Project string
|
||||
Due Date
|
||||
Recur Recurrer
|
||||
Done bool
|
||||
}
|
||||
|
||||
func NewFromMessage(msg *mstore.Message) *Task {
|
||||
|
@ -108,6 +111,7 @@ func NewFromMessage(msg *mstore.Message) *Task {
|
|||
t.Project = bodyFields[FIELD_PROJECT]
|
||||
t.Due = NewDateFromString(bodyFields[FIELD_DUE])
|
||||
t.Recur = NewRecurrer(bodyFields[FIELD_RECUR])
|
||||
t.Done = bodyFields[FIELD_DONE] == "true"
|
||||
|
||||
return t
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ func TestNewFromMessage(t *testing.T) {
|
|||
exp: &task.Task{},
|
||||
},
|
||||
{
|
||||
name: "id, action, project and folder",
|
||||
name: "id, action, project, folder and done",
|
||||
message: &mstore.Message{
|
||||
Folder: task.FOLDER_UNPLANNED,
|
||||
Body: fmt.Sprintf(`
|
||||
|
@ -38,6 +38,7 @@ due: no date
|
|||
version: %d
|
||||
action: %s
|
||||
project: %s
|
||||
done: true
|
||||
`, id, version, action, project),
|
||||
},
|
||||
hasId: true,
|
||||
|
@ -47,6 +48,7 @@ project: %s
|
|||
Folder: task.FOLDER_UNPLANNED,
|
||||
Action: action,
|
||||
Project: project,
|
||||
Done: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue