2014-12-06 13:05:40 -05:00
|
|
|
ModalView = require 'views/core/ModalView'
|
|
|
|
template = require 'templates/admin/administer-user-modal'
|
|
|
|
User = require 'models/User'
|
2015-11-10 17:33:27 -05:00
|
|
|
Prepaid = require 'models/Prepaid'
|
2014-12-06 13:05:40 -05:00
|
|
|
|
|
|
|
module.exports = class AdministerUserModal extends ModalView
|
|
|
|
id: "administer-user-modal"
|
|
|
|
template: template
|
2015-02-11 16:12:42 -05:00
|
|
|
|
2014-12-06 13:05:40 -05:00
|
|
|
events:
|
|
|
|
'click #save-changes': 'onSaveChanges'
|
2015-11-10 17:33:27 -05:00
|
|
|
'click #add-seats-btn': 'onClickAddSeatsButton'
|
2015-02-11 16:12:42 -05:00
|
|
|
|
2014-12-06 13:05:40 -05:00
|
|
|
constructor: (options, @userHandle) ->
|
|
|
|
super(options)
|
2016-02-18 11:44:40 -05:00
|
|
|
@user = @supermodel.loadModel(new User({_id:@userHandle}), {cache: false}).model
|
2015-02-11 16:12:42 -05:00
|
|
|
options = {cache: false, url: '/stripe/coupons'}
|
|
|
|
options.success = (@coupons) =>
|
2014-12-06 13:05:40 -05:00
|
|
|
@couponsResource = @supermodel.addRequestResource('coupon', options)
|
|
|
|
@couponsResource.load()
|
2015-02-11 16:12:42 -05:00
|
|
|
|
2014-12-06 13:05:40 -05:00
|
|
|
getRenderData: ->
|
|
|
|
c = super()
|
|
|
|
stripe = @user.get('stripe') or {}
|
|
|
|
c.free = stripe.free is true
|
|
|
|
c.freeUntil = _.isString(stripe.free)
|
|
|
|
c.freeUntilDate = if c.freeUntil then stripe.free else new Date().toISOString()[...10]
|
|
|
|
c.coupon = stripe.couponID
|
|
|
|
c.coupons = @coupons or []
|
|
|
|
for coupon in c.coupons
|
|
|
|
bits = [coupon.id]
|
|
|
|
if coupon.percent_off
|
|
|
|
bits.push "(#{coupon.percent_off}% off)"
|
|
|
|
else if coupon.amount_off
|
|
|
|
bits.push "($#{coupon.amount_off} off)"
|
|
|
|
if coupon.duration
|
|
|
|
bits.push "(duration: #{coupon.duration})"
|
|
|
|
if coupon.redeem_by
|
|
|
|
bits.push "(redeem by: #{moment(coupon.redeem_by).format('lll')}"
|
|
|
|
coupon.format = bits.join(' ')
|
|
|
|
c.none = not (c.free or c.freeUntil or c.coupon)
|
|
|
|
c.user = @user
|
|
|
|
c
|
2015-02-11 16:12:42 -05:00
|
|
|
|
2014-12-06 13:05:40 -05:00
|
|
|
onSaveChanges: ->
|
|
|
|
stripe = _.clone(@user.get('stripe') or {})
|
|
|
|
delete stripe.free
|
|
|
|
delete stripe.couponID
|
2015-02-11 16:12:42 -05:00
|
|
|
|
2014-12-06 13:05:40 -05:00
|
|
|
selection = @$el.find('input[name="stripe-benefit"]:checked').val()
|
|
|
|
dateVal = @$el.find('#free-until-date').val()
|
|
|
|
couponVal = @$el.find('#coupon-select').val()
|
|
|
|
switch selection
|
|
|
|
when 'free' then stripe.free = true
|
|
|
|
when 'free-until' then stripe.free = dateVal
|
|
|
|
when 'coupon' then stripe.couponID = couponVal
|
2015-02-11 16:12:42 -05:00
|
|
|
|
2014-12-06 13:05:40 -05:00
|
|
|
@user.set('stripe', stripe)
|
|
|
|
options = {}
|
|
|
|
options.success = => @hide()
|
|
|
|
@user.patch(options)
|
2015-11-10 17:33:27 -05:00
|
|
|
|
|
|
|
onClickAddSeatsButton: ->
|
|
|
|
maxRedeemers = parseInt(@$('#seats-input').val())
|
|
|
|
return unless maxRedeemers and maxRedeemers > 0
|
|
|
|
prepaid = new Prepaid({
|
|
|
|
maxRedeemers: maxRedeemers
|
|
|
|
type: 'course'
|
|
|
|
creator: @user.id
|
2015-11-12 15:16:40 -05:00
|
|
|
properties:
|
|
|
|
adminAdded: me.id
|
2015-11-10 17:33:27 -05:00
|
|
|
})
|
|
|
|
prepaid.save()
|
|
|
|
@state = 'creating-prepaid'
|
|
|
|
@renderSelectors('#prepaid-form')
|
|
|
|
@listenTo prepaid, 'sync', ->
|
|
|
|
@state = 'made-prepaid'
|
|
|
|
@renderSelectors('#prepaid-form')
|