Skip to content

Commit

Permalink
Merge pull request #188 from Viva-con-Agua/stage
Browse files Browse the repository at this point in the history
Stage
  • Loading branch information
deinelieblings authored Jul 9, 2024
2 parents 6849bba + a56d510 commit 6519480
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 78 deletions.
8 changes: 8 additions & 0 deletions dao/crew.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
63 changes: 0 additions & 63 deletions dao/taking.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -94,67 +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
}
Expand Down
47 changes: 39 additions & 8 deletions dao/update_ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import (
func UpdateTicker() {
ticker := time.NewTicker(1 * time.Hour)
quit := make(chan struct{})
EventStateUpdateTicker()
EventStateFinishTicker()
EventStateClosedTicker()
go func() {
for {
select {
case <-ticker.C:
EventStateUpdateTicker()
EventStateNoIncome()
EventStateFinishTicker()
EventStateClosedTicker()
case <-quit:
ticker.Stop()
return
Expand All @@ -29,7 +30,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()))
Expand All @@ -38,19 +40,48 @@ func EventStateUpdateTicker() {
log.Print(err)
}
}
func EventStateNoIncome() {

// EventStateClosed
func EventStateClosedTicker() {
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)
}
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)
}
if err := IDjango.Post(e, "/v1/pool/event/update/"); err != nil {
log.Print(err)
}
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 {
log.Print(err)
}

if err := IDjango.Post(participations, "/v1/pool/participations/create/"); err != nil {
log.Print(err)
}

Expand Down
9 changes: 6 additions & 3 deletions dao/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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_")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion handlers/token/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6519480

Please sign in to comment.