import {catchError, tap, map} from 'rxjs/operators'; /** * @Service:Agent Service * @Creation Date: Aug 3, 2017 * @Author: sbaireddy * @Version: 1.0 : */ import { Injectable } from '@angular/core'; // import { Subject } from 'rxjs/Subject'; // import { HttpClient, Headers, RequestOptions, Response } from '@angular/_http'; import { HttpClient } from '@angular/common/http'; // import { Observable } from 'rxjs/observable'; import { Observable } from 'rxjs/Rx'; // for debugging import { BaseService } from './base.service'; import * as ServiceConstants from '../shared/service.constants'; import Utils from '../shared/utils'; import { restUrl } from '../rest.url'; import { KeyValue } from '../shared/modal/key.value'; import { IWCToken, INotification, IAddress } from '../shared/modal/common'; import { User, Logon } from '../shared/modal/user'; // import { HttpClient } from 'selenium-webdriver/_http'; /** * This class provides the User Service with methods to login, logout, profile, profile, notifictions. */ @Injectable({providedIn:'root'}) export class UserService extends BaseService { user: User; getUser(): User { if (this.user) { return this.user; } else { return Utils.getUserDetail(); } } setUser(user: User): void { this.user = user; Utils.storeUserDetail(user); } /** * Creates a new UserService with the injected HttpClient. * @param {HttpClient} _http - The injected HttpClient. * @constructor */ constructor(public _http: HttpClient) { super(); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {IWCToken} The Observable for the HTTP request. */ login(storeId: number, requestParams: Logon): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-userService.login()'); } // getting the request options const options = this.httpOptions() // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue('storeId', storeId)); // getting the final url const url = Utils.replacePathParams(restUrl.user.login, pathParams); // service calling return this._http.post(url, requestParams, options).pipe( map((res: any) => { return res; }), tap(data => { sessionStorage.setItem('userId', '' + data.userId); sessionStorage.setItem('WCToken', data.WCToken); sessionStorage.setItem('WCTrustedToken', data.WCTrustedToken); sessionStorage.setItem('personalizationID', data.personalizationID); if (ServiceConstants.LOGGING) { console.log('[end]-userService.login()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {any} The Observable for the HTTP request. */ logout(storeId: number, userId: number): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-userService.logout()'); } // getting the request options const options = this.httpOptions() // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue('storeId', storeId)); // getting the final url const url = Utils.replacePathParams(restUrl.user.logout, pathParams); // service calling return this._http.delete(url, options).pipe( map((res: any) => res), tap(data => { if (sessionStorage.getItem('userId')) { sessionStorage.removeItem('userId'); } if (sessionStorage.getItem('WCToken')) { sessionStorage.removeItem('WCToken'); } if (sessionStorage.getItem('WCTrustedToken')) { sessionStorage.removeItem('WCTrustedToken'); } if (sessionStorage.getItem('personalizationID')) { sessionStorage.removeItem('personalizationID'); } if (ServiceConstants.LOGGING) { console.log('[end]-userService.logout()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {User} The Observable for the HTTP request. */ getProfile(storeId: number, userId: number): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-userService.getProfile()'); } // getting the request options const options = this.httpOptions() // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue('storeId', storeId)); pathParams.push(new KeyValue('userId', userId)); // getting the final url const url = Utils.replacePathParams(restUrl.user.getProfile, pathParams); // service calling // return this._http.get ( 'assets/data/profile.json', options) return this._http.get(url, options).pipe( map((res: any) => res), tap(data => { if (ServiceConstants.LOGGING) { console.log('[end]-userService.getProfile()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {any} The Observable for the HTTP request. */ updateProfile(storeId: number, userId: number, requestParams: any): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-userService.updateProfile()'); } // getting the request options const options = this.httpOptions() // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue('storeId', storeId)); pathParams.push(new KeyValue('userId', userId)); // getting the final url const url = Utils.replacePathParams(restUrl.user.updateProfile, pathParams); // service calling return this._http.put(url, requestParams, options).pipe( map((res: any) => res), tap(data => { if (ServiceConstants.LOGGING) { console.log('[end]-userService.updateProfile()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {Array} The Observable for the HTTP request. */ getNotifications(storeId: number, userId: number): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-userService.getNotifications()'); } // getting the request options const options = this.httpOptions() // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue('storeId', storeId)); pathParams.push(new KeyValue('userId', userId)); // getting the final url const url = Utils.replacePathParams(restUrl.user.getNotifications, pathParams); // service calling return this._http.get(url, options).pipe( map((res: any) => res.notifications), tap(data => { if (ServiceConstants.LOGGING) { console.log('[end]-userService.getNotifications()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {any} The Observable for the HTTP request. */ updateNotifications(storeId: number, userId: number, requestParams: any): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-userService.updateNotifications()'); } // getting the request options const options = this.httpOptions() // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue('storeId', storeId)); pathParams.push(new KeyValue('userId', userId)); // getting the final url const url = Utils.replacePathParams(restUrl.user.updateNotifications, pathParams); // service calling return this._http.put(url, requestParams, options).pipe( map((res: any) => res), tap(data => { if (ServiceConstants.LOGGING) { console.log('[end]-userService.updateNotifications()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {any} The Observable for the HTTP request. */ validateDealerCode(storeId: number, userId: number, requestParams: string): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-userService.validateDealerCode()'); } // getting the request options const options = this.httpOptions() // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue('storeId', storeId)); pathParams.push(new KeyValue('userId', userId)); // getting the final url const url = Utils.replacePathParams(restUrl.user.validateDealer, pathParams); // service calling return this._http.post(url, requestParams, options).pipe( map((res: any) => res), tap(data => { if (ServiceConstants.LOGGING) { console.log('[end]-userService.validateDealerCode()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {any} The Observable for the HTTP request. */ getDormantProperties(storeId: number): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-userService.getDormantProperties()'); } // getting the request options const options = this.httpOptions() // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue('storeId', storeId)); // getting the final url const url = Utils.replacePathParams(restUrl.dormant.dormantUrl, pathParams); // service calling return this._http.get(url, options).pipe( map((res: any) => res), tap(data => { if (ServiceConstants.LOGGING) { console.log('[end]-userService.getDormantProperties()'); } }),// for debug catchError(this.handleError)); } /** * Returns an Observable for the HTTP GET request for the JSON resource. * @return {User} The Observable for the HTTP request. */ getRetailStoreDetail(storeId: number, userId: number, retailStoreId: number): Observable { if (ServiceConstants.LOGGING) { console.log('[start]-userService.getRetailStoreDetail()'); } // getting the request options const options = this.httpOptions() // constracting the path params const pathParams: Array = new Array(); pathParams.push(new KeyValue('storeId', storeId)); pathParams.push(new KeyValue('userId', userId)); pathParams.push(new KeyValue('retailStoreId', retailStoreId)); // getting the final url const url = Utils.replacePathParams(restUrl.user.retalStoreDetail, pathParams); // service calling // return this._http.get ( 'assets/data/retail-store-detail.json', options) return this._http.get(url, options).pipe( map((res: any) => res), tap(data => { if (ServiceConstants.LOGGING) { console.log('[end]-userService.getRetailStoreDetail()'); } }),// for debug catchError(this.handleError)); } }