From 5a620d2325378bd0649058cb5ebe06ce91e3f0a0 Mon Sep 17 00:00:00 2001 From: Sarah Roberts Date: Tue, 24 Jan 2023 18:15:26 -0700 Subject: [PATCH] CORE-1840: added support for the paid subscription flag --- internal/controllers/subscriptions.go | 17 +++++++++++------ internal/controllers/users.go | 9 ++++++++- internal/db/subscriptions.go | 6 ++++-- internal/model/plan.go | 2 +- internal/model/subscriptions.go | 6 ++---- 5 files changed, 26 insertions(+), 14 deletions(-) diff --git a/internal/controllers/subscriptions.go b/internal/controllers/subscriptions.go index 31f3e97..6fa7b5c 100644 --- a/internal/controllers/subscriptions.go +++ b/internal/controllers/subscriptions.go @@ -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] @@ -72,7 +79,7 @@ func (sa *SubscriptionAdder) AddSubscription(tx *gorm.DB, username, planName *st logrus.Fields{ "username": *username, "planName": *planName, - "paid": paid, + "paid": *paid, }, ) @@ -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()) @@ -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 }) diff --git a/internal/controllers/users.go b/internal/controllers/users.go index 5c3259c..7300594 100644 --- a/internal/controllers/users.go +++ b/internal/controllers/users.go @@ -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" ) @@ -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) @@ -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) } diff --git a/internal/db/subscriptions.go b/internal/db/subscriptions.go index a97b2c3..58fdc0a 100644 --- a/internal/db/subscriptions.go +++ b/internal/db/subscriptions.go @@ -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 @@ -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 diff --git a/internal/model/plan.go b/internal/model/plan.go index 40205af..415ff42 100644 --- a/internal/model/plan.go +++ b/internal/model/plan.go @@ -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"` } diff --git a/internal/model/subscriptions.go b/internal/model/subscriptions.go index 38d92fa..ee17ecb 100644 --- a/internal/model/subscriptions.go +++ b/internal/model/subscriptions.go @@ -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.