import { Injectable } from '@angular/core'; import { Observable, Subject, throwError } from 'rxjs'; import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http'; import { catchError, retry } from 'rxjs/operators'; import { DatePipe } from '@angular/common'; import { GlobalfunctionExtension } from './_models/globalFunctions'; import { KendoIconsModule } from './kendo-icons.module'; import { environment } from '../environments/environment'; @Injectable({ providedIn: 'root', }) export class ApiService { public isSSO = false; public versionDeployement = false; public versionNumber = "20261012-01"; public static BASE_URL = ''; public base_url = ''; public KendoSVGIcon = KendoIconsModule.KendoSVGIcon; public payrollTypeId: number = 0; public userType: number = 0; public static PAYROLLTYPEID: number = 0; public static USERTYPE: number = 0; public isSelService : boolean = true; public isSelServiceFullScreen : boolean = false; public isNewDaswhboard : boolean = true; public menuItems: any[] = []; // For Development // public static BASE_URL = 'https://localhost:44374/api'; // public base_url = 'https://localhost:44374/api'; // public mobileAppDownloadLink = this.BASE_URL + 'MobileApp/Download'; public static AuthenticationToken = ''; public static readonly DEFAULT_PHOTO = './assets/photos/profile-default.jpg'; public selectedMenuName: string = ''; constructor(public httpClient: HttpClient, public datepipe: DatePipe) { if (!ApiService.AuthenticationToken) { const storedToken = localStorage.getItem('token'); if (storedToken) { ApiService.AuthenticationToken = storedToken; } } if (!environment.production) { // Dev mode (ng serve): always hit the local backend directly. ApiService.BASE_URL = environment.apiBaseUrl; this.base_url = environment.apiBaseUrl; } else { // Production build: derive the API URL from in // index.html, whatever it was published/deployed with. const baseHref = document.querySelector('base').getAttribute('href'); ApiService.BASE_URL = baseHref + 'api'; this.base_url = ApiService.BASE_URL; } } /** * URL of an employee's approved profile picture, for binding straight to an . * Serving the file over a URL instead of inlining base64 lets the browser cache it and * load many of them in parallel, which is what makes employee lists usable. * Falls back to the default avatar when the employee has no photo on record. */ public empPhotoUrl(employeeNo: string, version?: string): string { if (!employeeNo) { return ApiService.DEFAULT_PHOTO; } const url = `${ApiService.BASE_URL}/Employee/empPhotoFile/${encodeURIComponent(employeeNo)}`; // With a version stamp the server caches the URL for a year: the browser then never // re-requests it, and a replaced photo arrives as a different URL rather than going stale. return version ? `${url}?v=${encodeURIComponent(version)}` : url; } /** URL of the photo an employee has submitted but nobody has approved yet. */ public empPendingPhotoUrl(employeeNo: string): string { if (!employeeNo) { return ApiService.DEFAULT_PHOTO; } return `${ApiService.BASE_URL}/Employee/empPendingPhotoFile/${encodeURIComponent(employeeNo)}`; } createClientHeader(): { headers: HttpHeaders } { let token = ''; if (ApiService.AuthenticationToken) { token = `Bearer ${ApiService.AuthenticationToken}`; } const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token }) }; return httpOptions; } getApiDefaultData(str: string) { return (str === undefined || str === null || str.trim() === '') ? undefined : str; } getApiDateString(dDate: Date) { if (dDate === undefined || dDate === null) { return dDate; } var str: string = this.datepipe.transform(dDate, 'dd-MMM-yyyy'); return str; } getApiDefaultIntData(intData: number) { return (intData === undefined || intData === null) ? undefined : intData; } public httpGet(url: string, params?: any): Observable { var options: HttpHeaders; if (!options) { options = new HttpHeaders({ 'Content-Type': 'application/json;charset=utf-8' }); options = options.append('Accept', 'application/json'); } if (ApiService.AuthenticationToken) { options = options.append('Authorization', `Bearer ${ApiService.AuthenticationToken}`); } if (params) { const p = []; for (const k in params) { p.push((k + '=' + params[k]) as any); } url = url + '?' + (p.join('&')); } var endpoint: string; return this.httpClient.get(this.base_url + url, { headers: options, withCredentials: true }); } public httpGetBlob(url: string): Observable { var options: HttpHeaders = new HttpHeaders(); if (ApiService.AuthenticationToken) { options = options.append('Authorization', `Bearer ${ApiService.AuthenticationToken}`); } return this.httpClient.get(this.base_url + url, { headers: options, withCredentials: true, responseType: 'blob' }); } public httpGetByKey(url: string, key: any): Observable { return this.httpClient.get(ApiService.BASE_URL + url + key, this.createClientHeader()) .pipe( catchError(this.handleClientError) ); } public httpPost(url: string, body: any): Observable { var options: HttpHeaders; if (!options) { options = new HttpHeaders({ 'Content-Type': 'application/json;charset=utf-8' }); options = options.append('Accept', 'application/json'); } if (ApiService.AuthenticationToken) { options = options.append('Authorization', `Bearer ${ApiService.AuthenticationToken}`); } return this.httpClient.post(this.base_url + url, body, { headers: options, withCredentials: true }); //return this.httpClient.post(ApiService.BASE_URL + url, body, this.createClientHeader()) // .pipe( // catchError(this.handleClientError) // ); } public httpDelete(url: string, key: any): Observable { return this.httpClient.delete(ApiService.BASE_URL + url + key, this.createClientHeader()) .pipe( catchError(this.handleClientError) ); } public httpPostFile(url: string, body: any): Observable { var options: HttpHeaders; if (!options) { options = new HttpHeaders({ 'Content-Type': 'application/json;charset=utf-8' }); options = options.append('Accept', 'application/json'); } if (ApiService.AuthenticationToken) { options = options.append('Authorization', `Bearer ${ApiService.AuthenticationToken}`); } return this.httpClient.post(this.base_url + url, body, { headers: options, withCredentials: true }) .pipe( catchError(this.handleClientError) ); } public httpPostBlob(url: string, body: any): Observable { var options: HttpHeaders; if (!options) { //options = new HttpHeaders({ 'Content-Type': 'application/json;charset=utf-8' }); //options = options.append('Accept', 'application/json'); } if (ApiService.AuthenticationToken) { options = new HttpHeaders(); options = options.append('Authorization', `Bearer ${ApiService.AuthenticationToken}`); } return this.httpClient.post(this.base_url + url, body, { headers: options, withCredentials: true }) .pipe( catchError(this.handleClientError) ); } //public httpDownloadFile(url: string, body: any): Observable { // return this.httpClient.post(this.base_url + url, body, { responseType: 'blob' as 'json' }) // .pipe( // catchError(this.handleClientError) // ); //} public httpDownloadFile(url: string, body: any): Observable { var options: HttpHeaders; if (!options) { //options = new HttpHeaders({ 'Content-Type': 'application/json;charset=utf-8' }); //options = options.append('Accept', 'application/json'); } if (ApiService.AuthenticationToken) { options = new HttpHeaders(); options = options.append('Authorization', `Bearer ${ApiService.AuthenticationToken}`); } return this.httpClient.post(this.base_url + url, body, { headers: options, withCredentials: true, responseType: 'blob' as 'json' }) .pipe( catchError(this.handleClientError) ); } public httpDownloadFileNew(url: string, body: any): Observable { var options: HttpHeaders; if (!options) { options = new HttpHeaders({ 'Content-Type': 'application/json;charset=utf-8' }); options = options.append('Accept', 'application/json'); } return this.httpClient.post(this.base_url + url, body, { headers: options, withCredentials: true, responseType: 'blob' as 'json' }); } public handleClientError(error: HttpErrorResponse) { // return Observable.throw(error.message || 'An error has been occurred. Please try again or contact to system administrator'); return throwError(error.error || 'An error has been occurred. Please try again or contact to system administrator'); } } @Injectable() export class PubSubService { public subjects: Subject[] = []; publish(eventName: string) { this.subjects[eventName] = this.subjects[eventName] || new Subject(); this.subjects[eventName].next(); } on(eventName: string): Observable { this.subjects[eventName] = this.subjects[eventName] || new Subject(); return this.subjects[eventName].asObservable(); } } //export class EnumExtension { // static getNamesAndValues(e: any) { // return EnumExtension.getNames(e).map(n => ({ name: n.replace(/_/g, ' '), value: e[n] as T })); // } // static getName(e: any, val: number): string { // if (EnumExtension.getNamesAndValues(e).filter(c => c.value == val) && // EnumExtension.getNamesAndValues(e).filter(c => c.value == val).length > 0) { // return EnumExtension.getNamesAndValues(e).filter(c => c.value == val)[0].name; // } // return ''; // } // static getNames(e: any) { // return EnumExtension.getObjValues(e).filter(v => typeof v === 'string' && v.toLowerCase() !== 'none') as string[]; // } // static getValues(e: any) { // return EnumExtension.getObjValues(e).filter(v => typeof v === 'number') as T[]; // } // public static getObjValues(e: any): (number | string)[] { // return Object.keys(e).map(k => e[k]); // } //} @Injectable() export class BaseHrefService { }