blob: 1cf51b9e813e226bf9cc84431d69a0e4fafa3ad0 [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
Matteo Scandoloc105c1c2016-03-12 09:17:14 -080061 // or
Matteo Scandolo47342ba2016-03-11 14:41:14 -080062 // if is synced
Matteo Scandoloc105c1c2016-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 Scandolof0c32262016-03-11 13:47:27 -080068 this.truckroll = angular.copy(testResult);
Matteo Scandolof0c32262016-03-11 13:47:27 -080069 this.loader = false;
Matteo Scandoloc105c1c2016-03-12 09:17:14 -080070 Truckroll.delete({id: id});
Matteo Scandolof0c32262016-03-11 13:47:27 -080071 }
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});