Skip to content

Commit

Permalink
Merge pull request #46 from clemthenem/bugFix/staff-page
Browse files Browse the repository at this point in the history
Fixed bugs in the staff page and added improvments
  • Loading branch information
ouranos authored Jul 6, 2016
2 parents bce964a + 05ca7e2 commit b8917bd
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Service to update the current user

# We're not using angular-devise as the update functionality hasn't been
# merged yet.
# As we're using Devise + Her, we have custom routes to update the current user
# It then makes more sense to have an extra service rather than have customised
# fork of the upstream library


@App.service 'MnoeCurrentUser', (MnoeApiSvc, $window, $state) ->
_self = @

# Store the current_user promise
# Only one call will be executed even if there is multiple callers at the same time
userPromise = null

# Save the current user in variable to be able to reference it directly
@user = {}

# Get the current user admmin role
@getAdminRole = ->
return userPromise if userPromise?
userPromise = MnoeApiSvc.one('current_user').get().then(
(response) ->
adminRole = {admin_role: response.data.admin_role}
angular.copy(adminRole, _self.user)
response
)

return @
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Service for managing the users.
@App.service 'MnoeUsers', ($q, $log, MnoeAdminApiSvc, MnoeObservables, ADMIN_ROLES, OBS_KEYS) ->
@App.service 'MnoeUsers', ($q, $log, MnoeAdminApiSvc, MnoeObservables, OBS_KEYS) ->
_self = @

@list = (limit, offset, sort) ->
Expand All @@ -10,9 +10,9 @@
)

@staffs = (limit, offset, sort, params = {}) ->
# Require only users with an admin role
# Require only users with an admin role (gets any role, not necessarly defined in the frontend)
if ! params['where[admin_role.in][]']
params['where[admin_role.in][]'] = ADMIN_ROLES
params['where[admin_role.not]'] = ''

return _getStaffs(limit, offset, sort, params)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
console.log 'Remove staff role:' + staff
MnoeUsers.removeStaff(staff.id).then( ->
toastr.success("#{staff.name} #{staff.surname} has been successfully removed.")
)
)
)

# Fetch staffs
Expand All @@ -102,11 +102,11 @@
fetchStaffs(vm.staff.nbItems, 0).then( ->
# Notify me if a user is added
MnoeObservables.registerCb(OBS_KEYS.staffAdded, ->
displayCurrentState()
fetchStaffs(vm.staff.nbItems, vm.staff.offset)
)
# Notify me if the list changes
MnoeObservables.registerCb(OBS_KEYS.staffChanged, ->
displayCurrentState()
fetchStaffs(vm.staff.nbItems, vm.staff.offset)
)
)
return
Expand Down
3 changes: 2 additions & 1 deletion frontend-admin/src/app/index.constants.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@App
.constant('USER_ROLES', ['Member', 'Power User', 'Admin', 'Super Admin'])
.constant('ADMIN_ROLES', ['Admin', 'Staff'])
.constant('ADMIN_ROLES', ['admin', 'staff']) # Must be lower case
.constant('STAFF_PAGE_AUTH', ['admin'])
.constant('OBS_KEYS', {
userChanged: 'userListChanged',
staffChanged: 'staffListChanged',
Expand Down
8 changes: 7 additions & 1 deletion frontend-admin/src/app/views/dashboard.controller.coffee
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
@App.controller 'DashboardController', ($scope, $cookies, $sce, MnoeMarketplace, MnoErrorsHandler) ->
@App.controller 'DashboardController', ($scope, $cookies, $sce, MnoeMarketplace, MnoErrorsHandler, MnoeCurrentUser, STAFF_PAGE_AUTH) ->
'ngInject'
main = this

main.errorHandler = MnoErrorsHandler
main.staffPageAuthorized = STAFF_PAGE_AUTH

main.trustSrc = (src) ->
$sce.trustAsResourceUrl(src)
Expand Down Expand Up @@ -32,4 +33,9 @@
# Marketplace is cached
# MnoeMarketplace.getApps()

MnoeCurrentUser.getAdminRole().then(
# Display the layout
main.user = MnoeCurrentUser.user
)

return
2 changes: 1 addition & 1 deletion frontend-admin/src/app/views/dashboard.layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<li class="sidebar-list">
<a href="#/customers">Customers <span class="menu-icon fa fa-users"></span></a>
</li>
<li class="sidebar-list">
<li class="sidebar-list" ng-show="main.staffPageAuthorized.indexOf(main.user.admin_role)!=-1">
<a href="#/staff">Staff <span class="menu-icon fa fa-wrench"></span></a>
</li>
</ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
vm.isLoading = true
MnoeUsers.addStaff(vm.user).then(
(success) ->
toastr.success("#{vm.user.name} #{vm.user.surname} has been successfully added.")
# Close the modal returning the item to the parent window
$uibModalInstance.close(success.data)
# Send an email to the new staff
sendConfirmationEmail(vm.user)
(error) ->
toastr.error("An error occurred while adding #{vm.user.name} #{vm.user.surname}.")
$log.error("An error occurred:", error)
Expand All @@ -19,4 +18,23 @@
vm.onCancel = () ->
$uibModalInstance.dismiss('cancel')

sendConfirmationEmail = (user) ->
MnoeUsers.sendSignupEmail(user.email).then(
(success) ->
toastr.success("#{user.name} #{user.surname} has been successfully added.")
# Close the modal returning the item to the parent window
$uibModalInstance.close(success.data)
(error) ->
toastr.error("An error occurred while sending an email to #{user.email}.")
$log.error("An error occurred:", error)

# Remove the staff that has been added
MnoeUsers.removeStaff(vm.user.id).then(
(success) ->
(error) ->
toastr.error("An error occurred while deleting the user.")
$log.error("An error occurred:", error)
)
)

return

0 comments on commit b8917bd

Please sign in to comment.