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