From 33fc0260800ad1fadb3fb9f86aea5ab4d53d93cd Mon Sep 17 00:00:00 2001 From: Dennis Kleber Date: Tue, 20 Aug 2024 16:44:23 +0200 Subject: [PATCH] solves #172 --- dao/profile.go | 7 +++++ dao/updates.go | 23 +++++++++++++++++ models/profile.go | 66 ++++++++++++++++++++++++++++------------------- 3 files changed, 69 insertions(+), 27 deletions(-) diff --git a/dao/profile.go b/dao/profile.go index 9a3ffb3..5d7be43 100644 --- a/dao/profile.go +++ b/dao/profile.go @@ -4,6 +4,7 @@ import ( "context" "log" "pool-backend/models" + "time" "github.com/Viva-con-Agua/vcago/vmdb" "github.com/Viva-con-Agua/vcapool" @@ -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, diff --git a/dao/updates.go b/dao/updates.go index aa4f578..4feba0c 100644 --- a/dao/updates.go +++ b/dao/updates.go @@ -4,6 +4,7 @@ import ( "context" "log" "pool-backend/models" + "time" "github.com/Viva-con-Agua/vcago/vmdb" "github.com/google/uuid" @@ -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) { @@ -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) + } + } } diff --git a/models/profile.go b/models/profile.go index 6e48e79..17e48ee 100644 --- a/models/profile.go +++ b/models/profile.go @@ -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" @@ -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"` @@ -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(), } }