2014-11-28 20:49:41 -05:00
{ backboneFailure , genericFailure } = require ' core/errors '
RootView = require ' views/core/RootView '
2014-01-03 13:32:13 -05:00
template = require ' templates/admin '
2014-12-06 13:05:40 -05:00
AdministerUserModal = require ' views/admin/AdministerUserModal '
2014-01-03 13:32:13 -05:00
2014-07-23 10:02:45 -04:00
module.exports = class MainAdminView extends RootView
2014-06-30 22:16:26 -04:00
id: ' admin-view '
2014-01-03 13:32:13 -05:00
template: template
2014-08-30 20:09:57 -04:00
lastUserSearchValue: ' '
2014-04-25 19:57:42 -04:00
2014-02-26 17:14:43 -05:00
events:
2014-08-30 20:09:57 -04:00
' keyup ' : ' checkForFormSubmissionEnterPress '
2014-02-26 17:14:43 -05:00
' click # enter-espionage-mode ' : ' enterEspionageMode '
2014-08-30 20:09:57 -04:00
' click # user-search-button ' : ' searchForUser '
2014-06-09 05:59:27 -04:00
' click # increment-button ' : ' incrementUserAttribute '
2014-12-06 13:05:40 -05:00
' click # user-search-result ' : ' onClickUserSearchResult '
2015-03-19 18:02:45 -04:00
' click # create-free-sub-btn ' : ' onClickFreeSubLink '
2015-09-25 13:03:44 -04:00
' click # terminal-create ' : ' onClickTerminalSubLink '
2015-03-19 18:02:45 -04:00
getRenderData: ->
context = super ( )
context.freeSubLink = @ freeSubLink
context
2014-04-25 19:57:42 -04:00
2014-08-30 20:09:57 -04:00
checkForFormSubmissionEnterPress: (e) ->
if e . which is 13 and @ $el . find ( ' # espionage-name-or-email ' ) . val ( ) isnt ' '
@ enterEspionageMode ( )
return
if @ $el . find ( ' # user-search ' ) . val ( ) isnt @ lastUserSearchValue
@ searchForUser ( )
2014-04-25 19:57:42 -04:00
2014-08-30 20:09:57 -04:00
enterEspionageMode: ->
userNameOrEmail = @ $el . find ( ' # espionage-name-or-email ' ) . val ( ) . toLowerCase ( )
2014-02-26 17:14:43 -05:00
$ . ajax
2014-06-30 22:16:26 -04:00
type: ' POST ' ,
url: ' /auth/spy '
2014-08-30 20:09:57 -04:00
data: { nameOrEmailLower: userNameOrEmail }
2014-02-26 17:14:43 -05:00
success: @ espionageSuccess
error: @ espionageFailure
2014-04-25 19:57:42 -04:00
2014-02-26 17:14:43 -05:00
espionageSuccess: (model) ->
window . location . reload ( )
2014-04-25 19:57:42 -04:00
2014-08-30 20:09:57 -04:00
espionageFailure: (jqxhr, status, error)->
2014-02-26 17:14:43 -05:00
console . log " There was an error entering espionage mode: #{ error } "
2014-06-09 05:59:27 -04:00
2014-08-30 20:09:57 -04:00
searchForUser: ->
return @ onSearchRequestSuccess [ ] unless @lastUserSearchValue = @ $el . find ( ' # user-search ' ) . val ( ) . toLowerCase ( )
$ . ajax
type: ' POST ' ,
url: ' /db/user/-/admin_search '
data: { search: @ lastUserSearchValue }
success: @ onSearchRequestSuccess
error: @ onSearchRequestFailure
onSearchRequestSuccess: (users) =>
result = ' '
if users . length
2014-12-06 13:05:40 -05:00
result = ( " <tr data-user-id= ' #{ user . _id } ' ><td><code> #{ user . _id } </code></td><td> #{ _ . escape ( user . name or ' Anoner ' ) } </td><td> #{ _ . escape ( user . email ) } </td></tr> " for user in users )
2014-08-30 20:09:57 -04:00
result = " <table class= \" table \" > #{ result . join ( ' \n ' ) } </table> "
@ $el . find ( ' # user-search-result ' ) . html ( result )
onSearchRequestFailure: (jqxhr, status, error) =>
return if @ destroyed
console . warn " There was an error looking up #{ @ lastUserSearchValue } : " , error
2014-06-09 05:59:27 -04:00
incrementUserAttribute: (e) ->
val = $ ( ' # increment-field ' ) . val ( )
me . set ( val , me . get ( val ) + 1 )
me . save ( )
2015-03-19 18:02:45 -04:00
2014-12-06 13:05:40 -05:00
onClickUserSearchResult: (e) ->
userID = $ ( e . target ) . closest ( ' tr ' ) . data ( ' user-id ' )
@ openModalView new AdministerUserModal ( { } , userID ) if userID
2015-03-19 18:02:45 -04:00
onClickFreeSubLink: (e) =>
delete @ freeSubLink
return unless me . isAdmin ( )
options =
url: ' /db/prepaid/-/create '
2015-08-12 18:51:16 -04:00
data: { type: ' subscription ' , maxRedeemers: 1 }
2015-03-19 18:02:45 -04:00
method: ' POST '
options.success = (model, response, options) =>
# TODO: Don't hardcode domain.
if application . isProduction ( )
@freeSubLink = " https://codecombat.com/account/subscription?_ppc= #{ model . code } "
else
@freeSubLink = " http://localhost:3000/account/subscription?_ppc= #{ model . code } "
@ render ? ( )
options.error = (model, response, options) =>
console . error ' Failed to create prepaid ' , response
@ supermodel . addRequestResource ( ' create_prepaid ' , options , 0 ) . load ( )
2015-09-25 13:03:44 -04:00
onClickTerminalSubLink: (e) =>
@freeSubLink = ' '
return unless me . isAdmin ( )
options =
url: ' /db/prepaid/-/create '
method: ' POST '
data:
type: ' terminal_subscription '
maxRedeemers: parseInt ( $ ( " # users " ) . val ( ) )
months: parseInt ( $ ( " # months " ) . val ( ) )
options.success = (model, response, options) =>
# TODO: Don't hardcode domain.
if application . isProduction ( )
@freeSubLink = " https://codecombat.com/account/prepaid?_ppc= #{ model . code } "
else
@freeSubLink = " http://localhost:3000/account/prepaid?_ppc= #{ model . code } "
@ render ? ( )
options.error = (model, response, options) =>
console . error ' Failed to create prepaid ' , response
@ supermodel . addRequestResource ( ' create_prepaid ' , options , 0 ) . load ( )