2024-10-07 11:11:18 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"go-mod.ewintr.nl/planner/item"
|
|
|
|
"go-mod.ewintr.nl/planner/plan/storage"
|
|
|
|
"go-mod.ewintr.nl/planner/sync/client"
|
|
|
|
)
|
|
|
|
|
2024-10-29 07:22:04 +01:00
|
|
|
type Sync struct {
|
|
|
|
client client.Client
|
|
|
|
syncRepo storage.Sync
|
|
|
|
localIDRepo storage.LocalID
|
|
|
|
eventRepo storage.Event
|
2024-10-07 11:11:18 +02:00
|
|
|
}
|
|
|
|
|
2024-10-29 07:22:04 +01:00
|
|
|
func NewSync(client client.Client, syncRepo storage.Sync, localIDRepo storage.LocalID, eventRepo storage.Event) Command {
|
|
|
|
return &Sync{
|
|
|
|
client: client,
|
|
|
|
syncRepo: syncRepo,
|
|
|
|
localIDRepo: localIDRepo,
|
|
|
|
eventRepo: eventRepo,
|
2024-10-07 11:11:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-29 07:22:04 +01:00
|
|
|
func (sync *Sync) Execute(main []string, flags map[string]string) error {
|
|
|
|
if len(main) == 0 || main[0] != "sync" {
|
|
|
|
return ErrWrongCommand
|
|
|
|
}
|
|
|
|
|
|
|
|
return sync.do()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sync *Sync) do() error {
|
2024-10-07 11:11:18 +02:00
|
|
|
// local new and updated
|
2024-10-29 07:22:04 +01:00
|
|
|
sendItems, err := sync.syncRepo.FindAll()
|
2024-10-07 11:11:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get updated items: %v", err)
|
|
|
|
}
|
2024-10-29 07:22:04 +01:00
|
|
|
if err := sync.client.Update(sendItems); err != nil {
|
2024-10-07 11:11:18 +02:00
|
|
|
return fmt.Errorf("could not send updated items: %v", err)
|
|
|
|
}
|
2024-10-29 07:22:04 +01:00
|
|
|
if err := sync.syncRepo.DeleteAll(); err != nil {
|
2024-10-07 11:11:18 +02:00
|
|
|
return fmt.Errorf("could not clear updated items: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get new/updated items
|
2024-10-29 07:22:04 +01:00
|
|
|
ts, err := sync.syncRepo.LastUpdate()
|
2024-10-07 11:11:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not find timestamp of last update: %v", err)
|
|
|
|
}
|
2024-10-29 07:22:04 +01:00
|
|
|
recItems, err := sync.client.Updated([]item.Kind{item.KindEvent}, ts)
|
2024-10-07 11:11:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not receive updates: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
updated := make([]item.Item, 0)
|
|
|
|
for _, ri := range recItems {
|
|
|
|
if ri.Deleted {
|
2024-10-29 07:22:04 +01:00
|
|
|
if err := sync.localIDRepo.Delete(ri.ID); err != nil && !errors.Is(err, storage.ErrNotFound) {
|
2024-10-07 11:11:18 +02:00
|
|
|
return fmt.Errorf("could not delete local id: %v", err)
|
|
|
|
}
|
2024-10-29 07:22:04 +01:00
|
|
|
if err := sync.eventRepo.Delete(ri.ID); err != nil && !errors.Is(err, storage.ErrNotFound) {
|
2024-10-07 11:11:18 +02:00
|
|
|
return fmt.Errorf("could not delete event: %v", err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
updated = append(updated, ri)
|
|
|
|
}
|
|
|
|
|
2024-10-29 07:22:04 +01:00
|
|
|
lidMap, err := sync.localIDRepo.FindAll()
|
2024-10-07 11:11:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get local ids: %v", err)
|
|
|
|
}
|
|
|
|
for _, u := range updated {
|
|
|
|
var eBody item.EventBody
|
|
|
|
if err := json.Unmarshal([]byte(u.Body), &eBody); err != nil {
|
|
|
|
return fmt.Errorf("could not unmarshal event body: %v", err)
|
|
|
|
}
|
|
|
|
e := item.Event{
|
|
|
|
ID: u.ID,
|
|
|
|
EventBody: eBody,
|
|
|
|
}
|
2024-10-29 07:22:04 +01:00
|
|
|
if err := sync.eventRepo.Store(e); err != nil {
|
2024-10-07 11:11:18 +02:00
|
|
|
return fmt.Errorf("could not store event: %v", err)
|
|
|
|
}
|
|
|
|
lid, ok := lidMap[u.ID]
|
|
|
|
if !ok {
|
2024-10-29 07:22:04 +01:00
|
|
|
lid, err = sync.localIDRepo.Next()
|
2024-10-07 11:11:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get next local id: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-10-29 07:22:04 +01:00
|
|
|
if err := sync.localIDRepo.Store(u.ID, lid); err != nil {
|
2024-10-07 11:11:18 +02:00
|
|
|
return fmt.Errorf("could not store local id: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|