Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | import {IAppConfigurtaion, ICookie} from "../models/app-config"; |
| 3 | |
| 4 | interface ICookieService { |
| 5 | getUserId():string; |
| 6 | getFirstName():string; |
| 7 | getLastName():string; |
| 8 | getEmail():string; |
| 9 | getUserIdSuffix():string; |
| 10 | } |
| 11 | |
| 12 | export class CookieService implements ICookieService { |
| 13 | |
| 14 | static '$inject' = ['sdcConfig', '$document']; |
| 15 | private cookie:ICookie; |
| 16 | private cookiePrefix:string; |
| 17 | |
| 18 | |
| 19 | constructor(sdcConfig:IAppConfigurtaion, private $document) { |
| 20 | this.cookie = sdcConfig.cookie; |
| 21 | |
| 22 | this.cookiePrefix = ''; |
| 23 | let junctionName:string = this.getCookieByName(this.cookie.junctionName); |
| 24 | if ((junctionName !== null) && (junctionName !== '')) { |
| 25 | this.cookiePrefix = this.cookie.prefix + junctionName + '!'; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | private getCookieByName = (cookieName:string):string => { |
| 30 | cookieName += '='; |
| 31 | let cookies:Array<string> = this.$document[0].cookie.split(';'); |
| 32 | let cookieVal:string = ''; |
| 33 | cookies.forEach((cookie:string) => { |
| 34 | while (cookie.charAt(0) === ' ') { |
| 35 | cookie = cookie.substring(1); |
| 36 | } |
| 37 | if (cookie.indexOf(cookieName) === 0) { |
| 38 | cookieVal = cookie.substring(cookieName.length, cookie.length); |
| 39 | return; |
| 40 | } |
| 41 | }); |
| 42 | return cookieVal; |
| 43 | }; |
| 44 | |
| 45 | public getUserIdSuffix = ():string => { |
| 46 | return this.cookie.userIdSuffix; |
| 47 | }; |
| 48 | |
| 49 | public getUserId = ():string => { |
| 50 | let userIdCookieName:string = this.cookiePrefix + this.cookie.userIdSuffix; |
| 51 | let userId:string = this.getCookieByName(userIdCookieName); |
| 52 | return userId; |
| 53 | }; |
| 54 | |
| 55 | public getFirstName = ():string => { |
| 56 | let firstNameCookieName:string = this.cookiePrefix + this.cookie.userFirstName; |
| 57 | let firstName:string = this.getCookieByName(firstNameCookieName); |
| 58 | return firstName; |
| 59 | }; |
| 60 | |
| 61 | public getLastName = ():string => { |
| 62 | let lastNameCookieName:string = this.cookiePrefix + this.cookie.userLastName; |
| 63 | let lastName:string = this.getCookieByName(lastNameCookieName); |
| 64 | return lastName; |
| 65 | }; |
| 66 | |
| 67 | public getEmail = ():string => { |
| 68 | let emailCookieName:string = this.cookiePrefix + this.cookie.userEmail; |
| 69 | let email:string = this.getCookieByName(emailCookieName); |
| 70 | return email; |
| 71 | } |
| 72 | } |