2014-07-08 07:02:26 -04:00
|
|
|
define [
|
|
|
|
"base"
|
|
|
|
], (App) ->
|
2015-10-05 11:36:56 -04:00
|
|
|
App.controller "SubscriptionGroupMembersController", ($scope, queuedHttp) ->
|
2014-07-08 07:02:26 -04:00
|
|
|
$scope.users = window.users
|
|
|
|
$scope.groupSize = window.groupSize
|
|
|
|
$scope.selectedUsers = []
|
|
|
|
|
|
|
|
$scope.inputs =
|
|
|
|
emails: ""
|
|
|
|
|
|
|
|
parseEmails = (emailsString)->
|
|
|
|
regexBySpaceOrComma = /[\s,]+/
|
|
|
|
emails = emailsString.split(regexBySpaceOrComma)
|
|
|
|
emails = _.map emails, (email)->
|
|
|
|
email = email.trim()
|
|
|
|
emails = _.select emails, (email)->
|
|
|
|
email.indexOf("@") != -1
|
|
|
|
return emails
|
|
|
|
|
|
|
|
$scope.addMembers = () ->
|
|
|
|
emails = parseEmails($scope.inputs.emails)
|
|
|
|
for email in emails
|
|
|
|
queuedHttp
|
|
|
|
.post("/subscription/group/user", {
|
|
|
|
email: email,
|
|
|
|
_csrf: window.csrfToken
|
|
|
|
})
|
2017-06-20 11:27:05 -04:00
|
|
|
.then (response) ->
|
|
|
|
{ data } = response
|
2014-07-08 07:02:26 -04:00
|
|
|
$scope.users.push data.user if data.user?
|
|
|
|
$scope.inputs.emails = ""
|
|
|
|
|
|
|
|
$scope.removeMembers = () ->
|
|
|
|
for user in $scope.selectedUsers
|
|
|
|
do (user) ->
|
2017-06-08 11:53:22 -04:00
|
|
|
if user.holdingAccount and !user._id?
|
2017-06-09 05:39:48 -04:00
|
|
|
url = "/subscription/group/email/#{encodeURIComponent(user.email)}"
|
2017-06-08 07:12:08 -04:00
|
|
|
else
|
|
|
|
url = "/subscription/group/user/#{user._id}"
|
2014-07-08 07:02:26 -04:00
|
|
|
queuedHttp({
|
|
|
|
method: "DELETE",
|
2017-06-08 07:12:08 -04:00
|
|
|
url: url
|
2014-07-08 07:02:26 -04:00
|
|
|
headers:
|
|
|
|
"X-Csrf-Token": window.csrfToken
|
|
|
|
})
|
2017-06-20 06:49:55 -04:00
|
|
|
.then () ->
|
2014-07-08 07:02:26 -04:00
|
|
|
index = $scope.users.indexOf(user)
|
|
|
|
return if index == -1
|
|
|
|
$scope.users.splice(index, 1)
|
|
|
|
$scope.selectedUsers = []
|
|
|
|
|
|
|
|
$scope.updateSelectedUsers = () ->
|
|
|
|
$scope.selectedUsers = $scope.users.filter (user) -> user.selected
|
|
|
|
|
2015-10-05 11:36:56 -04:00
|
|
|
App.controller "SubscriptionGroupMemberListItemController", ($scope) ->
|
2014-07-08 07:02:26 -04:00
|
|
|
$scope.$watch "user.selected", (value) ->
|
|
|
|
if value?
|
|
|
|
$scope.updateSelectedUsers()
|