ducky/web

Paddy 2015-07-07

21:bc1478742a50 Go to Latest

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?

History
paddy@21 1 import Model from 'ampersand-model'
paddy@21 2 import refresh from '../helpers/oauth-refresh'
paddy@21 3
paddy@21 4 export default Model.extend({
paddy@21 5 props: {
paddy@21 6 'userID': 'string',
paddy@21 7 'stripe_subscription': 'string',
paddy@21 8 'plan': 'string',
paddy@21 9 'status': 'string',
paddy@21 10 'canceling': 'boolean',
paddy@21 11 'created': 'date',
paddy@21 12 'trialStart': 'date',
paddy@21 13 'trialEnd': 'date',
paddy@21 14 'periodStart': 'date',
paddy@21 15 'periodEnd': 'date',
paddy@21 16 'canceledAt': 'date',
paddy@21 17 'failedChargeAttempts': 'integer',
paddy@21 18 'lastFailedCharge': 'date',
paddy@21 19 'lastNotified': 'date',
paddy@21 20 },
paddy@21 21 idAttribute: 'userID',
paddy@21 22 sync: refresh.Sync,
paddy@21 23 parse (attrs) {
paddy@21 24 if (attrs.subscriptions && attrs.subscriptions.length == 1) {
paddy@21 25 attrs = attrs.subscriptions[0]
paddy@21 26 }
paddy@21 27 attrs.userID = attrs.user_id
paddy@21 28 attrs.stripeSubscription = attrs.stripeSubscription
paddy@21 29 attrs.trialStart = attrs.trial_start
paddy@21 30 attrs.trialEnd = attrs.trial_end
paddy@21 31 attrs.periodStart = attrs.period_start
paddy@21 32 attrs.periodEnd = attrs.period_end
paddy@21 33 attrs.canceledAt = attrs.canceled_at
paddy@21 34 attrs.failedChargeAttempts = attrs.failed_charge_attempts
paddy@21 35 attrs.lastFailedCharge = attrs.last_failed_charge
paddy@21 36 attrs.lastNotified = attrs.last_notified
paddy@21 37 return attrs
paddy@21 38 },
paddy@21 39 ajaxConfig () {
paddy@21 40 return {
paddy@21 41 headers: {
paddy@21 42 'Authorization': 'Bearer '+app.me.access_token,
paddy@21 43 'Accept': 'application/json',
paddy@21 44 }
paddy@21 45 }
paddy@21 46 },
paddy@21 47 })