Skip to content

Commit

Permalink
fix: update payment setting API
Browse files Browse the repository at this point in the history
  • Loading branch information
anhnh12 committed Jan 16, 2024
1 parent 028c514 commit 731cbc7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
17 changes: 10 additions & 7 deletions pkg/entities/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func (e *Entity) UpdateUserGeneralSettings(uri request.UserSettingBaseUriRequest
defaultMessages[i] = model.DefaultMessageSetting{
Action: s.Action,
Message: s.Message,
Enable: *s.Enable,
}
}

Expand Down Expand Up @@ -156,6 +157,14 @@ func (e *Entity) ValidateMoneySourceSetting(profileId string, s request.MoneySou
"profile_id": profileId,
})

mochiSource := mochiprofile.AssociatedAccount{
Platform: "mochi",
PlatformIdentifier: "mochi-balance",
}
if string(mochiSource.Platform) == s.Platform && mochiSource.PlatformIdentifier == s.PlatformIdentifier {
return nil
}

profile, err := e.svc.MochiProfile.GetByID(profileId, e.cfg.MochiBotSecret)
if err != nil {
logger.Error(err, "svc.MochiProfile.GetByID() failed")
Expand All @@ -166,18 +175,12 @@ func (e *Entity) ValidateMoneySourceSetting(profileId string, s request.MoneySou
return errors.New("invalid money source")
}

profile.AssociatedAccounts = append(profile.AssociatedAccounts, mochiprofile.AssociatedAccount{
Platform: "mochi",
PlatformIdentifier: "mochi-balance",
})

// a money source is considered as valid when it's either mochi wallet or connected wallet (evm,sol,etc.)
// other social platforms such as telegram, discord, email are invalid money source
validMoneySource := sliceutils.Some(profile.AssociatedAccounts, func(acc mochiprofile.AssociatedAccount) bool {
existingSource := string(acc.Platform) == s.Platform && strings.EqualFold(acc.PlatformIdentifier, s.PlatformIdentifier)
isMochi := s.Platform == "mochi" && existingSource
isConnectedWallet := strings.Contains(s.Platform, "chain") && existingSource
return isMochi || isConnectedWallet
return isConnectedWallet
})
if !validMoneySource {
return errors.New("invalid money source")
Expand Down
1 change: 1 addition & 0 deletions pkg/model/user_payment_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (m MoneySource) Value() (driver.Value, error) {
type DefaultMessageSetting struct {
Action string `json:"action"`
Message string `json:"message"`
Enable bool `json:"enable"`
}

type DefaultMessageSettings []DefaultMessageSetting
Expand Down
7 changes: 4 additions & 3 deletions pkg/request/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type MoneySource struct {
type DefaultMessageSetting struct {
Action string `json:"action" binding:"required"`
Message string `json:"message" binding:"required"`
Enable *bool `json:"enable" binding:"required"`
}

type TxLimitSetting struct {
Expand All @@ -42,9 +43,9 @@ type PaymentSetting struct {
DefaultReceiverPlatform string `json:"default_receiver_platform" binding:"required"`
TokenPriorities []string `json:"token_priorities" binding:"required"`
DefaultMessageEnable *bool `json:"default_message_enable" binding:"required"`
DefaultMessageSettings []DefaultMessageSetting `json:"default_message_settings" binding:"required"`
DefaultMessageSettings []DefaultMessageSetting `json:"default_message_settings" binding:"required,dive"`
TxLimitEnable *bool `json:"tx_limit_enable" binding:"required"`
TxLimitSettings []TxLimitSetting `json:"tx_limit_settings" binding:"required"`
TxLimitSettings []TxLimitSetting `json:"tx_limit_settings" binding:"required,dive"`
}

type PrivacyCustomSetting struct {
Expand Down Expand Up @@ -78,7 +79,7 @@ func (r *UpdateGeneralSettingsPayloadRequest) Bind(c *gin.Context) error {

func (s *PaymentSetting) validate() error {
// platforms
platforms := []string{"discord", "telegram", "google"}
platforms := []string{"discord", "telegram", "google", "twitter", "github", "reddit", "onchain"}
if !sliceutils.Contains(platforms, s.DefaultReceiverPlatform) {
return fmt.Errorf("default_receiver_platform: invalid value. Available values: %s", strings.Join(platforms, ","))
}
Expand Down

0 comments on commit 731cbc7

Please sign in to comment.