113 lines
3.4 KiB
Go
113 lines
3.4 KiB
Go
package firstrechargereward
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
|
|
"chatapp3-golang/internal/model"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
const (
|
|
activityStatusNotConfigured = "NOT_CONFIGURED"
|
|
activityStatusDisabled = "DISABLED"
|
|
activityStatusOngoing = "ONGOING"
|
|
activityStatusPending = "PENDING"
|
|
activityStatusRewarded = "REWARDED"
|
|
activityStatusFailed = "FAILED"
|
|
)
|
|
|
|
// GetHome returns app-facing first recharge reward config and the user's grant state.
|
|
func (s *Service) GetHome(ctx context.Context, user AuthUser, appVersion string) (*HomeResponse, error) {
|
|
sysOrigin, err := requireUser(user)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if s.java == nil {
|
|
return nil, NewAppError(http.StatusServiceUnavailable, "java_gateway_unavailable", "java gateway is unavailable")
|
|
}
|
|
|
|
bundle, err := s.loadConfigBundle(ctx, sysOrigin)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if bundle.Config == nil {
|
|
return &HomeResponse{
|
|
Configured: false,
|
|
Enabled: false,
|
|
ActivityStatus: activityStatusNotConfigured,
|
|
UserID: user.UserID,
|
|
SysOrigin: sysOrigin,
|
|
LevelConfigs: []LevelPayload{},
|
|
HasRewardRecord: false,
|
|
}, nil
|
|
}
|
|
|
|
rewardItems := s.loadRewardItemsMap(ctx, bundle.Levels)
|
|
resp := &HomeResponse{
|
|
Configured: true,
|
|
Enabled: bundle.Config.Enabled,
|
|
ActivityStatus: activityStatusOngoing,
|
|
UserID: user.UserID,
|
|
SysOrigin: bundle.Config.SysOrigin,
|
|
MinAppVersion: bundle.Config.MinAppVersion,
|
|
LevelConfigs: buildLevelPayloads(bundle.Levels, rewardItems, 0, 0, false),
|
|
HasRewardRecord: false,
|
|
}
|
|
if !bundle.Config.Enabled {
|
|
resp.ActivityStatus = activityStatusDisabled
|
|
}
|
|
if bundle.Config.Enabled && !isFirstRechargeRewardAppVersionSupported(appVersion, bundle.Config.MinAppVersion) {
|
|
resp.Enabled = false
|
|
resp.ActivityStatus = activityStatusDisabled
|
|
}
|
|
|
|
record, ok, err := s.loadUserGrantRecord(ctx, bundle.Config.SysOrigin, user.UserID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !ok {
|
|
return resp, nil
|
|
}
|
|
|
|
resp.HasRewardRecord = true
|
|
resp.RewardStatus = record.Status
|
|
resp.Rewarded = record.Status == statusSuccess
|
|
resp.MatchedLevel = record.Level
|
|
resp.RechargeAmount = formatAmountCents(record.RechargeAmountCents)
|
|
resp.RechargeAmountCents = record.RechargeAmountCents
|
|
resp.RewardGroupID = record.RewardGroupID
|
|
resp.RewardGroupName = record.RewardGroupName
|
|
resp.RewardItems = rewardItems[record.RewardGroupID]
|
|
resp.LevelConfigs = buildLevelPayloads(bundle.Levels, rewardItems, record.RechargeAmountCents, record.Level, false)
|
|
|
|
switch record.Status {
|
|
case statusSuccess:
|
|
resp.ActivityStatus = activityStatusRewarded
|
|
case statusPending:
|
|
resp.ActivityStatus = activityStatusPending
|
|
case statusFailed:
|
|
resp.ActivityStatus = activityStatusFailed
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
func (s *Service) loadUserGrantRecord(ctx context.Context, sysOrigin string, userID int64) (model.FirstRechargeRewardGrantRecord, bool, error) {
|
|
if userID <= 0 {
|
|
return model.FirstRechargeRewardGrantRecord{}, false, nil
|
|
}
|
|
var record model.FirstRechargeRewardGrantRecord
|
|
err := s.db.WithContext(ctx).
|
|
Where("sys_origin = ? AND user_id = ?", s.normalizeSysOrigin(sysOrigin), userID).
|
|
First(&record).Error
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return model.FirstRechargeRewardGrantRecord{}, false, nil
|
|
}
|
|
if err != nil {
|
|
return model.FirstRechargeRewardGrantRecord{}, false, err
|
|
}
|
|
return record, true, nil
|
|
}
|