Skip to content

Commit

Permalink
CORE-1840: added support for the paid subscription flag
Browse files Browse the repository at this point in the history
  • Loading branch information
slr71 committed Mar 27, 2023
1 parent b17ebd4 commit 5a620d2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
17 changes: 11 additions & 6 deletions internal/controllers/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,20 @@ func (sa *SubscriptionAdder) subscriptionError(username string, f string, args .
}

// AddSubscription subscribes a user to a subscription plan.
func (sa *SubscriptionAdder) AddSubscription(tx *gorm.DB, username, planName *string, paid bool) *model.SubscriptionResponse {
func (sa *SubscriptionAdder) AddSubscription(tx *gorm.DB, req model.SubscriptionRequest) *model.SubscriptionResponse {
username := req.Username
planName := req.PlanName
paid := req.Paid

if username == nil || *username == "" {
return sa.subscriptionError("", "no username provided in request")
}
if planName == nil || *planName == "" {
return sa.subscriptionError(*username, "no plan name provided in request")
}
if paid == nil {
return sa.subscriptionError(*username, "no paid indicator provided in request")
}

// Look up the plan information.
plan, ok := sa.plansByName[*planName]
Expand All @@ -72,7 +79,7 @@ func (sa *SubscriptionAdder) AddSubscription(tx *gorm.DB, username, planName *st
logrus.Fields{
"username": *username,
"planName": *planName,
"paid": paid,
"paid": *paid,
},
)

Expand Down Expand Up @@ -107,7 +114,7 @@ func (sa *SubscriptionAdder) AddSubscription(tx *gorm.DB, username, planName *st
}

// Add the subscription.
sub, err := db.SubscribeUserToPlan(sa.cfg.Ctx, tx, user, plan, paid)
sub, err := db.SubscribeUserToPlan(sa.cfg.Ctx, tx, user, plan, *paid)
if err != nil {
log.Error(err)
return sa.subscriptionError(*username, err.Error())
Expand Down Expand Up @@ -177,9 +184,7 @@ func (s Server) AddSubscriptions(ctx echo.Context) error {
_ = s.GORMDB.Transaction(func(tx *gorm.DB) error {
response[i] = subscriptionAdder.AddSubscription(
tx,
subscriptionRequest.Username,
subscriptionRequest.PlanName,
subscriptionRequest.Paid,
subscriptionRequest,
)
return nil
})
Expand Down
9 changes: 8 additions & 1 deletion internal/controllers/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/cyverse/QMS/internal/db"
"github.com/cyverse/QMS/internal/httpmodel"
"github.com/cyverse/QMS/internal/model"
"github.com/cyverse/QMS/internal/query"
"github.com/labstack/echo/v4"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -322,6 +323,12 @@ func (s Server) UpdateSubscription(ctx echo.Context) error {
return model.Error(ctx, "invalid plan name", http.StatusBadRequest)
}

paid := false
paid, err := query.ValidateBooleanQueryParam(ctx, "paid", &paid)
if err != nil {
return model.Error(ctx, err.Error(), http.StatusBadRequest)
}

log.Debugf("plan name from request is %s", planName)

username := strings.TrimSuffix(ctx.Param("username"), s.UsernameSuffix)
Expand Down Expand Up @@ -369,7 +376,7 @@ func (s Server) UpdateSubscription(ctx echo.Context) error {
log.Debug("deactivated all active plans for the user")

// Subscribe the user to the plan.
_, err = db.SubscribeUserToPlan(context, tx, user, plan, true)
_, err = db.SubscribeUserToPlan(context, tx, user, plan, paid)
if err != nil {
return model.Error(ctx, err.Error(), http.StatusInternalServerError)
}
Expand Down
6 changes: 4 additions & 2 deletions internal/db/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ func QuotasFromPlan(plan *model.Plan) []model.Quota {
}

// SubscribeUserToPlan subscribes the given user to the given plan.
func SubscribeUserToPlan(ctx context.Context, db *gorm.DB, user *model.User, plan *model.Plan, paid bool) (*model.Subscription, error) {
func SubscribeUserToPlan(
ctx context.Context, db *gorm.DB, user *model.User, plan *model.Plan, paid bool,
) (*model.Subscription, error) {
wrapMsg := "unable to add user plan"
var err error

Expand Down Expand Up @@ -67,7 +69,7 @@ func SubscribeUserToDefaultPlan(ctx context.Context, db *gorm.DB, username strin
}

// Subscribe the user to the plan.
return SubscribeUserToPlan(ctx, db, user, plan, true)
return SubscribeUserToPlan(ctx, db, user, plan, false)
}

// GetActiveSubscription retrieves the user plan record that is currently active for the user. The effective start
Expand Down
2 changes: 1 addition & 1 deletion internal/model/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type Subscription struct {
// The recorded usage amounts associated with the subscription
Usages []Usage `json:"usages"`

// Whether or not user's need to pay for the subscription.
// True if the user paid for the subscription.
Paid bool `json:"paid"`
}

Expand Down
6 changes: 2 additions & 4 deletions internal/model/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ type SubscriptionRequest struct {
// required: true
PlanName *string `json:"plan_name"`

// Whether the subscription needs to be paid for.
//
// required: false
Paid bool `json:"paid"`
// True if the user paid for the subscription.
Paid *bool `json:"paid"`
}

// SubscriptionRequests represents a list of subscription requests.
Expand Down

0 comments on commit 5a620d2

Please sign in to comment.