@@ -27,27 +27,11 @@ angular.module('strecku.terminal', [
27
27
} ) ;
28
28
29
29
// Log entries
30
- $scope . updatePurchases = ( ) => {
31
- $http . get ( '/api/v1/purchases?limit=25' )
32
- . then ( function ( res ) {
33
- $scope . log = res . data . purchases ;
34
- // Listen to new in realtime
35
- StoreLog . on ( 'purchase' , ( purchase ) => $scope . log . push ( purchase ) ) ;
36
- } )
37
- . catch ( err => console . error ( 'err' , err ) ) ;
38
- } ;
39
- $scope . updatePurchases ( ) ;
40
-
41
- // Additional user data
42
- $scope . $watch ( 'item.user' , function ( user ) {
43
- if ( user ) $q . all ( [
44
- $http . get ( `/api/v1/purchases?user=${ user . _id } &limit=5` ) ,
45
- // $http.get(`/api/v1/stores/this/summary?user=${user._id}`)
46
- ] ) . then ( function ( res ) {
47
- $scope . item . purchases = res [ 0 ] . data . purchases ;
48
- // $scope.item.summary = res[1].data;
49
- } ) ;
50
- } ) ;
30
+ $http . get ( '/api/v1/purchases?limit=25' ) . then ( res => {
31
+ $scope . log = res . data . purchases ;
32
+ // Listen to new in realtime
33
+ StoreLog . on ( 'purchase' , ( purchase ) => $scope . log . push ( purchase ) ) ;
34
+ } )
51
35
52
36
// Code scanner callback
53
37
$scope . onCode = function ( code ) {
@@ -81,7 +65,14 @@ angular.module('strecku.terminal', [
81
65
// Set/reset current
82
66
else {
83
67
timeout ( ) ;
84
- $scope . item = item ;
68
+ if ( item . type === 'user' ) {
69
+ $http . get ( `/api/v1/purchases?user=${ item . user . _id } &limit=5` ) . then ( res => {
70
+ item . purchases = res . data . purchases ;
71
+ $scope . item = item ;
72
+ } ) ;
73
+ } else {
74
+ $scope . item = item ;
75
+ }
85
76
}
86
77
}
87
78
function onNull ( code ) {
@@ -105,8 +96,19 @@ angular.module('strecku.terminal', [
105
96
timeout ( ) ;
106
97
} , timeout ) ;
107
98
}
108
- // Reset current
109
- else $scope . item = null ;
99
+ // cancel new code
100
+ else if ( $scope . item && $scope . item . pendingCode === code ) {
101
+ $scope . item = null ;
102
+ }
103
+ // new code = show list of users
104
+ else {
105
+ $http . get ( `/api/v1/users` ) . then ( res => {
106
+ $scope . item = {
107
+ pendingCode : code ,
108
+ users : res . data . users ,
109
+ } ;
110
+ } ) ;
111
+ }
110
112
}
111
113
112
114
// Helper functions
@@ -118,20 +120,17 @@ angular.module('strecku.terminal', [
118
120
$scope . item . product . _id == item . product . _id ) ) ;
119
121
}
120
122
function timeout ( ) {
121
- $scope . codeConfirm = null ;
122
123
$timeout . cancel ( $scope . timeout ) ;
123
- ( $scope . timeout = $timeout ( 5000 ) ) . then ( function ( ) {
124
- if ( ! $scope . codeConfirm ) $scope . item = null ;
124
+ ( $scope . timeout = $timeout ( 5000 ) ) . then ( ( ) => {
125
+ $scope . item = null ;
125
126
} ) ;
126
127
}
127
128
// Action implementations
128
129
function buyProduct ( user , product ) {
129
- return $http . post ( `/api/v1/purchases` , { user, product} )
130
- . then ( ( ) => {
131
- $scope . updatePurchases ( ) ;
132
- } ) ;
130
+ return $http . post ( `/api/v1/purchases` , { user, product } ) ;
133
131
} ;
134
132
function addCode ( target , code ) {
133
+ $timeout . cancel ( $scope . timeout ) ;
135
134
$scope . codeConfirm = code ;
136
135
return $mdDialog . show ( {
137
136
controller : ( ) => { } ,
@@ -142,4 +141,9 @@ angular.module('strecku.terminal', [
142
141
controllerAs : 'ctrl'
143
142
} ) . then ( ( ) => code ) ;
144
143
}
144
+
145
+ $scope . assignCode = ( code , user ) => {
146
+ $http . post ( `/api/v1/users/${ user . _id } /codes` , { code } ) ;
147
+ $scope . item = null ;
148
+ } ;
145
149
} ] ) ;
0 commit comments