subscription expiration six
This commit is contained in:
parent
dd802aca37
commit
09fc1e80f7
128
.cursor/plans/ielts_duolingo_lifetime_subscriptions_plan.md
Normal file
128
.cursor/plans/ielts_duolingo_lifetime_subscriptions_plan.md
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
# Plan: One-time (non-expiring) IELTS + DUOLINGO subscriptions
|
||||
|
||||
## Goal
|
||||
|
||||
Treat **IELTS** and **DUOLINGO** plan categories as one-time purchases that never expire. **LEARN_ENGLISH** continues to use duration-based expiry and 7-day reminders.
|
||||
|
||||
## Approach
|
||||
|
||||
Extend the existing DUOLINGO lifetime pattern (far-future `expires_at` sentinel `9999-12-31`) to also cover IELTS. Do **not** make `expires_at` nullable or add a `LIFETIME` duration unit.
|
||||
|
||||
Lifetime logic already funnels through `domain.IsLifetimeSubscriptionCategory` → `CalculateSubscriptionExpiresAt`. Checkout, subscribe, admin grant/extend, and activated-email copy already call those helpers.
|
||||
|
||||
## Product decisions (assumed)
|
||||
|
||||
1. Admin create/edit plan: hide duration for IELTS/DUOLINGO; show **One-time · Never expires**; still POST `duration_value=1`, `duration_unit=YEAR` so API validation passes.
|
||||
2. Admin Extend: hide/disable for lifetime-category active subscriptions (already lifetime).
|
||||
3. Migration: backfill existing IELTS `user_subscriptions` to the sentinel (same shape as `000079` for DUOLINGO; unscoped by status).
|
||||
|
||||
---
|
||||
|
||||
## Backend (`Yimaru-BackEnd`)
|
||||
|
||||
### 1. Lifetime predicate
|
||||
|
||||
**File:** `internal/domain/subscriptions.go`
|
||||
|
||||
- Update comments on `LifetimeSubscriptionExpiresAt` / `CalculateSubscriptionExpiresAt`.
|
||||
- Change `IsLifetimeSubscriptionCategory` to return true for `IELTS` **and** `DUOLINGO` (use `normalizeSubscriptionCategory`).
|
||||
|
||||
No changes needed in subscribe/checkout/admin apply services — they already call `CalculateSubscriptionExpiresAt`.
|
||||
|
||||
### 2. Expiry sweep queries
|
||||
|
||||
**File:** `db/query/subscriptions.sql`
|
||||
|
||||
In `GetExpiredSubscriptions` and `GetExpiringSubscriptions`, replace:
|
||||
|
||||
```sql
|
||||
AND sp.category <> 'DUOLINGO'
|
||||
```
|
||||
|
||||
with:
|
||||
|
||||
```sql
|
||||
AND sp.category = 'LEARN_ENGLISH'
|
||||
```
|
||||
|
||||
(`ListLearnEnglishSubscriptionsExpiringInSevenDays` already filters LEARN_ENGLISH only.)
|
||||
|
||||
### 3. Regenerate sqlc
|
||||
|
||||
```bash
|
||||
sqlc generate
|
||||
```
|
||||
|
||||
Updates `gen/db/subscriptions.sql.go`.
|
||||
|
||||
### 4. Migration `000099` (next after `000098`)
|
||||
|
||||
**Up** (`db/migrations/000099_ielts_lifetime_subscriptions.up.sql`) — mirror `000079`:
|
||||
|
||||
```sql
|
||||
UPDATE user_subscriptions us
|
||||
SET
|
||||
expires_at = TIMESTAMPTZ '9999-12-31 23:59:59+00',
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
FROM subscription_plans sp
|
||||
WHERE us.plan_id = sp.id
|
||||
AND sp.category = 'IELTS'
|
||||
AND us.expires_at < TIMESTAMPTZ '9999-01-01 00:00:00+00';
|
||||
```
|
||||
|
||||
**Down:** no-op (same as `000079`).
|
||||
|
||||
### 5. Tests
|
||||
|
||||
| File | Change |
|
||||
|------|--------|
|
||||
| `internal/domain/subscriptions_test.go` | Add IELTS lifetime expiry test; optional table-driven `IsLifetimeSubscriptionCategory` |
|
||||
| `internal/services/learnernotifications/subscription_test.go` | Expect IELTS `lifetime=true` / empty date label (today expects date) |
|
||||
|
||||
---
|
||||
|
||||
## Admin (`Yimaru-Admin`)
|
||||
|
||||
### 1. Shared helpers
|
||||
|
||||
**File:** `src/lib/subscriptionPlans.ts`
|
||||
|
||||
- `isLifetimePlanCategory(category)` → true for `IELTS` / `DUOLINGO`
|
||||
- `LIFETIME_DURATION_DEFAULT = { duration_value: 1, duration_unit: "YEAR" }`
|
||||
- `formatPlanDuration`: if `plan.category` is lifetime → `"One-time"` (widen signature to accept optional `category`)
|
||||
|
||||
### 2. Plan dialogs
|
||||
|
||||
| File | Change |
|
||||
|------|--------|
|
||||
| `CreateSubscriptionPlanDialog.tsx` | When lifetime category: hide duration inputs; show “One-time · Never expires”; force default duration in payload |
|
||||
| `EditSubscriptionPlanDialog.tsx` | Same (category already read-only) |
|
||||
|
||||
### 3. User subscription UX
|
||||
|
||||
| File | Change |
|
||||
|------|--------|
|
||||
| `UserSubscriptionsSection.tsx` | Lifetime expiry → **Never expires**; suppress days-left; hide/disable Extend when `isLifetimePlanCategory(plan_category)` |
|
||||
| `ExtendSubscriptionDialog.tsx` | Safety copy if opened for lifetime (optional) |
|
||||
| Call sites of `formatPlanDuration` | Pass `category` so lists/dashboard/grant show “One-time” |
|
||||
|
||||
---
|
||||
|
||||
## Out of scope
|
||||
|
||||
- Nullable `expires_at`
|
||||
- New `LIFETIME` / `ONE_TIME` duration unit or DB CHECK change
|
||||
- Reactivating historically `EXPIRED` IELTS rows (migration only updates `expires_at`, not `status`)
|
||||
- Changing exam-prep access gate flag
|
||||
|
||||
---
|
||||
|
||||
## Verification
|
||||
|
||||
1. `go test ./internal/domain/... ./internal/services/learnernotifications/...`
|
||||
2. `sqlc generate` + `go build ./...`
|
||||
3. Run migration up; IELTS rows show `expires_at = 9999-12-31`
|
||||
4. New IELTS/DUOLINGO subscribe/grant → sentinel expiry; LEARN_ENGLISH → real duration date
|
||||
5. Admin create IELTS plan → duration hidden, save succeeds
|
||||
6. User subscriptions panel → “Never expires”, no Extend for lifetime active sub
|
||||
7. 7-day reminder / GetExpiring still only LEARN_ENGLISH
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
ALTER TABLE subscription_plans
|
||||
DROP COLUMN IF EXISTS is_lifetime;
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
-- Per-plan lifetime (one-time) billing flag, independent of content category.
|
||||
ALTER TABLE subscription_plans
|
||||
ADD COLUMN IF NOT EXISTS is_lifetime BOOLEAN NOT NULL DEFAULT FALSE;
|
||||
|
||||
-- Preserve existing product behavior: IELTS and DUOLINGO plans remain one-time.
|
||||
UPDATE subscription_plans
|
||||
SET is_lifetime = TRUE
|
||||
WHERE UPPER(TRIM(category)) IN ('IELTS', 'DUOLINGO')
|
||||
AND is_lifetime = FALSE;
|
||||
|
|
@ -4,9 +4,9 @@
|
|||
|
||||
-- name: CreateSubscriptionPlan :one
|
||||
INSERT INTO subscription_plans (
|
||||
name, description, category, duration_value, duration_unit, price, currency, is_active
|
||||
name, description, category, duration_value, duration_unit, price, currency, is_lifetime, is_active
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, COALESCE($8, true))
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, COALESCE($8, false), COALESCE($9, true))
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetSubscriptionPlanByID :one
|
||||
|
|
@ -40,10 +40,20 @@ SET
|
|||
duration_unit = COALESCE(sqlc.narg('duration_unit')::varchar, duration_unit),
|
||||
price = COALESCE(sqlc.narg('price')::numeric, price),
|
||||
currency = COALESCE(sqlc.narg('currency')::varchar, currency),
|
||||
is_lifetime = COALESCE(sqlc.narg('is_lifetime')::boolean, is_lifetime),
|
||||
is_active = COALESCE(sqlc.narg('is_active')::boolean, is_active),
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = sqlc.arg('id');
|
||||
|
||||
-- name: SetLifetimeExpiresForActiveSubscriptionsByPlanID :exec
|
||||
UPDATE user_subscriptions
|
||||
SET
|
||||
expires_at = TIMESTAMPTZ '9999-12-31 23:59:59+00',
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE plan_id = $1
|
||||
AND status = 'ACTIVE'
|
||||
AND expires_at < TIMESTAMPTZ '9999-01-01 00:00:00+00';
|
||||
|
||||
-- name: DeleteSubscriptionPlan :exec
|
||||
DELETE FROM subscription_plans WHERE id = $1;
|
||||
|
||||
|
|
@ -63,6 +73,7 @@ SELECT
|
|||
us.*,
|
||||
sp.name AS plan_name,
|
||||
sp.category AS plan_category,
|
||||
sp.is_lifetime AS plan_is_lifetime,
|
||||
sp.duration_value,
|
||||
sp.duration_unit,
|
||||
sp.price,
|
||||
|
|
@ -109,6 +120,7 @@ SELECT
|
|||
us.*,
|
||||
sp.name AS plan_name,
|
||||
sp.category AS plan_category,
|
||||
sp.is_lifetime AS plan_is_lifetime,
|
||||
sp.duration_value,
|
||||
sp.duration_unit,
|
||||
sp.price,
|
||||
|
|
@ -126,6 +138,7 @@ SELECT
|
|||
us.*,
|
||||
sp.name AS plan_name,
|
||||
sp.category AS plan_category,
|
||||
sp.is_lifetime AS plan_is_lifetime,
|
||||
sp.duration_value,
|
||||
sp.duration_unit,
|
||||
sp.price,
|
||||
|
|
@ -142,6 +155,7 @@ SELECT
|
|||
us.*,
|
||||
sp.name AS plan_name,
|
||||
sp.category AS plan_category,
|
||||
sp.is_lifetime AS plan_is_lifetime,
|
||||
sp.duration_value,
|
||||
sp.duration_unit,
|
||||
sp.price,
|
||||
|
|
@ -156,6 +170,7 @@ SELECT
|
|||
us.*,
|
||||
sp.name AS plan_name,
|
||||
sp.category AS plan_category,
|
||||
sp.is_lifetime AS plan_is_lifetime,
|
||||
sp.duration_value,
|
||||
sp.duration_unit,
|
||||
sp.price,
|
||||
|
|
@ -220,7 +235,7 @@ SELECT us.*, sp.name AS plan_name
|
|||
FROM user_subscriptions us
|
||||
JOIN subscription_plans sp ON sp.id = us.plan_id
|
||||
WHERE us.status = 'ACTIVE'
|
||||
AND sp.category = 'LEARN_ENGLISH'
|
||||
AND sp.is_lifetime = FALSE
|
||||
AND us.expires_at <= CURRENT_TIMESTAMP;
|
||||
|
||||
-- name: ListLearnEnglishSubscriptionsExpiringInSevenDays :many
|
||||
|
|
@ -235,7 +250,7 @@ FROM user_subscriptions us
|
|||
JOIN subscription_plans sp ON sp.id = us.plan_id
|
||||
WHERE us.status = 'ACTIVE'
|
||||
AND us.expires_at > CURRENT_TIMESTAMP
|
||||
AND sp.category = 'LEARN_ENGLISH'
|
||||
AND sp.is_lifetime = FALSE
|
||||
AND us.expires_at::date = (CURRENT_DATE + INTERVAL '7 days')::date;
|
||||
|
||||
-- name: GetExpiringSubscriptions :many
|
||||
|
|
@ -248,7 +263,7 @@ FROM user_subscriptions us
|
|||
JOIN subscription_plans sp ON sp.id = us.plan_id
|
||||
JOIN users u ON u.id = us.user_id
|
||||
WHERE us.status = 'ACTIVE'
|
||||
AND sp.category = 'LEARN_ENGLISH'
|
||||
AND sp.is_lifetime = FALSE
|
||||
AND us.expires_at > CURRENT_TIMESTAMP
|
||||
AND us.expires_at <= CURRENT_TIMESTAMP + INTERVAL '7 days';
|
||||
|
||||
|
|
@ -305,6 +320,7 @@ SELECT
|
|||
us.updated_at,
|
||||
sp.name AS plan_name,
|
||||
sp.category AS plan_category,
|
||||
sp.is_lifetime AS plan_is_lifetime,
|
||||
sp.duration_value,
|
||||
sp.duration_unit,
|
||||
sp.price,
|
||||
|
|
|
|||
|
|
@ -545,6 +545,7 @@ type SubscriptionPlan struct {
|
|||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
Category string `json:"category"`
|
||||
IsLifetime bool `json:"is_lifetime"`
|
||||
}
|
||||
|
||||
type TeamInvitation struct {
|
||||
|
|
|
|||
|
|
@ -112,10 +112,10 @@ func (q *Queries) CountUserSubscriptions(ctx context.Context, userID int64) (int
|
|||
const CreateSubscriptionPlan = `-- name: CreateSubscriptionPlan :one
|
||||
|
||||
INSERT INTO subscription_plans (
|
||||
name, description, category, duration_value, duration_unit, price, currency, is_active
|
||||
name, description, category, duration_value, duration_unit, price, currency, is_lifetime, is_active
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, COALESCE($8, true))
|
||||
RETURNING id, name, description, duration_value, duration_unit, price, currency, is_active, created_at, updated_at, category
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, COALESCE($8, false), COALESCE($9, true))
|
||||
RETURNING id, name, description, duration_value, duration_unit, price, currency, is_active, created_at, updated_at, category, is_lifetime
|
||||
`
|
||||
|
||||
type CreateSubscriptionPlanParams struct {
|
||||
|
|
@ -127,6 +127,7 @@ type CreateSubscriptionPlanParams struct {
|
|||
Price pgtype.Numeric `json:"price"`
|
||||
Currency string `json:"currency"`
|
||||
Column8 interface{} `json:"column_8"`
|
||||
Column9 interface{} `json:"column_9"`
|
||||
}
|
||||
|
||||
// =====================
|
||||
|
|
@ -142,6 +143,7 @@ func (q *Queries) CreateSubscriptionPlan(ctx context.Context, arg CreateSubscrip
|
|||
arg.Price,
|
||||
arg.Currency,
|
||||
arg.Column8,
|
||||
arg.Column9,
|
||||
)
|
||||
var i SubscriptionPlan
|
||||
err := row.Scan(
|
||||
|
|
@ -156,6 +158,7 @@ func (q *Queries) CreateSubscriptionPlan(ctx context.Context, arg CreateSubscrip
|
|||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Category,
|
||||
&i.IsLifetime,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
|
@ -257,6 +260,7 @@ SELECT
|
|||
us.id, us.user_id, us.plan_id, us.starts_at, us.expires_at, us.status, us.payment_reference, us.payment_method, us.auto_renew, us.cancelled_at, us.created_at, us.updated_at,
|
||||
sp.name AS plan_name,
|
||||
sp.category AS plan_category,
|
||||
sp.is_lifetime AS plan_is_lifetime,
|
||||
sp.duration_value,
|
||||
sp.duration_unit,
|
||||
sp.price,
|
||||
|
|
@ -285,6 +289,7 @@ type GetActiveSubscriptionByUserIDRow struct {
|
|||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
PlanName string `json:"plan_name"`
|
||||
PlanCategory string `json:"plan_category"`
|
||||
PlanIsLifetime bool `json:"plan_is_lifetime"`
|
||||
DurationValue int32 `json:"duration_value"`
|
||||
DurationUnit string `json:"duration_unit"`
|
||||
Price pgtype.Numeric `json:"price"`
|
||||
|
|
@ -309,6 +314,7 @@ func (q *Queries) GetActiveSubscriptionByUserID(ctx context.Context, userID int6
|
|||
&i.UpdatedAt,
|
||||
&i.PlanName,
|
||||
&i.PlanCategory,
|
||||
&i.PlanIsLifetime,
|
||||
&i.DurationValue,
|
||||
&i.DurationUnit,
|
||||
&i.Price,
|
||||
|
|
@ -322,7 +328,7 @@ SELECT us.id, us.user_id, us.plan_id, us.starts_at, us.expires_at, us.status, us
|
|||
FROM user_subscriptions us
|
||||
JOIN subscription_plans sp ON sp.id = us.plan_id
|
||||
WHERE us.status = 'ACTIVE'
|
||||
AND sp.category = 'LEARN_ENGLISH'
|
||||
AND sp.is_lifetime = FALSE
|
||||
AND us.expires_at <= CURRENT_TIMESTAMP
|
||||
`
|
||||
|
||||
|
|
@ -386,7 +392,7 @@ FROM user_subscriptions us
|
|||
JOIN subscription_plans sp ON sp.id = us.plan_id
|
||||
JOIN users u ON u.id = us.user_id
|
||||
WHERE us.status = 'ACTIVE'
|
||||
AND sp.category = 'LEARN_ENGLISH'
|
||||
AND sp.is_lifetime = FALSE
|
||||
AND us.expires_at > CURRENT_TIMESTAMP
|
||||
AND us.expires_at <= CURRENT_TIMESTAMP + INTERVAL '7 days'
|
||||
`
|
||||
|
|
@ -467,7 +473,7 @@ func (q *Queries) GetSubscriptionDisplayStatusByUserID(ctx context.Context, user
|
|||
}
|
||||
|
||||
const GetSubscriptionPlanByID = `-- name: GetSubscriptionPlanByID :one
|
||||
SELECT id, name, description, duration_value, duration_unit, price, currency, is_active, created_at, updated_at, category FROM subscription_plans WHERE id = $1
|
||||
SELECT id, name, description, duration_value, duration_unit, price, currency, is_active, created_at, updated_at, category, is_lifetime FROM subscription_plans WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetSubscriptionPlanByID(ctx context.Context, id int64) (SubscriptionPlan, error) {
|
||||
|
|
@ -485,6 +491,7 @@ func (q *Queries) GetSubscriptionPlanByID(ctx context.Context, id int64) (Subscr
|
|||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Category,
|
||||
&i.IsLifetime,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
|
@ -494,6 +501,7 @@ SELECT
|
|||
us.id, us.user_id, us.plan_id, us.starts_at, us.expires_at, us.status, us.payment_reference, us.payment_method, us.auto_renew, us.cancelled_at, us.created_at, us.updated_at,
|
||||
sp.name AS plan_name,
|
||||
sp.category AS plan_category,
|
||||
sp.is_lifetime AS plan_is_lifetime,
|
||||
sp.duration_value,
|
||||
sp.duration_unit,
|
||||
sp.price,
|
||||
|
|
@ -518,6 +526,7 @@ type GetUserSubscriptionByIDRow struct {
|
|||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
PlanName string `json:"plan_name"`
|
||||
PlanCategory string `json:"plan_category"`
|
||||
PlanIsLifetime bool `json:"plan_is_lifetime"`
|
||||
DurationValue int32 `json:"duration_value"`
|
||||
DurationUnit string `json:"duration_unit"`
|
||||
Price pgtype.Numeric `json:"price"`
|
||||
|
|
@ -542,6 +551,7 @@ func (q *Queries) GetUserSubscriptionByID(ctx context.Context, id int64) (GetUse
|
|||
&i.UpdatedAt,
|
||||
&i.PlanName,
|
||||
&i.PlanCategory,
|
||||
&i.PlanIsLifetime,
|
||||
&i.DurationValue,
|
||||
&i.DurationUnit,
|
||||
&i.Price,
|
||||
|
|
@ -555,6 +565,7 @@ SELECT
|
|||
us.id, us.user_id, us.plan_id, us.starts_at, us.expires_at, us.status, us.payment_reference, us.payment_method, us.auto_renew, us.cancelled_at, us.created_at, us.updated_at,
|
||||
sp.name AS plan_name,
|
||||
sp.category AS plan_category,
|
||||
sp.is_lifetime AS plan_is_lifetime,
|
||||
sp.duration_value,
|
||||
sp.duration_unit,
|
||||
sp.price,
|
||||
|
|
@ -588,6 +599,7 @@ type GetUserSubscriptionHistoryRow struct {
|
|||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
PlanName string `json:"plan_name"`
|
||||
PlanCategory string `json:"plan_category"`
|
||||
PlanIsLifetime bool `json:"plan_is_lifetime"`
|
||||
DurationValue int32 `json:"duration_value"`
|
||||
DurationUnit string `json:"duration_unit"`
|
||||
Price pgtype.Numeric `json:"price"`
|
||||
|
|
@ -618,6 +630,7 @@ func (q *Queries) GetUserSubscriptionHistory(ctx context.Context, arg GetUserSub
|
|||
&i.UpdatedAt,
|
||||
&i.PlanName,
|
||||
&i.PlanCategory,
|
||||
&i.PlanIsLifetime,
|
||||
&i.DurationValue,
|
||||
&i.DurationUnit,
|
||||
&i.Price,
|
||||
|
|
@ -674,7 +687,7 @@ func (q *Queries) HasActiveSubscriptionByCategory(ctx context.Context, arg HasAc
|
|||
}
|
||||
|
||||
const ListActiveSubscriptionPlans = `-- name: ListActiveSubscriptionPlans :many
|
||||
SELECT id, name, description, duration_value, duration_unit, price, currency, is_active, created_at, updated_at, category FROM subscription_plans
|
||||
SELECT id, name, description, duration_value, duration_unit, price, currency, is_active, created_at, updated_at, category, is_lifetime FROM subscription_plans
|
||||
WHERE is_active = true
|
||||
ORDER BY price ASC
|
||||
`
|
||||
|
|
@ -700,6 +713,7 @@ func (q *Queries) ListActiveSubscriptionPlans(ctx context.Context) ([]Subscripti
|
|||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Category,
|
||||
&i.IsLifetime,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -716,6 +730,7 @@ SELECT
|
|||
us.id, us.user_id, us.plan_id, us.starts_at, us.expires_at, us.status, us.payment_reference, us.payment_method, us.auto_renew, us.cancelled_at, us.created_at, us.updated_at,
|
||||
sp.name AS plan_name,
|
||||
sp.category AS plan_category,
|
||||
sp.is_lifetime AS plan_is_lifetime,
|
||||
sp.duration_value,
|
||||
sp.duration_unit,
|
||||
sp.price,
|
||||
|
|
@ -743,6 +758,7 @@ type ListActiveSubscriptionsByUserIDRow struct {
|
|||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
PlanName string `json:"plan_name"`
|
||||
PlanCategory string `json:"plan_category"`
|
||||
PlanIsLifetime bool `json:"plan_is_lifetime"`
|
||||
DurationValue int32 `json:"duration_value"`
|
||||
DurationUnit string `json:"duration_unit"`
|
||||
Price pgtype.Numeric `json:"price"`
|
||||
|
|
@ -773,6 +789,7 @@ func (q *Queries) ListActiveSubscriptionsByUserID(ctx context.Context, userID in
|
|||
&i.UpdatedAt,
|
||||
&i.PlanName,
|
||||
&i.PlanCategory,
|
||||
&i.PlanIsLifetime,
|
||||
&i.DurationValue,
|
||||
&i.DurationUnit,
|
||||
&i.Price,
|
||||
|
|
@ -793,6 +810,7 @@ SELECT
|
|||
us.id, us.user_id, us.plan_id, us.starts_at, us.expires_at, us.status, us.payment_reference, us.payment_method, us.auto_renew, us.cancelled_at, us.created_at, us.updated_at,
|
||||
sp.name AS plan_name,
|
||||
sp.category AS plan_category,
|
||||
sp.is_lifetime AS plan_is_lifetime,
|
||||
sp.duration_value,
|
||||
sp.duration_unit,
|
||||
sp.price,
|
||||
|
|
@ -818,6 +836,7 @@ type ListAllUserSubscriptionsByUserIDRow struct {
|
|||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
PlanName string `json:"plan_name"`
|
||||
PlanCategory string `json:"plan_category"`
|
||||
PlanIsLifetime bool `json:"plan_is_lifetime"`
|
||||
DurationValue int32 `json:"duration_value"`
|
||||
DurationUnit string `json:"duration_unit"`
|
||||
Price pgtype.Numeric `json:"price"`
|
||||
|
|
@ -848,6 +867,7 @@ func (q *Queries) ListAllUserSubscriptionsByUserID(ctx context.Context, userID i
|
|||
&i.UpdatedAt,
|
||||
&i.PlanName,
|
||||
&i.PlanCategory,
|
||||
&i.PlanIsLifetime,
|
||||
&i.DurationValue,
|
||||
&i.DurationUnit,
|
||||
&i.Price,
|
||||
|
|
@ -875,7 +895,7 @@ FROM user_subscriptions us
|
|||
JOIN subscription_plans sp ON sp.id = us.plan_id
|
||||
WHERE us.status = 'ACTIVE'
|
||||
AND us.expires_at > CURRENT_TIMESTAMP
|
||||
AND sp.category = 'LEARN_ENGLISH'
|
||||
AND sp.is_lifetime = FALSE
|
||||
AND us.expires_at::date = (CURRENT_DATE + INTERVAL '7 days')::date
|
||||
`
|
||||
|
||||
|
|
@ -962,7 +982,7 @@ func (q *Queries) ListSubscriptionDisplayStatusesByUserIDs(ctx context.Context,
|
|||
}
|
||||
|
||||
const ListSubscriptionPlans = `-- name: ListSubscriptionPlans :many
|
||||
SELECT id, name, description, duration_value, duration_unit, price, currency, is_active, created_at, updated_at, category FROM subscription_plans
|
||||
SELECT id, name, description, duration_value, duration_unit, price, currency, is_active, created_at, updated_at, category, is_lifetime FROM subscription_plans
|
||||
WHERE (
|
||||
$1::boolean IS NULL
|
||||
OR $1::boolean = false
|
||||
|
|
@ -1001,6 +1021,7 @@ func (q *Queries) ListSubscriptionPlans(ctx context.Context, arg ListSubscriptio
|
|||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Category,
|
||||
&i.IsLifetime,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -1029,6 +1050,7 @@ SELECT
|
|||
us.updated_at,
|
||||
sp.name AS plan_name,
|
||||
sp.category AS plan_category,
|
||||
sp.is_lifetime AS plan_is_lifetime,
|
||||
sp.duration_value,
|
||||
sp.duration_unit,
|
||||
sp.price,
|
||||
|
|
@ -1080,6 +1102,7 @@ type ListUserSubscriptionsAdminRow struct {
|
|||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
PlanName string `json:"plan_name"`
|
||||
PlanCategory string `json:"plan_category"`
|
||||
PlanIsLifetime bool `json:"plan_is_lifetime"`
|
||||
DurationValue int32 `json:"duration_value"`
|
||||
DurationUnit string `json:"duration_unit"`
|
||||
Price pgtype.Numeric `json:"price"`
|
||||
|
|
@ -1125,6 +1148,7 @@ func (q *Queries) ListUserSubscriptionsAdmin(ctx context.Context, arg ListUserSu
|
|||
&i.UpdatedAt,
|
||||
&i.PlanName,
|
||||
&i.PlanCategory,
|
||||
&i.PlanIsLifetime,
|
||||
&i.DurationValue,
|
||||
&i.DurationUnit,
|
||||
&i.Price,
|
||||
|
|
@ -1143,6 +1167,21 @@ func (q *Queries) ListUserSubscriptionsAdmin(ctx context.Context, arg ListUserSu
|
|||
return items, nil
|
||||
}
|
||||
|
||||
const SetLifetimeExpiresForActiveSubscriptionsByPlanID = `-- name: SetLifetimeExpiresForActiveSubscriptionsByPlanID :exec
|
||||
UPDATE user_subscriptions
|
||||
SET
|
||||
expires_at = TIMESTAMPTZ '9999-12-31 23:59:59+00',
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE plan_id = $1
|
||||
AND status = 'ACTIVE'
|
||||
AND expires_at < TIMESTAMPTZ '9999-01-01 00:00:00+00'
|
||||
`
|
||||
|
||||
func (q *Queries) SetLifetimeExpiresForActiveSubscriptionsByPlanID(ctx context.Context, planID int64) error {
|
||||
_, err := q.db.Exec(ctx, SetLifetimeExpiresForActiveSubscriptionsByPlanID, planID)
|
||||
return err
|
||||
}
|
||||
|
||||
const UpdateAutoRenew = `-- name: UpdateAutoRenew :exec
|
||||
UPDATE user_subscriptions
|
||||
SET
|
||||
|
|
@ -1171,9 +1210,10 @@ SET
|
|||
duration_unit = COALESCE($5::varchar, duration_unit),
|
||||
price = COALESCE($6::numeric, price),
|
||||
currency = COALESCE($7::varchar, currency),
|
||||
is_active = COALESCE($8::boolean, is_active),
|
||||
is_lifetime = COALESCE($8::boolean, is_lifetime),
|
||||
is_active = COALESCE($9::boolean, is_active),
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = $9
|
||||
WHERE id = $10
|
||||
`
|
||||
|
||||
type UpdateSubscriptionPlanParams struct {
|
||||
|
|
@ -1184,6 +1224,7 @@ type UpdateSubscriptionPlanParams struct {
|
|||
DurationUnit pgtype.Text `json:"duration_unit"`
|
||||
Price pgtype.Numeric `json:"price"`
|
||||
Currency pgtype.Text `json:"currency"`
|
||||
IsLifetime pgtype.Bool `json:"is_lifetime"`
|
||||
IsActive pgtype.Bool `json:"is_active"`
|
||||
ID int64 `json:"id"`
|
||||
}
|
||||
|
|
@ -1197,6 +1238,7 @@ func (q *Queries) UpdateSubscriptionPlan(ctx context.Context, arg UpdateSubscrip
|
|||
arg.DurationUnit,
|
||||
arg.Price,
|
||||
arg.Currency,
|
||||
arg.IsLifetime,
|
||||
arg.IsActive,
|
||||
arg.ID,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ type AdminUserSubscriptionRecord struct {
|
|||
PlanID int64 `json:"plan_id"`
|
||||
PlanName string `json:"plan_name"`
|
||||
PlanCategory string `json:"plan_category"`
|
||||
IsLifetime bool `json:"is_lifetime"`
|
||||
StartsAt time.Time `json:"starts_at"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
Status string `json:"status"`
|
||||
|
|
|
|||
|
|
@ -107,6 +107,7 @@ type SubscriptionPlan struct {
|
|||
DurationUnit string
|
||||
Price float64
|
||||
Currency string
|
||||
IsLifetime bool
|
||||
IsActive bool
|
||||
CreatedAt time.Time
|
||||
UpdatedAt *time.Time
|
||||
|
|
@ -128,6 +129,7 @@ type UserSubscription struct {
|
|||
// Joined fields from plan
|
||||
PlanName *string
|
||||
PlanCategory *string
|
||||
PlanIsLifetime *bool
|
||||
DurationValue *int32
|
||||
DurationUnit *string
|
||||
Price *float64
|
||||
|
|
@ -142,6 +144,7 @@ type CreateSubscriptionPlanInput struct {
|
|||
DurationUnit string
|
||||
Price float64
|
||||
Currency string
|
||||
IsLifetime bool
|
||||
IsActive *bool
|
||||
}
|
||||
|
||||
|
|
@ -153,6 +156,7 @@ type UpdateSubscriptionPlanInput struct {
|
|||
DurationUnit *string
|
||||
Price *float64
|
||||
Currency *string
|
||||
IsLifetime *bool
|
||||
IsActive *bool
|
||||
}
|
||||
|
||||
|
|
@ -182,23 +186,33 @@ type AdminApplySubscriptionResult struct {
|
|||
Created bool // true when a new subscription row was granted
|
||||
}
|
||||
|
||||
// LifetimeSubscriptionExpiresAt is stored for one-time (non-expiring) IELTS and DUOLINGO subscriptions.
|
||||
// LifetimeSubscriptionExpiresAt is stored for one-time (non-expiring) subscription plans.
|
||||
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
|
||||
// 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, durationUnit
|
||||
}
|
||||
|
||||
// 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) {
|
||||
// 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)
|
||||
|
|
|
|||
|
|
@ -5,39 +5,41 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
func TestCalculateSubscriptionExpiresAt_duolingoIsLifetime(t *testing.T) {
|
||||
func TestCalculateSubscriptionExpiresAt_lifetime(t *testing.T) {
|
||||
start := time.Date(2026, 6, 10, 12, 0, 0, 0, time.UTC)
|
||||
got := CalculateSubscriptionExpiresAt(start, "DUOLINGO", 1, "MONTH")
|
||||
got := CalculateSubscriptionExpiresAt(start, true, 1, "MONTH")
|
||||
if !got.Equal(LifetimeSubscriptionExpiresAt) {
|
||||
t.Fatalf("expected lifetime expiry, got %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalculateSubscriptionExpiresAt_ieltsIsLifetime(t *testing.T) {
|
||||
func TestCalculateSubscriptionExpiresAt_usesDurationWhenNotLifetime(t *testing.T) {
|
||||
start := time.Date(2026, 6, 10, 12, 0, 0, 0, time.UTC)
|
||||
got := CalculateSubscriptionExpiresAt(start, "IELTS", 1, "MONTH")
|
||||
if !got.Equal(LifetimeSubscriptionExpiresAt) {
|
||||
t.Fatalf("expected lifetime expiry, got %v", got)
|
||||
got := CalculateSubscriptionExpiresAt(start, false, 1, "MONTH")
|
||||
want := start.AddDate(0, 1, 0)
|
||||
if !got.Equal(want) {
|
||||
t.Fatalf("got %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsLifetimeSubscriptionCategory(t *testing.T) {
|
||||
cases := []struct {
|
||||
category string
|
||||
want bool
|
||||
}{
|
||||
{"IELTS", true},
|
||||
{"ielts", true},
|
||||
{"DUOLINGO", true},
|
||||
{" duolingo ", true},
|
||||
{"LEARN_ENGLISH", false},
|
||||
{"", false},
|
||||
func TestIsLifetimeExpiry(t *testing.T) {
|
||||
if !IsLifetimeExpiry(LifetimeSubscriptionExpiresAt) {
|
||||
t.Fatal("sentinel should be lifetime")
|
||||
}
|
||||
for _, tc := range cases {
|
||||
if got := IsLifetimeSubscriptionCategory(tc.category); got != tc.want {
|
||||
t.Fatalf("IsLifetimeSubscriptionCategory(%q)=%v, want %v", tc.category, got, tc.want)
|
||||
if IsLifetimeExpiry(time.Date(2026, 12, 31, 0, 0, 0, 0, time.UTC)) {
|
||||
t.Fatal("normal date should not be lifetime")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeLifetimePlanCreateDuration(t *testing.T) {
|
||||
v, u := NormalizeLifetimePlanCreateDuration(true, 3, "MONTH")
|
||||
if v != LifetimeDurationValue || u != LifetimeDurationUnit {
|
||||
t.Fatalf("got %d %s", v, u)
|
||||
}
|
||||
v, u = NormalizeLifetimePlanCreateDuration(false, 3, "MONTH")
|
||||
if v != 3 || u != "MONTH" {
|
||||
t.Fatalf("got %d %s", v, u)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubscriptionGrantsContentAccess(t *testing.T) {
|
||||
|
|
@ -57,12 +59,3 @@ func TestSubscriptionGrantsContentAccess(t *testing.T) {
|
|||
t.Fatal("DUOLINGO plan should unlock Duolingo content")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalculateSubscriptionExpiresAt_learnEnglishUsesDuration(t *testing.T) {
|
||||
start := time.Date(2026, 6, 10, 12, 0, 0, 0, time.UTC)
|
||||
got := CalculateSubscriptionExpiresAt(start, "LEARN_ENGLISH", 1, "MONTH")
|
||||
want := start.AddDate(0, 1, 0)
|
||||
if !got.Equal(want) {
|
||||
t.Fatalf("expected %v, got %v", want, got)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -147,6 +147,7 @@ type UserProfileSubscription struct {
|
|||
PlanID int64 `json:"plan_id"`
|
||||
PlanName *string `json:"plan_name,omitempty"`
|
||||
Category *string `json:"category,omitempty"`
|
||||
IsLifetime *bool `json:"is_lifetime,omitempty"`
|
||||
StartsAt string `json:"starts_at"`
|
||||
ExpiresAt string `json:"expires_at"`
|
||||
Status string `json:"status"`
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ type SubscriptionStore interface {
|
|||
ListSubscriptionPlans(ctx context.Context, activeOnly bool, category *string) ([]domain.SubscriptionPlan, error)
|
||||
UpdateSubscriptionPlan(ctx context.Context, id int64, input domain.UpdateSubscriptionPlanInput) error
|
||||
DeleteSubscriptionPlan(ctx context.Context, id int64) error
|
||||
SetLifetimeExpiresForActiveSubscriptionsByPlanID(ctx context.Context, planID int64) error
|
||||
|
||||
// User Subscriptions
|
||||
CreateUserSubscription(ctx context.Context, input domain.CreateUserSubscriptionInput) (*domain.UserSubscription, error)
|
||||
|
|
|
|||
|
|
@ -50,7 +50,8 @@ func (s *Store) CreateSubscriptionPlan(ctx context.Context, input domain.CreateS
|
|||
DurationUnit: input.DurationUnit,
|
||||
Price: toPgNumeric(input.Price),
|
||||
Currency: input.Currency,
|
||||
Column8: input.IsActive,
|
||||
Column8: input.IsLifetime,
|
||||
Column9: input.IsActive,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -91,11 +92,16 @@ func (s *Store) UpdateSubscriptionPlan(ctx context.Context, id int64, input doma
|
|||
DurationUnit: optionalTextUpdate(input.DurationUnit),
|
||||
Price: numericPtrToNumeric(input.Price),
|
||||
Currency: optionalTextUpdate(input.Currency),
|
||||
IsLifetime: optionalBool(input.IsLifetime),
|
||||
IsActive: optionalBool(input.IsActive),
|
||||
ID: id,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Store) SetLifetimeExpiresForActiveSubscriptionsByPlanID(ctx context.Context, planID int64) error {
|
||||
return s.queries.SetLifetimeExpiresForActiveSubscriptionsByPlanID(ctx, planID)
|
||||
}
|
||||
|
||||
func (s *Store) DeleteSubscriptionPlan(ctx context.Context, id int64) error {
|
||||
return s.queries.DeleteSubscriptionPlan(ctx, id)
|
||||
}
|
||||
|
|
@ -137,7 +143,7 @@ func (s *Store) GetActiveSubscriptionByUserID(ctx context.Context, userID int64)
|
|||
out := mapUserSubscriptionPlanJoinRow(
|
||||
sub.ID, sub.UserID, sub.PlanID, sub.StartsAt, sub.ExpiresAt, sub.Status,
|
||||
sub.PaymentReference, sub.PaymentMethod, sub.AutoRenew, sub.CancelledAt,
|
||||
sub.CreatedAt, sub.UpdatedAt, sub.PlanName, sub.PlanCategory,
|
||||
sub.CreatedAt, sub.UpdatedAt, sub.PlanName, sub.PlanCategory, sub.PlanIsLifetime,
|
||||
sub.DurationValue, sub.DurationUnit, sub.Price, sub.Currency,
|
||||
)
|
||||
return &out, nil
|
||||
|
|
@ -177,7 +183,7 @@ func (s *Store) GetUserSubscriptionHistory(ctx context.Context, userID int64, li
|
|||
result[i] = mapUserSubscriptionPlanJoinRow(
|
||||
sub.ID, sub.UserID, sub.PlanID, sub.StartsAt, sub.ExpiresAt, sub.Status,
|
||||
sub.PaymentReference, sub.PaymentMethod, sub.AutoRenew, sub.CancelledAt,
|
||||
sub.CreatedAt, sub.UpdatedAt, sub.PlanName, sub.PlanCategory,
|
||||
sub.CreatedAt, sub.UpdatedAt, sub.PlanName, sub.PlanCategory, sub.PlanIsLifetime,
|
||||
sub.DurationValue, sub.DurationUnit, sub.Price, sub.Currency,
|
||||
)
|
||||
}
|
||||
|
|
@ -194,7 +200,7 @@ func (s *Store) ListAllUserSubscriptionsByUserID(ctx context.Context, userID int
|
|||
result[i] = mapUserSubscriptionPlanJoinRow(
|
||||
sub.ID, sub.UserID, sub.PlanID, sub.StartsAt, sub.ExpiresAt, sub.Status,
|
||||
sub.PaymentReference, sub.PaymentMethod, sub.AutoRenew, sub.CancelledAt,
|
||||
sub.CreatedAt, sub.UpdatedAt, sub.PlanName, sub.PlanCategory,
|
||||
sub.CreatedAt, sub.UpdatedAt, sub.PlanName, sub.PlanCategory, sub.PlanIsLifetime,
|
||||
sub.DurationValue, sub.DurationUnit, sub.Price, sub.Currency,
|
||||
)
|
||||
}
|
||||
|
|
@ -211,7 +217,7 @@ func (s *Store) ListActiveSubscriptionsByUserID(ctx context.Context, userID int6
|
|||
result[i] = mapUserSubscriptionPlanJoinRow(
|
||||
sub.ID, sub.UserID, sub.PlanID, sub.StartsAt, sub.ExpiresAt, sub.Status,
|
||||
sub.PaymentReference, sub.PaymentMethod, sub.AutoRenew, sub.CancelledAt,
|
||||
sub.CreatedAt, sub.UpdatedAt, sub.PlanName, sub.PlanCategory,
|
||||
sub.CreatedAt, sub.UpdatedAt, sub.PlanName, sub.PlanCategory, sub.PlanIsLifetime,
|
||||
sub.DurationValue, sub.DurationUnit, sub.Price, sub.Currency,
|
||||
)
|
||||
}
|
||||
|
|
@ -315,7 +321,7 @@ func (s *Store) ListUserSubscriptionsAdmin(ctx context.Context, filter domain.Us
|
|||
sub := mapUserSubscriptionPlanJoinRow(
|
||||
row.ID, row.UserID, row.PlanID, row.StartsAt, row.ExpiresAt, row.Status,
|
||||
row.PaymentReference, row.PaymentMethod, row.AutoRenew, row.CancelledAt,
|
||||
row.CreatedAt, row.UpdatedAt, row.PlanName, row.PlanCategory,
|
||||
row.CreatedAt, row.UpdatedAt, row.PlanName, row.PlanCategory, row.PlanIsLifetime,
|
||||
row.DurationValue, row.DurationUnit, row.Price, row.Currency,
|
||||
)
|
||||
items[i] = domain.UserSubscriptionAdminRow{
|
||||
|
|
@ -346,6 +352,7 @@ func subscriptionPlanToDomain(p dbgen.SubscriptionPlan) *domain.SubscriptionPlan
|
|||
DurationUnit: p.DurationUnit,
|
||||
Price: fromPgNumeric(p.Price),
|
||||
Currency: p.Currency,
|
||||
IsLifetime: p.IsLifetime,
|
||||
IsActive: p.IsActive,
|
||||
CreatedAt: p.CreatedAt.Time,
|
||||
UpdatedAt: timePtr(p.UpdatedAt),
|
||||
|
|
@ -373,7 +380,7 @@ func userSubscriptionWithPlanToDomain(s dbgen.GetUserSubscriptionByIDRow) *domai
|
|||
out := mapUserSubscriptionPlanJoinRow(
|
||||
s.ID, s.UserID, s.PlanID, s.StartsAt, s.ExpiresAt, s.Status,
|
||||
s.PaymentReference, s.PaymentMethod, s.AutoRenew, s.CancelledAt,
|
||||
s.CreatedAt, s.UpdatedAt, s.PlanName, s.PlanCategory,
|
||||
s.CreatedAt, s.UpdatedAt, s.PlanName, s.PlanCategory, s.PlanIsLifetime,
|
||||
s.DurationValue, s.DurationUnit, s.Price, s.Currency,
|
||||
)
|
||||
return &out
|
||||
|
|
@ -387,6 +394,7 @@ func mapUserSubscriptionPlanJoinRow(
|
|||
autoRenew bool,
|
||||
cancelledAt, createdAt, updatedAt pgtype.Timestamptz,
|
||||
planName, planCategory string,
|
||||
planIsLifetime bool,
|
||||
durationValue int32,
|
||||
durationUnit string,
|
||||
price pgtype.Numeric,
|
||||
|
|
@ -406,6 +414,7 @@ func mapUserSubscriptionPlanJoinRow(
|
|||
CreatedAt: createdAt.Time,
|
||||
UpdatedAt: timePtr(updatedAt),
|
||||
PlanName: &planName,
|
||||
PlanIsLifetime: &planIsLifetime,
|
||||
DurationValue: &durationValue,
|
||||
DurationUnit: &durationUnit,
|
||||
Price: float64Ptr(fromPgNumeric(price)),
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ func CreatePending(
|
|||
paymentMethod string,
|
||||
) (*domain.UserSubscription, error) {
|
||||
now := time.Now()
|
||||
expiresAt := domain.CalculateSubscriptionExpiresAt(now, plan.Category, plan.DurationValue, plan.DurationUnit)
|
||||
expiresAt := domain.CalculateSubscriptionExpiresAt(now, plan.IsLifetime, plan.DurationValue, plan.DurationUnit)
|
||||
status := string(domain.SubscriptionStatusPending)
|
||||
autoRenew := false
|
||||
ref := paymentRef
|
||||
|
|
@ -66,7 +66,7 @@ func Fulfill(
|
|||
paymentMethod string,
|
||||
) (*domain.UserSubscription, error) {
|
||||
startsAt := time.Now()
|
||||
expiresAt := domain.CalculateSubscriptionExpiresAt(startsAt, plan.Category, plan.DurationValue, plan.DurationUnit)
|
||||
expiresAt := domain.CalculateSubscriptionExpiresAt(startsAt, plan.IsLifetime, plan.DurationValue, plan.DurationUnit)
|
||||
paymentRef := payment.Nonce
|
||||
activeStatus := string(domain.SubscriptionStatusActive)
|
||||
autoRenew := false
|
||||
|
|
|
|||
|
|
@ -145,7 +145,7 @@ func (s *Service) sendSubscriptionActivatedEmail(userID int64, planCategory, pla
|
|||
return
|
||||
}
|
||||
|
||||
expiresLabel, isLifetime := subscriptionExpiresLabel(planCategory, expiresAt)
|
||||
expiresLabel, isLifetime := subscriptionExpiresLabel(expiresAt)
|
||||
vars := map[string]any{
|
||||
"FirstName": user.FirstName,
|
||||
"PlanName": planName,
|
||||
|
|
|
|||
|
|
@ -34,8 +34,8 @@ func subscriptionActivatedMessage(planCategory, planName string) string {
|
|||
}
|
||||
}
|
||||
|
||||
func subscriptionExpiresLabel(planCategory string, expiresAt time.Time) (expiresLabel string, isLifetime bool) {
|
||||
if domain.IsLifetimeSubscriptionCategory(planCategory) {
|
||||
func subscriptionExpiresLabel(expiresAt time.Time) (expiresLabel string, isLifetime bool) {
|
||||
if domain.IsLifetimeExpiry(expiresAt) {
|
||||
return "", true
|
||||
}
|
||||
return expiresAt.Format("Jan 2, 2006"), false
|
||||
|
|
|
|||
|
|
@ -19,19 +19,14 @@ func TestSubscriptionPackageLabel(t *testing.T) {
|
|||
func TestSubscriptionExpiresLabel(t *testing.T) {
|
||||
expiresAt := time.Date(2026, 12, 31, 0, 0, 0, 0, time.UTC)
|
||||
|
||||
label, lifetime := subscriptionExpiresLabel(string(domain.SubscriptionCategoryIELTS), expiresAt)
|
||||
label, lifetime := subscriptionExpiresLabel(domain.LifetimeSubscriptionExpiresAt)
|
||||
if !lifetime || label != "" {
|
||||
t.Fatalf("IELTS label=%q lifetime=%v", label, lifetime)
|
||||
t.Fatalf("lifetime label=%q lifetime=%v", label, lifetime)
|
||||
}
|
||||
|
||||
label, lifetime = subscriptionExpiresLabel(string(domain.SubscriptionCategoryDuolingo), expiresAt)
|
||||
if !lifetime || label != "" {
|
||||
t.Fatalf("DUOLINGO label=%q lifetime=%v", label, lifetime)
|
||||
}
|
||||
|
||||
label, lifetime = subscriptionExpiresLabel(string(domain.SubscriptionCategoryLearnEnglish), expiresAt)
|
||||
label, lifetime = subscriptionExpiresLabel(expiresAt)
|
||||
if lifetime || label != "Dec 31, 2026" {
|
||||
t.Fatalf("LEARN_ENGLISH label=%q lifetime=%v", label, lifetime)
|
||||
t.Fatalf("dated label=%q lifetime=%v", label, lifetime)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -37,6 +37,9 @@ func NewService(store ports.SubscriptionStore, payments ports.PaymentStore) *Ser
|
|||
// =====================
|
||||
|
||||
func (s *Service) CreatePlan(ctx context.Context, input domain.CreateSubscriptionPlanInput) (*domain.SubscriptionPlan, error) {
|
||||
input.DurationValue, input.DurationUnit = domain.NormalizeLifetimePlanCreateDuration(
|
||||
input.IsLifetime, input.DurationValue, input.DurationUnit,
|
||||
)
|
||||
return s.store.CreateSubscriptionPlan(ctx, input)
|
||||
}
|
||||
|
||||
|
|
@ -49,7 +52,21 @@ func (s *Service) ListPlans(ctx context.Context, activeOnly bool, category *stri
|
|||
}
|
||||
|
||||
func (s *Service) UpdatePlan(ctx context.Context, id int64, input domain.UpdateSubscriptionPlanInput) error {
|
||||
return s.store.UpdateSubscriptionPlan(ctx, id, input)
|
||||
if input.IsLifetime != nil && *input.IsLifetime {
|
||||
v := domain.LifetimeDurationValue
|
||||
u := domain.LifetimeDurationUnit
|
||||
input.DurationValue = &v
|
||||
input.DurationUnit = &u
|
||||
}
|
||||
if err := s.store.UpdateSubscriptionPlan(ctx, id, input); err != nil {
|
||||
return err
|
||||
}
|
||||
if input.IsLifetime != nil && *input.IsLifetime {
|
||||
if err := s.store.SetLifetimeExpiresForActiveSubscriptionsByPlanID(ctx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) DeletePlan(ctx context.Context, id int64) error {
|
||||
|
|
@ -81,7 +98,7 @@ func (s *Service) Subscribe(ctx context.Context, userID, planID int64, paymentRe
|
|||
|
||||
// Calculate expiry date
|
||||
startsAt := time.Now()
|
||||
expiresAt := domain.CalculateSubscriptionExpiresAt(startsAt, plan.Category, plan.DurationValue, plan.DurationUnit)
|
||||
expiresAt := domain.CalculateSubscriptionExpiresAt(startsAt, plan.IsLifetime, plan.DurationValue, plan.DurationUnit)
|
||||
|
||||
input := domain.CreateUserSubscriptionInput{
|
||||
UserID: userID,
|
||||
|
|
@ -118,7 +135,7 @@ func (s *Service) AdminGrantSubscription(ctx context.Context, userID, planID int
|
|||
|
||||
func (s *Service) createAdminGrantedSubscription(ctx context.Context, userID int64, plan *domain.SubscriptionPlan, newExpiresAt *time.Time, note *string) (*domain.UserSubscription, error) {
|
||||
startsAt := time.Now()
|
||||
expiresAt := domain.CalculateSubscriptionExpiresAt(startsAt, plan.Category, plan.DurationValue, plan.DurationUnit)
|
||||
expiresAt := domain.CalculateSubscriptionExpiresAt(startsAt, plan.IsLifetime, plan.DurationValue, plan.DurationUnit)
|
||||
if newExpiresAt != nil {
|
||||
if newExpiresAt.Before(time.Now()) {
|
||||
return nil, ErrExtendExpiresInPast
|
||||
|
|
@ -186,7 +203,7 @@ func (s *Service) AdminApplySubscription(ctx context.Context, userID, planID int
|
|||
if baseTime.Before(time.Now()) {
|
||||
baseTime = time.Now()
|
||||
}
|
||||
newExpiry := domain.CalculateSubscriptionExpiresAt(baseTime, plan.Category, plan.DurationValue, plan.DurationUnit)
|
||||
newExpiry := domain.CalculateSubscriptionExpiresAt(baseTime, plan.IsLifetime, plan.DurationValue, plan.DurationUnit)
|
||||
if newExpiry.Before(time.Now()) {
|
||||
return nil, ErrExtendExpiresInPast
|
||||
}
|
||||
|
|
@ -389,11 +406,7 @@ func (s *Service) resolveAdminExtendExpiry(ctx context.Context, sub *domain.User
|
|||
if err != nil {
|
||||
return time.Time{}, ErrPlanNotFound
|
||||
}
|
||||
category := plan.Category
|
||||
if sub.PlanCategory != nil && *sub.PlanCategory != "" {
|
||||
category = *sub.PlanCategory
|
||||
}
|
||||
return domain.CalculateSubscriptionExpiresAt(baseTime, category, plan.DurationValue, plan.DurationUnit), nil
|
||||
return domain.CalculateSubscriptionExpiresAt(baseTime, plan.IsLifetime, plan.DurationValue, plan.DurationUnit), nil
|
||||
}
|
||||
|
||||
if *input.DurationValue < 1 {
|
||||
|
|
@ -427,7 +440,7 @@ func (s *Service) RenewSubscription(ctx context.Context, subscriptionID int64) (
|
|||
if baseTime.Before(time.Now()) {
|
||||
baseTime = time.Now()
|
||||
}
|
||||
newExpiry := domain.CalculateSubscriptionExpiresAt(baseTime, plan.Category, plan.DurationValue, plan.DurationUnit)
|
||||
newExpiry := domain.CalculateSubscriptionExpiresAt(baseTime, plan.IsLifetime, plan.DurationValue, plan.DurationUnit)
|
||||
|
||||
err = s.store.ExtendSubscription(ctx, subscriptionID, newExpiry)
|
||||
if err != nil {
|
||||
|
|
@ -515,6 +528,12 @@ func toAdminUserSubscriptionRecord(sub domain.UserSubscription, now time.Time) d
|
|||
if sub.PlanCategory != nil {
|
||||
planCategory = *sub.PlanCategory
|
||||
}
|
||||
isLifetime := false
|
||||
if sub.PlanIsLifetime != nil {
|
||||
isLifetime = *sub.PlanIsLifetime
|
||||
} else {
|
||||
isLifetime = domain.IsLifetimeExpiry(sub.ExpiresAt)
|
||||
}
|
||||
durationValue := int32(0)
|
||||
if sub.DurationValue != nil {
|
||||
durationValue = *sub.DurationValue
|
||||
|
|
@ -538,6 +557,7 @@ func toAdminUserSubscriptionRecord(sub domain.UserSubscription, now time.Time) d
|
|||
PlanID: sub.PlanID,
|
||||
PlanName: planName,
|
||||
PlanCategory: planCategory,
|
||||
IsLifetime: isLifetime,
|
||||
StartsAt: sub.StartsAt,
|
||||
ExpiresAt: sub.ExpiresAt,
|
||||
Status: sub.Status,
|
||||
|
|
|
|||
|
|
@ -21,10 +21,11 @@ type createPlanReq struct {
|
|||
Name string `json:"name" validate:"required"`
|
||||
Description *string `json:"description"`
|
||||
Category string `json:"category" validate:"required,oneof=LEARN_ENGLISH IELTS DUOLINGO"`
|
||||
DurationValue int32 `json:"duration_value" validate:"required,min=1"`
|
||||
DurationUnit string `json:"duration_unit" validate:"required,oneof=DAY WEEK MONTH YEAR"`
|
||||
DurationValue int32 `json:"duration_value"`
|
||||
DurationUnit string `json:"duration_unit"`
|
||||
Price float64 `json:"price" validate:"required,min=0"`
|
||||
Currency string `json:"currency" validate:"required"`
|
||||
IsLifetime bool `json:"is_lifetime"`
|
||||
IsActive *bool `json:"is_active"`
|
||||
}
|
||||
|
||||
|
|
@ -36,6 +37,7 @@ type updatePlanReq struct {
|
|||
DurationUnit *string `json:"duration_unit"`
|
||||
Price *float64 `json:"price"`
|
||||
Currency *string `json:"currency"`
|
||||
IsLifetime *bool `json:"is_lifetime"`
|
||||
IsActive *bool `json:"is_active"`
|
||||
}
|
||||
|
||||
|
|
@ -48,6 +50,7 @@ type planRes struct {
|
|||
DurationUnit string `json:"duration_unit"`
|
||||
Price float64 `json:"price"`
|
||||
Currency string `json:"currency"`
|
||||
IsLifetime bool `json:"is_lifetime"`
|
||||
IsActive bool `json:"is_active"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
|
@ -75,6 +78,7 @@ type subscriptionRes struct {
|
|||
PlanID int64 `json:"plan_id"`
|
||||
PlanName *string `json:"plan_name,omitempty"`
|
||||
Category *string `json:"category,omitempty"`
|
||||
IsLifetime *bool `json:"is_lifetime,omitempty"`
|
||||
StartsAt string `json:"starts_at"`
|
||||
ExpiresAt string `json:"expires_at"`
|
||||
Status string `json:"status"`
|
||||
|
|
@ -121,6 +125,22 @@ func (h *Handler) CreateSubscriptionPlan(c *fiber.Ctx) error {
|
|||
Error: firstValidationError(valErrs),
|
||||
})
|
||||
}
|
||||
if !req.IsLifetime {
|
||||
if req.DurationValue < 1 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
||||
Message: "Validation failed",
|
||||
Error: "duration_value must be at least 1 for expiring plans",
|
||||
})
|
||||
}
|
||||
switch strings.ToUpper(strings.TrimSpace(req.DurationUnit)) {
|
||||
case "DAY", "WEEK", "MONTH", "YEAR":
|
||||
default:
|
||||
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
||||
Message: "Validation failed",
|
||||
Error: "duration_unit must be one of: DAY, WEEK, MONTH, YEAR",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
plan, err := h.subscriptionsSvc.CreatePlan(c.Context(), domain.CreateSubscriptionPlanInput{
|
||||
Name: req.Name,
|
||||
|
|
@ -130,6 +150,7 @@ func (h *Handler) CreateSubscriptionPlan(c *fiber.Ctx) error {
|
|||
DurationUnit: req.DurationUnit,
|
||||
Price: req.Price,
|
||||
Currency: req.Currency,
|
||||
IsLifetime: req.IsLifetime,
|
||||
IsActive: req.IsActive,
|
||||
})
|
||||
if err != nil {
|
||||
|
|
@ -260,6 +281,28 @@ func (h *Handler) UpdateSubscriptionPlan(c *fiber.Ctx) error {
|
|||
Error: firstValidationError(valErrs),
|
||||
})
|
||||
}
|
||||
if req.IsLifetime != nil && !*req.IsLifetime {
|
||||
if req.DurationValue == nil || *req.DurationValue < 1 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
||||
Message: "Validation failed",
|
||||
Error: "duration_value must be at least 1 for expiring plans",
|
||||
})
|
||||
}
|
||||
if req.DurationUnit == nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
||||
Message: "Validation failed",
|
||||
Error: "duration_unit is required for expiring plans",
|
||||
})
|
||||
}
|
||||
switch strings.ToUpper(strings.TrimSpace(*req.DurationUnit)) {
|
||||
case "DAY", "WEEK", "MONTH", "YEAR":
|
||||
default:
|
||||
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
||||
Message: "Validation failed",
|
||||
Error: "duration_unit must be one of: DAY, WEEK, MONTH, YEAR",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
err = h.subscriptionsSvc.UpdatePlan(c.Context(), id, domain.UpdateSubscriptionPlanInput{
|
||||
Name: req.Name,
|
||||
|
|
@ -269,6 +312,7 @@ func (h *Handler) UpdateSubscriptionPlan(c *fiber.Ctx) error {
|
|||
DurationUnit: req.DurationUnit,
|
||||
Price: req.Price,
|
||||
Currency: req.Currency,
|
||||
IsLifetime: req.IsLifetime,
|
||||
IsActive: req.IsActive,
|
||||
})
|
||||
if err != nil {
|
||||
|
|
@ -687,6 +731,7 @@ func planToRes(p *domain.SubscriptionPlan) *planRes {
|
|||
DurationUnit: p.DurationUnit,
|
||||
Price: p.Price,
|
||||
Currency: p.Currency,
|
||||
IsLifetime: p.IsLifetime,
|
||||
IsActive: p.IsActive,
|
||||
CreatedAt: p.CreatedAt.Format("2006-01-02T15:04:05Z07:00"),
|
||||
}
|
||||
|
|
@ -699,6 +744,7 @@ func subscriptionToRes(s *domain.UserSubscription) *subscriptionRes {
|
|||
PlanID: s.PlanID,
|
||||
PlanName: s.PlanName,
|
||||
Category: s.PlanCategory,
|
||||
IsLifetime: s.PlanIsLifetime,
|
||||
StartsAt: s.StartsAt.Format("2006-01-02T15:04:05Z07:00"),
|
||||
ExpiresAt: s.ExpiresAt.Format("2006-01-02T15:04:05Z07:00"),
|
||||
Status: s.Status,
|
||||
|
|
|
|||
|
|
@ -1648,6 +1648,7 @@ func profileSubscriptionsFromDomain(subs []domain.UserSubscription) []domain.Use
|
|||
PlanID: s.PlanID,
|
||||
PlanName: s.PlanName,
|
||||
Category: s.PlanCategory,
|
||||
IsLifetime: s.PlanIsLifetime,
|
||||
StartsAt: s.StartsAt.Format("2006-01-02T15:04:05Z07:00"),
|
||||
ExpiresAt: s.ExpiresAt.Format("2006-01-02T15:04:05Z07:00"),
|
||||
Status: s.Status,
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user