957cb6d82033eecfc779aae3f92e25d4809f4a24
[portal.git] / ecomp-portal-FE-common / client / app / services / menus / menus.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 'use strict';
21
22 (function () {
23   class MenusService {
24     constructor($q, $log, $http, conf, uuid, utilsService) {
25         this.$q = $q;
26         this.$log = $log;
27         this.$http = $http;
28         this.conf = conf;
29         this.uuid = uuid;
30         this.utilsService = utilsService;
31     }
32
33     GetFunctionalMenuForUser() {
34       let deferred = this.$q.defer();
35       // this.$log.info('MenusService::GetFunctionalMenuForUser via REST API');
36       this.$http({
37         method: 'GET',
38         url: this.conf.api.functionalMenuForAuthUser,
39         cache: false,
40         headers: {
41           'X-ECOMP-RequestID':this.uuid.generate()
42         }
43       }).then( res => {
44               // If response comes back as a redirected HTML page which IS NOT a success
45               if (this.utilsService.isValidJSON(res)== false) {
46                   deferred.reject('MenusService::GetFunctionalMenuForUser Failed');
47               } else {
48                   // this.$log.info('MenusService::GetFunctionalMenuForUser success:');
49                   deferred.resolve(res.data);
50               }
51           })
52           .catch( status => {
53             this.$log.error('MenusService::rejection:' + status);
54             deferred.reject(status);
55           });
56
57       return deferred.promise;
58     }
59
60     getEcompPortalTitle () {
61         let deferred = this.$q.defer();
62         this.$http({
63                 method: 'GET',
64                 url: this.conf.api.ecompTitle,
65                 cache: false,
66                 headers: {
67                         'X-ECOMP-RequestID':this.uuid.generate()
68                 }
69         }).then( res => {
70                 if (res.data==null || !this.utilsService.isValidJSON(res.data)) {                       
71                         deferred.reject('MenusService::getEcompPortalTitle rest call failed');
72                 } else {
73                         if(res.data.status!='OK' && res.data.message!=null)
74                                 deferred.reject('MenusService::getEcompPortalTitle rest call failed ' + res.data.message);
75                         else
76                                 deferred.resolve(res.data);
77                 }
78         })
79         .catch( status => {
80                 this.$log.error('MenusService::getEcompPortalTitle rejection:' + status);
81                 deferred.reject(status);
82         });
83
84         return deferred.promise;
85     }
86
87     getFavoriteItems() {
88       let deferred = this.$q.defer();
89       // this.$log.info('MenusService::getFavoriteItems via REST API');
90         this.$http({
91             method: 'GET',
92             url: this.conf.api.getFavoriteItems +'?date='+new Date().getTime(),
93             cache: false,
94             headers: {
95                 'X-ECOMP-RequestID':this.uuid.generate()
96             }
97         }).then( res => {
98                 // If response comes back as a redirected HTML page which IS NOT a success
99             if (this.utilsService.isValidJSON(res)== false) {
100                     deferred.reject('MenusService::getFavoriteItems has no favorites');
101                 } else {
102                     // this.$log.info('MenusService::getFavoriteItems success:');
103                     deferred.resolve(res.data);
104                 }
105             })
106             .catch( status => {
107                 this.$log.error('MenusService::getFavoriteItems rejection:' + status);
108                 deferred.reject(status);
109             });
110
111         return deferred.promise;
112     }
113     
114     setFavoriteItem(menuId) {
115       let deferred = this.$q.defer();
116       // this.$log.info('menus-service.service::setFavoriteItem  via REST API' + menuId);
117       let url = this.conf.api.setFavoriteItem;
118       this.$http({
119           method: 'POST',
120           url: url,
121           cache: false,
122           headers: {
123               'X-ECOMP-RequestID':this.uuid.generate(),
124               'Content-Type': 'application/json'
125           },
126           data: menuId
127       }).then(res => {
128               // If response comes back as a redirected HTML page which IS NOT a success
129               if (this.utilsService.isValidJSON(res)== false) {
130                   deferred.reject('MenusService::setFavoriteItem Failed');
131               } else {
132                   // this.$log.info('MenusService::setFavoriteItem success:');
133                   deferred.resolve(res.data);
134               }
135           })
136           .catch(errRes => {
137               this.$log.error('MenusService::setFavoriteItem rejection:' + JSON.stringify(errRes));
138               deferred.reject(errRes);
139           });
140       return deferred.promise;
141     }
142     
143       removeFavoriteItem(menuId) {
144           let deferred = this.$q.defer();
145           // this.$log.info('menus-service.service::removeFavoriteItem  via REST API');
146           let url = this.conf.api.removeFavoriteItem.replace(':menuId', menuId);
147           this.$http({
148               method: 'DELETE',
149               url: url,
150               cache: false,
151               headers: {
152                   'X-ECOMP-RequestID':this.uuid.generate()
153               }
154           }).then(res => {
155               // If response comes back as a redirected HTML page which IS NOT a success
156               if (this.utilsService.isValidJSON(res)== false) {
157                   deferred.reject('MenusService::removeFavoriteItem Failed');
158               } else {
159                   // this.$log.info('MenusService::removeFavoriteItem success:');
160                   deferred.resolve(res.data);
161               }
162           })
163               .catch(errRes => {
164                   this.$log.error('MenusService::removeFavoriteItem rejection:' + status);
165                   deferred.reject(errRes);
166               });
167           return deferred.promise;
168       }
169
170     
171   }
172   MenusService.$inject = ['$q', '$log', '$http', 'conf', 'uuid4', 'utilsService'];
173   angular.module('ecompApp').service('menusService', MenusService)
174 })();