Link checkout payments to a pending subscription row and activate it on success, or cancel it when payment fails, is cancelled, or expires. Co-authored-by: Cursor <cursoragent@cursor.com>
476 lines
15 KiB
Go
476 lines
15 KiB
Go
package repository
|
|
|
|
import (
|
|
dbgen "Yimaru-Backend/gen/db"
|
|
"Yimaru-Backend/internal/domain"
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
"github.com/shopspring/decimal"
|
|
)
|
|
|
|
// Helper functions for numeric conversions
|
|
func toPgNumeric(val float64) pgtype.Numeric {
|
|
d := decimal.NewFromFloat(val)
|
|
var num pgtype.Numeric
|
|
_ = num.Scan(d.String())
|
|
return num
|
|
}
|
|
|
|
func fromPgNumeric(num pgtype.Numeric) float64 {
|
|
if !num.Valid {
|
|
return 0
|
|
}
|
|
f, _ := num.Float64Value()
|
|
return f.Float64
|
|
}
|
|
|
|
func toPgTimestamptz(t time.Time) pgtype.Timestamptz {
|
|
return pgtype.Timestamptz{Time: t, Valid: true}
|
|
}
|
|
|
|
func toPgTimestamptzPtr(t *time.Time) pgtype.Timestamptz {
|
|
if t == nil {
|
|
return pgtype.Timestamptz{Valid: false}
|
|
}
|
|
return pgtype.Timestamptz{Time: *t, Valid: true}
|
|
}
|
|
|
|
// =====================
|
|
// Subscription Plans
|
|
// =====================
|
|
|
|
func (s *Store) CreateSubscriptionPlan(ctx context.Context, input domain.CreateSubscriptionPlanInput) (*domain.SubscriptionPlan, error) {
|
|
plan, err := s.queries.CreateSubscriptionPlan(ctx, dbgen.CreateSubscriptionPlanParams{
|
|
Name: input.Name,
|
|
Description: toPgText(input.Description),
|
|
Category: input.Category,
|
|
DurationValue: input.DurationValue,
|
|
DurationUnit: input.DurationUnit,
|
|
Price: toPgNumeric(input.Price),
|
|
Currency: input.Currency,
|
|
Column8: input.IsActive,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return subscriptionPlanToDomain(plan), nil
|
|
}
|
|
|
|
func (s *Store) GetSubscriptionPlanByID(ctx context.Context, id int64) (*domain.SubscriptionPlan, error) {
|
|
plan, err := s.queries.GetSubscriptionPlanByID(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return subscriptionPlanToDomain(plan), nil
|
|
}
|
|
|
|
func (s *Store) ListSubscriptionPlans(ctx context.Context, activeOnly bool) ([]domain.SubscriptionPlan, error) {
|
|
var plans []dbgen.SubscriptionPlan
|
|
var err error
|
|
|
|
if activeOnly {
|
|
plans, err = s.queries.ListActiveSubscriptionPlans(ctx)
|
|
} else {
|
|
plans, err = s.queries.ListSubscriptionPlans(ctx, false)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
result := make([]domain.SubscriptionPlan, len(plans))
|
|
for i, p := range plans {
|
|
result[i] = *subscriptionPlanToDomain(p)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (s *Store) UpdateSubscriptionPlan(ctx context.Context, id int64, input domain.UpdateSubscriptionPlanInput) error {
|
|
return s.queries.UpdateSubscriptionPlan(ctx, dbgen.UpdateSubscriptionPlanParams{
|
|
Name: optionalTextUpdate(input.Name),
|
|
Description: optionalTextUpdate(input.Description),
|
|
Category: optionalTextUpdate(input.Category),
|
|
DurationValue: optionalInt4(input.DurationValue),
|
|
DurationUnit: optionalTextUpdate(input.DurationUnit),
|
|
Price: numericPtrToNumeric(input.Price),
|
|
Currency: optionalTextUpdate(input.Currency),
|
|
IsActive: optionalBool(input.IsActive),
|
|
ID: id,
|
|
})
|
|
}
|
|
|
|
func (s *Store) DeleteSubscriptionPlan(ctx context.Context, id int64) error {
|
|
return s.queries.DeleteSubscriptionPlan(ctx, id)
|
|
}
|
|
|
|
// =====================
|
|
// User Subscriptions
|
|
// =====================
|
|
|
|
func (s *Store) CreateUserSubscription(ctx context.Context, input domain.CreateUserSubscriptionInput) (*domain.UserSubscription, error) {
|
|
sub, err := s.queries.CreateUserSubscription(ctx, dbgen.CreateUserSubscriptionParams{
|
|
UserID: input.UserID,
|
|
PlanID: input.PlanID,
|
|
Column3: input.StartsAt,
|
|
ExpiresAt: toPgTimestamptz(input.ExpiresAt),
|
|
Column5: input.Status,
|
|
PaymentReference: toPgText(input.PaymentReference),
|
|
PaymentMethod: toPgText(input.PaymentMethod),
|
|
Column8: input.AutoRenew,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return userSubscriptionToDomain(sub), nil
|
|
}
|
|
|
|
func (s *Store) GetUserSubscriptionByID(ctx context.Context, id int64) (*domain.UserSubscription, error) {
|
|
sub, err := s.queries.GetUserSubscriptionByID(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return userSubscriptionWithPlanToDomain(sub), nil
|
|
}
|
|
|
|
func (s *Store) GetActiveSubscriptionByUserID(ctx context.Context, userID int64) (*domain.UserSubscription, error) {
|
|
sub, err := s.queries.GetActiveSubscriptionByUserID(ctx, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &domain.UserSubscription{
|
|
ID: sub.ID,
|
|
UserID: sub.UserID,
|
|
PlanID: sub.PlanID,
|
|
StartsAt: sub.StartsAt.Time,
|
|
ExpiresAt: sub.ExpiresAt.Time,
|
|
Status: sub.Status,
|
|
PaymentReference: fromPgText(sub.PaymentReference),
|
|
PaymentMethod: fromPgText(sub.PaymentMethod),
|
|
AutoRenew: sub.AutoRenew,
|
|
CancelledAt: timePtr(sub.CancelledAt),
|
|
CreatedAt: sub.CreatedAt.Time,
|
|
UpdatedAt: timePtr(sub.UpdatedAt),
|
|
PlanName: &sub.PlanName,
|
|
DurationValue: &sub.DurationValue,
|
|
DurationUnit: &sub.DurationUnit,
|
|
Price: float64Ptr(fromPgNumeric(sub.Price)),
|
|
Currency: &sub.Currency,
|
|
}, nil
|
|
}
|
|
|
|
func (s *Store) ListSubscriptionDisplayStatusesByUserIDs(ctx context.Context, userIDs []int64) (map[int64]string, error) {
|
|
if len(userIDs) == 0 {
|
|
return map[int64]string{}, nil
|
|
}
|
|
rows, err := s.queries.ListSubscriptionDisplayStatusesByUserIDs(ctx, userIDs)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make(map[int64]string, len(rows))
|
|
for _, r := range rows {
|
|
out[r.UserID] = r.SubscriptionStatus
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (s *Store) GetSubscriptionDisplayStatusByUserID(ctx context.Context, userID int64) (string, error) {
|
|
return s.queries.GetSubscriptionDisplayStatusByUserID(ctx, userID)
|
|
}
|
|
|
|
func (s *Store) GetUserSubscriptionHistory(ctx context.Context, userID int64, limit, offset int32) ([]domain.UserSubscription, error) {
|
|
subs, err := s.queries.GetUserSubscriptionHistory(ctx, dbgen.GetUserSubscriptionHistoryParams{
|
|
UserID: userID,
|
|
Limit: pgtype.Int4{Int32: limit, Valid: true},
|
|
Offset: pgtype.Int4{Int32: offset, Valid: true},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
result := make([]domain.UserSubscription, len(subs))
|
|
for i, sub := range subs {
|
|
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.DurationValue, sub.DurationUnit, sub.Price, sub.Currency,
|
|
)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (s *Store) ListAllUserSubscriptionsByUserID(ctx context.Context, userID int64) ([]domain.UserSubscription, error) {
|
|
subs, err := s.queries.ListAllUserSubscriptionsByUserID(ctx, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result := make([]domain.UserSubscription, len(subs))
|
|
for i, sub := range subs {
|
|
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.DurationValue, sub.DurationUnit, sub.Price, sub.Currency,
|
|
)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (s *Store) ListActiveSubscriptionsByUserID(ctx context.Context, userID int64) ([]domain.UserSubscription, error) {
|
|
subs, err := s.queries.ListActiveSubscriptionsByUserID(ctx, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result := make([]domain.UserSubscription, len(subs))
|
|
for i, sub := range subs {
|
|
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.DurationValue, sub.DurationUnit, sub.Price, sub.Currency,
|
|
)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (s *Store) HasActiveSubscription(ctx context.Context, userID int64) (bool, error) {
|
|
return s.queries.HasActiveSubscription(ctx, userID)
|
|
}
|
|
|
|
func (s *Store) HasActiveSubscriptionByCategory(ctx context.Context, userID int64, category string) (bool, error) {
|
|
return s.queries.HasActiveSubscriptionByCategory(ctx, dbgen.HasActiveSubscriptionByCategoryParams{
|
|
UserID: userID,
|
|
Category: category,
|
|
})
|
|
}
|
|
|
|
func (s *Store) CancelUserSubscription(ctx context.Context, id int64) error {
|
|
return s.queries.CancelUserSubscription(ctx, id)
|
|
}
|
|
|
|
func (s *Store) ActivatePendingUserSubscription(
|
|
ctx context.Context,
|
|
id int64,
|
|
startsAt, expiresAt time.Time,
|
|
paymentReference, paymentMethod string,
|
|
) (*domain.UserSubscription, error) {
|
|
sub, err := s.queries.ActivatePendingUserSubscription(ctx, dbgen.ActivatePendingUserSubscriptionParams{
|
|
ID: id,
|
|
StartsAt: toPgTimestamptz(startsAt),
|
|
ExpiresAt: toPgTimestamptz(expiresAt),
|
|
PaymentReference: toPgText(&paymentReference),
|
|
PaymentMethod: toPgText(&paymentMethod),
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return userSubscriptionToDomain(sub), nil
|
|
}
|
|
|
|
func (s *Store) CancelPendingSubscriptionsForTerminalPayments(ctx context.Context) (int64, error) {
|
|
return s.queries.CancelPendingSubscriptionsForTerminalPayments(ctx)
|
|
}
|
|
|
|
func (s *Store) UpdateSubscriptionStatus(ctx context.Context, id int64, status string) error {
|
|
return s.queries.UpdateUserSubscriptionStatus(ctx, dbgen.UpdateUserSubscriptionStatusParams{
|
|
Status: status,
|
|
ID: id,
|
|
})
|
|
}
|
|
|
|
func (s *Store) UpdateAutoRenew(ctx context.Context, id int64, autoRenew bool) error {
|
|
return s.queries.UpdateAutoRenew(ctx, dbgen.UpdateAutoRenewParams{
|
|
AutoRenew: autoRenew,
|
|
ID: id,
|
|
})
|
|
}
|
|
|
|
func (s *Store) ExtendSubscription(ctx context.Context, id int64, newExpiresAt time.Time) error {
|
|
return s.queries.ExtendSubscription(ctx, dbgen.ExtendSubscriptionParams{
|
|
ExpiresAt: toPgTimestamptz(newExpiresAt),
|
|
ID: id,
|
|
})
|
|
}
|
|
|
|
func (s *Store) AdminExtendUserSubscription(ctx context.Context, id, planID int64, newExpiresAt time.Time) error {
|
|
return s.queries.AdminExtendUserSubscription(ctx, dbgen.AdminExtendUserSubscriptionParams{
|
|
PlanID: planID,
|
|
ExpiresAt: toPgTimestamptz(newExpiresAt),
|
|
ID: id,
|
|
})
|
|
}
|
|
|
|
func (s *Store) ListUserSubscriptionsAdmin(ctx context.Context, filter domain.UserSubscriptionAdminFilter) (domain.UserSubscriptionAdminPage, error) {
|
|
rows, err := s.queries.ListUserSubscriptionsAdmin(ctx, dbgen.ListUserSubscriptionsAdminParams{
|
|
UserID: int64PtrToPgInt8(filter.UserID),
|
|
PlanID: int64PtrToPgInt8(filter.PlanID),
|
|
Status: toPgText(filter.Status),
|
|
PlanCategory: toPgText(filter.PlanCategory),
|
|
CreatedFrom: toPgTimestamptzPtr(filter.CreatedFrom),
|
|
CreatedTo: toPgTimestamptzPtr(filter.CreatedTo),
|
|
ExpiresFrom: toPgTimestamptzPtr(filter.ExpiresFrom),
|
|
ExpiresTo: toPgTimestamptzPtr(filter.ExpiresTo),
|
|
Limit: filter.Limit,
|
|
Offset: filter.Offset,
|
|
})
|
|
if err != nil {
|
|
return domain.UserSubscriptionAdminPage{}, err
|
|
}
|
|
if len(rows) == 0 {
|
|
return domain.UserSubscriptionAdminPage{
|
|
Items: []domain.UserSubscriptionAdminRow{},
|
|
Total: 0,
|
|
Limit: filter.Limit,
|
|
Offset: filter.Offset,
|
|
}, nil
|
|
}
|
|
|
|
items := make([]domain.UserSubscriptionAdminRow, len(rows))
|
|
for i, row := range rows {
|
|
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.DurationValue, row.DurationUnit, row.Price, row.Currency,
|
|
)
|
|
items[i] = domain.UserSubscriptionAdminRow{
|
|
UserSubscription: sub,
|
|
UserEmail: derefString(fromPgText(row.UserEmail)),
|
|
UserFirstName: derefString(fromPgText(row.UserFirstName)),
|
|
UserLastName: derefString(fromPgText(row.UserLastName)),
|
|
}
|
|
}
|
|
|
|
return domain.UserSubscriptionAdminPage{
|
|
Items: items,
|
|
Total: rows[0].TotalCount,
|
|
Limit: filter.Limit,
|
|
Offset: filter.Offset,
|
|
}, nil
|
|
}
|
|
|
|
// Helper conversion functions
|
|
|
|
func subscriptionPlanToDomain(p dbgen.SubscriptionPlan) *domain.SubscriptionPlan {
|
|
return &domain.SubscriptionPlan{
|
|
ID: p.ID,
|
|
Name: p.Name,
|
|
Description: fromPgText(p.Description),
|
|
Category: p.Category,
|
|
DurationValue: p.DurationValue,
|
|
DurationUnit: p.DurationUnit,
|
|
Price: fromPgNumeric(p.Price),
|
|
Currency: p.Currency,
|
|
IsActive: p.IsActive,
|
|
CreatedAt: p.CreatedAt.Time,
|
|
UpdatedAt: timePtr(p.UpdatedAt),
|
|
}
|
|
}
|
|
|
|
func userSubscriptionToDomain(s dbgen.UserSubscription) *domain.UserSubscription {
|
|
return &domain.UserSubscription{
|
|
ID: s.ID,
|
|
UserID: s.UserID,
|
|
PlanID: s.PlanID,
|
|
StartsAt: s.StartsAt.Time,
|
|
ExpiresAt: s.ExpiresAt.Time,
|
|
Status: s.Status,
|
|
PaymentReference: fromPgText(s.PaymentReference),
|
|
PaymentMethod: fromPgText(s.PaymentMethod),
|
|
AutoRenew: s.AutoRenew,
|
|
CancelledAt: timePtr(s.CancelledAt),
|
|
CreatedAt: s.CreatedAt.Time,
|
|
UpdatedAt: timePtr(s.UpdatedAt),
|
|
}
|
|
}
|
|
|
|
func userSubscriptionWithPlanToDomain(s dbgen.GetUserSubscriptionByIDRow) *domain.UserSubscription {
|
|
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.DurationValue, s.DurationUnit, s.Price, s.Currency,
|
|
)
|
|
return &out
|
|
}
|
|
|
|
func mapUserSubscriptionPlanJoinRow(
|
|
id, userID, planID int64,
|
|
startsAt, expiresAt pgtype.Timestamptz,
|
|
status string,
|
|
paymentReference, paymentMethod pgtype.Text,
|
|
autoRenew bool,
|
|
cancelledAt, createdAt, updatedAt pgtype.Timestamptz,
|
|
planName, planCategory string,
|
|
durationValue int32,
|
|
durationUnit string,
|
|
price pgtype.Numeric,
|
|
currency string,
|
|
) domain.UserSubscription {
|
|
out := domain.UserSubscription{
|
|
ID: id,
|
|
UserID: userID,
|
|
PlanID: planID,
|
|
StartsAt: startsAt.Time,
|
|
ExpiresAt: expiresAt.Time,
|
|
Status: status,
|
|
PaymentReference: fromPgText(paymentReference),
|
|
PaymentMethod: fromPgText(paymentMethod),
|
|
AutoRenew: autoRenew,
|
|
CancelledAt: timePtr(cancelledAt),
|
|
CreatedAt: createdAt.Time,
|
|
UpdatedAt: timePtr(updatedAt),
|
|
PlanName: &planName,
|
|
DurationValue: &durationValue,
|
|
DurationUnit: &durationUnit,
|
|
Price: float64Ptr(fromPgNumeric(price)),
|
|
Currency: ¤cy,
|
|
}
|
|
if planCategory != "" {
|
|
out.PlanCategory = &planCategory
|
|
}
|
|
return out
|
|
}
|
|
|
|
func optionalInt4(v *int32) pgtype.Int4 {
|
|
if v == nil {
|
|
return pgtype.Int4{Valid: false}
|
|
}
|
|
return pgtype.Int4{Int32: *v, Valid: true}
|
|
}
|
|
|
|
func numericPtrToNumeric(val *float64) pgtype.Numeric {
|
|
if val == nil {
|
|
return pgtype.Numeric{Valid: false}
|
|
}
|
|
return toPgNumeric(*val)
|
|
}
|
|
|
|
func optionalBool(b *bool) pgtype.Bool {
|
|
if b == nil {
|
|
return pgtype.Bool{Valid: false}
|
|
}
|
|
return pgtype.Bool{Bool: *b, Valid: true}
|
|
}
|
|
|
|
func float64Ptr(f float64) *float64 {
|
|
return &f
|
|
}
|
|
|
|
func (s *Store) ListLearnEnglishSubscriptionsExpiringInSevenDays(ctx context.Context) ([]domain.SubscriptionExpiryReminder, error) {
|
|
rows, err := s.queries.ListLearnEnglishSubscriptionsExpiringInSevenDays(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]domain.SubscriptionExpiryReminder, 0, len(rows))
|
|
for _, row := range rows {
|
|
out = append(out, domain.SubscriptionExpiryReminder{
|
|
UserID: row.UserID,
|
|
PlanName: row.PlanName,
|
|
ExpiresAt: row.ExpiresAt.Time,
|
|
})
|
|
}
|
|
return out, nil
|
|
}
|