blob: 69c0c4f58248d287a65c94f83c1746ffc36cbf3c [file] [log] [blame]
Matteo Scandolod7552052016-03-11 13:47:27 -08001'use strict';
2
3angular.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 Scandolo30746462016-03-11 14:41:14 -080045 delete this.truckroll.is_synced;
Matteo Scandolod7552052016-03-11 13:47:27 -080046
47 const test = new Truckroll(this.truckroll);
48 this.loader = true;
49 test.$save()
50 .then((res) => {
51 this.waitForTest(res.id);
52 })
53 };
54
55 this.waitForTest = (id) => {
56 Truckroll.get({id: id}).$promise
57 .then((testResult) => {
Matteo Scandolo30746462016-03-11 14:41:14 -080058 // if error
59 if(testResult.backend_status.indexOf('2') >= 0 || (testResult.result_code && testResult.result_code.indexOf('2') >= 0)){
60 this.truckroll = angular.copy(testResult);
61 this.loader = false;
62 // not deleting failed test for debugging
63 }
64 // if is synced
65 else if(testResult.is_synced){
Matteo Scandolod7552052016-03-11 13:47:27 -080066 this.truckroll = angular.copy(testResult);
67 Truckroll.delete({id: id});
68 this.loader = false;
69 }
Matteo Scandolo30746462016-03-11 14:41:14 -080070 // else keep polling
Matteo Scandolod7552052016-03-11 13:47:27 -080071 else{
72 $timeout(() => {
73 this.waitForTest(id);
74 }, 2000)
75 }
76 })
77 };
78 }
79 };
80});