Michael Lando | dd60339 | 2017-07-12 00:54:52 +0300 | [diff] [blame] | 1 | /*- |
| 2 | * ============LICENSE_START======================================================= |
| 3 | * SDC |
| 4 | * ================================================================================ |
| 5 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
| 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 | * ============LICENSE_END========================================================= |
| 19 | */ |
| 20 | |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 21 | 'use strict'; |
| 22 | import {IUserProperties} from "../models/user"; |
| 23 | import {ICookie, IAppConfigurtaion} from "../models/app-config"; |
| 24 | import {CookieService} from "./cookie-service"; |
| 25 | |
| 26 | // Define an interface of the object you want to use, providing it's properties |
| 27 | export interface IUserResource extends IUserProperties,ng.resource.IResource<IUserResource> { |
| 28 | |
| 29 | } |
| 30 | |
| 31 | // Define your resource, adding the signature of the custom actions |
| 32 | export interface IUserResourceClass extends ng.resource.IResourceClass<IUserResource> { |
| 33 | authorize():IUserResource; |
| 34 | getLoggedinUser():IUserResource; |
| 35 | setLoggedinUser(user:IUserResource):void; |
| 36 | getAllUsers(success?:Function, error?:Function):Array<IUserResource>; |
| 37 | createUser(IResourceResource, success?:Function, error?:Function):void; |
| 38 | editUserRole(IResourceResource, success?:Function, error?:Function):void; |
| 39 | deleteUser(IResourceResource, success?:Function, error?:Function):void; |
| 40 | } |
| 41 | |
| 42 | export class UserResourceService { |
| 43 | |
| 44 | public static getResource = ($resource:ng.resource.IResourceService, |
| 45 | sdcConfig:IAppConfigurtaion, |
| 46 | cookieService:CookieService):IUserResourceClass => { |
| 47 | |
| 48 | let url:string = sdcConfig.api.root + sdcConfig.api.GET_user; |
| 49 | let authorizeUrl:string = sdcConfig.api.root + sdcConfig.api.GET_user_authorize; |
| 50 | let authorizeActionHeaders:any = {}; |
| 51 | let cookie:ICookie = sdcConfig.cookie; |
| 52 | authorizeActionHeaders[cookie.userFirstName] = cookieService.getFirstName(); |
| 53 | authorizeActionHeaders[cookie.userLastName] = cookieService.getLastName(); |
| 54 | authorizeActionHeaders[cookie.userEmail] = cookieService.getEmail(); |
| 55 | authorizeActionHeaders[cookie.userIdSuffix] = cookieService.getUserId(); |
| 56 | |
| 57 | // Define your custom actions here as IActionDescriptor |
| 58 | let authorizeAction:ng.resource.IActionDescriptor = { |
| 59 | method: 'GET', |
| 60 | isArray: false, |
| 61 | url: authorizeUrl, |
| 62 | headers: authorizeActionHeaders |
| 63 | }; |
| 64 | |
| 65 | let getAllUsers:ng.resource.IActionDescriptor = { |
| 66 | method: 'GET', |
| 67 | isArray: true, |
| 68 | url: sdcConfig.api.root + sdcConfig.api.GET_all_users |
| 69 | }; |
| 70 | |
| 71 | let editUserRole:ng.resource.IActionDescriptor = { |
| 72 | method: 'POST', |
| 73 | isArray: false, |
| 74 | url: sdcConfig.api.root + sdcConfig.api.POST_edit_user_role, |
| 75 | transformRequest: (data, headers)=> { |
| 76 | data.payloadData = undefined; |
| 77 | data.payloadName = undefined; |
| 78 | return JSON.stringify(data); |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | let deleteUser:ng.resource.IActionDescriptor = { |
| 83 | method: 'DELETE', |
| 84 | isArray: false, |
| 85 | url: sdcConfig.api.root + sdcConfig.api.DELETE_delete_user |
| 86 | }; |
| 87 | |
| 88 | let createUser:ng.resource.IActionDescriptor = { |
| 89 | method: 'POST', |
| 90 | isArray: false, |
| 91 | url: sdcConfig.api.root + sdcConfig.api.POST_create_user, |
| 92 | transformRequest: (data, headers)=> { |
| 93 | data.payloadData = undefined; |
| 94 | data.payloadName = undefined; |
| 95 | return JSON.stringify(data); |
| 96 | } |
| 97 | }; |
| 98 | let userResource:IUserResourceClass = <IUserResourceClass>$resource( |
| 99 | url, |
| 100 | {id: '@id'}, |
| 101 | { |
| 102 | authorize: authorizeAction, |
| 103 | getAllUsers: getAllUsers, |
| 104 | createUser: createUser, |
| 105 | editUserRole: editUserRole, |
| 106 | deleteUser: deleteUser |
| 107 | } |
| 108 | ); |
| 109 | |
| 110 | let _loggedinUser:IUserResource; |
| 111 | |
| 112 | userResource.getLoggedinUser = () => { |
| 113 | return _loggedinUser; |
| 114 | }; |
| 115 | |
| 116 | userResource.setLoggedinUser = (loggedinUser:IUserResource) => { |
| 117 | _loggedinUser = loggedinUser; |
| 118 | }; |
| 119 | |
| 120 | return userResource; |
| 121 | } |
| 122 | } |
| 123 | UserResourceService.getResource.$inject = ['$resource', 'sdcConfig', 'Sdc.Services.CookieService']; |