blob: 1cf51b9e813e226bf9cc84431d69a0e4fafa3ad0 [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 Scandolobc389672016-03-11 16:22:29 -080047 delete this.truckroll.backend_status;
Matteo Scandolod7552052016-03-11 13:47:27 -080048
49 const test = new Truckroll(this.truckroll);
50 this.loader = true;
51 test.$save()
52 .then((res) => {
53 this.waitForTest(res.id);
54 })
55 };
56
57 this.waitForTest = (id) => {
58 Truckroll.get({id: id}).$promise
59 .then((testResult) => {
Matteo Scandolo30746462016-03-11 14:41:14 -080060 // if error
Matteo Scandolo6970a812016-03-12 09:17:14 -080061 // or
Matteo Scandolo30746462016-03-11 14:41:14 -080062 // if is synced
Matteo Scandolo6970a812016-03-12 09:17:14 -080063 if(
64 testResult.backend_status.indexOf('2') >= 0 ||
65 (testResult.result_code && testResult.result_code.indexOf('2') >= 0) ||
66 testResult.is_synced
67 ){
Matteo Scandolod7552052016-03-11 13:47:27 -080068 this.truckroll = angular.copy(testResult);
Matteo Scandolod7552052016-03-11 13:47:27 -080069 this.loader = false;
Matteo Scandolo6970a812016-03-12 09:17:14 -080070 Truckroll.delete({id: id});
Matteo Scandolod7552052016-03-11 13:47:27 -080071 }
Matteo Scandolo30746462016-03-11 14:41:14 -080072 // else keep polling
Matteo Scandolod7552052016-03-11 13:47:27 -080073 else{
74 $timeout(() => {
75 this.waitForTest(id);
76 }, 2000)
77 }
78 })
79 };
80 }
81 };
82});