From c3e00455c995d87872cc13ee8de9a76f2c7dc7bf Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Mon, 9 Dec 2024 08:44:19 +0100 Subject: [PATCH] wip --- item/recur.go | 11 +++++++++++ sync/service/recur.go | 42 ++++++++++++++++++++++++------------------ 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/item/recur.go b/item/recur.go index 02b371b..545ffcb 100644 --- a/item/recur.go +++ b/item/recur.go @@ -69,6 +69,17 @@ func (r *Recur) onMonths(date time.Time) bool { } } +func (r *Recur) NextAfter(old time.Time) time.Time { + day, _ := time.ParseDuration("1d") + test := old.Add(day) + for { + if r.On(test) || test.After(time.Date(2500, 1, 1, 0, 0, 0, 0, time.UTC)) { + return test + } + test.Add(day) + } +} + func (r *Recur) Valid() bool { return r.Start.IsZero() || !slices.Contains(ValidPeriods, r.Period) } diff --git a/sync/service/recur.go b/sync/service/recur.go index f01de48..c69d10c 100644 --- a/sync/service/recur.go +++ b/sync/service/recur.go @@ -8,14 +8,16 @@ import ( ) type Recur struct { - repo Recurrer - logger *slog.Logger + repoSync Syncer + repoRecur Recurrer + logger *slog.Logger } -func NewRecur(repo Recurrer, interval time.Duration, logger *slog.Logger) *Recur { +func NewRecur(repoRecur Recurrer, repoSync Syncer, interval time.Duration, logger *slog.Logger) *Recur { r := &Recur{ - repo: repo, - logger: logger, + repoRecur: repoRecur, + repoSync: repoSync, + logger: logger, } go r.run(interval) @@ -33,26 +35,30 @@ func (r *Recur) run(interval time.Duration) { } func (r *Recur) recur() error { - items, err := r.repo.RecursBefore(time.Now()) + items, err := r.repoRecur.RecursBefore(time.Now()) if err != nil { return err } for _, i := range items { - if err := r.repo.RecursNext(i.ID, NewNext(i.Recurrer, i.RecurNext)); err != nil { + // spawn instance + ne, err := item.NewEvent(i) + if err != nil { + return err + } + ne.Start = i.RecurNext // TODO something with time + ni, err := ne.Item() + if err != nil { + return err + } + if err := r.repoSync.Update(ni); err != nil { + return err + } + + // set next + if err := r.repoRecur.RecursNext(i.ID, i.Recurrer.NextAfter(i.RecurNext)); err != nil { return err } } return nil } - -func NewNext(r *item.Recur, old time.Time) time.Time { - day, _ := time.ParseDuration("1d") - test := old.Add(day) - for { - if r.On(test) || test.After(time.Date(2500, 1, 1, 0, 0, 0, 0, time.UTC)) { - return test - } - test.Add(day) - } -}