blob: 6aee06d8ef31d60d11f085adf569106c8658ceb8 [file] [log] [blame]
Matteo Scandolod7552052016-03-11 13:47:27 -08001'use strict';
2
3angular.module('xos.truckroll', [
4 'ngResource',
5 'ngCookies',
Matteo Scandolod7552052016-03-11 13:47:27 -08006 'ui.router',
7 'xos.helpers'
8])
9.config(($stateProvider) => {
10 $stateProvider
11 .state('user-list', {
12 url: '/',
13 template: '<truckroll></truckroll>'
14 });
15})
16.config(function($httpProvider){
17 $httpProvider.interceptors.push('NoHyperlinks');
18})
Matteo Scandolod7552052016-03-11 13:47:27 -080019.directive('truckroll', function(){
20 return {
21 restrict: 'E',
22 scope: {},
23 bindToController: true,
24 controllerAs: 'vm',
25 templateUrl: 'templates/truckroll.tpl.html',
Matteo Scandolo280dcd32016-05-16 09:59:38 -070026 controller: function($timeout, $log, Subscribers, Truckroll){
Matteo Scandolod7552052016-03-11 13:47:27 -080027 Subscribers.query().$promise
28 .then((subscribers) => {
29 this.subscribers = subscribers;
30 });
31
32 this.loader = false;
33
34 this.runTest = () => {
35
36 // clean previous tests
Matteo Scandolo8c518c62016-08-03 13:31:13 -070037 delete this.truckroll.id;
Matteo Scandolod7552052016-03-11 13:47:27 -080038 delete this.truckroll.result;
Matteo Scandolo30746462016-03-11 14:41:14 -080039 delete this.truckroll.is_synced;
Matteo Scandolocc9930e2016-03-11 15:01:01 -080040 delete this.truckroll.result_code;
Matteo Scandolobc389672016-03-11 16:22:29 -080041 delete this.truckroll.backend_status;
Matteo Scandolod7552052016-03-11 13:47:27 -080042
43 const test = new Truckroll(this.truckroll);
44 this.loader = true;
45 test.$save()
46 .then((res) => {
47 this.waitForTest(res.id);
48 })
49 };
50
51 this.waitForTest = (id) => {
Matteo Scandolo8c518c62016-08-03 13:31:13 -070052
Matteo Scandolod7552052016-03-11 13:47:27 -080053 Truckroll.get({id: id}).$promise
54 .then((testResult) => {
Matteo Scandolo30746462016-03-11 14:41:14 -080055 // if error
Matteo Scandolo6970a812016-03-12 09:17:14 -080056 // or
Matteo Scandolo30746462016-03-11 14:41:14 -080057 // if is synced
Matteo Scandolo6970a812016-03-12 09:17:14 -080058 if(
59 testResult.backend_status.indexOf('2') >= 0 ||
60 (testResult.result_code && testResult.result_code.indexOf('2') >= 0) ||
61 testResult.is_synced
62 ){
Matteo Scandolod7552052016-03-11 13:47:27 -080063 this.truckroll = angular.copy(testResult);
Matteo Scandolod7552052016-03-11 13:47:27 -080064 this.loader = false;
Matteo Scandolo6970a812016-03-12 09:17:14 -080065 Truckroll.delete({id: id});
Matteo Scandolod7552052016-03-11 13:47:27 -080066 }
Matteo Scandolo30746462016-03-11 14:41:14 -080067 // else keep polling
Matteo Scandolod7552052016-03-11 13:47:27 -080068 else{
69 $timeout(() => {
70 this.waitForTest(id);
71 }, 2000)
72 }
73 })
74 };
75 }
76 };
77});