Matteo Scandolo | f0c3226 | 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 | 47342ba | 2016-03-11 14:41:14 -0800 | [diff] [blame] | 45 | delete this.truckroll.is_synced; |
Matteo Scandolo | 3147317 | 2016-03-11 15:01:01 -0800 | [diff] [blame] | 46 | delete this.truckroll.result_code; |
Matteo Scandolo | f0c3226 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 47 | |
| 48 | const test = new Truckroll(this.truckroll); |
| 49 | this.loader = true; |
| 50 | test.$save() |
| 51 | .then((res) => { |
| 52 | this.waitForTest(res.id); |
| 53 | }) |
| 54 | }; |
| 55 | |
| 56 | this.waitForTest = (id) => { |
| 57 | Truckroll.get({id: id}).$promise |
| 58 | .then((testResult) => { |
Matteo Scandolo | 47342ba | 2016-03-11 14:41:14 -0800 | [diff] [blame] | 59 | // if error |
| 60 | if(testResult.backend_status.indexOf('2') >= 0 || (testResult.result_code && testResult.result_code.indexOf('2') >= 0)){ |
| 61 | this.truckroll = angular.copy(testResult); |
| 62 | this.loader = false; |
| 63 | // not deleting failed test for debugging |
| 64 | } |
| 65 | // if is synced |
| 66 | else if(testResult.is_synced){ |
Matteo Scandolo | f0c3226 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 67 | this.truckroll = angular.copy(testResult); |
| 68 | Truckroll.delete({id: id}); |
| 69 | this.loader = false; |
| 70 | } |
Matteo Scandolo | 47342ba | 2016-03-11 14:41:14 -0800 | [diff] [blame] | 71 | // else keep polling |
Matteo Scandolo | f0c3226 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 72 | else{ |
| 73 | $timeout(() => { |
| 74 | this.waitForTest(id); |
| 75 | }, 2000) |
| 76 | } |
| 77 | }) |
| 78 | }; |
| 79 | } |
| 80 | }; |
| 81 | }); |