blob: 6d72b5b9fa9907f4234f16fcef29fa8e0b112d98 [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 Scandolocc9930e2016-03-11 15:01:01 -080046 delete this.truckroll.result_code;
Matteo Scandolod7552052016-03-11 13:47:27 -080047
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 Scandolo30746462016-03-11 14:41:14 -080059 // 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 Scandolod7552052016-03-11 13:47:27 -080067 this.truckroll = angular.copy(testResult);
68 Truckroll.delete({id: id});
69 this.loader = false;
70 }
Matteo Scandolo30746462016-03-11 14:41:14 -080071 // else keep polling
Matteo Scandolod7552052016-03-11 13:47:27 -080072 else{
73 $timeout(() => {
74 this.waitForTest(id);
75 }, 2000)
76 }
77 })
78 };
79 }
80 };
81});