import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable, throwError } from 'rxjs'; import { catchError, map } from 'rxjs/operators'; import { ConfigService } from '@arrow/bom/config'; import { LoggerService } from '@arrow/bom/logger'; import { Bom } from '@arrow/bom/store'; import { BadBom, UserResponse } from './user-response.model'; import { FeatureFlagService } from '@arrow/bom/feature-flag'; @Injectable() export class UserService2 { public isAnonymous: boolean = true; public email: string; constructor( private http: HttpClient, private config: ConfigService, private log: LoggerService, private _ffService: FeatureFlagService ) {} private handleError(error: any): any { this.log.warn(error); return throwError(error.error || 'Server error'); } /** * Retrieve the list of user's BOMs * @return Observable */ public getBoms(): Observable { return this.http .get(this.getBomsForUserUrl(), { withCredentials: true }) .pipe(map((data: UserResponse) => data.payload.map(this.bomTransform))) .pipe(catchError(error => this.handleError(error))); } //TODO: Remove this flag after successful testing. private getBomsForUserUrl(){ return `${this.config.apiCoreUrl}/api/boms/@current`; } //TODO: Remove this flag after successful testing. private getUserIdentityUrl(){ return `${this.config.apiCoreUrl}/api/users/@current/identity`; } private bomTransform(bom: BadBom): Bom { return { id: bom.bomId, name: bom.bomName, partCount: bom.partCount, created: bom.cdate, edited: bom.lastEdited, permissions: bom.permission, source: bom.source, owner: bom.ownerEmail, group: bom.sharedWith, isShared: bom.isShared }; } public getUserAuth(): Observable { const AUTH_URL = this.getUserIdentityUrl(); return this.http.get(AUTH_URL, { withCredentials: true }); } public getUserEndCustomers(): Observable { const AUTH_URL = `${this.config.apiCoreUrl}/service/end-customers`; return this.http.get(AUTH_URL, { withCredentials: true }); } public createCPN(bomId, partId, cpn, ecs): Observable { return this.http .post( `${this.config.apiCoreUrl}/api/cpns`, { bomId: bomId, partId: partId, cpn: cpn, endCustomers: ecs }, { withCredentials: true } ) .pipe(catchError(error => this.handleError(error))); } }