From faa2b0a2e4e1fb8ca5ae78f2650889a95d615989 Mon Sep 17 00:00:00 2001 From: Dennis Kleber Date: Wed, 26 Jun 2024 14:26:57 +0200 Subject: [PATCH 1/3] bugfix for Takings - Delete --- dao/deposit.go | 13 +++++++------ dao/taking.go | 9 +++++---- handlers/token/role_history.go | 2 +- handlers/token/taking.go | 8 +++++--- models/profile.go | 15 ++++++++------- models/taking.go | 2 +- 6 files changed, 27 insertions(+), 22 deletions(-) diff --git a/dao/deposit.go b/dao/deposit.go index 04c47d8..4f97bc8 100644 --- a/dao/deposit.go +++ b/dao/deposit.go @@ -7,6 +7,7 @@ import ( "github.com/Viva-con-Agua/vcago" "github.com/Viva-con-Agua/vcago/vmdb" + "github.com/Viva-con-Agua/vcago/vmod" "github.com/Viva-con-Agua/vcapool" "go.mongodb.org/mongo-driver/bson" ) @@ -114,11 +115,11 @@ func DepositUpdate(ctx context.Context, i *models.DepositUpdate, token *vcapool. ctx = context.Background() if i.Status == "confirmed" { go func() { - + ctxAsync := context.Background() for _, unit := range i.DepositUnit { event := new(models.EventUpdate) if err = EventCollection.FindOne( - ctx, + ctxAsync, bson.D{{Key: "taking_id", Value: unit.TakingID}}, event, ); err != nil { @@ -131,7 +132,7 @@ func DepositUpdate(ctx context.Context, i *models.DepositUpdate, token *vcapool. event.EventState.State = "closed" e := new(models.Event) if err = EventCollection.UpdateOneAggregate( - ctx, + ctxAsync, event.Match(), vmdb.UpdateSet(event), e, @@ -147,7 +148,7 @@ func DepositUpdate(ctx context.Context, i *models.DepositUpdate, token *vcapool. // Add takings to CRM var taking *models.Taking - if taking, err = TakingGetByID(ctx, &models.TakingParam{ID: unit.TakingID}, token); err != nil { + if taking, err = TakingGetByID(ctx, &vmod.IDParam{ID: unit.TakingID}, token); err != nil { log.Print(err) } @@ -160,7 +161,7 @@ func DepositUpdate(ctx context.Context, i *models.DepositUpdate, token *vcapool. participations := new([]models.Participation) if err = ParticipationCollection.Aggregate( - ctx, + ctxAsync, models.ParticipationPipeline().Match(bson.D{{Key: "event_id", Value: e.ID}}).Pipe, participations, ); err != nil { @@ -228,7 +229,7 @@ func DepositSync(ctx context.Context, i *models.DepositParam, token *vcapool.Acc // Add takings to CRM var taking *models.Taking - if taking, err = TakingGetByID(ctx, &models.TakingParam{ID: unit.TakingID}, token); err != nil { + if taking, err = TakingGetByID(ctx, &vmod.IDParam{ID: unit.TakingID}, token); err != nil { log.Print(err) } taking.EditorID = token.ID diff --git a/dao/taking.go b/dao/taking.go index 1196be0..1503bd5 100644 --- a/dao/taking.go +++ b/dao/taking.go @@ -5,6 +5,7 @@ import ( "pool-backend/models" "github.com/Viva-con-Agua/vcago/vmdb" + "github.com/Viva-con-Agua/vcago/vmod" "github.com/Viva-con-Agua/vcapool" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo/options" @@ -135,11 +136,11 @@ func TakingGet(ctx context.Context, query *models.TakingQuery, token *vcapool.Ac return } -func TakingGetByID(ctx context.Context, param *models.TakingParam, token *vcapool.AccessToken) (result *models.Taking, err error) { +func TakingGetByID(ctx context.Context, param *vmod.IDParam, token *vcapool.AccessToken) (result *models.Taking, err error) { if err = models.TakingPermission(token); err != nil { return } - filter := param.PermittedFilter(token) + filter := models.TakingPermittedFilter(param, token) if err = TakingCollection.AggregateOne( ctx, models.TakingPipeline().Match(filter).Pipe, @@ -168,10 +169,10 @@ func TakingDeletetByIDSystem(ctx context.Context, id string) (err error) { return } -func TakingDeletetByID(ctx context.Context, param *models.TakingParam, token *vcapool.AccessToken) (err error) { +func TakingDeletetByID(ctx context.Context, param *vmod.IDParam, token *vcapool.AccessToken) (err error) { if err = models.TakingPermission(token); err != nil { return } - err = TakingCollection.DeleteOne(ctx, param.PermittedFilter(token)) + err = TakingCollection.DeleteOne(ctx, models.TakingPermittedFilter(param, token)) return } diff --git a/handlers/token/role_history.go b/handlers/token/role_history.go index a2987a4..037f782 100644 --- a/handlers/token/role_history.go +++ b/handlers/token/role_history.go @@ -45,7 +45,7 @@ func (i *RoleHistoryHandler) Create(cc echo.Context) (err error) { func (i *RoleHistoryHandler) CreateBulk(cc echo.Context) (err error) { c := cc.(vcago.Context) body := new(models.RoleHistoryBulkRequest) - if c.BindAndValidate(body); err != nil { + if err = c.BindAndValidate(body); err != nil { return } token := new(vcapool.AccessToken) diff --git a/handlers/token/taking.go b/handlers/token/taking.go index 583ae7e..f988f85 100644 --- a/handlers/token/taking.go +++ b/handlers/token/taking.go @@ -5,6 +5,7 @@ import ( "pool-backend/models" "github.com/Viva-con-Agua/vcago" + "github.com/Viva-con-Agua/vcago/vmod" "github.com/Viva-con-Agua/vcapool" "github.com/labstack/echo/v4" ) @@ -21,6 +22,7 @@ func (i *TakingHandler) Routes(group *echo.Group) { group.PUT("", i.Update, accessCookie) group.GET("", i.Get, accessCookie) group.GET("/:id", i.GetByID, accessCookie) + group.DELETE("/:id", i.DeleteByID, accessCookie) } @@ -78,7 +80,7 @@ func (i TakingHandler) Get(cc echo.Context) (err error) { func (i TakingHandler) GetByID(cc echo.Context) (err error) { c := cc.(vcago.Context) - body := new(models.TakingParam) + body := new(vmod.IDParam) if err = c.BindAndValidate(body); err != nil { return } @@ -93,9 +95,9 @@ func (i TakingHandler) GetByID(cc echo.Context) (err error) { return c.Selected(result) } -func (i TakingHandler) Delete(cc echo.Context) (err error) { +func (i TakingHandler) DeleteByID(cc echo.Context) (err error) { c := cc.(vcago.Context) - body := new(models.TakingParam) + body := new(vmod.IDParam) if err = c.BindAndValidate(body); err != nil { return } diff --git a/models/profile.go b/models/profile.go index 51f8ef4..6e48e79 100644 --- a/models/profile.go +++ b/models/profile.go @@ -24,13 +24,14 @@ type ( Birthdate int64 `bson:"birthdate" json:"birthdate"` } Profile struct { - ID string `bson:"_id" json:"id"` - Gender string `bson:"gender" json:"gender"` - Phone string `bson:"phone" json:"phone"` - Mattermost string `bson:"mattermost_username" json:"mattermost_username"` - Birthdate int64 `bson:"birthdate" json:"birthdate"` - UserID string `bson:"user_id" json:"user_id"` - Modified vmod.Modified `bson:"modified" json:"modified"` + ID string `bson:"_id" json:"id"` + Gender string `bson:"gender" json:"gender"` + Phone string `bson:"phone" json:"phone"` + Mattermost string `bson:"mattermost_username" json:"mattermost_username"` + Birthdate int64 `bson:"birthdate" json:"birthdate"` + + UserID string `bson:"user_id" json:"user_id"` + Modified vmod.Modified `bson:"modified" json:"modified"` } ProfileParam struct { ID string `param:"id"` diff --git a/models/taking.go b/models/taking.go index c83ebd3..94a7dec 100644 --- a/models/taking.go +++ b/models/taking.go @@ -228,7 +228,7 @@ func (i *TakingUpdate) PermittedFilter(token *vcapool.AccessToken) bson.D { return filter.Bson() } -func (i *TakingParam) PermittedFilter(token *vcapool.AccessToken) bson.D { +func TakingPermittedFilter(i *vmod.IDParam, token *vcapool.AccessToken) bson.D { filter := vmdb.NewFilter() filter.EqualString("_id", i.ID) if !token.Roles.Validate("employee;admin") { From e814866de2a339ee411a75129d1be4c17cc00f84 Mon Sep 17 00:00:00 2001 From: Dennis Kleber Date: Thu, 27 Jun 2024 11:07:24 +0200 Subject: [PATCH 2/3] solves #181 --- dao/update_ticker.go | 12 ++++++++++ models/taking.go | 55 ++++++++++++++++++++++++-------------------- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/dao/update_ticker.go b/dao/update_ticker.go index e70b9fc..f26c886 100644 --- a/dao/update_ticker.go +++ b/dao/update_ticker.go @@ -36,9 +36,21 @@ func EventStateFinishTicker() { filter.EqualString("event_state.state", "published") filter.LteInt64("end_at", fmt.Sprint(time.Now().Unix())) update := bson.D{{Key: "event_state.state", Value: "finished"}} + events := new([]models.Event) + if err := EventCollection.Find(context.Background(), filter.Bson(), events); err != nil { + log.Print(err) + } if err := EventCollection.UpdateMany(context.Background(), filter.Bson(), vmdb.UpdateSet(update)); err != nil { log.Print(err) } + for _, value := range *events { + filterTaking := bson.D{{Key: "_id", Value: value.TakingID}, {Key: "taking_id", Value: bson.D{{Key: "$ne", Value: ""}}}} + updateTaking := bson.D{{Key: "date_of_taking", Value: value.EndAt}} + if err := TakingCollection.UpdateOne(context.Background(), filterTaking, vmdb.UpdateSet(updateTaking), nil); err != nil { + log.Print(err) + } + } + } // EventStateClosed diff --git a/models/taking.go b/models/taking.go index 94a7dec..798e371 100644 --- a/models/taking.go +++ b/models/taking.go @@ -13,29 +13,32 @@ import ( type ( TakingCreate struct { - Name string `json:"name" bson:"name"` - CrewID string `json:"crew_id" bson:"crew_id"` - NewSource []SourceCreate `json:"new_sources"` - Comment string `json:"comment"` + Name string `json:"name" bson:"name"` + CrewID string `json:"crew_id" bson:"crew_id"` + NewSource []SourceCreate `json:"new_sources"` + DateOfTaking int64 `json:"date_of_taking" bson:"date_of_taking"` + Comment string `json:"comment"` } TakingUpdate struct { - ID string `json:"id" bson:"_id"` - Name string `json:"name" bson:"name"` - CrewID string `json:"crew_id" bson:"crew_id"` - Sources []SourceUpdate `json:"sources" bson:"-"` - State TakingStateUpdate `json:"state" bson:"state"` - Comment string `json:"comment"` + ID string `json:"id" bson:"_id"` + Name string `json:"name" bson:"name"` + CrewID string `json:"crew_id" bson:"crew_id"` + Sources []SourceUpdate `json:"sources" bson:"-"` + State TakingStateUpdate `json:"state" bson:"state"` + DateOfTaking int64 `json:"date_of_taking" bson:"date_of_taking"` + Comment string `json:"comment"` } TakingDatabase struct { - ID string `json:"id" bson:"_id"` - Name string `json:"name" bson:"name"` - CrewID string `json:"crew_id" bson:"crew_id"` - Type string `json:"type" bson:"type"` - Comment string `json:"comment" bson:"comment"` - State TakingState `json:"state" bson:"state"` - Currency string `json:"-" bson:"currency"` - Modified vmod.Modified `json:"modified" bson:"modified"` + ID string `json:"id" bson:"_id"` + Name string `json:"name" bson:"name"` + CrewID string `json:"crew_id" bson:"crew_id"` + Type string `json:"type" bson:"type"` + Comment string `json:"comment" bson:"comment"` + State TakingState `json:"state" bson:"state"` + Currency string `json:"-" bson:"currency"` + DateOfTaking int64 `json:"date_of_taking" bson:"date_of_taking"` + Modified vmod.Modified `json:"modified" bson:"modified"` } Taking struct { ID string `json:"id" bson:"_id"` @@ -46,6 +49,7 @@ type ( Event Event `json:"event" bson:"event"` Source []Source `json:"sources" bson:"sources"` State TakingState `json:"state" bson:"state"` + DateOfTaking int64 `json:"date_of_taking" bson:"date_of_taking"` Comment string `json:"comment" bson:"comment"` EditorID string `json:"editor_id" bson:"-"` DepositUnits []DepositUnitTaking `json:"deposit_units" bson:"deposit_units"` @@ -138,13 +142,14 @@ func TakingPipelineTicker() *vmdb.Pipeline { func (i *TakingCreate) TakingDatabase() *TakingDatabase { return &TakingDatabase{ - ID: uuid.NewString(), - Name: i.Name, - CrewID: i.CrewID, - Type: "manually", - Currency: i.NewSource[0].Money.Currency, - Comment: i.Comment, - Modified: vmod.NewModified(), + ID: uuid.NewString(), + Name: i.Name, + CrewID: i.CrewID, + Type: "manually", + Currency: i.NewSource[0].Money.Currency, + DateOfTaking: i.DateOfTaking, + Comment: i.Comment, + Modified: vmod.NewModified(), } } From f49c0a0bc663b1159927b3c1ff63dec99d557118 Mon Sep 17 00:00:00 2001 From: Dennis Kleber Date: Fri, 28 Jun 2024 13:05:13 +0200 Subject: [PATCH 3/3] solves #180 --- dao/update_ticker.go | 2 +- models/taking.go | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/dao/update_ticker.go b/dao/update_ticker.go index f26c886..afbf3e5 100644 --- a/dao/update_ticker.go +++ b/dao/update_ticker.go @@ -64,7 +64,7 @@ func EventStateClosedTicker() { }, bson.D{{Key: "state.no_income", Value: true}}, }} - filter.Append(bson.E{Key: "$or", Value: confirmedFilter}) + filter.Append(confirmedFilter) filter.EqualString("event.event_state.state", "finished") pipeline := models.TakingPipeline().Match(filter.Bson()).Pipe takings := []models.Taking{} diff --git a/models/taking.go b/models/taking.go index 798e371..417d97e 100644 --- a/models/taking.go +++ b/models/taking.go @@ -76,6 +76,8 @@ type ( Search string `query:"search"` CrewID []string `query:"crew_id"` EventName string `query:"event_name"` + TypeOfEvent []string `query:"type_of_event"` + ArtistName string `query:"artist_name"` EventState []string `query:"event_state"` EventEndFrom string `query:"event_end_from"` EventEndTo string `query:"event_end_to"` @@ -110,6 +112,7 @@ func TakingPipeline() *vmdb.Pipeline { pipe.Lookup(SourceCollection, "_id", "taking_id", "sources") pipe.LookupUnwind(CrewCollection, "crew_id", "_id", "crew") pipe.LookupUnwind(EventCollection, "_id", "taking_id", "event") + pipe.LookupList(ArtistCollection, "artist_ids", "_id", "event.artists") pipe.Append(bson.D{{Key: "$addFields", Value: bson.D{ {Key: "state.wait.amount", Value: bson.D{{Key: "$sum", Value: "$wait.money.amount"}}}, }}}) @@ -194,7 +197,9 @@ func (i *TakingQuery) PermittedFilter(token *vcapool.AccessToken) bson.D { filter.LikeString("name", i.Name) filter.SearchString([]string{"name", "event.name"}, i.Search) filter.EqualStringList("event.event_state.state", i.EventState) + filter.EqualStringList("event.type_of_event", i.TypeOfEvent) filter.LikeString("event.name", i.EventName) + filter.LikeString("event.artists.name", i.ArtistName) filter.GteInt64("event.end_at", i.EventEndFrom) filter.LteInt64("event.end_at", i.EventEndTo) status := bson.A{}