363 lines
11 KiB
Go
363 lines
11 KiB
Go
package domain
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
ErrFreePlanMustHaveZeroPrice = errors.New("free plans must have price 0")
|
|
ErrPaidPlanMustHavePositivePrice = errors.New("paid plans must have price greater than 0")
|
|
// ErrDuplicateSubscriptionPlanPeriod: only one plan (free or paid) per category + billing period.
|
|
ErrDuplicateSubscriptionPlanPeriod = errors.New("a subscription plan already exists for this category and billing period")
|
|
)
|
|
|
|
type SubscriptionCategory string
|
|
|
|
const (
|
|
// EDUCATION is the canonical category for makeup artistry courses and passes.
|
|
SubscriptionCategoryEducation SubscriptionCategory = "EDUCATION"
|
|
// SubscriptionCategoryLearnEnglish remains for historical plan and subscription records.
|
|
SubscriptionCategoryLearnEnglish SubscriptionCategory = "LEARN_ENGLISH"
|
|
SubscriptionCategoryIELTS SubscriptionCategory = "IELTS"
|
|
SubscriptionCategoryDuolingo SubscriptionCategory = "DUOLINGO"
|
|
)
|
|
|
|
// SubscriptionExpiryReminder is a Learn English subscription expiring in seven days.
|
|
type SubscriptionExpiryReminder struct {
|
|
UserID int64
|
|
PlanName string
|
|
ExpiresAt time.Time
|
|
}
|
|
|
|
// CategorySubscriptionGateDisabled skips subscription enforcement on learner-facing routes (temporary).
|
|
var CategorySubscriptionGateDisabled = false
|
|
|
|
// IsValidSubscriptionCategory reports whether category is a known plan category.
|
|
func IsValidSubscriptionCategory(category string) bool {
|
|
switch normalizeSubscriptionCategory(category) {
|
|
case string(SubscriptionCategoryEducation):
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// IsLMSSubscriptionCategory reports plan categories that unlock Learn English (LMS) content.
|
|
func IsLMSSubscriptionCategory(category string) bool {
|
|
normalized := normalizeSubscriptionCategory(category)
|
|
return normalized == string(SubscriptionCategoryEducation) || normalized == string(SubscriptionCategoryLearnEnglish)
|
|
}
|
|
|
|
// IsExamPrepSubscriptionCategory reports plan categories that unlock exam-prep content.
|
|
func IsExamPrepSubscriptionCategory(category string) bool {
|
|
switch normalizeSubscriptionCategory(category) {
|
|
case string(SubscriptionCategoryIELTS), string(SubscriptionCategoryDuolingo):
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// IsLMSContentCategory reports content categories stored on LMS programs.
|
|
func IsLMSContentCategory(category string) bool {
|
|
return IsLMSSubscriptionCategory(category)
|
|
}
|
|
|
|
// IsExamPrepContentCategory reports content categories stored on exam-prep catalog courses.
|
|
func IsExamPrepContentCategory(category string) bool {
|
|
return IsExamPrepSubscriptionCategory(category)
|
|
}
|
|
|
|
// SubscriptionGrantsContentAccess reports whether an active plan category unlocks the given content category.
|
|
func SubscriptionGrantsContentAccess(subscriptionCategory, contentCategory string) bool {
|
|
sub := normalizeSubscriptionCategory(subscriptionCategory)
|
|
content := normalizeSubscriptionCategory(contentCategory)
|
|
if IsLMSContentCategory(content) {
|
|
return IsLMSSubscriptionCategory(sub)
|
|
}
|
|
if IsExamPrepContentCategory(content) {
|
|
return sub == content
|
|
}
|
|
return false
|
|
}
|
|
|
|
func normalizeSubscriptionCategory(category string) string {
|
|
return strings.ToUpper(strings.TrimSpace(category))
|
|
}
|
|
|
|
type DurationUnit string
|
|
|
|
const (
|
|
DurationUnitDay DurationUnit = "DAY"
|
|
DurationUnitWeek DurationUnit = "WEEK"
|
|
DurationUnitMonth DurationUnit = "MONTH"
|
|
DurationUnitYear DurationUnit = "YEAR"
|
|
)
|
|
|
|
type SubscriptionStatus string
|
|
|
|
const (
|
|
SubscriptionStatusPending SubscriptionStatus = "PENDING"
|
|
SubscriptionStatusActive SubscriptionStatus = "ACTIVE"
|
|
SubscriptionStatusExpired SubscriptionStatus = "EXPIRED"
|
|
SubscriptionStatusCancelled SubscriptionStatus = "CANCELLED"
|
|
)
|
|
|
|
type SubscriptionPlan struct {
|
|
ID int64
|
|
Name string
|
|
Description *string
|
|
Category string
|
|
DurationValue int32
|
|
DurationUnit string
|
|
Price float64
|
|
Currency string
|
|
IsLifetime bool
|
|
IsFree bool
|
|
IsActive bool
|
|
CreatedAt time.Time
|
|
UpdatedAt *time.Time
|
|
}
|
|
|
|
type UserSubscription struct {
|
|
ID int64
|
|
UserID int64
|
|
PlanID int64
|
|
StartsAt time.Time
|
|
ExpiresAt time.Time
|
|
Status string
|
|
PaymentReference *string
|
|
PaymentMethod *string
|
|
AutoRenew bool
|
|
CancelledAt *time.Time
|
|
CreatedAt time.Time
|
|
UpdatedAt *time.Time
|
|
// Joined fields from plan
|
|
PlanName *string
|
|
PlanCategory *string
|
|
PlanIsLifetime *bool
|
|
DurationValue *int32
|
|
DurationUnit *string
|
|
Price *float64
|
|
Currency *string
|
|
}
|
|
|
|
type CreateSubscriptionPlanInput struct {
|
|
Name string
|
|
Description *string
|
|
Category string
|
|
DurationValue int32
|
|
DurationUnit string
|
|
Price float64
|
|
Currency string
|
|
IsLifetime bool
|
|
IsFree bool
|
|
IsActive *bool
|
|
}
|
|
|
|
type UpdateSubscriptionPlanInput struct {
|
|
Name *string
|
|
Description *string
|
|
Category *string
|
|
DurationValue *int32
|
|
DurationUnit *string
|
|
Price *float64
|
|
Currency *string
|
|
IsLifetime *bool
|
|
IsFree *bool
|
|
IsActive *bool
|
|
}
|
|
|
|
// NormalizeSubscriptionPlanPricing enforces free vs paid pricing rules.
|
|
func NormalizeSubscriptionPlanPricing(isFree bool, price float64) (bool, float64, error) {
|
|
if isFree {
|
|
if price > 0 {
|
|
return false, 0, ErrFreePlanMustHaveZeroPrice
|
|
}
|
|
return true, 0, nil
|
|
}
|
|
if price <= 0 {
|
|
return false, 0, ErrPaidPlanMustHavePositivePrice
|
|
}
|
|
return false, price, nil
|
|
}
|
|
|
|
type CreateUserSubscriptionInput struct {
|
|
UserID int64
|
|
PlanID int64
|
|
StartsAt *time.Time
|
|
ExpiresAt time.Time
|
|
Status *string
|
|
PaymentReference *string
|
|
PaymentMethod *string
|
|
AutoRenew *bool
|
|
}
|
|
|
|
// AdminExtendSubscriptionInput is kept for API completeness; the admin panel
|
|
// unified endpoint only sends plan_id and renews one period of that plan.
|
|
type AdminExtendSubscriptionInput struct {
|
|
NewExpiresAt *time.Time
|
|
DurationValue *int32
|
|
DurationUnit *string
|
|
RenewOnePeriod bool
|
|
}
|
|
|
|
// AdminApplySubscriptionInput is the unified admin grant-or-extend request.
|
|
type AdminApplySubscriptionInput struct {
|
|
PlanID int64
|
|
// RecordPayment when true creates a SUCCESS payment at the plan price.
|
|
// When false, access is granted/extended with no payments row.
|
|
RecordPayment bool
|
|
}
|
|
|
|
// AdminApplySubscriptionResult is the outcome of the unified grant-or-extend admin API.
|
|
type AdminApplySubscriptionResult struct {
|
|
Subscription *UserSubscription
|
|
Created bool // true when a new subscription row was granted
|
|
PaymentRecorded bool // true when a SUCCESS admin payment was inserted
|
|
}
|
|
|
|
// LifetimeSubscriptionExpiresAt is stored for one-time (non-expiring) subscription plans.
|
|
var LifetimeSubscriptionExpiresAt = time.Date(9999, 12, 31, 23, 59, 59, 0, time.UTC)
|
|
|
|
// LifetimeDurationValue / LifetimeDurationUnit are stored placeholders for lifetime plans
|
|
// (duration_unit CHECK still requires DAY|WEEK|MONTH|YEAR).
|
|
const (
|
|
LifetimeDurationValue int32 = 1
|
|
LifetimeDurationUnit string = "YEAR"
|
|
)
|
|
|
|
// IsLifetimeExpiry reports whether expiresAt is the far-future lifetime sentinel.
|
|
func IsLifetimeExpiry(expiresAt time.Time) bool {
|
|
return !expiresAt.IsZero() && expiresAt.UTC().Year() >= 9000
|
|
}
|
|
|
|
// NormalizeLifetimePlanCreateDuration forces placeholder duration when a plan is one-time.
|
|
func NormalizeLifetimePlanCreateDuration(isLifetime bool, durationValue int32, durationUnit string) (int32, string) {
|
|
if isLifetime {
|
|
return LifetimeDurationValue, LifetimeDurationUnit
|
|
}
|
|
return durationValue, strings.ToUpper(strings.TrimSpace(durationUnit))
|
|
}
|
|
|
|
// SubscriptionPlanPeriod identifies a plan's category and billing period (price-independent).
|
|
type SubscriptionPlanPeriod struct {
|
|
Category string
|
|
IsLifetime bool
|
|
DurationValue int32
|
|
DurationUnit string
|
|
}
|
|
|
|
func SubscriptionPlanPeriodFromCreate(input CreateSubscriptionPlanInput) SubscriptionPlanPeriod {
|
|
return subscriptionPlanPeriod(
|
|
input.Category,
|
|
input.IsLifetime,
|
|
input.DurationValue,
|
|
input.DurationUnit,
|
|
)
|
|
}
|
|
|
|
func SubscriptionPlanPeriodFromPlan(plan SubscriptionPlan) SubscriptionPlanPeriod {
|
|
return subscriptionPlanPeriod(
|
|
plan.Category,
|
|
plan.IsLifetime,
|
|
plan.DurationValue,
|
|
plan.DurationUnit,
|
|
)
|
|
}
|
|
|
|
func SubscriptionPlanPeriodFromUpdate(existing SubscriptionPlan, input UpdateSubscriptionPlanInput) SubscriptionPlanPeriod {
|
|
category := existing.Category
|
|
if input.Category != nil {
|
|
category = *input.Category
|
|
}
|
|
isLifetime := existing.IsLifetime
|
|
if input.IsLifetime != nil {
|
|
isLifetime = *input.IsLifetime
|
|
}
|
|
durationValue := existing.DurationValue
|
|
if input.DurationValue != nil {
|
|
durationValue = *input.DurationValue
|
|
}
|
|
durationUnit := existing.DurationUnit
|
|
if input.DurationUnit != nil {
|
|
durationUnit = *input.DurationUnit
|
|
}
|
|
return subscriptionPlanPeriod(category, isLifetime, durationValue, durationUnit)
|
|
}
|
|
|
|
func subscriptionPlanPeriod(category string, isLifetime bool, durationValue int32, durationUnit string) SubscriptionPlanPeriod {
|
|
dv, du := NormalizeLifetimePlanCreateDuration(isLifetime, durationValue, durationUnit)
|
|
return SubscriptionPlanPeriod{
|
|
Category: normalizeSubscriptionCategory(category),
|
|
IsLifetime: isLifetime,
|
|
DurationValue: dv,
|
|
DurationUnit: du,
|
|
}
|
|
}
|
|
|
|
// SameSubscriptionPlanPeriod reports whether two plans share category and billing period
|
|
// (is_free and price are ignored — free and paid plans cannot coexist for the same period).
|
|
func SameSubscriptionPlanPeriod(a, b SubscriptionPlanPeriod) bool {
|
|
return a.Category == b.Category &&
|
|
a.IsLifetime == b.IsLifetime &&
|
|
a.DurationValue == b.DurationValue &&
|
|
a.DurationUnit == b.DurationUnit
|
|
}
|
|
|
|
// CalculateSubscriptionExpiresAt returns when a subscription ends.
|
|
// Lifetime plans never expire (sentinel expires_at).
|
|
func CalculateSubscriptionExpiresAt(startTime time.Time, isLifetime bool, durationValue int32, durationUnit string) time.Time {
|
|
if isLifetime {
|
|
return LifetimeSubscriptionExpiresAt
|
|
}
|
|
return CalculateExpiryDate(startTime, durationValue, durationUnit)
|
|
}
|
|
|
|
// CalculateExpiryDate calculates the expiry date based on plan duration
|
|
func CalculateExpiryDate(startTime time.Time, durationValue int32, durationUnit string) time.Time {
|
|
switch durationUnit {
|
|
case string(DurationUnitDay):
|
|
return startTime.AddDate(0, 0, int(durationValue))
|
|
case string(DurationUnitWeek):
|
|
return startTime.AddDate(0, 0, int(durationValue)*7)
|
|
case string(DurationUnitMonth):
|
|
return startTime.AddDate(0, int(durationValue), 0)
|
|
case string(DurationUnitYear):
|
|
return startTime.AddDate(int(durationValue), 0, 0)
|
|
default:
|
|
return startTime.AddDate(0, int(durationValue), 0) // Default to months
|
|
}
|
|
}
|
|
|
|
// UserSubscriptionAdminRow includes learner identity for admin list/export.
|
|
type UserSubscriptionAdminRow struct {
|
|
UserSubscription
|
|
UserEmail string
|
|
UserFirstName string
|
|
UserLastName string
|
|
}
|
|
|
|
// UserSubscriptionAdminFilter filters the global admin subscription list.
|
|
type UserSubscriptionAdminFilter struct {
|
|
UserID *int64
|
|
PlanID *int64
|
|
Status *string
|
|
PlanCategory *string
|
|
CreatedFrom *time.Time
|
|
CreatedTo *time.Time
|
|
ExpiresFrom *time.Time
|
|
ExpiresTo *time.Time
|
|
Limit int32
|
|
Offset int32
|
|
}
|
|
|
|
// UserSubscriptionAdminPage is a paginated admin subscription list.
|
|
type UserSubscriptionAdminPage struct {
|
|
Items []UserSubscriptionAdminRow
|
|
Total int64
|
|
Limit, Offset int32
|
|
}
|