From 173ef92c01001269ea9525e7b8642c8f08e8b5fb Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 23 Mar 2025 19:57:19 +0800 Subject: [PATCH] :sparkles: Program member benifits --- pkg/authkit/models/programs.go | 11 +++++++ pkg/internal/services/programs.go | 51 +++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/pkg/authkit/models/programs.go b/pkg/authkit/models/programs.go index 8c4f27c..7914e90 100644 --- a/pkg/authkit/models/programs.go +++ b/pkg/authkit/models/programs.go @@ -11,6 +11,15 @@ type ProgramPrice struct { Amount float64 `json:"amount"` } +type ProgramBadge struct { + Type string `json:"type"` + Metadata map[string]any `json:"metadata"` +} + +type ProgramGroup struct { + ID uint `json:"id"` +} + type Program struct { BaseModel @@ -19,6 +28,8 @@ type Program struct { Alias string `json:"alias" gorm:"uniqueIndex"` ExpRequirement int64 `json:"exp_requirement"` Price datatypes.JSONType[ProgramPrice] `json:"price"` + Badge datatypes.JSONType[ProgramBadge] `json:"badge"` + Group datatypes.JSONType[ProgramGroup] `json:"group"` Appearance datatypes.JSONMap `json:"appearance"` } diff --git a/pkg/internal/services/programs.go b/pkg/internal/services/programs.go index d4500c3..64a1d38 100644 --- a/pkg/internal/services/programs.go +++ b/pkg/internal/services/programs.go @@ -9,7 +9,9 @@ import ( "git.solsynth.dev/hypernet/passport/pkg/internal/database" "git.solsynth.dev/hypernet/passport/pkg/internal/gap" "git.solsynth.dev/hypernet/wallet/pkg/proto" + "github.com/rs/zerolog/log" "github.com/samber/lo" + "gorm.io/datatypes" ) func JoinProgram(user models.Account, program models.Program) (models.ProgramMember, error) { @@ -36,6 +38,8 @@ func JoinProgram(user models.Account, program models.Program) (models.ProgramMem } if err := database.C.Create(&member).Error; err != nil { return member, err + } else { + PostJoinProgram(member) } return member, nil } @@ -47,6 +51,8 @@ func LeaveProgram(user models.Account, program models.Program) error { } if err := database.C.Delete(&member).Error; err != nil { return err + } else { + PostLeaveProgram(member) } return nil } @@ -86,3 +92,48 @@ func PeriodicChargeProgramFee() { } } } + +func PostJoinProgram(member models.ProgramMember) error { + badge := member.Program.Badge.Data() + if len(badge.Type) > 0 { + accountBadge := models.Badge{ + Type: badge.Type, + AccountID: member.AccountID, + Metadata: datatypes.JSONMap(badge.Metadata), + } + if err := database.C.Create(&accountBadge).Error; err != nil { + log.Error().Err(err).Msg("Failed to create badge for program member...") + return err + } + } + group := member.Program.Group.Data() + if group.ID > 0 { + accountGroup := models.AccountGroupMember{ + GroupID: group.ID, + AccountID: member.AccountID, + } + if err := database.C.Create(&accountGroup).Error; err != nil { + log.Error().Err(err).Msg("Failed to create group for program member...") + return err + } + } + return nil +} + +func PostLeaveProgram(member models.ProgramMember) error { + badge := member.Program.Badge.Data() + if len(badge.Type) > 0 { + if err := database.C.Where("account_id = ? AND type = ?", member.AccountID, badge.Type).Delete(&models.Badge{}).Error; err != nil { + log.Error().Err(err).Msg("Failed to delete badge for program member...") + return err + } + } + group := member.Program.Group.Data() + if group.ID > 0 { + if err := database.C.Where("account_id = ? AND group_id = ?", member.AccountID, group.ID).Delete(&models.AccountGroupMember{}).Error; err != nil { + log.Error().Err(err).Msg("Failed to delete group for program member...") + return err + } + } + return nil +}