ducky/subscriptions
ducky/subscriptions/api/subscription_handlers.go
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.
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 {