blob: 514126e4ebdd7e6669f9726e36ffd7af993361ff [file] [log] [blame]
Matteo Scandolof0c32262016-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 Scandolo47342ba2016-03-11 14:41:14 -080045 delete this.truckroll.is_synced;
Matteo Scandolo31473172016-03-11 15:01:01 -080046 delete this.truckroll.result_code;
Matteo Scandoloe9bfa802016-03-11 16:22:29 -080047 delete this.truckroll.backend_status;
Matteo Scandolof0c32262016-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 Scandolo47342ba2016-03-11 14:41:14 -080060 // if error
61 if(testResult.backend_status.indexOf('2') >= 0 || (testResult.result_code && testResult.result_code.indexOf('2') >= 0)){
62 this.truckroll = angular.copy(testResult);
63 this.loader = false;
64 // not deleting failed test for debugging
65 }
66 // if is synced
67 else if(testResult.is_synced){
Matteo Scandolof0c32262016-03-11 13:47:27 -080068 this.truckroll = angular.copy(testResult);
69 Truckroll.delete({id: id});
70 this.loader = false;
71 }
Matteo Scandolo47342ba2016-03-11 14:41:14 -080072 // else keep polling
Matteo Scandolof0c32262016-03-11 13:47:27 -080073 else{
74 $timeout(() => {
75 this.waitForTest(id);
76 }, 2000)
77 }
78 })
79 };
80 }
81 };
82});