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