From 0216fb72dd5e8fc814cc30934226cf93fd9a36fc Mon Sep 17 00:00:00 2001 From: Dennis Kleber Date: Wed, 20 Dec 2023 16:03:38 +0100 Subject: [PATCH 1/2] update event only if endAt is in the past --- dao/taking.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dao/taking.go b/dao/taking.go index de42eed..842623e 100644 --- a/dao/taking.go +++ b/dao/taking.go @@ -4,6 +4,7 @@ import ( "context" "log" "pool-backend/models" + "time" "github.com/Viva-con-Agua/vcago/vmdb" "github.com/Viva-con-Agua/vcapool" @@ -106,7 +107,7 @@ func TakingUpdate(ctx context.Context, i *models.TakingUpdate, token *vcapool.Ac err = nil } - if event.ID != "" { + if event.ID != "" && event.EndAt < time.Now().Unix() { event.EventState.OldState = event.EventState.State event.EventState.State = "closed" e := new(models.Event) From a7283394c061443d924f63cac24ebec802ec154a Mon Sep 17 00:00:00 2001 From: Dennis Kleber Date: Wed, 20 Dec 2023 16:20:29 +0100 Subject: [PATCH 2/2] add update ticker for eventstate in the case that an event has no income --- dao/update_ticker.go | 20 ++++++++++++++++++++ models/taking.go | 6 ++++++ 2 files changed, 26 insertions(+) diff --git a/dao/update_ticker.go b/dao/update_ticker.go index 3a3f2b8..1fafdaa 100644 --- a/dao/update_ticker.go +++ b/dao/update_ticker.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "log" + "pool-backend/models" "time" "github.com/Viva-con-Agua/vcago/vmdb" @@ -19,6 +20,7 @@ func UpdateTicker() { select { case <-ticker.C: EventStateUpdateTicker() + EventStateNoIncome() case <-quit: ticker.Stop() return @@ -36,3 +38,21 @@ func EventStateUpdateTicker() { log.Print(err) } } +func EventStateNoIncome() { + filter := vmdb.NewFilter() + filter.EqualBool("no_income", "true") + filter.EqualString("event.event_state.state", "finished") + pipeline := models.TakingPipelineTicker().Match(filter.Bson()).Pipe + takings := []models.Taking{} + if err := TakingCollection.Aggregate(context.Background(), pipeline, takings); err != nil { + log.Print(err) + } + for i := range takings { + updateFilter := bson.D{{Key: "_id", Value: takings[i].Event.ID}} + update := bson.D{{Key: "event_state.state", Value: "closed"}} + if err := TakingCollection.UpdateOne(context.Background(), updateFilter, vmdb.UpdateSet(update), nil); err != nil { + log.Print(err) + } + + } +} diff --git a/models/taking.go b/models/taking.go index 75ba198..c83ebd3 100644 --- a/models/taking.go +++ b/models/taking.go @@ -130,6 +130,12 @@ func TakingPipeline() *vmdb.Pipeline { return pipe } +func TakingPipelineTicker() *vmdb.Pipeline { + pipe := vmdb.NewPipeline() + pipe.LookupUnwind(EventCollection, "_id", "taking_id", "event") + return pipe +} + func (i *TakingCreate) TakingDatabase() *TakingDatabase { return &TakingDatabase{ ID: uuid.NewString(),