summaryrefslogtreecommitdiffstats
path: root/ecomp-portal-FE/client/app/services/widgets/widgets.service.js
blob: fd749c46f452a87cd6d35a205ba5a094a5fb62cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
 * Created by doritrieur on 12/3/15.
 */
'use strict';

(function () {
    class WidgetsService {
        constructor($q, $log, $http, conf,uuid) {
            this.$q = $q;
            this.$log = $log;
            this.$http = $http;
            this.conf = conf;
            this.uuid = uuid;
        }

        getUserWidgets() {
            let deferred = this.$q.defer();
            this.$log.info('WidgetsService::getUserWidgets');
            this.$http({
                method: "GET",
                url: this.conf.api.widgets,
                cache: false,
                headers: {
                    'X-Widgets-Type': 'all',
                    'X-ECOMP-RequestID':this.uuid.generate()
                }
            }).then(res => {
                    // If response comes back as a redirected HTML page which IS NOT a success
                    if (res == null || res.data==null) {
                        deferred.reject("WidgetsService::getUserWidgets Failed");
                    } else {
                        this.$log.info('WidgetsService::getUserWidgets Succeeded');
                        deferred.resolve(res.data);
                    }
                })
                .catch(status => {
                    deferred.reject(status);
                });
            return deferred.promise;
        }

        getManagedWidgets() {
            let deferred = this.$q.defer();
            this.$log.info('WidgetsService::getManagedWidgets');
            this.$http({
                method: "GET",
                url: this.conf.api.widgets,
                cache: false,
                headers: {
                    'X-Widgets-Type': 'managed',
                    'X-ECOMP-RequestID':this.uuid.generate()
                }
            }).then(res => {
                    // If response comes back as a redirected HTML page which IS NOT a success
                    if (Object.keys(res.data).length == 0) {
                        deferred.reject("WidgetsService::getManagedWidgets Failed");
                    } else {
                        this.$log.info('WidgetsService::getManagedWidgets Succeeded');
                        deferred.resolve(res.data);
                    }
                })
                .catch(status => {
                    deferred.reject(status);
                });
            return deferred.promise;
        }

        createWidget(widgetData) {
            let deferred = this.$q.defer();
            this.$log.info('WidgetsService::createWidget');
            this.$http({
                method: "POST",
                url: this.conf.api.widgets,
                cache: false,
                data: widgetData,
                headers: {
                    'X-ECOMP-RequestID':this.uuid.generate()
                }
            }).then(res => {
                    // If response comes back as a redirected HTML page which IS NOT a success
                    if (Object.keys(res.data).length == 0) {
                        deferred.reject("WidgetsService::createWidget Failed");
                    } else {
                        this.$log.info('WidgetsService::createWidget Succeeded');
                        deferred.resolve(res.data);
                    }
                })
                .catch(errRes => {
                    deferred.reject(errRes);
                });
            return deferred.promise;
        }

        updateWidget(widgetId, widgetData) {
            let deferred = this.$q.defer();
            this.$log.info('WidgetsService::updateWidget');
            let url = this.conf.api.widgets + '/' + widgetId;
            this.$http({
                method: "PUT",
                url: url,
                cache: false,
                data: widgetData,
                headers: {
                    'X-ECOMP-RequestID':this.uuid.generate()
                }
            }).then(res => {
                    // If response comes back as a redirected HTML page which IS NOT a success
                    if (Object.keys(res.data).length == 0) {
                        deferred.reject("WidgetsService::updateWidget Failed");
                    } else {
                        this.$log.info('WidgetsService::updateWidget Succeeded');
                        deferred.resolve(res.data);
                    }
                })
                .catch(errRes => {
                    deferred.reject(errRes);
                });
            return deferred.promise;
        }

        deleteWidget(widgetId) {
            let deferred = this.$q.defer();
            this.$log.info('WidgetsService::deleteWidget');
            let url = this.conf.api.widgets + '/' + widgetId;
            this.$http({
                method: "DELETE",
                url: url,
                cache: false,
                headers: {
                    'X-ECOMP-RequestID':this.uuid.generate()
                }
            }).then(res => {
                    // If response comes back as a redirected HTML page which IS NOT a success
                    if (Object.keys(res.data).length == 0) {
                        deferred.reject("WidgetsService::deleteWidget Failed");
                    } else {
                        this.$log.info('WidgetsService::deleteWidget Succeeded');
                        deferred.resolve(res.data);
                    }
                })
                .catch(status => {
                    deferred.reject(status);
                });
            return deferred.promise;
        }

        checkIfWidgetUrlExists(urlToValidate) {
            let deferred = this.$q.defer();
            this.$log.info('WidgetsService::checkIfWidgetUrlExists:' + urlToValidate);
            let reqBody = {
                url: urlToValidate
            };
            this.$http({
                method: "POST",
                url: this.conf.api.widgetsValidation,
                cache: false,
                data: reqBody,
                headers: {
                    'X-ECOMP-RequestID':this.uuid.generate()
                }
            }).then(() => {
                //if exists return true
                    deferred.resolve(true);
                })
                .catch(response => {
                    if(response.data.status === 404){
                        deferred.resolve(false);
                    }else{
                        deferred.reject(response.data);
                    }
                });
            return deferred.promise;
        }

    }
    WidgetsService.$inject = ['$q', '$log', '$http', 'conf','uuid4'];
    angular.module('ecompApp').service('widgetsService', WidgetsService)
})();