Skip to content

Commit

Permalink
Merge pull request #190 from Viva-con-Agua/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
deinelieblings authored Jul 10, 2024
2 parents a56d510 + d44808a commit 6c977a6
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 48 deletions.
13 changes: 7 additions & 6 deletions dao/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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)
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions dao/taking.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
14 changes: 13 additions & 1 deletion dao/update_ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -52,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{}
Expand Down
2 changes: 1 addition & 1 deletion handlers/token/role_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions handlers/token/taking.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)

}

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
15 changes: 8 additions & 7 deletions models/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
62 changes: 36 additions & 26 deletions models/taking.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"`
Expand All @@ -72,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"`
Expand Down Expand Up @@ -106,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"}}},
}}})
Expand Down Expand Up @@ -138,13 +145,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(),
}
}

Expand Down Expand Up @@ -189,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{}
Expand Down Expand Up @@ -228,7 +238,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") {
Expand Down

0 comments on commit 6c977a6

Please sign in to comment.