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 {IAppConfigurtaion, ICookie} from "../models/app-config"; |
| 23 | |
| 24 | interface ICookieService { |
| 25 | getUserId():string; |
| 26 | getFirstName():string; |
| 27 | getLastName():string; |
| 28 | getEmail():string; |
| 29 | getUserIdSuffix():string; |
| 30 | } |
| 31 | |
| 32 | export class CookieService implements ICookieService { |
| 33 | |
| 34 | static '$inject' = ['sdcConfig', '$document']; |
| 35 | private cookie:ICookie; |
| 36 | private cookiePrefix:string; |
| 37 | |
| 38 | |
| 39 | constructor(sdcConfig:IAppConfigurtaion, private $document) { |
| 40 | this.cookie = sdcConfig.cookie; |
| 41 | |
| 42 | this.cookiePrefix = ''; |
| 43 | let junctionName:string = this.getCookieByName(this.cookie.junctionName); |
| 44 | if ((junctionName !== null) && (junctionName !== '')) { |
| 45 | this.cookiePrefix = this.cookie.prefix + junctionName + '!'; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | private getCookieByName = (cookieName:string):string => { |
| 50 | cookieName += '='; |
| 51 | let cookies:Array<string> = this.$document[0].cookie.split(';'); |
| 52 | let cookieVal:string = ''; |
| 53 | cookies.forEach((cookie:string) => { |
| 54 | while (cookie.charAt(0) === ' ') { |
| 55 | cookie = cookie.substring(1); |
| 56 | } |
| 57 | if (cookie.indexOf(cookieName) === 0) { |
| 58 | cookieVal = cookie.substring(cookieName.length, cookie.length); |
| 59 | return; |
| 60 | } |
| 61 | }); |
| 62 | return cookieVal; |
| 63 | }; |
| 64 | |
| 65 | public getUserIdSuffix = ():string => { |
| 66 | return this.cookie.userIdSuffix; |
| 67 | }; |
| 68 | |
| 69 | public getUserId = ():string => { |
| 70 | let userIdCookieName:string = this.cookiePrefix + this.cookie.userIdSuffix; |
| 71 | let userId:string = this.getCookieByName(userIdCookieName); |
| 72 | return userId; |
| 73 | }; |
| 74 | |
| 75 | public getFirstName = ():string => { |
| 76 | let firstNameCookieName:string = this.cookiePrefix + this.cookie.userFirstName; |
| 77 | let firstName:string = this.getCookieByName(firstNameCookieName); |
| 78 | return firstName; |
| 79 | }; |
| 80 | |
| 81 | public getLastName = ():string => { |
| 82 | let lastNameCookieName:string = this.cookiePrefix + this.cookie.userLastName; |
| 83 | let lastName:string = this.getCookieByName(lastNameCookieName); |
| 84 | return lastName; |
| 85 | }; |
| 86 | |
| 87 | public getEmail = ():string => { |
| 88 | let emailCookieName:string = this.cookiePrefix + this.cookie.userEmail; |
| 89 | let email:string = this.getCookieByName(emailCookieName); |
| 90 | return email; |
| 91 | } |
| 92 | } |