f947248dbb54da8f870b30e2a0120461cfa35aa8
[portal.git] / ecomp-portal-FE-common / client / app / services / menus / menus.service.js
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 'use strict';
39
40 (function () {
41   class MenusService {
42     constructor($q, $log, $http, conf, uuid, utilsService) {
43         this.$q = $q;
44         this.$log = $log;
45         this.$http = $http;
46         this.conf = conf;
47         this.uuid = uuid;
48         this.utilsService = utilsService;
49     }
50
51     GetFunctionalMenuForUser() {
52       let deferred = this.$q.defer();
53       // this.$log.info('MenusService::GetFunctionalMenuForUser via REST API');
54       this.$http({
55         method: 'GET',
56         url: this.conf.api.functionalMenuForAuthUser,
57         cache: false,
58         headers: {
59           'X-ECOMP-RequestID':this.uuid.generate()
60         }
61       }).then( res => {
62               // If response comes back as a redirected HTML page which IS NOT a success
63               if (this.utilsService.isValidJSON(res)== false) {
64                   deferred.reject('MenusService::GetFunctionalMenuForUser Failed');
65               } else {
66                   // this.$log.info('MenusService::GetFunctionalMenuForUser success:');
67                   deferred.resolve(res.data);
68               }
69           })
70           .catch( status => {
71             this.$log.error('MenusService::rejection:' + status);
72             deferred.reject(status);
73           });
74
75       return deferred.promise;
76     }
77
78     getEcompPortalTitle () {
79         let deferred = this.$q.defer();
80         this.$http({
81                 method: 'GET',
82                 url: this.conf.api.ecompTitle,
83                 cache: false,
84                 headers: {
85                         'X-ECOMP-RequestID':this.uuid.generate()
86                 }
87         }).then( res => {
88                 if (res.data==null || !this.utilsService.isValidJSON(res.data)) {                       
89                         deferred.reject('MenusService::getEcompPortalTitle rest call failed');
90                 } else {
91                         if(res.data.status!='OK' && res.data.message!=null)
92                                 deferred.reject('MenusService::getEcompPortalTitle rest call failed ' + res.data.message);
93                         else
94                                 deferred.resolve(res.data);
95                 }
96         })
97         .catch( status => {
98                 this.$log.error('MenusService::getEcompPortalTitle rejection:' + status);
99                 deferred.reject(status);
100         });
101
102         return deferred.promise;
103     }
104
105     getFavoriteItems() {
106       let deferred = this.$q.defer();
107       // this.$log.info('MenusService::getFavoriteItems via REST API');
108         this.$http({
109             method: 'GET',
110             url: this.conf.api.getFavoriteItems +'?date='+new Date().getTime(),
111             cache: false,
112             headers: {
113                 'X-ECOMP-RequestID':this.uuid.generate()
114             }
115         }).then( res => {
116                 // If response comes back as a redirected HTML page which IS NOT a success
117             if (this.utilsService.isValidJSON(res)== false) {
118                     deferred.reject('MenusService::getFavoriteItems has no favorites');
119                 } else {
120                     // this.$log.info('MenusService::getFavoriteItems success:');
121                     deferred.resolve(res.data);
122                 }
123             })
124             .catch( status => {
125                 this.$log.error('MenusService::getFavoriteItems rejection:' + status);
126                 deferred.reject(status);
127             });
128
129         return deferred.promise;
130     }
131     
132     setFavoriteItem(menuId) {
133       let deferred = this.$q.defer();
134       // this.$log.info('menus-service.service::setFavoriteItem  via REST API' + menuId);
135       let url = this.conf.api.setFavoriteItem;
136       this.$http({
137           method: 'POST',
138           url: url,
139           cache: false,
140           headers: {
141               'X-ECOMP-RequestID':this.uuid.generate(),
142               'Content-Type': 'application/json'
143           },
144           data: menuId
145       }).then(res => {
146               // If response comes back as a redirected HTML page which IS NOT a success
147               if (this.utilsService.isValidJSON(res)== false) {
148                   deferred.reject('MenusService::setFavoriteItem Failed');
149               } else {
150                   // this.$log.info('MenusService::setFavoriteItem success:');
151                   deferred.resolve(res.data);
152               }
153           })
154           .catch(errRes => {
155               this.$log.error('MenusService::setFavoriteItem rejection:' + JSON.stringify(errRes));
156               deferred.reject(errRes);
157           });
158       return deferred.promise;
159     }
160     
161       removeFavoriteItem(menuId) {
162           let deferred = this.$q.defer();
163           // this.$log.info('menus-service.service::removeFavoriteItem  via REST API');
164           let url = this.conf.api.removeFavoriteItem.replace(':menuId', menuId);
165           this.$http({
166               method: 'DELETE',
167               url: url,
168               cache: false,
169               headers: {
170                   'X-ECOMP-RequestID':this.uuid.generate()
171               }
172           }).then(res => {
173               // If response comes back as a redirected HTML page which IS NOT a success
174               if (this.utilsService.isValidJSON(res)== false) {
175                   deferred.reject('MenusService::removeFavoriteItem Failed');
176               } else {
177                   // this.$log.info('MenusService::removeFavoriteItem success:');
178                   deferred.resolve(res.data);
179               }
180           })
181               .catch(errRes => {
182                   this.$log.error('MenusService::removeFavoriteItem rejection:' + status);
183                   deferred.reject(errRes);
184               });
185           return deferred.promise;
186       }
187
188     
189   }
190   MenusService.$inject = ['$q', '$log', '$http', 'conf', 'uuid4', 'utilsService'];
191   angular.module('ecompApp').service('menusService', MenusService)
192 })();