Skip to content

Commit

Permalink
Simplified subscription creation
Browse files Browse the repository at this point in the history
  • Loading branch information
begedin committed Jan 26, 2017
1 parent 85627c8 commit 790e0fc
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 20 deletions.
7 changes: 3 additions & 4 deletions app/controllers/project/donate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
Controller,
computed: { alias, bool, not },
get,
getProperties,
inject: { service },
set,
RSVP
Expand Down Expand Up @@ -119,11 +120,9 @@ export default Controller.extend({
// subscription

_createSubscription(quantity) {
let user = get(this, 'user');
let stripeConnectPlan = get(this, 'project.stripeConnectPlan');
let store = get(this, 'store');
let { project, store, user } = getProperties(this, 'project', 'store', 'user');

return store.createRecord('stripe-connect-subscription', { quantity, user, stripeConnectPlan })
return store.createRecord('stripe-connect-subscription', { project, quantity, user })
.save()
.then((record) =>RSVP.resolve(record))
.catch((response) => this._handleSubscriptionCreationError(response));
Expand Down
2 changes: 1 addition & 1 deletion app/models/stripe-connect-subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import { belongsTo } from 'ember-data/relationships';
export default Model.extend({
quantity: attr('dollar-cents'),

stripeConnectPlan: belongsTo('stripe-connect-plan', { async: true }),
project: belongsTo('project', { async: true }),
user: belongsTo('user', { async: true })
});
6 changes: 2 additions & 4 deletions app/services/user-subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ export default Service.extend({
let user = this.get('user');

if (user) {
let subscriptions = user.get('stripeConnectSubscriptions');
let planId = project.belongsTo('stripeConnectPlan').id();
return RSVP.hash({ subscriptions, planId }).then(({ subscriptions, planId }) => {
return user.get('stripeConnectSubscriptions').then((subscriptions) => {
let subscription = subscriptions.find((subscription) => {
return subscription.belongsTo('stripeConnectPlan').id() === planId;
return subscription.belongsTo('project').id() === project.id;
});
return RSVP.resolve(subscription);
});
Expand Down
2 changes: 1 addition & 1 deletion config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ module.exports = function(environment) {

ENV.sentry.development = true;

ENV.stripe.publishableKey = 'pk_test_uulykWQvn6axvKzslwN8lqby';
ENV.stripe.publishableKey = 'pk_test_POrczFzlcBoJtYlOj6dFLNOl';

ENV['ember-cli-mirage'] = {
enabled: false
Expand Down
1 change: 0 additions & 1 deletion mirage/models/stripe-connect-subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ import { Model, belongsTo } from 'ember-cli-mirage';

export default Model.extend({
project: belongsTo(),
stripeConnectPlan: belongsTo(),
user: belongsTo()
});
8 changes: 4 additions & 4 deletions tests/acceptance/project-donate-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ test('It redirects to project route if already a subscriber, with a flash', func
let organization = createOrganizationWithSluggedRoute();
let project = server.create('project', { organization });

let stripeConnectPlan = project.createStripeConnectPlan({ project });
project.createStripeConnectPlan({ project });

server.create('stripeConnectSubscription', { stripeConnectPlan, user });
server.create('stripeConnectSubscription', { project, user });

projectDonatePage.visit({
amount: 10,
Expand All @@ -121,7 +121,7 @@ test('Allows creating a card and donating (creating a subscription)', function(a

let organization = createOrganizationWithSluggedRoute();
let project = server.create('project', { organization });
let plan = project.createStripeConnectPlan();
project.createStripeConnectPlan();

projectDonatePage.visit({
amount: 10,
Expand Down Expand Up @@ -154,7 +154,7 @@ test('Allows creating a card and donating (creating a subscription)', function(a
let subscription = server.schema.stripeConnectSubscriptions.findBy({ quantity: 1000 });
assert.ok(subscription, 'Subscription was created sucessfully.');
assert.equal(subscription.userId, user.id, 'User was set to current user.');
assert.equal(subscription.stripeConnectPlanId, plan.id, 'Plan was set.');
assert.equal(subscription.projectId, project.id, 'Project was set.');
assert.equal(currentRouteName(), 'project.thank-you', 'User was redirected to the thank you route.');
});
});
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/models/stripe-connect-subscription-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { testForBelongsTo } from 'code-corps-ember/tests/helpers/relationship';

moduleForModel('stripe-connect-subscription', 'Unit | Model | stripe connect subscription', {
needs: [
'model:stripe-connect-plan',
'model:project',
'model:user'
]
});
Expand All @@ -16,5 +16,5 @@ test('it exists', function(assert) {

testForAttributes('stripe-connect-subscription', ['quantity']);

testForBelongsTo('stripe-connect-subscription', 'stripeConnectPlan');
testForBelongsTo('stripe-connect-subscription', 'project');
testForBelongsTo('stripe-connect-subscription', 'user');
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2272,9 +2272,9 @@ ember-autoresize@0.5.20:
dependencies:
ember-cli-babel "^5.1.6"

ember-can@0.8.1:
version "0.8.1"
resolved "https://registry.yarnpkg.com/ember-can/-/ember-can-0.8.1.tgz#f2000b7bdfa8b4a278b941d4e72cea06df5560ef"
ember-can@0.8.2:
version "0.8.2"
resolved "https://registry.yarnpkg.com/ember-can/-/ember-can-0.8.2.tgz#02eb6ffdb0df9064cb0b6cab63c1c8c5da537fba"
dependencies:
ember-cli-babel "^5.1.5"

Expand Down

0 comments on commit 790e0fc

Please sign in to comment.