blob: 8f43c35d9b933534fa6a5aa754f554b669180147 [file] [log] [blame]
Matteo Scandolof0c32262016-03-11 13:47:27 -08001'use strict';
2
3angular.module('xos.truckroll', [
4 'ngResource',
5 'ngCookies',
Matteo Scandoloc105c1c2016-03-12 09:17:14 -08006 'ngAnimate',
Matteo Scandolof0c32262016-03-11 13:47:27 -08007 'ngLodash',
8 'ui.router',
9 'xos.helpers'
10])
11.config(($stateProvider) => {
12 $stateProvider
13 .state('user-list', {
14 url: '/',
15 template: '<truckroll></truckroll>'
16 });
17})
18.config(function($httpProvider){
19 $httpProvider.interceptors.push('NoHyperlinks');
20})
21.service('Subscribers', function($resource){
22 return $resource('/xos/subscribers/:id');
23})
24.service('Truckroll', function($resource){
25 return $resource('/xoslib/truckroll/:id');
26})
27.directive('truckroll', function(){
28 return {
29 restrict: 'E',
30 scope: {},
31 bindToController: true,
32 controllerAs: 'vm',
33 templateUrl: 'templates/truckroll.tpl.html',
34 controller: function($timeout, Subscribers, Truckroll){
35 Subscribers.query().$promise
36 .then((subscribers) => {
37 this.subscribers = subscribers;
38 });
39
40 this.loader = false;
41
42 this.runTest = () => {
43
44 // clean previous tests
45 delete this.truckroll.result;
Matteo Scandolo47342ba2016-03-11 14:41:14 -080046 delete this.truckroll.is_synced;
Matteo Scandolo31473172016-03-11 15:01:01 -080047 delete this.truckroll.result_code;
Matteo Scandoloe9bfa802016-03-11 16:22:29 -080048 delete this.truckroll.backend_status;
Matteo Scandolof0c32262016-03-11 13:47:27 -080049
50 const test = new Truckroll(this.truckroll);
51 this.loader = true;
52 test.$save()
53 .then((res) => {
54 this.waitForTest(res.id);
55 })
56 };
57
58 this.waitForTest = (id) => {
59 Truckroll.get({id: id}).$promise
60 .then((testResult) => {
Matteo Scandolo47342ba2016-03-11 14:41:14 -080061 // if error
Matteo Scandoloc105c1c2016-03-12 09:17:14 -080062 // or
Matteo Scandolo47342ba2016-03-11 14:41:14 -080063 // if is synced
Matteo Scandoloc105c1c2016-03-12 09:17:14 -080064 if(
65 testResult.backend_status.indexOf('2') >= 0 ||
66 (testResult.result_code && testResult.result_code.indexOf('2') >= 0) ||
67 testResult.is_synced
68 ){
Matteo Scandolof0c32262016-03-11 13:47:27 -080069 this.truckroll = angular.copy(testResult);
Matteo Scandolof0c32262016-03-11 13:47:27 -080070 this.loader = false;
Matteo Scandoloc105c1c2016-03-12 09:17:14 -080071 Truckroll.delete({id: id});
Matteo Scandolof0c32262016-03-11 13:47:27 -080072 }
Matteo Scandolo47342ba2016-03-11 14:41:14 -080073 // else keep polling
Matteo Scandolof0c32262016-03-11 13:47:27 -080074 else{
75 $timeout(() => {
76 this.waitForTest(id);
77 }, 2000)
78 }
79 })
80 };
81 }
82 };
83});