2018-06-15 06:30:15 -04:00
|
|
|
define [
|
|
|
|
"base"
|
|
|
|
], (App) ->
|
2018-06-22 06:24:14 -04:00
|
|
|
App.controller "UserAffiliationsController", ["$scope", "UserAffiliationsDataService", "$q", "_", ($scope, UserAffiliationsDataService, $q, _) ->
|
2018-06-20 09:22:06 -04:00
|
|
|
$scope.userEmails = []
|
2018-06-15 06:30:15 -04:00
|
|
|
|
2018-06-19 12:10:55 -04:00
|
|
|
LOCAL_AND_DOMAIN_REGEX = /([^@]+)@(.+)/
|
2018-06-21 12:18:01 -04:00
|
|
|
EMAIL_REGEX = /^([A-Za-z0-9_\-\.]+)@([^\.]+)\.([A-Za-z0-9_\-\.]+)([^\.])$/
|
2018-06-15 06:30:15 -04:00
|
|
|
|
2018-06-19 12:10:55 -04:00
|
|
|
_matchLocalAndDomain = (userEmailInput) ->
|
|
|
|
match = userEmailInput?.match LOCAL_AND_DOMAIN_REGEX
|
2018-06-15 06:30:15 -04:00
|
|
|
if match?
|
|
|
|
{ local: match[1], domain: match[2] }
|
|
|
|
else
|
|
|
|
{ local: null, domain: null }
|
|
|
|
|
|
|
|
$scope.getEmailSuggestion = (userInput) ->
|
2018-06-19 12:10:55 -04:00
|
|
|
userInputLocalAndDomain = _matchLocalAndDomain(userInput)
|
2018-06-21 12:18:01 -04:00
|
|
|
$scope.ui.isValidEmail = EMAIL_REGEX.test userInput
|
|
|
|
$scope.ui.isBlacklistedEmail = false
|
|
|
|
$scope.ui.showManualUniversitySelectionUI = false
|
2018-06-19 12:10:55 -04:00
|
|
|
if userInputLocalAndDomain.domain?
|
2018-06-21 12:18:01 -04:00
|
|
|
$scope.ui.isBlacklistedEmail = UserAffiliationsDataService.isDomainBlacklisted userInputLocalAndDomain.domain
|
2018-06-19 12:10:55 -04:00
|
|
|
UserAffiliationsDataService.getUniversityDomainFromPartialDomainInput(userInputLocalAndDomain.domain)
|
2018-06-25 11:13:43 -04:00
|
|
|
.then (universityDomain) ->
|
2018-06-26 11:18:53 -04:00
|
|
|
currentUserInputLocalAndDomain = _matchLocalAndDomain $scope.newAffiliation.email
|
|
|
|
if currentUserInputLocalAndDomain.domain == universityDomain.hostname
|
2018-06-19 12:10:55 -04:00
|
|
|
$scope.newAffiliation.university = universityDomain.university
|
|
|
|
$scope.newAffiliation.department = universityDomain.department
|
|
|
|
else
|
|
|
|
$scope.newAffiliation.university = null
|
|
|
|
$scope.newAffiliation.department = null
|
|
|
|
$q.resolve "#{userInputLocalAndDomain.local}@#{universityDomain.hostname}"
|
2018-06-15 06:30:15 -04:00
|
|
|
.catch () ->
|
|
|
|
$scope.newAffiliation.university = null
|
|
|
|
$scope.newAffiliation.department = null
|
|
|
|
$q.reject null
|
2018-06-19 12:10:55 -04:00
|
|
|
else
|
|
|
|
$scope.newAffiliation.university = null
|
|
|
|
$scope.newAffiliation.department = null
|
2018-06-26 11:50:55 -04:00
|
|
|
$q.reject null
|
2018-06-15 06:30:15 -04:00
|
|
|
|
2018-06-19 12:10:55 -04:00
|
|
|
|
|
|
|
$scope.selectUniversityManually = () ->
|
|
|
|
$scope.newAffiliation.university = null
|
|
|
|
$scope.newAffiliation.department = null
|
2018-06-21 12:18:01 -04:00
|
|
|
$scope.ui.showManualUniversitySelectionUI = true
|
2018-06-19 12:10:55 -04:00
|
|
|
|
2018-07-04 11:37:40 -04:00
|
|
|
$scope.changeAffiliation = (userEmail) ->
|
2018-07-06 08:45:34 -04:00
|
|
|
if userEmail.affiliation?.institution?.id?
|
|
|
|
UserAffiliationsDataService.getUniversityDetails userEmail.affiliation.institution.id
|
|
|
|
.then (universityDetails) -> $scope.affiliationToChange.university = universityDetails
|
|
|
|
|
2018-07-04 11:37:40 -04:00
|
|
|
$scope.affiliationToChange.email = userEmail.email
|
|
|
|
$scope.affiliationToChange.role = userEmail.affiliation.role
|
|
|
|
$scope.affiliationToChange.department = userEmail.affiliation.department
|
|
|
|
|
2018-07-17 06:12:47 -04:00
|
|
|
$scope.saveAffiliationChange = (userEmail) ->
|
|
|
|
userEmail.affiliation.role = $scope.affiliationToChange.role
|
|
|
|
userEmail.affiliation.department = $scope.affiliationToChange.department
|
|
|
|
_resetAffiliationToChange()
|
|
|
|
_monitorRequest(
|
|
|
|
UserAffiliationsDataService
|
|
|
|
.addRoleAndDepartment(
|
|
|
|
userEmail.email,
|
|
|
|
userEmail.affiliation.role,
|
|
|
|
userEmail.affiliation.department
|
|
|
|
)
|
|
|
|
)
|
2018-07-06 09:04:38 -04:00
|
|
|
.then () ->
|
2018-07-17 06:12:47 -04:00
|
|
|
setTimeout () -> _getUserEmails()
|
2018-07-04 11:37:40 -04:00
|
|
|
|
|
|
|
$scope.cancelAffiliationChange = (email) ->
|
2018-07-17 06:12:47 -04:00
|
|
|
_resetAffiliationToChange()
|
2018-07-04 11:37:40 -04:00
|
|
|
|
|
|
|
$scope.isChangingAffiliation = (email) ->
|
|
|
|
$scope.affiliationToChange.email == email
|
|
|
|
|
2018-06-21 12:18:01 -04:00
|
|
|
$scope.showAddEmailForm = () ->
|
|
|
|
$scope.ui.showAddEmailUI = true
|
|
|
|
|
|
|
|
$scope.addNewEmail = () ->
|
2018-06-19 12:10:55 -04:00
|
|
|
if !$scope.newAffiliation.university?
|
2018-06-20 09:22:06 -04:00
|
|
|
addEmailPromise = UserAffiliationsDataService
|
|
|
|
.addUserEmail $scope.newAffiliation.email
|
2018-06-19 12:10:55 -04:00
|
|
|
else
|
|
|
|
if $scope.newAffiliation.university.isUserSuggested
|
2018-06-20 09:22:06 -04:00
|
|
|
addEmailPromise = UserAffiliationsDataService
|
|
|
|
.addUserAffiliationWithUnknownUniversity(
|
|
|
|
$scope.newAffiliation.email,
|
|
|
|
$scope.newAffiliation.university.name,
|
|
|
|
$scope.newAffiliation.country.code,
|
|
|
|
$scope.newAffiliation.role,
|
|
|
|
$scope.newAffiliation.department
|
|
|
|
)
|
2018-06-19 12:10:55 -04:00
|
|
|
else
|
2018-06-20 09:22:06 -04:00
|
|
|
addEmailPromise = UserAffiliationsDataService
|
|
|
|
.addUserAffiliation(
|
|
|
|
$scope.newAffiliation.email,
|
|
|
|
$scope.newAffiliation.university.id
|
|
|
|
$scope.newAffiliation.role,
|
|
|
|
$scope.newAffiliation.department
|
|
|
|
)
|
2018-07-17 06:12:47 -04:00
|
|
|
|
|
|
|
$scope.ui.isAddingNewEmail = true
|
|
|
|
$scope.ui.showAddEmailUI = false
|
|
|
|
_monitorRequest(addEmailPromise)
|
|
|
|
.then () ->
|
|
|
|
_resetNewAffiliation()
|
|
|
|
_resetAddingEmail()
|
|
|
|
setTimeout () -> _getUserEmails()
|
|
|
|
.finally () ->
|
|
|
|
$scope.ui.isAddingNewEmail = false
|
2018-06-21 12:18:01 -04:00
|
|
|
|
2018-07-04 11:54:04 -04:00
|
|
|
$scope.setDefaultUserEmail = (userEmail) ->
|
2018-07-17 06:12:47 -04:00
|
|
|
_monitorRequest(
|
|
|
|
UserAffiliationsDataService
|
|
|
|
.setDefaultUserEmail userEmail.email
|
|
|
|
)
|
|
|
|
.then () ->
|
|
|
|
for email in $scope.userEmails or []
|
|
|
|
email.default = false
|
|
|
|
userEmail.default = true
|
2018-06-21 12:18:01 -04:00
|
|
|
|
2018-07-04 11:54:04 -04:00
|
|
|
$scope.removeUserEmail = (userEmail) ->
|
2018-07-17 06:12:47 -04:00
|
|
|
$scope.userEmails = $scope.userEmails.filter (ue) -> ue != userEmail
|
|
|
|
_monitorRequest(
|
|
|
|
UserAffiliationsDataService
|
|
|
|
.removeUserEmail userEmail.email
|
|
|
|
)
|
2018-07-05 06:36:02 -04:00
|
|
|
|
2018-07-12 12:13:26 -04:00
|
|
|
$scope.resendConfirmationEmail = (userEmail) ->
|
2018-07-17 06:12:47 -04:00
|
|
|
$scope.ui.isResendingConfirmation = true
|
|
|
|
_monitorRequest(
|
|
|
|
UserAffiliationsDataService
|
|
|
|
.resendConfirmationEmail userEmail.email
|
|
|
|
)
|
|
|
|
.finally () ->
|
|
|
|
$scope.ui.isResendingConfirmation = false
|
2018-07-12 12:13:26 -04:00
|
|
|
|
2018-07-05 06:36:02 -04:00
|
|
|
$scope.acknowledgeError = () ->
|
|
|
|
_reset()
|
|
|
|
_getUserEmails()
|
2018-06-21 12:18:01 -04:00
|
|
|
|
2018-07-17 06:12:47 -04:00
|
|
|
_resetAffiliationToChange = () ->
|
|
|
|
$scope.affiliationToChange =
|
|
|
|
email: ""
|
|
|
|
university: null
|
|
|
|
role: null
|
|
|
|
department: null
|
|
|
|
|
|
|
|
_resetNewAffiliation = () ->
|
2018-06-21 12:18:01 -04:00
|
|
|
$scope.newAffiliation =
|
|
|
|
email: ""
|
|
|
|
country: null
|
|
|
|
university: null
|
|
|
|
role: null
|
|
|
|
department: null
|
2018-07-17 06:12:47 -04:00
|
|
|
|
|
|
|
_resetAddingEmail = () ->
|
|
|
|
$scope.ui.showAddEmailUI = false
|
|
|
|
$scope.ui.isValidEmail = false
|
|
|
|
$scope.ui.isBlacklistedEmail = false
|
|
|
|
$scope.ui.showManualUniversitySelectionUI = false
|
|
|
|
|
|
|
|
_reset = () ->
|
2018-06-21 12:18:01 -04:00
|
|
|
$scope.ui =
|
2018-07-05 06:36:02 -04:00
|
|
|
hasError: false
|
2018-07-17 06:12:47 -04:00
|
|
|
errorMessage: ""
|
2018-07-04 11:37:40 -04:00
|
|
|
showChangeAffiliationUI: false
|
2018-07-17 06:12:47 -04:00
|
|
|
isMakingRequest: false
|
2018-06-21 12:18:01 -04:00
|
|
|
isLoadingEmails: false
|
|
|
|
isAddingNewEmail: false
|
2018-07-17 06:12:47 -04:00
|
|
|
isResendingConfirmation: false
|
|
|
|
_resetAffiliationToChange()
|
|
|
|
_resetNewAffiliation()
|
|
|
|
_resetAddingEmail()
|
2018-06-21 12:18:01 -04:00
|
|
|
_reset()
|
|
|
|
|
2018-07-17 06:12:47 -04:00
|
|
|
_monitorRequest = (promise) ->
|
|
|
|
$scope.ui.hasError = false
|
|
|
|
$scope.ui.isMakingRequest = true
|
|
|
|
promise
|
|
|
|
.catch (response) ->
|
|
|
|
$scope.ui.hasError = true
|
|
|
|
$scope.ui.errorMessage = response?.data?.message
|
|
|
|
.finally () ->
|
|
|
|
$scope.ui.isMakingRequest = false
|
|
|
|
return promise
|
|
|
|
|
2018-06-20 09:22:06 -04:00
|
|
|
# Populates the emails table
|
2018-06-21 12:18:01 -04:00
|
|
|
_getUserEmails = () ->
|
|
|
|
$scope.ui.isLoadingEmails = true
|
2018-07-17 06:12:47 -04:00
|
|
|
_monitorRequest(
|
|
|
|
UserAffiliationsDataService
|
|
|
|
.getUserEmails()
|
|
|
|
)
|
2018-06-21 12:18:01 -04:00
|
|
|
.then (emails) ->
|
|
|
|
$scope.userEmails = emails
|
2018-07-17 06:12:47 -04:00
|
|
|
.finally () ->
|
2018-06-21 12:18:01 -04:00
|
|
|
$scope.ui.isLoadingEmails = false
|
|
|
|
_getUserEmails()
|
2018-06-19 12:10:55 -04:00
|
|
|
|
2018-06-15 06:30:15 -04:00
|
|
|
]
|