1
- zenergyApp . controller ( "accountManagementPageController" , [ "$scope" , "$http" , "tokenService" , "$window" , function ( $scope , $http , $window ) {
1
+ zenergyApp . controller ( "accountManagementPageController" , [ "$scope" , "$http" , "tokenService" , "$window" , "$location" , function ( $scope , $http , tokenService , $window , $location ) {
2
2
3
- console . log ( tokenService . userId ) ;
4
-
5
- /* var response = $http({
6
- url: '/api/users/',
7
- method: 'GET',
8
- data: { userId: 1, password: CryptoJS.MD5($scope.user.password).toString(), lastName: $scope.user.lastName, firstName: $scope.user.firstName, adr1: $scope.user.adr1, adr2: $scope.user.adr2, pc: $scope.user.pc, town: $scope.user.town, mail: $scope.user.mail, phone: $scope.user.phone },
9
- headers: {
10
- 'Content-Type': 'application/json'
11
- }
12
- }).then(function successCallback(response) {
13
- $scope.hasError = false;
14
- window.location.replace("/home"); // $location.path("/#");
15
- }, function errorCallback(response) {
16
- $scope.hasError = true;
17
- $scope.user.mail = '';
18
- $scope.user.password = '';
19
- });*/
3
+ // Get user data
4
+ var response = $http ( {
5
+ url : '/api/users/' + tokenService . getUserId ( ) ,
6
+ method : 'GET' ,
7
+ headers : {
8
+ 'Content-Type' : 'application/json'
9
+ }
10
+ } ) . then ( function successCallback ( response ) {
11
+ $scope . hasError = false ;
12
+ $scope . user = { mail : response . data . mail , password : response . data . password , lastName : response . data . lastname , firstName : response . data . firstname , adr1 : response . data . adr1 , adr2 : response . data . adr2 , pc : response . data . pc , town : response . data . town , phone : response . data . phone , member : response . data . member } ;
13
+ $scope . isNotMember = $scope . user . member == null ;
14
+ if ( ! $scope . isNotMember ) {
15
+ var date = new Date ( response . data . member . dateMembership ) ;
16
+ $scope . user . dateMembership = date . getMonth ( ) + 1 + "/" + date . getDate ( ) + "/" + date . getFullYear ( ) ;
17
+ }
18
+ } ) ;
20
19
21
- $scope . user = { mail : '' , password : '' , passwordBis : '' , lastName : '' , firstName : '' , adr1 : '' , adr2 : '' , pc : '' , town : '' , phone : '' } ;
22
20
$scope . hasError = false ;
23
21
$scope . passNotMatch = false ;
24
22
25
-
26
-
23
+ // Modify account info
27
24
$scope . changeInformations = function ( ) {
28
-
29
25
if ( ! $scope . hasError ) {
30
26
var response = $http ( {
31
- url : '/api/Account/update' ,
27
+ url : '/api/users/' + tokenService . getUserId ( ) ,
32
28
method : 'PUT' ,
33
- data : { userId : 1 , password : CryptoJS . MD5 ( $scope . user . password ) . toString ( ) , lastName : $scope . user . lastName , firstName : $scope . user . firstName , adr1 : $scope . user . adr1 , adr2 : $scope . user . adr2 , pc : $scope . user . pc , town : $scope . user . town , mail : $scope . user . mail , phone : $scope . user . phone } ,
29
+ data : { userId : tokenService . getUserId ( ) , password : $scope . user . password , lastName : $scope . user . lastName , firstName : $scope . user . firstName , adr1 : $scope . user . adr1 , adr2 : $scope . user . adr2 , pc : $scope . user . pc , town : $scope . user . town , mail : $scope . user . mail , phone : $scope . user . phone } ,
34
30
headers : {
35
31
'Content-Type' : 'application/json'
36
32
}
37
33
} ) . then ( function successCallback ( response ) {
38
34
$scope . hasError = false ;
39
- window . location . replace ( "/home" ) ; // $location.path("/#");
35
+ bootbox . alert ( "Your account is updated!" , function ( ) {
36
+ window . location . reload ( true ) ;
37
+ } ) ;
40
38
} , function errorCallback ( response ) {
41
39
$scope . hasError = true ;
42
- $scope . user . mail = '' ;
43
- $scope . user . password = '' ;
44
40
} ) ;
45
41
}
46
42
} ;
47
43
44
+ // Change password
48
45
$scope . changePassword = function ( ) {
49
46
50
- if ( $scope . user . password != $scope . user . passwordBis ) {
51
- $scope . hasError = true ;
47
+ if ( $scope . user . newPassword != $scope . user . newPasswordBis ) {
52
48
$scope . passNotMatch = true ;
53
49
}
54
50
else {
55
- $scope . hasError = false ;
56
51
$scope . passNotMatch = false ;
57
52
}
58
53
59
- if ( ! $scope . hasError ) {
54
+ if ( ! $scope . passNotMatch ) {
60
55
var response = $http ( {
61
- url : '/api/Account/update' ,
62
- method : 'POST ' ,
63
- data : { userId : 1 , password : CryptoJS . MD5 ( $scope . user . password ) . toString ( ) , lastName : $scope . user . lastName , firstName : $scope . user . firstName , adr1 : $scope . user . adr1 , adr2 : $scope . user . adr2 , pc : $scope . user . pc , town : $scope . user . town , mail : $scope . user . mail , phone : $scope . user . phone } ,
56
+ url : '/api/users/' + tokenService . getUserId ( ) ,
57
+ method : 'PUT ' ,
58
+ data : { userId : tokenService . getUserId ( ) , password : CryptoJS . MD5 ( $scope . user . newPassword ) . toString ( ) , lastName : $scope . user . lastName , firstName : $scope . user . firstName , adr1 : $scope . user . adr1 , adr2 : $scope . user . adr2 , pc : $scope . user . pc , town : $scope . user . town , mail : $scope . user . mail , phone : $scope . user . phone } ,
64
59
headers : {
65
60
'Content-Type' : 'application/json'
66
61
}
67
62
} ) . then ( function successCallback ( response ) {
68
63
$scope . hasError = false ;
69
- window . location . replace ( "/home" ) ;
64
+ window . location . reload ( true ) ;
65
+ bootbox . alert ( "Your account is updated!" , function ( ) {
66
+ window . location . reload ( true ) ;
67
+ } ) ;
70
68
} , function errorCallback ( response ) {
71
69
$scope . hasError = true ;
72
- $scope . user . mail = '' ;
73
- $scope . user . password = '' ;
74
70
} ) ;
75
71
}
76
72
} ;
73
+
74
+ // Add membership
75
+ $scope . becomeMember = function ( ) {
76
+ var today = new Date ( ) ;
77
+
78
+ var response = $http ( {
79
+ url : '/api/members/' ,
80
+ method : 'POST' ,
81
+ data : { userId : tokenService . getUserId ( ) , dateMembership : today } ,
82
+ headers : {
83
+ 'Content-Type' : 'application/json'
84
+ }
85
+ } ) . then ( function successCallback ( response ) {
86
+ $scope . hasError = false ;
87
+ window . location . reload ( true ) ;
88
+ } , function errorCallback ( response ) {
89
+ $scope . hasError = true ;
90
+ } ) ;
91
+ } ;
77
92
} ] ) ;
0 commit comments