From 4d9bbda1c1bbd57f387e2b24c0a2d3fe002253b9 Mon Sep 17 00:00:00 2001 From: Dennis Kleber Date: Wed, 6 Mar 2024 15:40:42 +0100 Subject: [PATCH 1/5] changes --- dao/taking.go | 2 -- dao/update_ticker.go | 21 +++++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/dao/taking.go b/dao/taking.go index 842623e..56cd05d 100644 --- a/dao/taking.go +++ b/dao/taking.go @@ -136,10 +136,8 @@ func TakingUpdate(ctx context.Context, i *models.TakingUpdate, token *vcapool.Ac if err = IDjango.Post(e, "/v1/pool/event/update/"); err != nil { log.Print(err) } - // Add participations to event participations := new([]models.Participation) - if err = ParticipationCollection.Aggregate( ctx, models.ParticipationPipeline().Match(bson.D{{Key: "event_id", Value: e.ID}}).Pipe, diff --git a/dao/update_ticker.go b/dao/update_ticker.go index 1fafdaa..0d2e3e9 100644 --- a/dao/update_ticker.go +++ b/dao/update_ticker.go @@ -53,6 +53,27 @@ func EventStateNoIncome() { if err := TakingCollection.UpdateOne(context.Background(), updateFilter, vmdb.UpdateSet(update), nil); err != nil { log.Print(err) } + if err := IDjango.Post(i, "/v1/pool/taking/create/"); err != nil { + log.Print(err) + } + + // Update CRM event + if err := IDjango.Post(e, "/v1/pool/event/update/"); err != nil { + log.Print(err) + } + // Add participations to event + participations := new([]models.Participation) + if err := ParticipationCollection.Aggregate( + context.Background(), + models.ParticipationPipeline().Match(bson.D{{Key: "event_id", Value: e.ID}}).Pipe, + participations, + ); err != nil { + return + } + if err := IDjango.Post(participations, "/v1/pool/participations/create/"); err != nil { + log.Print(err) + err = nil + } } } From 2330f13c5e093288b44dac52786a1133ecde7b45 Mon Sep 17 00:00:00 2001 From: Dennis Kleber Date: Thu, 13 Jun 2024 11:31:22 +0200 Subject: [PATCH 2/5] add EventStateClose to the Ticker. Clean TakingUpdate dao --- dao/taking.go | 61 -------------------------------------------- dao/update_ticker.go | 35 +++++++++++++++---------- 2 files changed, 22 insertions(+), 74 deletions(-) diff --git a/dao/taking.go b/dao/taking.go index 56cd05d..1196be0 100644 --- a/dao/taking.go +++ b/dao/taking.go @@ -2,9 +2,7 @@ package dao import ( "context" - "log" "pool-backend/models" - "time" "github.com/Viva-con-Agua/vcago/vmdb" "github.com/Viva-con-Agua/vcapool" @@ -94,65 +92,6 @@ func TakingUpdate(ctx context.Context, i *models.TakingUpdate, token *vcapool.Ac } } } - if !takingDatabase.State.NoIncome && i.State.NoIncome { - event := new(models.EventUpdate) - if err = EventCollection.FindOne( - ctx, - bson.D{{Key: "taking_id", Value: takingDatabase.ID}}, - event, - ); err != nil { - if !vmdb.ErrNoDocuments(err) { - return - } - err = nil - } - - if event.ID != "" && event.EndAt < time.Now().Unix() { - event.EventState.OldState = event.EventState.State - event.EventState.State = "closed" - e := new(models.Event) - if err = EventCollection.UpdateOneAggregate( - ctx, - event.Match(), - vmdb.UpdateSet(event), - e, - models.EventPipeline(token).Match(event.Match()).Pipe, - ); err != nil { - return - } - - // Add takings to CRM - var taking *models.Taking - if taking, err = TakingGetByID(ctx, &models.TakingParam{ID: takingDatabase.ID}, token); err != nil { - log.Print(err) - } - - taking.EditorID = token.ID - if err = IDjango.Post(taking, "/v1/pool/taking/create/"); err != nil { - log.Print(err) - } - - // Update CRM event - if err = IDjango.Post(e, "/v1/pool/event/update/"); err != nil { - log.Print(err) - } - // Add participations to event - participations := new([]models.Participation) - if err = ParticipationCollection.Aggregate( - ctx, - models.ParticipationPipeline().Match(bson.D{{Key: "event_id", Value: e.ID}}).Pipe, - participations, - ); err != nil { - return - } - - if err = IDjango.Post(participations, "/v1/pool/participations/create/"); err != nil { - log.Print(err) - err = nil - } - - } - } if err = ActivityCollection.InsertOne(ctx, TakingUpdatedActivity.New(token.ID, takingDatabase.ID)); err != nil { return } diff --git a/dao/update_ticker.go b/dao/update_ticker.go index 0d2e3e9..6fbbc0e 100644 --- a/dao/update_ticker.go +++ b/dao/update_ticker.go @@ -14,13 +14,13 @@ import ( func UpdateTicker() { ticker := time.NewTicker(1 * time.Hour) quit := make(chan struct{}) - EventStateUpdateTicker() + EventStateFinishTicker() go func() { for { select { case <-ticker.C: - EventStateUpdateTicker() - EventStateNoIncome() + EventStateFinishTicker() + EventStateClosed() case <-quit: ticker.Stop() return @@ -29,7 +29,8 @@ func UpdateTicker() { }() } -func EventStateUpdateTicker() { +// EventStateFinishTicker sets all published events that are already over to finished +func EventStateFinishTicker() { filter := vmdb.NewFilter() filter.EqualString("event_state.state", "published") filter.LteInt64("end_at", fmt.Sprint(time.Now().Unix())) @@ -38,11 +39,21 @@ func EventStateUpdateTicker() { log.Print(err) } } -func EventStateNoIncome() { + +// EventStateClosed +func EventStateClosed() { filter := vmdb.NewFilter() - filter.EqualBool("no_income", "true") + confirmedFilter := bson.E{Key: "$or", Value: bson.A{ + bson.D{ + {Key: "state.confirmed.amount", Value: bson.D{{Key: "$gte", Value: 1}}}, + {Key: "state.wait.amount", Value: 0}, + {Key: "state.open.amount", Value: 0}, + }, + bson.D{{Key: "state.no_income", Value: true}}, + }} + filter.Append(bson.E{Key: "$or", Value: confirmedFilter}) filter.EqualString("event.event_state.state", "finished") - pipeline := models.TakingPipelineTicker().Match(filter.Bson()).Pipe + pipeline := models.TakingPipeline().Match(filter.Bson()).Pipe takings := []models.Taking{} if err := TakingCollection.Aggregate(context.Background(), pipeline, takings); err != nil { log.Print(err) @@ -50,29 +61,27 @@ func EventStateNoIncome() { 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 { + e := new(models.Event) + if err := EventCollection.UpdateOne(context.Background(), updateFilter, vmdb.UpdateSet(update), e); err != nil { log.Print(err) } if err := IDjango.Post(i, "/v1/pool/taking/create/"); err != nil { log.Print(err) } - - // Update CRM event if err := IDjango.Post(e, "/v1/pool/event/update/"); err != nil { log.Print(err) } - // Add participations to event participations := new([]models.Participation) if err := ParticipationCollection.Aggregate( context.Background(), models.ParticipationPipeline().Match(bson.D{{Key: "event_id", Value: e.ID}}).Pipe, participations, ); err != nil { - return + log.Print(err) } + if err := IDjango.Post(participations, "/v1/pool/participations/create/"); err != nil { log.Print(err) - err = nil } } From 9d72b400b443fa0d1c75d6abc441ec5e25727df7 Mon Sep 17 00:00:00 2001 From: Dennis Kleber Date: Thu, 13 Jun 2024 11:34:48 +0200 Subject: [PATCH 3/5] fix --- dao/update_ticker.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dao/update_ticker.go b/dao/update_ticker.go index 6fbbc0e..e70b9fc 100644 --- a/dao/update_ticker.go +++ b/dao/update_ticker.go @@ -15,12 +15,13 @@ func UpdateTicker() { ticker := time.NewTicker(1 * time.Hour) quit := make(chan struct{}) EventStateFinishTicker() + EventStateClosedTicker() go func() { for { select { case <-ticker.C: EventStateFinishTicker() - EventStateClosed() + EventStateClosedTicker() case <-quit: ticker.Stop() return @@ -41,7 +42,7 @@ func EventStateFinishTicker() { } // EventStateClosed -func EventStateClosed() { +func EventStateClosedTicker() { filter := vmdb.NewFilter() confirmedFilter := bson.E{Key: "$or", Value: bson.A{ bson.D{ From e13f83c25c26c12fc2f7bd6737b472f2cf85becd Mon Sep 17 00:00:00 2001 From: Dennis Kleber Date: Fri, 21 Jun 2024 11:26:35 +0200 Subject: [PATCH 4/5] When the name or email of a crew is updated, all user_crew models are also updated --- dao/crew.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/dao/crew.go b/dao/crew.go index 50ab8db..a65b8f4 100644 --- a/dao/crew.go +++ b/dao/crew.go @@ -97,6 +97,14 @@ func CrewUpdate(ctx context.Context, i *models.CrewUpdate, token *vcapool.Access return } } + if crew.Email != i.Email || crew.Name != i.Name { + filter := bson.D{{Key: "crew_id", Value: i.ID}} + update := bson.D{{Key: "email", Value: i.Email}, {Key: "name", Value: i.Name}} + if err = UserCrewCollection.UpdateMany(ctx, filter, vmdb.UpdateSet(update)); err != nil { + return + } + + } return } From 96237ca1fa4357946c38b7c59aa6b19435b4374e Mon Sep 17 00:00:00 2001 From: Dennis Kleber Date: Fri, 21 Jun 2024 14:24:04 +0200 Subject: [PATCH 5/5] solves #173 --- dao/users.go | 9 ++++++--- go.mod | 2 +- go.sum | 4 ++-- handlers/token/users.go | 2 +- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/dao/users.go b/dao/users.go index a97f9d2..f76650b 100644 --- a/dao/users.go +++ b/dao/users.go @@ -34,16 +34,19 @@ func UserInsert(ctx context.Context, i *models.UserDatabase) (result *models.Use return } -func UsersGet(ctx context.Context, i *models.UserQuery, token *vcapool.AccessToken) (result *[]models.ListUser, list_size int64, err error) { +func UsersGet(i *models.UserQuery, token *vcapool.AccessToken) (result *[]models.ListUser, list_size int64, err error) { if err = models.UsersPermission(token); err != nil { return } - ctx = context.Background() + ctx := context.Background() filter := i.PermittedFilter(token) sort := i.Sort() + opt := options.Aggregate() + opt.SetCollation(&options.Collation{Locale: "en_US", NumericOrdering: true}) + pipeline := models.SortedUserPermittedPipeline(token).SortFields(sort).Match(filter).Sort(sort).Skip(i.Skip, 0).Limit(i.Limit, 100).Pipe result = new([]models.ListUser) - if err = UserCollection.Aggregate(ctx, pipeline, result); err != nil { + if err = UserCollection.Aggregate(ctx, pipeline, result, opt); err != nil { return } opts := options.Count().SetHint("_id_") diff --git a/go.mod b/go.mod index c8ecae6..f81ea76 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module pool-backend go 1.17 require ( - github.com/Viva-con-Agua/vcago v1.4.42 + github.com/Viva-con-Agua/vcago v1.5.5 github.com/Viva-con-Agua/vcapool v0.3.4 github.com/google/uuid v1.3.0 github.com/labstack/echo/v4 v4.9.0 diff --git a/go.sum b/go.sum index d836145..ce118d4 100644 --- a/go.sum +++ b/go.sum @@ -39,8 +39,8 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/Viva-con-Agua/vcago v1.4.16/go.mod h1:kxaxW9VYWIaCdWoM+PLHNwXmtPj4FPYHPxmPK6JsA1U= -github.com/Viva-con-Agua/vcago v1.4.42 h1:+UxsScecaZqEYTNbrUPHxc4Rn/wFJEayxDAjcJjnphU= -github.com/Viva-con-Agua/vcago v1.4.42/go.mod h1:uJ7sBZk8IOEJbHgZ97uY+WE6TLMGkhiPvey3CxFOs4A= +github.com/Viva-con-Agua/vcago v1.5.5 h1:bnxyyVpuLoLHwmWOdJEqwF+3x4PEek3do8j0BSUFQ9I= +github.com/Viva-con-Agua/vcago v1.5.5/go.mod h1:uJ7sBZk8IOEJbHgZ97uY+WE6TLMGkhiPvey3CxFOs4A= github.com/Viva-con-Agua/vcapool v0.3.4 h1:I1IGg7BTfXihMkTwLu+qYygcvfX9zZVMZw9+0B9ZJMs= github.com/Viva-con-Agua/vcapool v0.3.4/go.mod h1:yTwmyj5DbsDduM23aEbK48BW0ONg9092hbmm1HhMso4= github.com/acobaugh/osrelease v0.0.0-20181218015638-a93a0a55a249/go.mod h1:iU1PxQMQwoHZZWmMKrMkrNlY+3+p9vxIjpZOVyxWa0g= diff --git a/handlers/token/users.go b/handlers/token/users.go index 09cec9d..e08e1dc 100644 --- a/handlers/token/users.go +++ b/handlers/token/users.go @@ -37,7 +37,7 @@ func (i *UserHandler) Get(cc echo.Context) (err error) { } result := new([]models.ListUser) var listSize int64 - if result, listSize, err = dao.UsersGet(c.Ctx(), body, token); err != nil { + if result, listSize, err = dao.UsersGet(body, token); err != nil { return } return c.Listed(result, listSize)