f
This commit is contained in:
16
internal/application/product/self_subscribe_policy.go
Normal file
16
internal/application/product/self_subscribe_policy.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package product
|
||||
|
||||
import "context"
|
||||
|
||||
// SelfSubscribePolicy 是否允许用户在控制台自助发起「订阅产品」
|
||||
type SelfSubscribePolicy interface {
|
||||
Allow(ctx context.Context, userID string) (allowed bool, message string, err error)
|
||||
}
|
||||
|
||||
// DefaultAllowSelfSubscribe 未装配下属模块时:恒允许
|
||||
type DefaultAllowSelfSubscribe struct{}
|
||||
|
||||
// Allow 恒允许
|
||||
func (DefaultAllowSelfSubscribe) Allow(_ context.Context, _ string) (bool, string, error) {
|
||||
return true, "", nil
|
||||
}
|
||||
@@ -23,6 +23,7 @@ type SubscriptionApplicationServiceImpl struct {
|
||||
productSubscriptionService *product_service.ProductSubscriptionService
|
||||
userRepo user_repositories.UserRepository
|
||||
apiCallRepository domain_api_repo.ApiCallRepository
|
||||
selfSubscribePolicy SelfSubscribePolicy
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
@@ -31,12 +32,17 @@ func NewSubscriptionApplicationService(
|
||||
productSubscriptionService *product_service.ProductSubscriptionService,
|
||||
userRepo user_repositories.UserRepository,
|
||||
apiCallRepository domain_api_repo.ApiCallRepository,
|
||||
selfSubscribePolicy SelfSubscribePolicy,
|
||||
logger *zap.Logger,
|
||||
) SubscriptionApplicationService {
|
||||
if selfSubscribePolicy == nil {
|
||||
selfSubscribePolicy = DefaultAllowSelfSubscribe{}
|
||||
}
|
||||
return &SubscriptionApplicationServiceImpl{
|
||||
productSubscriptionService: productSubscriptionService,
|
||||
userRepo: userRepo,
|
||||
apiCallRepository: apiCallRepository,
|
||||
selfSubscribePolicy: selfSubscribePolicy,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
@@ -157,7 +163,17 @@ func (s *SubscriptionApplicationServiceImpl) BatchUpdateSubscriptionPrices(ctx c
|
||||
// CreateSubscription 创建订阅
|
||||
// 业务流程:1. 创建订阅
|
||||
func (s *SubscriptionApplicationServiceImpl) CreateSubscription(ctx context.Context, cmd *commands.CreateSubscriptionCommand) error {
|
||||
_, err := s.productSubscriptionService.CreateSubscription(ctx, cmd.UserID, cmd.ProductID)
|
||||
allow, msg, err := s.selfSubscribePolicy.Allow(ctx, cmd.UserID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !allow {
|
||||
if msg == "" {
|
||||
msg = "当前账号不允许自助订阅"
|
||||
}
|
||||
return fmt.Errorf("%s", msg)
|
||||
}
|
||||
_, err = s.productSubscriptionService.CreateSubscription(ctx, cmd.UserID, cmd.ProductID)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user