Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 19 | 'use strict'; |
| 20 | |
| 21 | angular.module('xos.truckroll', [ |
| 22 | 'ngResource', |
| 23 | 'ngCookies', |
Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 24 | 'ui.router', |
| 25 | 'xos.helpers' |
| 26 | ]) |
| 27 | .config(($stateProvider) => { |
| 28 | $stateProvider |
| 29 | .state('user-list', { |
| 30 | url: '/', |
| 31 | template: '<truckroll></truckroll>' |
| 32 | }); |
| 33 | }) |
| 34 | .config(function($httpProvider){ |
| 35 | $httpProvider.interceptors.push('NoHyperlinks'); |
| 36 | }) |
Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 37 | .directive('truckroll', function(){ |
| 38 | return { |
| 39 | restrict: 'E', |
| 40 | scope: {}, |
| 41 | bindToController: true, |
| 42 | controllerAs: 'vm', |
| 43 | templateUrl: 'templates/truckroll.tpl.html', |
Matteo Scandolo | 280dcd3 | 2016-05-16 09:59:38 -0700 | [diff] [blame] | 44 | controller: function($timeout, $log, Subscribers, Truckroll){ |
Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 45 | Subscribers.query().$promise |
| 46 | .then((subscribers) => { |
| 47 | this.subscribers = subscribers; |
| 48 | }); |
| 49 | |
| 50 | this.loader = false; |
| 51 | |
| 52 | this.runTest = () => { |
| 53 | |
| 54 | // clean previous tests |
Matteo Scandolo | 8c518c6 | 2016-08-03 13:31:13 -0700 | [diff] [blame] | 55 | delete this.truckroll.id; |
Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 56 | delete this.truckroll.result; |
Matteo Scandolo | 3074646 | 2016-03-11 14:41:14 -0800 | [diff] [blame] | 57 | delete this.truckroll.is_synced; |
Matteo Scandolo | cc9930e | 2016-03-11 15:01:01 -0800 | [diff] [blame] | 58 | delete this.truckroll.result_code; |
Matteo Scandolo | bc38967 | 2016-03-11 16:22:29 -0800 | [diff] [blame] | 59 | delete this.truckroll.backend_status; |
Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 60 | |
| 61 | const test = new Truckroll(this.truckroll); |
| 62 | this.loader = true; |
| 63 | test.$save() |
| 64 | .then((res) => { |
| 65 | this.waitForTest(res.id); |
| 66 | }) |
| 67 | }; |
| 68 | |
| 69 | this.waitForTest = (id) => { |
Matteo Scandolo | 8c518c6 | 2016-08-03 13:31:13 -0700 | [diff] [blame] | 70 | |
Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 71 | Truckroll.get({id: id}).$promise |
| 72 | .then((testResult) => { |
Matteo Scandolo | 3074646 | 2016-03-11 14:41:14 -0800 | [diff] [blame] | 73 | // if error |
Matteo Scandolo | 6970a81 | 2016-03-12 09:17:14 -0800 | [diff] [blame] | 74 | // or |
Matteo Scandolo | 3074646 | 2016-03-11 14:41:14 -0800 | [diff] [blame] | 75 | // if is synced |
Matteo Scandolo | 6970a81 | 2016-03-12 09:17:14 -0800 | [diff] [blame] | 76 | if( |
| 77 | testResult.backend_status.indexOf('2') >= 0 || |
| 78 | (testResult.result_code && testResult.result_code.indexOf('2') >= 0) || |
| 79 | testResult.is_synced |
| 80 | ){ |
Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 81 | this.truckroll = angular.copy(testResult); |
Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 82 | this.loader = false; |
Matteo Scandolo | 6970a81 | 2016-03-12 09:17:14 -0800 | [diff] [blame] | 83 | Truckroll.delete({id: id}); |
Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 84 | } |
Matteo Scandolo | 3074646 | 2016-03-11 14:41:14 -0800 | [diff] [blame] | 85 | // else keep polling |
Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 86 | else{ |
| 87 | $timeout(() => { |
| 88 | this.waitForTest(id); |
| 89 | }, 2000) |
| 90 | } |
| 91 | }) |
| 92 | }; |
| 93 | } |
| 94 | }; |
| 95 | }); |