601c9ff989d18f263a0bb1eee102f3a5bc5c66a4
[portal.git] / ecomp-portal-FE-common / client / app / services / widgets / widgets.service.js
1 /*-
2  * ================================================================================
3  * ECOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 /**
21  * Created by doritrieur on 12/3/15.
22  */
23 'use strict';
24
25 (function () {
26     class WidgetsService {
27         constructor($q, $log, $http, conf, uuid, utilsService) {
28             this.$q = $q;
29             this.$log = $log;
30             this.$http = $http;
31             this.conf = conf;
32             this.uuid = uuid;
33             this.utilsService = utilsService;
34         }
35
36         getUserWidgets() {
37             let deferred = this.$q.defer();
38             this.$log.info('WidgetsService::getUserWidgets');
39             this.$http({
40                 method: 'GET',
41                 url: this.conf.api.widgets,
42                 cache: false,
43                 headers: {
44                     'X-Widgets-Type': 'all',
45                     'X-ECOMP-RequestID':this.uuid.generate()
46                 }
47             }).then(res => {
48                 // If response comes back as a redirected HTML page which IS NOT a success
49                 if (this.utilsService.isValidJSON(res) == false) {
50                         var msg = 'WidgetsService::getUserWidgets Failed';
51                         this.$log.error(msg);
52                         deferred.reject(msg);
53                 } else {
54                         // this.$log.info('WidgetsService::getUserWidgets Succeeded');
55                         deferred.resolve(res.data);
56                 }
57             })
58             .catch(status => {
59                 deferred.reject(status);
60             });
61             return deferred.promise;
62         }
63
64         getManagedWidgets() {
65             let deferred = this.$q.defer();
66             this.$log.info('WidgetsService::getManagedWidgets');
67             this.$http({
68                 method: 'GET',
69                 url: this.conf.api.widgets,
70                 cache: false,
71                 headers: {
72                     'X-Widgets-Type': 'managed',
73                     'X-ECOMP-RequestID':this.uuid.generate()
74                 }
75             }).then(res => {
76                     // If response comes back as a redirected HTML page which IS NOT a success
77                     if (this.utilsService.isValidJSON(res)== false) {
78                         deferred.reject('WidgetsService::getManagedWidgets Failed');
79                     } else if(Object.keys(res).length == 0 && this.utilsService.isValidJSON(res)) {
80                         deferred.resolve(null);
81                     } else {
82                         this.$log.info('WidgetsService::getManagedWidgets Succeeded');
83                         deferred.resolve(res.data);
84                     }
85                 })
86                 .catch(status => {
87                     deferred.reject(status);
88                 });
89             return deferred.promise;
90         }
91
92         createWidget(widgetData) {
93             let deferred = this.$q.defer();
94             this.$log.info('WidgetsService::createWidget');
95             this.$http({
96                 method: 'POST',
97                 url: this.conf.api.widgets,
98                 cache: false,
99                 data: widgetData,
100                 headers: {
101                     'X-ECOMP-RequestID':this.uuid.generate()
102                 }
103             }).then(res => {
104                     // If response comes back as a redirected HTML page which IS NOT a success
105                     if (this.utilsService.isValidJSON(res)== false) {
106                         deferred.reject('WidgetsService::createWidget Failed');
107                     } else {
108                         this.$log.info('WidgetsService::createWidget Succeeded');
109                         deferred.resolve(res.data);
110                     }
111                 })
112                 .catch(errRes => {
113                     deferred.reject(errRes);
114                 });
115             return deferred.promise;
116         }
117
118         updateWidget(widgetId, widgetData) {
119             let deferred = this.$q.defer();
120             this.$log.info('WidgetsService::updateWidget');
121             let url = this.conf.api.widgets + '/' + widgetId;
122             this.$http({
123                 method: 'PUT',
124                 url: url,
125                 cache: false,
126                 data: widgetData,
127                 headers: {
128                     'X-ECOMP-RequestID':this.uuid.generate()
129                 }
130             }).then(res => {
131                     // If response comes back as a redirected HTML page which IS NOT a success
132                     if (this.utilsService.isValidJSON(res)== false) {
133                         deferred.reject('WidgetsService::updateWidget Failed');
134                     } else {
135                         this.$log.info('WidgetsService::updateWidget Succeeded');
136                         deferred.resolve(res.data);
137                     }
138                 })
139                 .catch(errRes => {
140                     deferred.reject(errRes);
141                 });
142             return deferred.promise;
143         }
144
145         deleteWidget(widgetId) {
146             let deferred = this.$q.defer();
147             this.$log.info('WidgetsService::deleteWidget');
148             let url = this.conf.api.widgets + '/' + widgetId;
149             this.$http({
150                 method: 'DELETE',
151                 url: url,
152                 cache: false,
153                 headers: {
154                     'X-ECOMP-RequestID':this.uuid.generate()
155                 }
156             }).then(res => {
157                     // If response comes back as a redirected HTML page which IS NOT a success
158                     if (this.utilsService.isValidJSON(res)== false) {
159                         deferred.reject('WidgetsService::deleteWidget Failed');
160                     } else {
161                         this.$log.info('WidgetsService::deleteWidget Succeeded');
162                         deferred.resolve(res.data);
163                     }
164                 })
165                 .catch(status => {
166                     deferred.reject(status);
167                 });
168             return deferred.promise;
169         }
170
171         checkIfWidgetUrlExists(urlToValidate) {
172             let deferred = this.$q.defer();
173             this.$log.info('WidgetsService::checkIfWidgetUrlExists:' + urlToValidate);
174             let reqBody = {
175                 url: urlToValidate
176             };
177             this.$http({
178                 method: 'POST',
179                 url: this.conf.api.widgetsValidation,
180                 cache: false,
181                 data: reqBody,
182                 headers: {
183                     'X-ECOMP-RequestID':this.uuid.generate()
184                 }
185             }).then(() => {
186                 //if exists return true
187                     deferred.resolve(true);
188                 })
189                 .catch(response => {
190                     if (this.utilsService.isValidJSON(res)== false) {
191                         deferred.reject('WidgetsService::checkIfWidgetUrlExists Failed');
192                     } else {
193                         if (response.data.status === 404) {
194                             deferred.resolve(false);
195                         } else {
196                             deferred.reject(response.data);
197                         }
198                     }
199                 });
200             return deferred.promise;
201         }
202
203     }
204     WidgetsService.$inject = ['$q', '$log', '$http', 'conf','uuid4', 'utilsService'];
205     angular.module('ecompApp').service('widgetsService', WidgetsService)
206 })();