Extend the DUOLINGO far-future expires_at pattern to IELTS, backfill existing IELTS rows, and limit expiry sweeps to LEARN_ENGLISH only. Co-authored-by: Cursor <cursoragent@cursor.com>
251 lines
7.3 KiB
Go
251 lines
7.3 KiB
Go
package domain
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type SubscriptionCategory string
|
|
|
|
const (
|
|
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
|
|
|
|
// ExamPrepSubscriptionGateDisabled skips IELTS/DUOLINGO subscription checks on exam-prep routes (temporary).
|
|
var ExamPrepSubscriptionGateDisabled = true
|
|
|
|
// IsValidSubscriptionCategory reports whether category is a known plan category.
|
|
func IsValidSubscriptionCategory(category string) bool {
|
|
switch normalizeSubscriptionCategory(category) {
|
|
case string(SubscriptionCategoryLearnEnglish),
|
|
string(SubscriptionCategoryIELTS),
|
|
string(SubscriptionCategoryDuolingo):
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// IsLMSSubscriptionCategory reports plan categories that unlock Learn English (LMS) content.
|
|
func IsLMSSubscriptionCategory(category string) bool {
|
|
return normalizeSubscriptionCategory(category) == 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 sub == string(SubscriptionCategoryLearnEnglish)
|
|
}
|
|
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
|
|
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
|
|
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
|
|
IsActive *bool
|
|
}
|
|
|
|
type UpdateSubscriptionPlanInput struct {
|
|
Name *string
|
|
Description *string
|
|
Category *string
|
|
DurationValue *int32
|
|
DurationUnit *string
|
|
Price *float64
|
|
Currency *string
|
|
IsActive *bool
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// LifetimeSubscriptionExpiresAt is stored for one-time (non-expiring) IELTS and DUOLINGO subscriptions.
|
|
var LifetimeSubscriptionExpiresAt = time.Date(9999, 12, 31, 23, 59, 59, 0, time.UTC)
|
|
|
|
// IsLifetimeSubscriptionCategory reports categories that grant permanent access after purchase.
|
|
func IsLifetimeSubscriptionCategory(category string) bool {
|
|
switch normalizeSubscriptionCategory(category) {
|
|
case string(SubscriptionCategoryIELTS), string(SubscriptionCategoryDuolingo):
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// CalculateSubscriptionExpiresAt returns when a subscription ends.
|
|
// IELTS and DUOLINGO plans are one-time and never expire.
|
|
func CalculateSubscriptionExpiresAt(startTime time.Time, category string, durationValue int32, durationUnit string) time.Time {
|
|
if IsLifetimeSubscriptionCategory(category) {
|
|
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
|
|
}
|