|
(function() { |
|
'use strict'; |
|
|
|
// initialize the cobot api client with the storred access token |
|
var cobot = Cobot.Api(window.cobot.access_token); |
|
|
|
// resize Cobot's iframe according to height of this app |
|
window.setInterval(function() { |
|
window.Cobot.iframeResize($('body').outerHeight()); |
|
}, 200); |
|
|
|
// app |
|
window.MemberCancellations = Ember.Application.create({rootElement: '#app'}); |
|
|
|
// helpers for the template |
|
Ember.Handlebars.helper('amount', function(value) { |
|
return parseFloat(value).toFixed(2); |
|
}); |
|
|
|
Ember.Handlebars.helper('date', function(value) { |
|
if(value) { |
|
return value.substr(0, 10); |
|
} |
|
}); |
|
|
|
// routes |
|
|
|
MemberCancellations.Router.map(function () { |
|
this.route('index', { path: '/' }); |
|
this.route('member_cancellations', {path: '/member_cancellations/:year_month'}) |
|
}); |
|
|
|
MemberCancellations.IndexRoute = Ember.Route.extend({ |
|
redirect: function() { |
|
this.transitionTo('member_cancellations', moment().format('YYYY-MM')); |
|
} |
|
}); |
|
|
|
MemberCancellations.MemberCancellationsRoute = Ember.Route.extend({ |
|
model: function(params) { |
|
var monthMoment = moment(params.year_month + '-01'), |
|
from = monthMoment.clone().startOf('month'), |
|
to = monthMoment.clone().endOf('month'); |
|
return Ember.RSVP.hash({ |
|
memberships: this.loadMembers(from, to), |
|
month: monthMoment |
|
}); |
|
}, |
|
|
|
loadMembers: function(from, to) { |
|
return new Ember.RSVP.Promise(function(resolve, reject) { |
|
//making an api request to the activities for a space |
|
cobotClient.get(cobot.subdomain, '/activities', {types: 'membership_canceled', |
|
from: from.format('YYYY-MM-DD'), to: to.format('YYYY-MM-DD')}).then(function(activities) { |
|
var ids = activities.map(function(a) { |
|
return a.attributes.membership_id; |
|
}); |
|
//Api request to get all memberships with the passed in ids. |
|
cobotClient.get(cobot.subdomain, '/memberships', {ids: ids.join(',')}).then(function(memberships) { |
|
resolve(memberships); |
|
}); |
|
}) |
|
}) |
|
} |
|
}); |
|
|
|
// controllers |
|
|
|
MemberCancellations.MemberCancellationsController = Ember.Controller.extend({ |
|
memberships: Ember.computed('model.memberships', function() { |
|
return this.get('model.memberships'); |
|
}), |
|
membershipsSortBy: ['canceled_to'], |
|
sortedMemberships: Ember.computed.sort('memberships', 'membershipsSortBy'), |
|
|
|
csv: function() { |
|
var csv = planChangesCSV(this.get('memberships')); |
|
return 'data:text/csv;base64,' + csv; |
|
|
|
// encoding the csv |
|
function planChangesCSV(memberships) { |
|
return b64EncodeUnicode(header() + _.chain(memberships).select(function(m) { |
|
return m.get('canceled_to'); }).map(toRow).join("\n").value()); |
|
|
|
function toRow(m) { |
|
return [ |
|
m.get('name'), |
|
m.get('plan.name'), |
|
m.get('canceled_to'), |
|
].map(addQuotes).join(','); |
|
} |
|
|
|
function addQuotes(thing) { |
|
if(typeof thing === 'string') { |
|
return '"' + (thing + '').replace(/"/g, '\'') + '"'; |
|
} else { |
|
return thing; |
|
} |
|
} |
|
|
|
function header() { |
|
return "Name,Plan,Date\n"; |
|
} |
|
} |
|
}.property('model'), |
|
|
|
filename: function() { |
|
return 'member_cancellations_' + this.get('currentMonth.date').format('YYYY-MM') + '.csv'; |
|
}.property('month'), |
|
|
|
month: Ember.computed.readOnly('model.month'), |
|
previousMonth: function() { |
|
var previous = this.get('currentMonth.date').clone().subtract('month', 1); |
|
return this._monthMetadata(previous); |
|
}.property('month'), |
|
currentMonth: function() { |
|
var current = this.get('month'); |
|
return {label: current.format("MMM YYYY"), date: current}; |
|
}.property('month'), |
|
nextMonth: function() { |
|
var next = this.get('currentMonth.date').clone().add('month', 1); |
|
if(!next.isAfter(moment().endOf('month'))) { |
|
return this._monthMetadata(next); |
|
} |
|
}.property('month'), |
|
|
|
_monthMetadata: function(month) { |
|
return {label: month.format("MMM YYYY"), year_month: month.format('YYYY-MM')}; |
|
} |
|
}); |
|
})(); |