Program member benifits

This commit is contained in:
LittleSheep 2025-03-23 19:57:19 +08:00
parent 23e5ba432b
commit 173ef92c01
2 changed files with 62 additions and 0 deletions

View File

@ -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"`
}

View File

@ -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
}