[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-common / client / app / services / portal-admins / portal-admins.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 PortalAdminsService {
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         getPortalAdmins() {
34             let deferred = this.$q.defer();
35             this.$log.info('PortalAdminsService::get all portal admins list');
36             this.$http({
37                     url: this.conf.api.portalAdmins,
38                     method: 'GET',
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('PortalAdminsService::getPortalAdmins Failed');
47                     } else {
48                         deferred.resolve(res.data);
49                     }
50                 })
51                 .catch(status => {
52                     deferred.reject(status);
53                 });
54             return deferred.promise;
55         }
56
57         addPortalAdmin(userData) {
58             let deferred = this.$q.defer();
59             this.$log.info('PortalAdminsService::addPortalAdmin: ' + JSON.stringify(userData));
60             this.$http({
61                 url: this.conf.api.portalAdmin,
62                 method: 'POST',
63                 cache: false,
64                 headers: {
65                     'X-ECOMP-RequestID':this.uuid.generate()
66                 },
67                 data: userData
68             }).then(res => {
69                 // If response comes back as a redirected HTML page which IS NOT a success
70                 this.$log.debug('PortalAdminsService:: this.conf.api.portalAdmin: ' + JSON.stringify(res));
71                 if (this.utilsService.isValidJSON(res)== false) {
72                     deferred.reject('PortalAdminsService::addPortalAdmin Failed');
73                 } else {
74                     deferred.resolve(res.data);
75                 }
76             })
77                 .catch(errRes => {
78                     this.$log.debug('PortalAdminsService:: this.conf.api.portalAdmin: ' + JSON.stringify(errRes));
79                     deferred.reject(errRes);
80                 });
81             return deferred.promise;
82         }
83
84         removePortalAdmin(userId,orUserId) {
85             let deferred = this.$q.defer();
86             let userInfo = userId+"-"+orUserId;
87             let url = this.conf.api.portalAdmin + '/' + userInfo;
88             this.$log.info('PortalAdminsService:: remove Portal Admin');
89             this.$http({
90                 url: url,
91                 method: 'DELETE',
92                 cache: false,
93                 headers: {
94                     'X-ECOMP-RequestID':this.uuid.generate()
95                 }
96             }).then(res => {
97                 // If response comes back as a redirected HTML page which IS NOT a success
98                 if (this.utilsService.isValidJSON(res)== false) {
99                     deferred.reject('PortalAdminsService::removePortalAdmin Failed');
100                 } else {
101                     deferred.resolve(res.data);
102                 }
103             }).catch(errRes => {
104                 deferred.reject(errRes);
105             });
106
107             return deferred.promise;
108         }
109     }
110     PortalAdminsService.$inject = ['$q', '$log', '$http', 'conf', 'uuid4', 'utilsService'];
111     angular.module('ecompApp').service('portalAdminsService', PortalAdminsService)
112 })();