Skip to content

Commit

Permalink
Merge pull request #163 from Viva-con-Agua/bugfix/no_income_status
Browse files Browse the repository at this point in the history
Bugfix/no income status
  • Loading branch information
TobiKaestle authored Dec 22, 2023
2 parents 9ac15b2 + a728339 commit e9d2ff6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion dao/taking.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"log"
"pool-backend/models"
"time"

"github.com/Viva-con-Agua/vcago/vmdb"
"github.com/Viva-con-Agua/vcapool"
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions dao/update_ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"pool-backend/models"
"time"

"github.com/Viva-con-Agua/vcago/vmdb"
Expand All @@ -19,6 +20,7 @@ func UpdateTicker() {
select {
case <-ticker.C:
EventStateUpdateTicker()
EventStateNoIncome()
case <-quit:
ticker.Stop()
return
Expand All @@ -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)
}

}
}
6 changes: 6 additions & 0 deletions models/taking.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit e9d2ff6

Please sign in to comment.