ducky/web
2015-07-07
ducky/web/src/models/subscription.js
Implement subscriptions. Create a Subscription model and a Subscriptions collection, and attach them to the app context. Add a helper to our Profile model to retrieve the Subscription of that model. Still not sure this should be on the Profile--wouldn't it be better on the Me model? Isn't that generally where we would need it?
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/models/subscription.js Tue Jul 07 21:29:19 2015 -0400 1.3 @@ -0,0 +1,47 @@ 1.4 +import Model from 'ampersand-model' 1.5 +import refresh from '../helpers/oauth-refresh' 1.6 + 1.7 +export default Model.extend({ 1.8 + props: { 1.9 + 'userID': 'string', 1.10 + 'stripe_subscription': 'string', 1.11 + 'plan': 'string', 1.12 + 'status': 'string', 1.13 + 'canceling': 'boolean', 1.14 + 'created': 'date', 1.15 + 'trialStart': 'date', 1.16 + 'trialEnd': 'date', 1.17 + 'periodStart': 'date', 1.18 + 'periodEnd': 'date', 1.19 + 'canceledAt': 'date', 1.20 + 'failedChargeAttempts': 'integer', 1.21 + 'lastFailedCharge': 'date', 1.22 + 'lastNotified': 'date', 1.23 + }, 1.24 + idAttribute: 'userID', 1.25 + sync: refresh.Sync, 1.26 + parse (attrs) { 1.27 + if (attrs.subscriptions && attrs.subscriptions.length == 1) { 1.28 + attrs = attrs.subscriptions[0] 1.29 + } 1.30 + attrs.userID = attrs.user_id 1.31 + attrs.stripeSubscription = attrs.stripeSubscription 1.32 + attrs.trialStart = attrs.trial_start 1.33 + attrs.trialEnd = attrs.trial_end 1.34 + attrs.periodStart = attrs.period_start 1.35 + attrs.periodEnd = attrs.period_end 1.36 + attrs.canceledAt = attrs.canceled_at 1.37 + attrs.failedChargeAttempts = attrs.failed_charge_attempts 1.38 + attrs.lastFailedCharge = attrs.last_failed_charge 1.39 + attrs.lastNotified = attrs.last_notified 1.40 + return attrs 1.41 + }, 1.42 + ajaxConfig () { 1.43 + return { 1.44 + headers: { 1.45 + 'Authorization': 'Bearer '+app.me.access_token, 1.46 + 'Accept': 'application/json', 1.47 + } 1.48 + } 1.49 + }, 1.50 +})