Advertisement
Guest User

Untitled

a guest
Jun 10th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. angular.module('myApp')
  2.     .controller('AddUserController',
  3.         function($scope,
  4.             $rootScope,
  5.             $http,
  6.             REST_URL,
  7.             $uibModal,
  8.             $uibModalInstance,
  9.             GoogleAnalytics,
  10.             company,
  11.             user,
  12.             CompanyService,
  13.             $routeParams) {
  14.             'use strict';
  15.  
  16.             $scope.company = company;
  17.             $scope.getLocations = getLocations;
  18.             $scope.save = save;
  19.             $scope.close = close;
  20.             $scope.user = user || {};
  21.             $scope.editUser = !!user;
  22.             $scope.selected = {
  23.                 location: $scope.editUser ? { id: user.defaultLocationId, name: user.locationName } : null,
  24.             };
  25.  
  26.             function getLocations() {
  27.                 var params = {
  28.                     sortName: 'name',
  29.                     sortDirection: 'ASC',
  30.                     start: 0,
  31.                     limit: 100,
  32.                     archiving: 'non-archived'
  33.                 };
  34.  
  35.                 CompanyService.getLocations(REST_URL, $routeParams.id, params).then(function(result) {
  36.                     $scope.locations = result.data;
  37.                 });
  38.             }
  39.  
  40.             function save() {
  41.                 var userDetail = {
  42.                     user: {
  43.                         name: $scope.user.name,
  44.                         username: $scope.user.email,
  45.                         password: $scope.user.password,
  46.                         defaultLocationId: $scope.selected.location.id,
  47.                         createdBy: $rootScope.globals.userid,
  48.                         modifiedBy: $rootScope.globals.userid,
  49.                     },
  50.                     company: company,
  51.                     sellerCompany: $rootScope.globals.sellerCompany,
  52.                     defaultLocation: $scope.selected.location,
  53.                     roleList: [{
  54.                         role: 'buyer',
  55.                     }],
  56.                     sendNotification: true,
  57.                 };
  58.  
  59.                 if ($scope.editUser) {
  60.                     delete userDetail.user.password;
  61.                     delete userDetail.user.createdBy;
  62.                     delete userDetail.roleList;
  63.                     delete userDetail.sendNotification;
  64.                 }
  65.  
  66.                 if ($scope.editUser) {
  67.                     $http.put(REST_URL + '/users/' + $scope.user.id, userDetail).then(success);
  68.                 } else {
  69.                     $http.post(REST_URL + '/users', userDetail).then(success);
  70.                 }
  71.             }
  72.  
  73.             function success(result) {
  74.                 noty({
  75.                     layout: 'topRight',
  76.                     type: 'success',
  77.                     text: 'User: ' + $scope.user.name + ' was successfully ' + ($scope.editUser ? 'updated' : 'created'),
  78.                     animation: {
  79.                         open: { height: 'toggle' },
  80.                         close: { height: 'toggle' },
  81.                         easing: 'swing',
  82.                         speed: 500
  83.                     },
  84.                     timeout: 3000,
  85.                     closeWith: ['click'],
  86.                 });
  87.  
  88.                 $uibModalInstance.close(true);
  89.             }
  90.  
  91.             function close() {
  92.                 $uibModalInstance.close(false);
  93.             }
  94.         });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement