UNPKG

consumerportal

Version:

mydna Custimised for you

199 lines (175 loc) 6.41 kB
module services { import IHttpService = angular.IHttpService; export interface IUserService { user(): angular.IPromise<IUser> contact(): angular.IPromise<IUserContact> isLoggedIn(): boolean isLoggedOut(): angular.IPromise<any> login(frm: any): angular.IPromise<any> getAuthToken(): string setDetails(any): angular.IPromise<any> } export interface IUser { City: string Country: string Email: string FirstName: string Gender: string LastName: string MiddleName: string PatientId: string Username: string dob: string Age: number } export interface IUserContact { Address: string Email: string Gender: string MiddleName: string Mobile: string PostCode: string State: string Suburb: string dob: string } export class UserService implements IUserService { static $inject = [ '$rootScope', '$q', '$location', '$http', '$httpParamSerializer', 'apiSrvc', 'mydnaApis', 'errorHandlerSrvc', 'appLocalStorage', ]; storedUser: IUser = null; storedContact: IUserContact = null; private deferred: angular.IDeferred<any>[] = []; private loading: boolean = false; constructor( private $rootScope: angular.IRootScopeService, private $q: angular.IQService, private $location: angular.ILocationService, private $http: IHttpService, private $httpParamSerializer: any, private service: apiSrvc.IApiService, private api: ImyDNAApis, private errorService: errorHandlerSrvc.IErrorHandlerService, private storage: app.IAppStorage ) { $rootScope.$on('changeUser', (ev: any, user: IUser) => { if (!user) { this.storedUser = null; this.storedContact = null; } }); } login(frm: any): angular.IPromise<any> { let deferred = this.$q.defer(); this.$http({ method: 'POST', url: OAuthTokenEndPoint, headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, data: this.$httpParamSerializer(frm) }).then((data: any) => { this.storage.setItem('Token', data.data.access_token); if (frm.RememberMe) { this.storage.setItem(('RememberMe'), frm.Username); } else { this.storage.removeItem("RememberMe"); } deferred.resolve(data); }, (err: any) => { deferred.reject(err); }); return deferred.promise; } logout() { this.storage.removeItem("Token"); this.$rootScope.$broadcast('changeUser', null); this.$location.path('/login'); } getAuthToken() { return this.storage.getItem('Token'); } isLoggedIn(): boolean { return this.storedUser !== null; } isLoggedOut(): angular.IPromise<any> { let defer = this.$q.defer(); this.user().then(() => { defer.reject(); this.$location.path('/dashboard'); }, () => { defer.resolve(); }); return defer.promise; } user(): angular.IPromise<IUser> { let loggedIn: boolean = this.isLoggedIn(); let defer = this.$q.defer(); this.get(this.api.AcctMgmtCurrent, 'storedUser', false).then((value: IUser) => { if (value && !value.Age) { let d: number[] = value.dob.split('/').map((v: string) => { return parseInt(v); }); let today: Date = new Date(); today.setHours(0, 0, 0, 0); let birthday: Date = new Date (d[2], d[1]-1, d[0]); let age = today.getFullYear() - birthday.getFullYear(); birthday.setFullYear(today.getFullYear()); if (today < birthday) age--; this.storedUser.Age = value.Age = age; if (!loggedIn) { this.$rootScope.$broadcast('changeUser', this.storedUser); } } defer.resolve(this.storedUser); }, (err: any) => { defer.reject(); }); return defer.promise; } test(): angular.IPromise<any> { return this.get(this.api.User, null, false); } notuser() { let defer = this.$q.defer(); if (this.storedUser) { defer.reject() } else { defer.resolve(true); } return defer.promise; } contact(): angular.IPromise<IUserContact> { return this.get(this.api.ContactDetails, 'storedContact', false); } setDetails(values: any) { return this.service.get_request_params(this.api.PatientDetails, values).then((values: any) => { //this.api }) } private get(url: string, store: string, forceReload: boolean): angular.IPromise<any> { let defer = this.$q.defer(); if (this[store] && !forceReload) { defer.resolve(this[store]); } else { this.service.get_request_params(url).then((values: any[]) => { this[store] = values; defer.resolve(this[store]); }, (err:any) => { defer.reject(); }); } return defer.promise; } } angular .module('app') .service('userService', UserService); }