2024-10-07 11:11:18 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2024-10-11 07:24:38 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
2024-10-07 11:11:18 +02:00
|
|
|
"github.com/urfave/cli/v2"
|
2024-10-11 07:24:38 +02:00
|
|
|
"go-mod.ewintr.nl/planner/item"
|
2024-10-07 11:11:18 +02:00
|
|
|
"go-mod.ewintr.nl/planner/plan/storage"
|
2024-10-07 11:32:53 +02:00
|
|
|
"go-mod.ewintr.nl/planner/sync/client"
|
2024-10-07 11:11:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var SyncCmd = &cli.Command{
|
|
|
|
Name: "sync",
|
2024-10-09 07:26:39 +02:00
|
|
|
Usage: "Synchronize with server",
|
2024-10-07 11:11:18 +02:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "full",
|
|
|
|
Aliases: []string{"f"},
|
|
|
|
Usage: "Force full sync",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-10-17 07:29:53 +02:00
|
|
|
func NewSyncCmd(client client.Client, syncRepo storage.Sync, localIDRepo storage.LocalID, eventRepo storage.Event) *cli.Command {
|
2024-10-07 11:11:18 +02:00
|
|
|
SyncCmd.Action = func(cCtx *cli.Context) error {
|
2024-10-11 07:24:38 +02:00
|
|
|
return Sync(client, syncRepo, localIDRepo, eventRepo, cCtx.Bool("full"))
|
2024-10-07 11:11:18 +02:00
|
|
|
}
|
|
|
|
return SyncCmd
|
|
|
|
}
|
|
|
|
|
2024-10-17 07:29:53 +02:00
|
|
|
func Sync(client client.Client, syncRepo storage.Sync, localIDRepo storage.LocalID, eventRepo storage.Event, full bool) error {
|
2024-10-22 07:26:59 +02:00
|
|
|
// local new and updated
|
2024-10-11 07:24:38 +02:00
|
|
|
sendItems, err := syncRepo.FindAll()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get updated items: %v", err)
|
|
|
|
}
|
|
|
|
if err := client.Update(sendItems); err != nil {
|
|
|
|
return fmt.Errorf("could not send updated items: %v", err)
|
|
|
|
}
|
|
|
|
if err := syncRepo.DeleteAll(); err != nil {
|
|
|
|
return fmt.Errorf("could not clear updated items: %v", err)
|
|
|
|
}
|
2024-10-07 11:32:53 +02:00
|
|
|
|
2024-10-11 07:24:38 +02:00
|
|
|
// get new/updated items
|
2024-10-22 07:26:59 +02:00
|
|
|
ts, err := syncRepo.LastUpdate()
|
|
|
|
if err != nil {
|
2024-10-23 07:36:17 +02:00
|
|
|
return fmt.Errorf("could not find timestamp of last update: %v", err)
|
2024-10-22 07:26:59 +02:00
|
|
|
}
|
|
|
|
recItems, err := client.Updated([]item.Kind{item.KindEvent}, ts)
|
2024-10-11 07:24:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not receive updates: %v", err)
|
|
|
|
}
|
2024-10-07 11:32:53 +02:00
|
|
|
|
2024-10-22 07:26:59 +02:00
|
|
|
// import to local
|
2024-10-11 07:24:38 +02:00
|
|
|
lidMap, err := localIDRepo.FindAll()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get local ids: %v", err)
|
|
|
|
}
|
|
|
|
for _, ri := range recItems {
|
|
|
|
var eBody item.EventBody
|
2024-10-18 07:26:11 +02:00
|
|
|
if err := json.Unmarshal([]byte(ri.Body), &eBody); err != nil {
|
2024-10-11 07:24:38 +02:00
|
|
|
return fmt.Errorf("could not unmarshal event body: %v", err)
|
|
|
|
}
|
|
|
|
e := item.Event{
|
|
|
|
ID: ri.ID,
|
|
|
|
EventBody: eBody,
|
|
|
|
}
|
2024-10-07 11:32:53 +02:00
|
|
|
|
2024-10-11 07:24:38 +02:00
|
|
|
if err := eventRepo.Store(e); err != nil {
|
|
|
|
return fmt.Errorf("could not store event: %v", err)
|
|
|
|
}
|
|
|
|
lid, ok := lidMap[ri.ID]
|
|
|
|
if !ok {
|
|
|
|
lid, err = localIDRepo.Next()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get next local id: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := localIDRepo.Store(ri.ID, lid); err != nil {
|
|
|
|
return fmt.Errorf("could not store local id: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2024-10-07 11:32:53 +02:00
|
|
|
|
2024-10-07 11:11:18 +02:00
|
|
|
return nil
|
|
|
|
}
|