Skip to content

Commit

Permalink
Merge pull request #207 from Viva-con-Agua/dk/taking_update
Browse files Browse the repository at this point in the history
Dk/taking update
  • Loading branch information
TobiKaestle authored Aug 22, 2024
2 parents ec99c4a + a653c87 commit 31a1478
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 35 deletions.
7 changes: 7 additions & 0 deletions dao/profile.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 All @@ -30,6 +31,12 @@ func ProfileGetByID(ctx context.Context, i *models.UserParam, token *vcapool.Acc

func ProfileUpdate(ctx context.Context, i *models.ProfileUpdate, token *vcapool.AccessToken) (result *models.Profile, err error) {
filter := i.PermittedFilter(token)
birthdate := time.Unix(i.Birthdate, 0)
if i.Birthdate != 0 {
i.BirthdateDatetime = birthdate.Format("2006-01-02")
} else {
i.BirthdateDatetime = ""
}
if err = ProfileCollection.UpdateOne(
ctx,
filter,
Expand Down
6 changes: 3 additions & 3 deletions dao/taking.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,18 @@ func TakingUpdate(ctx context.Context, i *models.TakingUpdate, token *vcapool.Ac
return
}

func TakingGet(ctx context.Context, query *models.TakingQuery, token *vcapool.AccessToken) (result *[]models.Taking, listSize int64, err error) {
func TakingGet(ctx context.Context, query *models.TakingQuery, token *vcapool.AccessToken) (result []models.Taking, listSize int64, err error) {
if err = models.TakingPermission(token); err != nil {
return
}
result = new([]models.Taking)
result = []models.Taking{}
filter := query.PermittedFilter(token)
sort := query.Sort()
pipeline := models.TakingPipeline().SortFields(sort).Match(filter).Sort(sort).Skip(query.Skip, 0).Limit(query.Limit, 100).Pipe
if err = TakingCollection.Aggregate(
ctx,
pipeline,
result,
&result,
); err != nil {
return
}
Expand Down
23 changes: 23 additions & 0 deletions dao/update_ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ func UpdateTicker() {
quit := make(chan struct{})
EventStateFinishTicker()
EventStateClosedTicker()
UserActiveStateTicker()
go func() {
for {
select {
case <-ticker.C:
EventStateFinishTicker()
EventStateClosedTicker()
UserActiveStateTicker()
case <-quit:
ticker.Stop()
return
Expand Down Expand Up @@ -89,3 +91,24 @@ func EventStateClosedTicker() {

}
}

func UserActiveStateTicker() {
checkDate := time.Now().Unix() - 15768000
filter := bson.D{
{Key: "last_login_date", Value: bson.D{{Key: "$lte", Value: checkDate}}},
{Key: "active.status", Value: "confirmed"},
}
userList := []models.User{}
pipeline := models.UserPipeline(false).Match(filter).Pipe
if err := UserCollection.Aggregate(context.Background(), pipeline, &userList); err != nil {
log.Print(err)
}
for _, user := range userList {
update := bson.D{{Key: "status", Value: "rejected"}}
userFilter := bson.D{{Key: "_id", Value: user.Active.ID}}
if err := ActiveCollection.UpdateOne(context.Background(), userFilter, vmdb.UpdateSet(update), nil); err != nil {
log.Print(err)
}
}

}
23 changes: 23 additions & 0 deletions dao/updates.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/google/uuid"
Expand Down Expand Up @@ -78,6 +79,10 @@ func UpdateDatabase() {
UpdateDateOfTaking1(ctx)
InsertUpdate(ctx, "date_of_taking_1")
}
if !CheckUpdated(ctx, "birthdate_1") {
UpdateProfileBirthdate(ctx)
InsertUpdate(ctx, "birthdate_1")
}
}

func UpdateCrewMaibox(ctx context.Context) {
Expand Down Expand Up @@ -184,5 +189,23 @@ func UpdateDateOfTaking1(ctx context.Context) {
log.Print(err)
}
}
}

func UpdateProfileBirthdate(ctx context.Context) {
profileList := []models.ProfileUpdate{}
if err := ProfileCollection.Find(ctx, bson.D{{}}, &profileList); err != nil {
log.Print(err)
}
for _, profile := range profileList {
birthdate := time.Unix(profile.Birthdate, 0)
if profile.Birthdate != 0 {
profile.BirthdateDatetime = birthdate.Format("2006-01-02")
} else {
profile.BirthdateDatetime = ""
}
filter := bson.D{{Key: "_id", Value: profile.ID}}
if err := ProfileCollection.UpdateOne(ctx, filter, vmdb.UpdateSet(profile), nil); err != nil {
log.Print(err)
}
}
}
2 changes: 1 addition & 1 deletion handlers/token/artist.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (i *ArtistHandler) Update(cc echo.Context) (err error) {
func (i *ArtistHandler) Delete(cc echo.Context) (err error) {
c := cc.(vcago.Context)
body := new(models.ArtistParam)
if c.BindAndValidate(body); err != nil {
if err = c.BindAndValidate(body); err != nil {
return c.ErrorResponse(err)
}
token := new(vcapool.AccessToken)
Expand Down
2 changes: 1 addition & 1 deletion handlers/token/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (i *LoginHandler) Callback(cc echo.Context) (err error) {
func (i *LoginHandler) LoginAPI(cc echo.Context) (err error) {
c := cc.(vcago.Context)
body := new(models.UserEmail)
if c.BindAndValidate(body); err != nil {
if err = c.BindAndValidate(body); err != nil {
return
}
result := new(models.User)
Expand Down
2 changes: 1 addition & 1 deletion handlers/token/organizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (i *OrganizerHandler) Update(cc echo.Context) (err error) {
func (i *OrganizerHandler) Delete(cc echo.Context) (err error) {
c := cc.(vcago.Context)
body := new(models.OrganizerParam)
if c.BindAndValidate(body); err != nil {
if err = c.BindAndValidate(body); err != nil {
return c.ErrorResponse(err)
}
token := new(vcapool.AccessToken)
Expand Down
2 changes: 1 addition & 1 deletion handlers/token/taking.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (i TakingHandler) Get(cc echo.Context) (err error) {
if err = c.AccessToken(token); err != nil {
return
}
result := new([]models.Taking)
var result []models.Taking
var listSize int64
if result, listSize, err = dao.TakingGet(c.Ctx(), body, token); err != nil {
return
Expand Down
66 changes: 39 additions & 27 deletions models/profile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package models

import (
"time"

"github.com/Viva-con-Agua/vcago"
"github.com/Viva-con-Agua/vcago/vmdb"
"github.com/Viva-con-Agua/vcago/vmod"
Expand All @@ -11,35 +13,38 @@ import (

type (
ProfileCreate struct {
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"`
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"`
BirthdateDatetime string `bson:"birthdate_datetime" json:"birthdate_datetime"`
}
ProfileUpdate 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"`
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"`
BirthdateDatetime string `bson:"birthdate_datetime" json:"birthdate_datetime"`
}
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"`
BirthdateDatetime string `bson:"birthdate_datetime" json:"birthdate_datetime"`
UserID string `bson:"user_id" json:"user_id"`
Modified vmod.Modified `bson:"modified" json:"modified"`
}
ProfileParam struct {
ID string `param:"id"`
}
ProfileMinimal struct {
Mattermost string `bson:"mattermost_username" json:"mattermost_username"`
Birthdate int64 `bson:"birthdate" json:"birthdate"`
UserID string `bson:"user_id" json:"user_id"`
Mattermost string `bson:"mattermost_username" json:"mattermost_username"`
Birthdate int64 `bson:"birthdate" json:"birthdate"`
BirthdateDatetime string `bson:"birthdate_datetime" json:"birthdate_datetime"`
UserID string `bson:"user_id" json:"user_id"`
}
ProfileImport struct {
Gender string `bson:"gender" json:"gender"`
Expand All @@ -60,14 +65,21 @@ func (i *ProfileParam) ProfileSyncPermission(token *vcapool.AccessToken) (err er
}

func (i *ProfileCreate) Profile(userID string) *Profile {
birthdate := time.Unix(i.Birthdate, 0)
birthdateDatetime := ""
if i.Birthdate != 0 {
birthdateDatetime = birthdate.Format("2006-01-02")
}

return &Profile{
ID: uuid.NewString(),
Gender: i.Gender,
Phone: i.Phone,
Mattermost: i.Mattermost,
Birthdate: i.Birthdate,
UserID: userID,
Modified: vmod.NewModified(),
ID: uuid.NewString(),
Gender: i.Gender,
Phone: i.Phone,
Mattermost: i.Mattermost,
Birthdate: i.Birthdate,
BirthdateDatetime: birthdateDatetime,
UserID: userID,
Modified: vmod.NewModified(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion models/taking.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +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, "event.artist_ids", "_id", "event.artists")
pipe.Lookup(ArtistCollection, "event.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
3 changes: 3 additions & 0 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type (
DropsID string `bson:"drops_id" json:"drops_id"`
LastUpdate string `bson:"last_update" json:"last_update"`
MailboxID string `bson:"mailbox_id" json:"mailbox_id"`
LastLoginDate int64 `bson:"last_login_date" json:"last_login_date"`
Modified vmod.Modified `json:"modified" bson:"modified"`
}
UserUpdate struct {
Expand All @@ -44,6 +45,7 @@ type (
PrivacyPolicy bool `bson:"privacy_policy" json:"privacy_policy"`
Confirmed bool `bson:"confirmed" json:"confirmed"`
DropsID string `bson:"drops_id" json:"drops_id"`
LastLoginDate int64 `bson:"last_login_date" json:"last_login_date"`
LastUpdate string `bson:"last_update" json:"last_update"`
}
User struct {
Expand All @@ -59,6 +61,7 @@ type (
Confirmed bool `bson:"confirmed" json:"confirmed"`
LastUpdate string `bson:"last_update" json:"last_update"`
MailboxID string `bson:"mailbox_id" json:"mailbox_id"`
LastLoginDate int64 `bson:"last_login_date" json:"last_login_date"`
//extends the vcago.User
DropsID string `bson:"drops_id" json:"drops_id"`
Profile Profile `json:"profile" bson:"profile,truncate"`
Expand Down

0 comments on commit 31a1478

Please sign in to comment.