ducky/subscriptions

Paddy 2015-09-27 Parent:2c8250237566 Child:39625b37485d

11:0ae1ff0ee306 Browse Files

Add comments, move ChangingSystemProperties to the api package. Add comments to all our exported types and variables in subscription.go, both to make golint happy and because it's good to have comments. Move the subscriptions.ChangingSystemProperties helper to api.changingSystemProperties, because it returns API-specific strings and there's no real reason it has to be in the subscriptions package--everything it needs to work on is exported.

api/subscription_handlers.go subscription.go

     1.1 --- a/api/subscription_handlers.go	Sat Jul 18 03:28:51 2015 -0400
     1.2 +++ b/api/subscription_handlers.go	Sun Sep 27 21:18:45 2015 -0700
     1.3 @@ -18,6 +18,45 @@
     1.4  	ScopeSubscriptionAdmin = auth.Scope{ID: "subscriptions_admin", Name: "Administer Subscriptions", Description: "Read and update subscription information, bypassing ACL."}
     1.5  )
     1.6  
     1.7 +// changingSystemProperties takes a SubscriptionChange and returns a
     1.8 +// slice of JSON pointers to the properties that are changing but are
     1.9 +// also designated as "system properties" in the API. If no properties
    1.10 +// meet these criteria, an empty slice is returned.
    1.11 +func changingSystemProperties(change subscriptions.SubscriptionChange) []string {
    1.12 +	var changes []string
    1.13 +	if change.StripeSubscription != nil {
    1.14 +		changes = append(changes, "/stripe_subscription")
    1.15 +	}
    1.16 +	if change.Status != nil {
    1.17 +		changes = append(changes, "/status")
    1.18 +	}
    1.19 +	if change.TrialStart != nil {
    1.20 +		changes = append(changes, "/trial_start")
    1.21 +	}
    1.22 +	if change.TrialEnd != nil {
    1.23 +		changes = append(changes, "/trial_end")
    1.24 +	}
    1.25 +	if change.PeriodStart != nil {
    1.26 +		changes = append(changes, "/period_start")
    1.27 +	}
    1.28 +	if change.PeriodEnd != nil {
    1.29 +		changes = append(changes, "/period_end")
    1.30 +	}
    1.31 +	if change.CanceledAt != nil {
    1.32 +		changes = append(changes, "/canceled_at")
    1.33 +	}
    1.34 +	if change.FailedChargeAttempts != nil {
    1.35 +		changes = append(changes, "/failed_charge_attempts")
    1.36 +	}
    1.37 +	if change.LastFailedCharge != nil {
    1.38 +		changes = append(changes, "/last_failed_charge")
    1.39 +	}
    1.40 +	if change.LastNotified != nil {
    1.41 +		changes = append(changes, "/last_notified")
    1.42 +	}
    1.43 +	return changes
    1.44 +}
    1.45 +
    1.46  func HandleSubscriptions(router *trout.Router, c context.Context) {
    1.47  	router.Endpoint("/subscriptions").Methods("POST", "OPTIONS").Handler(
    1.48  		api.CORSMiddleware(api.NegotiateMiddleware(api.ContextWrapper(c, CreateSubscriptionHandler))))
    1.49 @@ -156,7 +195,7 @@
    1.50  	// BUG(paddy): Need to validate the request when updating a subscription
    1.51  
    1.52  	// only admin users can update the system-controlled properties
    1.53 -	changedSysProps := subscriptions.ChangingSystemProperties(req)
    1.54 +	changedSysProps := changingSystemProperties(req)
    1.55  	if len(changedSysProps) > 0 && !api.CheckScopes(r, ScopeSubscriptionAdmin.ID) {
    1.56  		errs := make([]api.RequestError, len(changedSysProps))
    1.57  		for pos, prop := range changedSysProps {
     2.1 --- a/subscription.go	Sat Jul 18 03:28:51 2015 -0400
     2.2 +++ b/subscription.go	Sun Sep 27 21:18:45 2015 -0700
     2.3 @@ -35,6 +35,8 @@
     2.4  		PendingPlan:         true,
     2.5  	}
     2.6  
     2.7 +	// Version tracks the build ID of the binary, set using
     2.8 +	// ldflags.
     2.9  	Version string
    2.10  )
    2.11  
    2.12 @@ -172,41 +174,12 @@
    2.13  	}
    2.14  }
    2.15  
    2.16 -func ChangingSystemProperties(change SubscriptionChange) []string {
    2.17 -	var changes []string
    2.18 -	if change.StripeSubscription != nil {
    2.19 -		changes = append(changes, "/stripe_subscription")
    2.20 -	}
    2.21 -	if change.Status != nil {
    2.22 -		changes = append(changes, "/status")
    2.23 -	}
    2.24 -	if change.TrialStart != nil {
    2.25 -		changes = append(changes, "/trial_start")
    2.26 -	}
    2.27 -	if change.TrialEnd != nil {
    2.28 -		changes = append(changes, "/trial_end")
    2.29 -	}
    2.30 -	if change.PeriodStart != nil {
    2.31 -		changes = append(changes, "/period_start")
    2.32 -	}
    2.33 -	if change.PeriodEnd != nil {
    2.34 -		changes = append(changes, "/period_end")
    2.35 -	}
    2.36 -	if change.CanceledAt != nil {
    2.37 -		changes = append(changes, "/canceled_at")
    2.38 -	}
    2.39 -	if change.FailedChargeAttempts != nil {
    2.40 -		changes = append(changes, "/failed_charge_attempts")
    2.41 -	}
    2.42 -	if change.LastFailedCharge != nil {
    2.43 -		changes = append(changes, "/last_failed_charge")
    2.44 -	}
    2.45 -	if change.LastNotified != nil {
    2.46 -		changes = append(changes, "/last_notified")
    2.47 -	}
    2.48 -	return changes
    2.49 -}
    2.50 -
    2.51 +// IsAcceptablePlan returns true if the user can select the specified
    2.52 +// plan, taking into account their admin status. If a plan exists, and
    2.53 +// is not designated as an admin-only plan, any user selecting it will
    2.54 +// return true. If a plan exists, but is designated as admin-only,
    2.55 +// IsAcceptablePlan will only return true if admin is true. If the plan
    2.56 +// doesn't exist, IsAcceptablePlan always returns false.
    2.57  func IsAcceptablePlan(plan string, admin bool) bool {
    2.58  	for p, adminOnly := range planOptions {
    2.59  		if plan == p {
    2.60 @@ -238,6 +211,8 @@
    2.61  	// was omitted."
    2.62  }
    2.63  
    2.64 +// SubscriptionStore is an interface describing datastore interactions for
    2.65 +// Subscriptions.
    2.66  type SubscriptionStore interface {
    2.67  	Reset() error
    2.68  	CreateSubscription(sub Subscription) error