Skip to content

Commit

Permalink
solves #172
Browse files Browse the repository at this point in the history
  • Loading branch information
deinelieblings committed Aug 20, 2024
1 parent 62bc35c commit 33fc026
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 27 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
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)
}
}
}
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

0 comments on commit 33fc026

Please sign in to comment.