|
| 1 | +package services |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/NdoleStudio/httpsms/pkg/entities" |
| 8 | + "github.com/NdoleStudio/httpsms/pkg/repositories" |
| 9 | + "github.com/NdoleStudio/httpsms/pkg/telemetry" |
| 10 | + "github.com/palantir/stacktrace" |
| 11 | +) |
| 12 | + |
| 13 | +// entityLimits maps entity name → subscription plan → max count. |
| 14 | +// A limit of 0 means unlimited. If a plan is not listed, it defaults to unlimited (0). |
| 15 | +var entityLimits = map[string]map[entities.SubscriptionName]int{ |
| 16 | + "MessageSendSchedule": { |
| 17 | + entities.SubscriptionNameFree: 1, |
| 18 | + }, |
| 19 | +} |
| 20 | + |
| 21 | +// EntitlementCheckResult holds the outcome of an entitlement check. |
| 22 | +type EntitlementCheckResult struct { |
| 23 | + Allowed bool |
| 24 | + Message string |
| 25 | +} |
| 26 | + |
| 27 | +// EntitlementService checks whether a user can create more of a given entity |
| 28 | +// based on their subscription plan. |
| 29 | +type EntitlementService struct { |
| 30 | + service |
| 31 | + logger telemetry.Logger |
| 32 | + tracer telemetry.Tracer |
| 33 | + enabled bool |
| 34 | + userRepository repositories.UserRepository |
| 35 | +} |
| 36 | + |
| 37 | +// NewEntitlementService creates a new EntitlementService. |
| 38 | +// The enabled flag should come from the ENTITLEMENT_ENABLED environment variable. |
| 39 | +func NewEntitlementService( |
| 40 | + logger telemetry.Logger, |
| 41 | + tracer telemetry.Tracer, |
| 42 | + enabled bool, |
| 43 | + userRepository repositories.UserRepository, |
| 44 | +) *EntitlementService { |
| 45 | + return &EntitlementService{ |
| 46 | + logger: logger.WithService(fmt.Sprintf("%T", &EntitlementService{})), |
| 47 | + tracer: tracer, |
| 48 | + enabled: enabled, |
| 49 | + userRepository: userRepository, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// Check verifies if the user can create another instance of the given entity. |
| 54 | +func (service *EntitlementService) Check( |
| 55 | + ctx context.Context, |
| 56 | + userID entities.UserID, |
| 57 | + entityName string, |
| 58 | + currentCount int, |
| 59 | +) (*EntitlementCheckResult, error) { |
| 60 | + ctx, span := service.tracer.Start(ctx) |
| 61 | + defer span.End() |
| 62 | + |
| 63 | + if !service.enabled { |
| 64 | + return &EntitlementCheckResult{Allowed: true}, nil |
| 65 | + } |
| 66 | + |
| 67 | + limits, exists := entityLimits[entityName] |
| 68 | + if !exists { |
| 69 | + return &EntitlementCheckResult{Allowed: true}, nil |
| 70 | + } |
| 71 | + |
| 72 | + user, err := service.userRepository.Load(ctx, userID) |
| 73 | + if err != nil { |
| 74 | + return nil, service.tracer.WrapErrorSpan( |
| 75 | + span, |
| 76 | + stacktrace.Propagate(err, fmt.Sprintf("cannot load user [%s] for entitlement check", userID)), |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + limit, hasLimit := limits[user.SubscriptionName] |
| 81 | + if !hasLimit || limit == 0 { |
| 82 | + return &EntitlementCheckResult{Allowed: true}, nil |
| 83 | + } |
| 84 | + |
| 85 | + if currentCount >= limit { |
| 86 | + return &EntitlementCheckResult{ |
| 87 | + Allowed: false, |
| 88 | + Message: fmt.Sprintf( |
| 89 | + "Upgrade to a paid plan to create more than %d send schedule. Visit https://httpsms.com/pricing for details.", |
| 90 | + limit, |
| 91 | + ), |
| 92 | + }, nil |
| 93 | + } |
| 94 | + |
| 95 | + return &EntitlementCheckResult{Allowed: true}, nil |
| 96 | +} |
0 commit comments