Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | angular.module('xos.truckroll', [ |
| 4 | 'ngResource', |
| 5 | 'ngCookies', |
| 6 | 'ngLodash', |
| 7 | 'ui.router', |
| 8 | 'xos.helpers' |
| 9 | ]) |
| 10 | .config(($stateProvider) => { |
| 11 | $stateProvider |
| 12 | .state('user-list', { |
| 13 | url: '/', |
| 14 | template: '<truckroll></truckroll>' |
| 15 | }); |
| 16 | }) |
| 17 | .config(function($httpProvider){ |
| 18 | $httpProvider.interceptors.push('NoHyperlinks'); |
| 19 | }) |
| 20 | .service('Subscribers', function($resource){ |
| 21 | return $resource('/xos/subscribers/:id'); |
| 22 | }) |
| 23 | .service('Truckroll', function($resource){ |
| 24 | return $resource('/xoslib/truckroll/:id'); |
| 25 | }) |
| 26 | .directive('truckroll', function(){ |
| 27 | return { |
| 28 | restrict: 'E', |
| 29 | scope: {}, |
| 30 | bindToController: true, |
| 31 | controllerAs: 'vm', |
| 32 | templateUrl: 'templates/truckroll.tpl.html', |
| 33 | controller: function($timeout, Subscribers, Truckroll){ |
| 34 | Subscribers.query().$promise |
| 35 | .then((subscribers) => { |
| 36 | this.subscribers = subscribers; |
| 37 | }); |
| 38 | |
| 39 | this.loader = false; |
| 40 | |
| 41 | this.runTest = () => { |
| 42 | |
| 43 | // clean previous tests |
| 44 | delete this.truckroll.result; |
Matteo Scandolo | 3074646 | 2016-03-11 14:41:14 -0800 | [diff] [blame] | 45 | delete this.truckroll.is_synced; |
Matteo Scandolo | cc9930e | 2016-03-11 15:01:01 -0800 | [diff] [blame] | 46 | delete this.truckroll.result_code; |
Matteo Scandolo | bc38967 | 2016-03-11 16:22:29 -0800 | [diff] [blame] | 47 | delete this.truckroll.backend_status; |
Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 48 | |
| 49 | const test = new Truckroll(this.truckroll); |
| 50 | this.loader = true; |
| 51 | test.$save() |
| 52 | .then((res) => { |
| 53 | this.waitForTest(res.id); |
| 54 | }) |
| 55 | }; |
| 56 | |
| 57 | this.waitForTest = (id) => { |
| 58 | Truckroll.get({id: id}).$promise |
| 59 | .then((testResult) => { |
Matteo Scandolo | 3074646 | 2016-03-11 14:41:14 -0800 | [diff] [blame] | 60 | // if error |
Matteo Scandolo | 6970a81 | 2016-03-12 09:17:14 -0800 | [diff] [blame] | 61 | // or |
Matteo Scandolo | 3074646 | 2016-03-11 14:41:14 -0800 | [diff] [blame] | 62 | // if is synced |
Matteo Scandolo | 6970a81 | 2016-03-12 09:17:14 -0800 | [diff] [blame] | 63 | if( |
| 64 | testResult.backend_status.indexOf('2') >= 0 || |
| 65 | (testResult.result_code && testResult.result_code.indexOf('2') >= 0) || |
| 66 | testResult.is_synced |
| 67 | ){ |
Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 68 | this.truckroll = angular.copy(testResult); |
Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 69 | this.loader = false; |
Matteo Scandolo | 6970a81 | 2016-03-12 09:17:14 -0800 | [diff] [blame] | 70 | Truckroll.delete({id: id}); |
Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 71 | } |
Matteo Scandolo | 3074646 | 2016-03-11 14:41:14 -0800 | [diff] [blame] | 72 | // else keep polling |
Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 73 | else{ |
| 74 | $timeout(() => { |
| 75 | this.waitForTest(id); |
| 76 | }, 2000) |
| 77 | } |
| 78 | }) |
| 79 | }; |
| 80 | } |
| 81 | }; |
| 82 | }); |