Return 409 with a clear message instead of a foreign-key 500 when a plan is still referenced by user subscriptions. Co-authored-by: Cursor <cursoragent@cursor.com>
1276 lines
39 KiB
Go
1276 lines
39 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.30.0
|
|
// source: subscriptions.sql
|
|
|
|
package dbgen
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const ActivatePendingUserSubscription = `-- name: ActivatePendingUserSubscription :one
|
|
UPDATE user_subscriptions
|
|
SET
|
|
status = 'ACTIVE',
|
|
starts_at = $2,
|
|
expires_at = $3,
|
|
payment_reference = COALESCE($4, payment_reference),
|
|
payment_method = COALESCE($5, payment_method),
|
|
cancelled_at = NULL,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = $1
|
|
AND status = 'PENDING'
|
|
RETURNING id, user_id, plan_id, starts_at, expires_at, status, payment_reference, payment_method, auto_renew, cancelled_at, created_at, updated_at
|
|
`
|
|
|
|
type ActivatePendingUserSubscriptionParams struct {
|
|
ID int64 `json:"id"`
|
|
StartsAt pgtype.Timestamptz `json:"starts_at"`
|
|
ExpiresAt pgtype.Timestamptz `json:"expires_at"`
|
|
PaymentReference pgtype.Text `json:"payment_reference"`
|
|
PaymentMethod pgtype.Text `json:"payment_method"`
|
|
}
|
|
|
|
// Activate a checkout PENDING subscription after successful payment.
|
|
func (q *Queries) ActivatePendingUserSubscription(ctx context.Context, arg ActivatePendingUserSubscriptionParams) (UserSubscription, error) {
|
|
row := q.db.QueryRow(ctx, ActivatePendingUserSubscription,
|
|
arg.ID,
|
|
arg.StartsAt,
|
|
arg.ExpiresAt,
|
|
arg.PaymentReference,
|
|
arg.PaymentMethod,
|
|
)
|
|
var i UserSubscription
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.UserID,
|
|
&i.PlanID,
|
|
&i.StartsAt,
|
|
&i.ExpiresAt,
|
|
&i.Status,
|
|
&i.PaymentReference,
|
|
&i.PaymentMethod,
|
|
&i.AutoRenew,
|
|
&i.CancelledAt,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const AdminExtendUserSubscription = `-- name: AdminExtendUserSubscription :exec
|
|
UPDATE user_subscriptions
|
|
SET
|
|
plan_id = $1,
|
|
expires_at = $2,
|
|
status = 'ACTIVE',
|
|
cancelled_at = NULL,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = $3
|
|
`
|
|
|
|
type AdminExtendUserSubscriptionParams struct {
|
|
PlanID int64 `json:"plan_id"`
|
|
ExpiresAt pgtype.Timestamptz `json:"expires_at"`
|
|
ID int64 `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) AdminExtendUserSubscription(ctx context.Context, arg AdminExtendUserSubscriptionParams) error {
|
|
_, err := q.db.Exec(ctx, AdminExtendUserSubscription, arg.PlanID, arg.ExpiresAt, arg.ID)
|
|
return err
|
|
}
|
|
|
|
const CancelUserSubscription = `-- name: CancelUserSubscription :exec
|
|
UPDATE user_subscriptions
|
|
SET
|
|
status = 'CANCELLED',
|
|
cancelled_at = CURRENT_TIMESTAMP,
|
|
auto_renew = false,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) CancelUserSubscription(ctx context.Context, id int64) error {
|
|
_, err := q.db.Exec(ctx, CancelUserSubscription, id)
|
|
return err
|
|
}
|
|
|
|
const CountUserSubscriptions = `-- name: CountUserSubscriptions :one
|
|
SELECT COUNT(*) FROM user_subscriptions WHERE user_id = $1
|
|
`
|
|
|
|
func (q *Queries) CountUserSubscriptions(ctx context.Context, userID int64) (int64, error) {
|
|
row := q.db.QueryRow(ctx, CountUserSubscriptions, userID)
|
|
var count int64
|
|
err := row.Scan(&count)
|
|
return count, err
|
|
}
|
|
|
|
const CountUserSubscriptionsByPlanID = `-- name: CountUserSubscriptionsByPlanID :one
|
|
SELECT COUNT(*)::bigint FROM user_subscriptions WHERE plan_id = $1
|
|
`
|
|
|
|
func (q *Queries) CountUserSubscriptionsByPlanID(ctx context.Context, planID int64) (int64, error) {
|
|
row := q.db.QueryRow(ctx, CountUserSubscriptionsByPlanID, planID)
|
|
var column_1 int64
|
|
err := row.Scan(&column_1)
|
|
return column_1, err
|
|
}
|
|
|
|
const CreateSubscriptionPlan = `-- name: CreateSubscriptionPlan :one
|
|
|
|
INSERT INTO subscription_plans (
|
|
name, description, category, duration_value, duration_unit, price, currency, is_lifetime, is_active
|
|
)
|
|
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 {
|
|
Name string `json:"name"`
|
|
Description pgtype.Text `json:"description"`
|
|
Category string `json:"category"`
|
|
DurationValue int32 `json:"duration_value"`
|
|
DurationUnit string `json:"duration_unit"`
|
|
Price pgtype.Numeric `json:"price"`
|
|
Currency string `json:"currency"`
|
|
Column8 interface{} `json:"column_8"`
|
|
Column9 interface{} `json:"column_9"`
|
|
}
|
|
|
|
// =====================
|
|
// Subscription Plans
|
|
// =====================
|
|
func (q *Queries) CreateSubscriptionPlan(ctx context.Context, arg CreateSubscriptionPlanParams) (SubscriptionPlan, error) {
|
|
row := q.db.QueryRow(ctx, CreateSubscriptionPlan,
|
|
arg.Name,
|
|
arg.Description,
|
|
arg.Category,
|
|
arg.DurationValue,
|
|
arg.DurationUnit,
|
|
arg.Price,
|
|
arg.Currency,
|
|
arg.Column8,
|
|
arg.Column9,
|
|
)
|
|
var i SubscriptionPlan
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.DurationValue,
|
|
&i.DurationUnit,
|
|
&i.Price,
|
|
&i.Currency,
|
|
&i.IsActive,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Category,
|
|
&i.IsLifetime,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const CreateUserSubscription = `-- name: CreateUserSubscription :one
|
|
|
|
INSERT INTO user_subscriptions (
|
|
user_id, plan_id, starts_at, expires_at, status, payment_reference, payment_method, auto_renew
|
|
)
|
|
VALUES ($1, $2, COALESCE($3, CURRENT_TIMESTAMP), $4, COALESCE($5, 'ACTIVE'), $6, $7, COALESCE($8, false))
|
|
RETURNING id, user_id, plan_id, starts_at, expires_at, status, payment_reference, payment_method, auto_renew, cancelled_at, created_at, updated_at
|
|
`
|
|
|
|
type CreateUserSubscriptionParams struct {
|
|
UserID int64 `json:"user_id"`
|
|
PlanID int64 `json:"plan_id"`
|
|
Column3 interface{} `json:"column_3"`
|
|
ExpiresAt pgtype.Timestamptz `json:"expires_at"`
|
|
Column5 interface{} `json:"column_5"`
|
|
PaymentReference pgtype.Text `json:"payment_reference"`
|
|
PaymentMethod pgtype.Text `json:"payment_method"`
|
|
Column8 interface{} `json:"column_8"`
|
|
}
|
|
|
|
// =====================
|
|
// User Subscriptions
|
|
// =====================
|
|
func (q *Queries) CreateUserSubscription(ctx context.Context, arg CreateUserSubscriptionParams) (UserSubscription, error) {
|
|
row := q.db.QueryRow(ctx, CreateUserSubscription,
|
|
arg.UserID,
|
|
arg.PlanID,
|
|
arg.Column3,
|
|
arg.ExpiresAt,
|
|
arg.Column5,
|
|
arg.PaymentReference,
|
|
arg.PaymentMethod,
|
|
arg.Column8,
|
|
)
|
|
var i UserSubscription
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.UserID,
|
|
&i.PlanID,
|
|
&i.StartsAt,
|
|
&i.ExpiresAt,
|
|
&i.Status,
|
|
&i.PaymentReference,
|
|
&i.PaymentMethod,
|
|
&i.AutoRenew,
|
|
&i.CancelledAt,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const DeleteSubscriptionPlan = `-- name: DeleteSubscriptionPlan :exec
|
|
DELETE FROM subscription_plans WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteSubscriptionPlan(ctx context.Context, id int64) error {
|
|
_, err := q.db.Exec(ctx, DeleteSubscriptionPlan, id)
|
|
return err
|
|
}
|
|
|
|
const ExpireUserSubscription = `-- name: ExpireUserSubscription :exec
|
|
UPDATE user_subscriptions
|
|
SET
|
|
status = 'EXPIRED',
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) ExpireUserSubscription(ctx context.Context, id int64) error {
|
|
_, err := q.db.Exec(ctx, ExpireUserSubscription, id)
|
|
return err
|
|
}
|
|
|
|
const ExtendSubscription = `-- name: ExtendSubscription :exec
|
|
UPDATE user_subscriptions
|
|
SET
|
|
expires_at = $1,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = $2
|
|
`
|
|
|
|
type ExtendSubscriptionParams struct {
|
|
ExpiresAt pgtype.Timestamptz `json:"expires_at"`
|
|
ID int64 `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) ExtendSubscription(ctx context.Context, arg ExtendSubscriptionParams) error {
|
|
_, err := q.db.Exec(ctx, ExtendSubscription, arg.ExpiresAt, arg.ID)
|
|
return err
|
|
}
|
|
|
|
const GetActiveSubscriptionByUserID = `-- name: GetActiveSubscriptionByUserID :one
|
|
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,
|
|
sp.currency
|
|
FROM user_subscriptions us
|
|
JOIN subscription_plans sp ON sp.id = us.plan_id
|
|
WHERE us.user_id = $1
|
|
AND us.status = 'ACTIVE'
|
|
AND us.expires_at > CURRENT_TIMESTAMP
|
|
ORDER BY us.expires_at DESC
|
|
LIMIT 1
|
|
`
|
|
|
|
type GetActiveSubscriptionByUserIDRow struct {
|
|
ID int64 `json:"id"`
|
|
UserID int64 `json:"user_id"`
|
|
PlanID int64 `json:"plan_id"`
|
|
StartsAt pgtype.Timestamptz `json:"starts_at"`
|
|
ExpiresAt pgtype.Timestamptz `json:"expires_at"`
|
|
Status string `json:"status"`
|
|
PaymentReference pgtype.Text `json:"payment_reference"`
|
|
PaymentMethod pgtype.Text `json:"payment_method"`
|
|
AutoRenew bool `json:"auto_renew"`
|
|
CancelledAt pgtype.Timestamptz `json:"cancelled_at"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
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"`
|
|
Currency string `json:"currency"`
|
|
}
|
|
|
|
func (q *Queries) GetActiveSubscriptionByUserID(ctx context.Context, userID int64) (GetActiveSubscriptionByUserIDRow, error) {
|
|
row := q.db.QueryRow(ctx, GetActiveSubscriptionByUserID, userID)
|
|
var i GetActiveSubscriptionByUserIDRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.UserID,
|
|
&i.PlanID,
|
|
&i.StartsAt,
|
|
&i.ExpiresAt,
|
|
&i.Status,
|
|
&i.PaymentReference,
|
|
&i.PaymentMethod,
|
|
&i.AutoRenew,
|
|
&i.CancelledAt,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.PlanName,
|
|
&i.PlanCategory,
|
|
&i.PlanIsLifetime,
|
|
&i.DurationValue,
|
|
&i.DurationUnit,
|
|
&i.Price,
|
|
&i.Currency,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const GetExpiredSubscriptions = `-- name: GetExpiredSubscriptions :many
|
|
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
|
|
FROM user_subscriptions us
|
|
JOIN subscription_plans sp ON sp.id = us.plan_id
|
|
WHERE us.status = 'ACTIVE'
|
|
AND sp.is_lifetime = FALSE
|
|
AND us.expires_at <= CURRENT_TIMESTAMP
|
|
`
|
|
|
|
type GetExpiredSubscriptionsRow struct {
|
|
ID int64 `json:"id"`
|
|
UserID int64 `json:"user_id"`
|
|
PlanID int64 `json:"plan_id"`
|
|
StartsAt pgtype.Timestamptz `json:"starts_at"`
|
|
ExpiresAt pgtype.Timestamptz `json:"expires_at"`
|
|
Status string `json:"status"`
|
|
PaymentReference pgtype.Text `json:"payment_reference"`
|
|
PaymentMethod pgtype.Text `json:"payment_method"`
|
|
AutoRenew bool `json:"auto_renew"`
|
|
CancelledAt pgtype.Timestamptz `json:"cancelled_at"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
PlanName string `json:"plan_name"`
|
|
}
|
|
|
|
func (q *Queries) GetExpiredSubscriptions(ctx context.Context) ([]GetExpiredSubscriptionsRow, error) {
|
|
rows, err := q.db.Query(ctx, GetExpiredSubscriptions)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []GetExpiredSubscriptionsRow
|
|
for rows.Next() {
|
|
var i GetExpiredSubscriptionsRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.UserID,
|
|
&i.PlanID,
|
|
&i.StartsAt,
|
|
&i.ExpiresAt,
|
|
&i.Status,
|
|
&i.PaymentReference,
|
|
&i.PaymentMethod,
|
|
&i.AutoRenew,
|
|
&i.CancelledAt,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.PlanName,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const GetExpiringSubscriptions = `-- name: GetExpiringSubscriptions :many
|
|
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,
|
|
u.email,
|
|
u.first_name
|
|
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.is_lifetime = FALSE
|
|
AND us.expires_at > CURRENT_TIMESTAMP
|
|
AND us.expires_at <= CURRENT_TIMESTAMP + INTERVAL '7 days'
|
|
`
|
|
|
|
type GetExpiringSubscriptionsRow struct {
|
|
ID int64 `json:"id"`
|
|
UserID int64 `json:"user_id"`
|
|
PlanID int64 `json:"plan_id"`
|
|
StartsAt pgtype.Timestamptz `json:"starts_at"`
|
|
ExpiresAt pgtype.Timestamptz `json:"expires_at"`
|
|
Status string `json:"status"`
|
|
PaymentReference pgtype.Text `json:"payment_reference"`
|
|
PaymentMethod pgtype.Text `json:"payment_method"`
|
|
AutoRenew bool `json:"auto_renew"`
|
|
CancelledAt pgtype.Timestamptz `json:"cancelled_at"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
PlanName string `json:"plan_name"`
|
|
Email pgtype.Text `json:"email"`
|
|
FirstName pgtype.Text `json:"first_name"`
|
|
}
|
|
|
|
func (q *Queries) GetExpiringSubscriptions(ctx context.Context) ([]GetExpiringSubscriptionsRow, error) {
|
|
rows, err := q.db.Query(ctx, GetExpiringSubscriptions)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []GetExpiringSubscriptionsRow
|
|
for rows.Next() {
|
|
var i GetExpiringSubscriptionsRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.UserID,
|
|
&i.PlanID,
|
|
&i.StartsAt,
|
|
&i.ExpiresAt,
|
|
&i.Status,
|
|
&i.PaymentReference,
|
|
&i.PaymentMethod,
|
|
&i.AutoRenew,
|
|
&i.CancelledAt,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.PlanName,
|
|
&i.Email,
|
|
&i.FirstName,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const GetSubscriptionDisplayStatusByUserID = `-- name: GetSubscriptionDisplayStatusByUserID :one
|
|
SELECT COALESCE(
|
|
(SELECT us.status::text FROM user_subscriptions us
|
|
WHERE us.user_id = $1
|
|
AND us.status = 'ACTIVE' AND us.expires_at > CURRENT_TIMESTAMP
|
|
ORDER BY us.expires_at DESC LIMIT 1),
|
|
(SELECT us.status::text FROM user_subscriptions us
|
|
WHERE us.user_id = $1
|
|
AND us.status = 'PENDING'
|
|
ORDER BY us.created_at DESC LIMIT 1),
|
|
'Unsubscribed'
|
|
)::text AS subscription_status
|
|
`
|
|
|
|
func (q *Queries) GetSubscriptionDisplayStatusByUserID(ctx context.Context, userID int64) (string, error) {
|
|
row := q.db.QueryRow(ctx, GetSubscriptionDisplayStatusByUserID, userID)
|
|
var subscription_status string
|
|
err := row.Scan(&subscription_status)
|
|
return subscription_status, err
|
|
}
|
|
|
|
const GetSubscriptionPlanByID = `-- name: GetSubscriptionPlanByID :one
|
|
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) {
|
|
row := q.db.QueryRow(ctx, GetSubscriptionPlanByID, id)
|
|
var i SubscriptionPlan
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.DurationValue,
|
|
&i.DurationUnit,
|
|
&i.Price,
|
|
&i.Currency,
|
|
&i.IsActive,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Category,
|
|
&i.IsLifetime,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const GetUserSubscriptionByID = `-- name: GetUserSubscriptionByID :one
|
|
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,
|
|
sp.currency
|
|
FROM user_subscriptions us
|
|
JOIN subscription_plans sp ON sp.id = us.plan_id
|
|
WHERE us.id = $1
|
|
`
|
|
|
|
type GetUserSubscriptionByIDRow struct {
|
|
ID int64 `json:"id"`
|
|
UserID int64 `json:"user_id"`
|
|
PlanID int64 `json:"plan_id"`
|
|
StartsAt pgtype.Timestamptz `json:"starts_at"`
|
|
ExpiresAt pgtype.Timestamptz `json:"expires_at"`
|
|
Status string `json:"status"`
|
|
PaymentReference pgtype.Text `json:"payment_reference"`
|
|
PaymentMethod pgtype.Text `json:"payment_method"`
|
|
AutoRenew bool `json:"auto_renew"`
|
|
CancelledAt pgtype.Timestamptz `json:"cancelled_at"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
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"`
|
|
Currency string `json:"currency"`
|
|
}
|
|
|
|
func (q *Queries) GetUserSubscriptionByID(ctx context.Context, id int64) (GetUserSubscriptionByIDRow, error) {
|
|
row := q.db.QueryRow(ctx, GetUserSubscriptionByID, id)
|
|
var i GetUserSubscriptionByIDRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.UserID,
|
|
&i.PlanID,
|
|
&i.StartsAt,
|
|
&i.ExpiresAt,
|
|
&i.Status,
|
|
&i.PaymentReference,
|
|
&i.PaymentMethod,
|
|
&i.AutoRenew,
|
|
&i.CancelledAt,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.PlanName,
|
|
&i.PlanCategory,
|
|
&i.PlanIsLifetime,
|
|
&i.DurationValue,
|
|
&i.DurationUnit,
|
|
&i.Price,
|
|
&i.Currency,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const GetUserSubscriptionHistory = `-- name: GetUserSubscriptionHistory :many
|
|
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,
|
|
sp.currency
|
|
FROM user_subscriptions us
|
|
JOIN subscription_plans sp ON sp.id = us.plan_id
|
|
WHERE us.user_id = $1
|
|
ORDER BY us.created_at DESC
|
|
LIMIT $3::INT
|
|
OFFSET $2::INT
|
|
`
|
|
|
|
type GetUserSubscriptionHistoryParams struct {
|
|
UserID int64 `json:"user_id"`
|
|
Offset pgtype.Int4 `json:"offset"`
|
|
Limit pgtype.Int4 `json:"limit"`
|
|
}
|
|
|
|
type GetUserSubscriptionHistoryRow struct {
|
|
ID int64 `json:"id"`
|
|
UserID int64 `json:"user_id"`
|
|
PlanID int64 `json:"plan_id"`
|
|
StartsAt pgtype.Timestamptz `json:"starts_at"`
|
|
ExpiresAt pgtype.Timestamptz `json:"expires_at"`
|
|
Status string `json:"status"`
|
|
PaymentReference pgtype.Text `json:"payment_reference"`
|
|
PaymentMethod pgtype.Text `json:"payment_method"`
|
|
AutoRenew bool `json:"auto_renew"`
|
|
CancelledAt pgtype.Timestamptz `json:"cancelled_at"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
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"`
|
|
Currency string `json:"currency"`
|
|
}
|
|
|
|
func (q *Queries) GetUserSubscriptionHistory(ctx context.Context, arg GetUserSubscriptionHistoryParams) ([]GetUserSubscriptionHistoryRow, error) {
|
|
rows, err := q.db.Query(ctx, GetUserSubscriptionHistory, arg.UserID, arg.Offset, arg.Limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []GetUserSubscriptionHistoryRow
|
|
for rows.Next() {
|
|
var i GetUserSubscriptionHistoryRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.UserID,
|
|
&i.PlanID,
|
|
&i.StartsAt,
|
|
&i.ExpiresAt,
|
|
&i.Status,
|
|
&i.PaymentReference,
|
|
&i.PaymentMethod,
|
|
&i.AutoRenew,
|
|
&i.CancelledAt,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.PlanName,
|
|
&i.PlanCategory,
|
|
&i.PlanIsLifetime,
|
|
&i.DurationValue,
|
|
&i.DurationUnit,
|
|
&i.Price,
|
|
&i.Currency,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const HasActiveSubscription = `-- name: HasActiveSubscription :one
|
|
SELECT EXISTS(
|
|
SELECT 1 FROM user_subscriptions
|
|
WHERE user_id = $1
|
|
AND status = 'ACTIVE'
|
|
AND expires_at > CURRENT_TIMESTAMP
|
|
) AS has_subscription
|
|
`
|
|
|
|
func (q *Queries) HasActiveSubscription(ctx context.Context, userID int64) (bool, error) {
|
|
row := q.db.QueryRow(ctx, HasActiveSubscription, userID)
|
|
var has_subscription bool
|
|
err := row.Scan(&has_subscription)
|
|
return has_subscription, err
|
|
}
|
|
|
|
const HasActiveSubscriptionByCategory = `-- name: HasActiveSubscriptionByCategory :one
|
|
SELECT EXISTS(
|
|
SELECT 1
|
|
FROM user_subscriptions us
|
|
JOIN subscription_plans sp ON sp.id = us.plan_id
|
|
WHERE us.user_id = $1
|
|
AND sp.category = $2
|
|
AND us.status = 'ACTIVE'
|
|
AND us.expires_at > CURRENT_TIMESTAMP
|
|
) AS has_subscription
|
|
`
|
|
|
|
type HasActiveSubscriptionByCategoryParams struct {
|
|
UserID int64 `json:"user_id"`
|
|
Category string `json:"category"`
|
|
}
|
|
|
|
func (q *Queries) HasActiveSubscriptionByCategory(ctx context.Context, arg HasActiveSubscriptionByCategoryParams) (bool, error) {
|
|
row := q.db.QueryRow(ctx, HasActiveSubscriptionByCategory, arg.UserID, arg.Category)
|
|
var has_subscription bool
|
|
err := row.Scan(&has_subscription)
|
|
return has_subscription, err
|
|
}
|
|
|
|
const ListActiveSubscriptionPlans = `-- name: ListActiveSubscriptionPlans :many
|
|
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
|
|
`
|
|
|
|
func (q *Queries) ListActiveSubscriptionPlans(ctx context.Context) ([]SubscriptionPlan, error) {
|
|
rows, err := q.db.Query(ctx, ListActiveSubscriptionPlans)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []SubscriptionPlan
|
|
for rows.Next() {
|
|
var i SubscriptionPlan
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.DurationValue,
|
|
&i.DurationUnit,
|
|
&i.Price,
|
|
&i.Currency,
|
|
&i.IsActive,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Category,
|
|
&i.IsLifetime,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const ListActiveSubscriptionsByUserID = `-- name: ListActiveSubscriptionsByUserID :many
|
|
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,
|
|
sp.currency
|
|
FROM user_subscriptions us
|
|
JOIN subscription_plans sp ON sp.id = us.plan_id
|
|
WHERE us.user_id = $1
|
|
AND us.status = 'ACTIVE'
|
|
AND us.expires_at > CURRENT_TIMESTAMP
|
|
ORDER BY us.expires_at DESC
|
|
`
|
|
|
|
type ListActiveSubscriptionsByUserIDRow struct {
|
|
ID int64 `json:"id"`
|
|
UserID int64 `json:"user_id"`
|
|
PlanID int64 `json:"plan_id"`
|
|
StartsAt pgtype.Timestamptz `json:"starts_at"`
|
|
ExpiresAt pgtype.Timestamptz `json:"expires_at"`
|
|
Status string `json:"status"`
|
|
PaymentReference pgtype.Text `json:"payment_reference"`
|
|
PaymentMethod pgtype.Text `json:"payment_method"`
|
|
AutoRenew bool `json:"auto_renew"`
|
|
CancelledAt pgtype.Timestamptz `json:"cancelled_at"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
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"`
|
|
Currency string `json:"currency"`
|
|
}
|
|
|
|
func (q *Queries) ListActiveSubscriptionsByUserID(ctx context.Context, userID int64) ([]ListActiveSubscriptionsByUserIDRow, error) {
|
|
rows, err := q.db.Query(ctx, ListActiveSubscriptionsByUserID, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListActiveSubscriptionsByUserIDRow
|
|
for rows.Next() {
|
|
var i ListActiveSubscriptionsByUserIDRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.UserID,
|
|
&i.PlanID,
|
|
&i.StartsAt,
|
|
&i.ExpiresAt,
|
|
&i.Status,
|
|
&i.PaymentReference,
|
|
&i.PaymentMethod,
|
|
&i.AutoRenew,
|
|
&i.CancelledAt,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.PlanName,
|
|
&i.PlanCategory,
|
|
&i.PlanIsLifetime,
|
|
&i.DurationValue,
|
|
&i.DurationUnit,
|
|
&i.Price,
|
|
&i.Currency,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const ListAllUserSubscriptionsByUserID = `-- name: ListAllUserSubscriptionsByUserID :many
|
|
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,
|
|
sp.currency
|
|
FROM user_subscriptions us
|
|
JOIN subscription_plans sp ON sp.id = us.plan_id
|
|
WHERE us.user_id = $1
|
|
ORDER BY us.created_at DESC
|
|
`
|
|
|
|
type ListAllUserSubscriptionsByUserIDRow struct {
|
|
ID int64 `json:"id"`
|
|
UserID int64 `json:"user_id"`
|
|
PlanID int64 `json:"plan_id"`
|
|
StartsAt pgtype.Timestamptz `json:"starts_at"`
|
|
ExpiresAt pgtype.Timestamptz `json:"expires_at"`
|
|
Status string `json:"status"`
|
|
PaymentReference pgtype.Text `json:"payment_reference"`
|
|
PaymentMethod pgtype.Text `json:"payment_method"`
|
|
AutoRenew bool `json:"auto_renew"`
|
|
CancelledAt pgtype.Timestamptz `json:"cancelled_at"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
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"`
|
|
Currency string `json:"currency"`
|
|
}
|
|
|
|
func (q *Queries) ListAllUserSubscriptionsByUserID(ctx context.Context, userID int64) ([]ListAllUserSubscriptionsByUserIDRow, error) {
|
|
rows, err := q.db.Query(ctx, ListAllUserSubscriptionsByUserID, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListAllUserSubscriptionsByUserIDRow
|
|
for rows.Next() {
|
|
var i ListAllUserSubscriptionsByUserIDRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.UserID,
|
|
&i.PlanID,
|
|
&i.StartsAt,
|
|
&i.ExpiresAt,
|
|
&i.Status,
|
|
&i.PaymentReference,
|
|
&i.PaymentMethod,
|
|
&i.AutoRenew,
|
|
&i.CancelledAt,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.PlanName,
|
|
&i.PlanCategory,
|
|
&i.PlanIsLifetime,
|
|
&i.DurationValue,
|
|
&i.DurationUnit,
|
|
&i.Price,
|
|
&i.Currency,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const ListLearnEnglishSubscriptionsExpiringInSevenDays = `-- name: ListLearnEnglishSubscriptionsExpiringInSevenDays :many
|
|
SELECT
|
|
us.id,
|
|
us.user_id,
|
|
us.plan_id,
|
|
us.expires_at,
|
|
sp.name AS plan_name,
|
|
sp.category AS plan_category
|
|
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.is_lifetime = FALSE
|
|
AND us.expires_at::date = (CURRENT_DATE + INTERVAL '7 days')::date
|
|
`
|
|
|
|
type ListLearnEnglishSubscriptionsExpiringInSevenDaysRow struct {
|
|
ID int64 `json:"id"`
|
|
UserID int64 `json:"user_id"`
|
|
PlanID int64 `json:"plan_id"`
|
|
ExpiresAt pgtype.Timestamptz `json:"expires_at"`
|
|
PlanName string `json:"plan_name"`
|
|
PlanCategory string `json:"plan_category"`
|
|
}
|
|
|
|
func (q *Queries) ListLearnEnglishSubscriptionsExpiringInSevenDays(ctx context.Context) ([]ListLearnEnglishSubscriptionsExpiringInSevenDaysRow, error) {
|
|
rows, err := q.db.Query(ctx, ListLearnEnglishSubscriptionsExpiringInSevenDays)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListLearnEnglishSubscriptionsExpiringInSevenDaysRow
|
|
for rows.Next() {
|
|
var i ListLearnEnglishSubscriptionsExpiringInSevenDaysRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.UserID,
|
|
&i.PlanID,
|
|
&i.ExpiresAt,
|
|
&i.PlanName,
|
|
&i.PlanCategory,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const ListSubscriptionDisplayStatusesByUserIDs = `-- name: ListSubscriptionDisplayStatusesByUserIDs :many
|
|
WITH input AS (
|
|
SELECT unnest($1::bigint[])::bigint AS user_id
|
|
)
|
|
SELECT
|
|
input.user_id,
|
|
COALESCE(
|
|
(SELECT us.status::text FROM user_subscriptions us
|
|
WHERE us.user_id = input.user_id
|
|
AND us.status = 'ACTIVE' AND us.expires_at > CURRENT_TIMESTAMP
|
|
ORDER BY us.expires_at DESC LIMIT 1),
|
|
(SELECT us.status::text FROM user_subscriptions us
|
|
WHERE us.user_id = input.user_id
|
|
AND us.status = 'PENDING'
|
|
ORDER BY us.created_at DESC LIMIT 1),
|
|
'Unsubscribed'
|
|
)::text AS subscription_status
|
|
FROM input
|
|
`
|
|
|
|
type ListSubscriptionDisplayStatusesByUserIDsRow struct {
|
|
UserID int64 `json:"user_id"`
|
|
SubscriptionStatus string `json:"subscription_status"`
|
|
}
|
|
|
|
// Display status for admin user lists: ACTIVE (non-expired), else latest PENDING, else Unsubscribed.
|
|
func (q *Queries) ListSubscriptionDisplayStatusesByUserIDs(ctx context.Context, dollar_1 []int64) ([]ListSubscriptionDisplayStatusesByUserIDsRow, error) {
|
|
rows, err := q.db.Query(ctx, ListSubscriptionDisplayStatusesByUserIDs, dollar_1)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListSubscriptionDisplayStatusesByUserIDsRow
|
|
for rows.Next() {
|
|
var i ListSubscriptionDisplayStatusesByUserIDsRow
|
|
if err := rows.Scan(&i.UserID, &i.SubscriptionStatus); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const ListSubscriptionPlans = `-- name: ListSubscriptionPlans :many
|
|
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
|
|
OR is_active = true
|
|
)
|
|
AND (
|
|
$2::varchar IS NULL
|
|
OR category = $2::varchar
|
|
)
|
|
ORDER BY price ASC
|
|
`
|
|
|
|
type ListSubscriptionPlansParams struct {
|
|
ActiveOnly pgtype.Bool `json:"active_only"`
|
|
Category pgtype.Text `json:"category"`
|
|
}
|
|
|
|
func (q *Queries) ListSubscriptionPlans(ctx context.Context, arg ListSubscriptionPlansParams) ([]SubscriptionPlan, error) {
|
|
rows, err := q.db.Query(ctx, ListSubscriptionPlans, arg.ActiveOnly, arg.Category)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []SubscriptionPlan
|
|
for rows.Next() {
|
|
var i SubscriptionPlan
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.DurationValue,
|
|
&i.DurationUnit,
|
|
&i.Price,
|
|
&i.Currency,
|
|
&i.IsActive,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Category,
|
|
&i.IsLifetime,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const ListUserSubscriptionsAdmin = `-- name: ListUserSubscriptionsAdmin :many
|
|
SELECT
|
|
COUNT(*) OVER () AS total_count,
|
|
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,
|
|
sp.currency,
|
|
u.email AS user_email,
|
|
u.first_name AS user_first_name,
|
|
u.last_name AS user_last_name
|
|
FROM user_subscriptions us
|
|
JOIN subscription_plans sp ON sp.id = us.plan_id
|
|
JOIN users u ON u.id = us.user_id
|
|
WHERE ($1::bigint IS NULL OR us.user_id = $1::bigint)
|
|
AND ($2::bigint IS NULL OR us.plan_id = $2::bigint)
|
|
AND ($3::varchar IS NULL OR us.status = $3::varchar)
|
|
AND ($4::varchar IS NULL OR sp.category = $4::varchar)
|
|
AND ($5::timestamptz IS NULL OR us.created_at >= $5::timestamptz)
|
|
AND ($6::timestamptz IS NULL OR us.created_at < $6::timestamptz)
|
|
AND ($7::timestamptz IS NULL OR us.expires_at >= $7::timestamptz)
|
|
AND ($8::timestamptz IS NULL OR us.expires_at < $8::timestamptz)
|
|
ORDER BY us.created_at DESC
|
|
LIMIT $10 OFFSET $9
|
|
`
|
|
|
|
type ListUserSubscriptionsAdminParams struct {
|
|
UserID pgtype.Int8 `json:"user_id"`
|
|
PlanID pgtype.Int8 `json:"plan_id"`
|
|
Status pgtype.Text `json:"status"`
|
|
PlanCategory pgtype.Text `json:"plan_category"`
|
|
CreatedFrom pgtype.Timestamptz `json:"created_from"`
|
|
CreatedTo pgtype.Timestamptz `json:"created_to"`
|
|
ExpiresFrom pgtype.Timestamptz `json:"expires_from"`
|
|
ExpiresTo pgtype.Timestamptz `json:"expires_to"`
|
|
Offset int32 `json:"offset"`
|
|
Limit int32 `json:"limit"`
|
|
}
|
|
|
|
type ListUserSubscriptionsAdminRow struct {
|
|
TotalCount int64 `json:"total_count"`
|
|
ID int64 `json:"id"`
|
|
UserID int64 `json:"user_id"`
|
|
PlanID int64 `json:"plan_id"`
|
|
StartsAt pgtype.Timestamptz `json:"starts_at"`
|
|
ExpiresAt pgtype.Timestamptz `json:"expires_at"`
|
|
Status string `json:"status"`
|
|
PaymentReference pgtype.Text `json:"payment_reference"`
|
|
PaymentMethod pgtype.Text `json:"payment_method"`
|
|
AutoRenew bool `json:"auto_renew"`
|
|
CancelledAt pgtype.Timestamptz `json:"cancelled_at"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
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"`
|
|
Currency string `json:"currency"`
|
|
UserEmail pgtype.Text `json:"user_email"`
|
|
UserFirstName pgtype.Text `json:"user_first_name"`
|
|
UserLastName pgtype.Text `json:"user_last_name"`
|
|
}
|
|
|
|
func (q *Queries) ListUserSubscriptionsAdmin(ctx context.Context, arg ListUserSubscriptionsAdminParams) ([]ListUserSubscriptionsAdminRow, error) {
|
|
rows, err := q.db.Query(ctx, ListUserSubscriptionsAdmin,
|
|
arg.UserID,
|
|
arg.PlanID,
|
|
arg.Status,
|
|
arg.PlanCategory,
|
|
arg.CreatedFrom,
|
|
arg.CreatedTo,
|
|
arg.ExpiresFrom,
|
|
arg.ExpiresTo,
|
|
arg.Offset,
|
|
arg.Limit,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListUserSubscriptionsAdminRow
|
|
for rows.Next() {
|
|
var i ListUserSubscriptionsAdminRow
|
|
if err := rows.Scan(
|
|
&i.TotalCount,
|
|
&i.ID,
|
|
&i.UserID,
|
|
&i.PlanID,
|
|
&i.StartsAt,
|
|
&i.ExpiresAt,
|
|
&i.Status,
|
|
&i.PaymentReference,
|
|
&i.PaymentMethod,
|
|
&i.AutoRenew,
|
|
&i.CancelledAt,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.PlanName,
|
|
&i.PlanCategory,
|
|
&i.PlanIsLifetime,
|
|
&i.DurationValue,
|
|
&i.DurationUnit,
|
|
&i.Price,
|
|
&i.Currency,
|
|
&i.UserEmail,
|
|
&i.UserFirstName,
|
|
&i.UserLastName,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
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
|
|
auto_renew = $1,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = $2
|
|
`
|
|
|
|
type UpdateAutoRenewParams struct {
|
|
AutoRenew bool `json:"auto_renew"`
|
|
ID int64 `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) UpdateAutoRenew(ctx context.Context, arg UpdateAutoRenewParams) error {
|
|
_, err := q.db.Exec(ctx, UpdateAutoRenew, arg.AutoRenew, arg.ID)
|
|
return err
|
|
}
|
|
|
|
const UpdateSubscriptionPlan = `-- name: UpdateSubscriptionPlan :exec
|
|
UPDATE subscription_plans
|
|
SET
|
|
name = COALESCE($1::varchar, name),
|
|
description = COALESCE($2::text, description),
|
|
category = COALESCE($3::varchar, category),
|
|
duration_value = COALESCE($4::int, duration_value),
|
|
duration_unit = COALESCE($5::varchar, duration_unit),
|
|
price = COALESCE($6::numeric, price),
|
|
currency = COALESCE($7::varchar, currency),
|
|
is_lifetime = COALESCE($8::boolean, is_lifetime),
|
|
is_active = COALESCE($9::boolean, is_active),
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = $10
|
|
`
|
|
|
|
type UpdateSubscriptionPlanParams struct {
|
|
Name pgtype.Text `json:"name"`
|
|
Description pgtype.Text `json:"description"`
|
|
Category pgtype.Text `json:"category"`
|
|
DurationValue pgtype.Int4 `json:"duration_value"`
|
|
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"`
|
|
}
|
|
|
|
func (q *Queries) UpdateSubscriptionPlan(ctx context.Context, arg UpdateSubscriptionPlanParams) error {
|
|
_, err := q.db.Exec(ctx, UpdateSubscriptionPlan,
|
|
arg.Name,
|
|
arg.Description,
|
|
arg.Category,
|
|
arg.DurationValue,
|
|
arg.DurationUnit,
|
|
arg.Price,
|
|
arg.Currency,
|
|
arg.IsLifetime,
|
|
arg.IsActive,
|
|
arg.ID,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const UpdateUserSubscriptionStatus = `-- name: UpdateUserSubscriptionStatus :exec
|
|
UPDATE user_subscriptions
|
|
SET
|
|
status = $1,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = $2
|
|
`
|
|
|
|
type UpdateUserSubscriptionStatusParams struct {
|
|
Status string `json:"status"`
|
|
ID int64 `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) UpdateUserSubscriptionStatus(ctx context.Context, arg UpdateUserSubscriptionStatusParams) error {
|
|
_, err := q.db.Exec(ctx, UpdateUserSubscriptionStatus, arg.Status, arg.ID)
|
|
return err
|
|
}
|