EchoTex_Payroll/HRM.UI/ClientApp/src/app/app.api.service.ts
2024-11-03 12:31:54 +06:00

370 lines
12 KiB
TypeScript

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';
@Injectable({
providedIn: 'root',
})
export class ApiService {
public isSSO = false;
public versionDeployement = false;
public versionNumber = `V-${GlobalfunctionExtension.generateVersionNumber(new Date(2024, 10, 3))}-`+"01";
public static BASE_URL = '';
public base_url = '';
// public currentLink = '';
//YYYYMMDD
// public versionNumber = `V-${new Date(2024, 7, 18).toISOString().slice(0, 10).replace(/-/g, '')}-`+"01";
public payrollTypeId: number = 0;
public userType: number = 0;
public static PAYROLLTYPEID: number = 0;
public static USERTYPE: number = 0;
// For Development
// public static BASE_URL = 'https://localhost:44374/api';
// public base_url = 'https://localhost:44374/api';
// For patromaxHR
// public base_url = '/petromaxHR/api';
// public static BASE_URL = '/petromaxHR/api';
// For LiFung BD CELCloud
//public base_url = '/lifungbd/api';
//public static BASE_URL = '/lifungbd/api';
// For WeLink
/*public static BASE_URL = '/welinkventures/api';
public base_url = '/welinkventures/api';*/
// For BAT
/*public static BASE_URL = '/batDemo/api';
public base_url = '/batDemo/api';*/
// For Demo
//public static BASE_URL = '/celDemo/api';
//public base_url = '/celDemo/api';
// For LiFung App Test
//public base_url = '/lifungtest/api';
//public static BASE_URL = '/lifungtest/api';
// For SQ Demo
//public base_url = '/SQDEMO/api';
//public static BASE_URL = '/SQDEMO/api';
// For Synovia Test
/*public base_url = '/synovia/api';
public static BASE_URL = '/synovia/api';*/
// For Synovia Live
// public base_url = '/synoviahr/api';
// public static BASE_URL = '/synoviahr/api';
// For BProperty test
//public base_url = '/bproperty/api';
//public static BASE_URL = '/bproperty/api';
// For BProperty Live
//public base_url = '/api';
//public static BASE_URL = '/api';
// For Azure
/*public static BASE_URL = '/api';
public base_url = '/api';*/
// For H&M Live
//public base_url = '/hnm/api';
//public static BASE_URL = '/hnm/api';
// For echotex
// public static BASE_URL = '/echotex/api';
// public base_url = '/echotex/api';
// For echotex test
//public static BASE_URL = '/echotextest/api';
//public base_url = '/echotextest/api';
// For echotexnew
// public static BASE_URL = '/echotexnew/api';
// public base_url = '/echotexnew/api';
// For EchoTex Live
// public static BASE_URL = '/api';
// public base_url = '/api';
// For EchoTex Data Migration
//public static BASE_URL = '/EchotexDataMigration/api';
//public base_url = '/EchotexDataMigration/api';
// For SGS
// public static BASE_URL = '/sgsrevamp/api';
// public base_url = '/sgsrevamp/api';
// For SGS_Live
// public static BASE_URL = '/HRIS/api';
// public base_url = '/HRIS/api';
// For SGS_Live UAT
// public static BASE_URL = '/HRIS_UAT/api';
// public base_url = '/HRIS_UAT/api';
// For sqpayroll Test
//public static BASE_URL = '/sqpayroll/api';
//public base_url = '/sqpayroll/api';
// For erecruitmentadmin
//public static BASE_URL = '/erecruitmentadmin/api';
//public base_url = '/erecruitmentadmin/api';
// public mobileAppDownloadLink = this.BASE_URL + 'MobileApp/Download';
public static AuthenticationToken = '';
public selectedMenuName: string = '';
constructor(public httpClient: HttpClient, public datepipe: DatePipe) {
// ApiService.PAYROLLTYPEID=this.payrollTypeId;
// ApiService.USERTYPE = this.userType;
const baseHref = document.querySelector('base').getAttribute('href');
if (baseHref === "/" && !this.versionDeployement) {
ApiService.BASE_URL = 'https://localhost:44374/api';
this.base_url = 'https://localhost:44374/api';
}
else {
ApiService.BASE_URL = baseHref + 'api';
this.base_url = ApiService.BASE_URL;
}
}
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<T>(url: string, params?: any): Observable<T> {
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<T>(this.base_url + url, { headers: options, withCredentials: true });
}
public httpGetByKey<T>(url: string, key: any): Observable<T> {
return this.httpClient.get<T>(ApiService.BASE_URL + url + key, this.createClientHeader())
.pipe(
catchError(this.handleClientError)
);
}
public httpPost<T>(url: string, body: any): Observable<T> {
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<T>(this.base_url + url, body, { headers: options, withCredentials: true });
//return this.httpClient.post<T>(ApiService.BASE_URL + url, body, this.createClientHeader())
// .pipe(
// catchError(this.handleClientError)
// );
}
public httpDelete<T>(url: string, key: any): Observable<T> {
return this.httpClient.delete<T>(ApiService.BASE_URL + url + key, this.createClientHeader())
.pipe(
catchError(this.handleClientError)
);
}
public httpPostFile(url: string, body: any): Observable<Blob> {
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<Blob>(this.base_url + url, body, { headers: options, withCredentials: true })
.pipe(
catchError(this.handleClientError)
);
}
public httpPostBlob(url: string, body: any): Observable<any> {
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<Blob>(this.base_url + url, body, { headers: options, withCredentials: true })
.pipe(
catchError(this.handleClientError)
);
}
//public httpDownloadFile(url: string, body: any): Observable<Blob> {
// return this.httpClient.post<Blob>(this.base_url + url, body, { responseType: 'blob' as 'json' })
// .pipe(
// catchError(this.handleClientError)
// );
//}
public httpDownloadFile(url: string, body: any): Observable<Blob> {
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<Blob>(this.base_url + url, body, {
headers: options,
withCredentials: true,
responseType: 'blob' as 'json'
})
.pipe(
catchError(this.handleClientError)
);
}
public httpDownloadFileNew(url: string, body: any): Observable<Blob> {
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<Blob>(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<any>[] = [];
publish(eventName: string) {
this.subjects[eventName] = this.subjects[eventName] || new Subject<any>();
this.subjects[eventName].next();
}
on(eventName: string): Observable<any> {
this.subjects[eventName] = this.subjects[eventName] || new Subject<any>();
return this.subjects[eventName].asObservable();
}
}
//export class EnumExtension {
// static getNamesAndValues<T extends number>(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<T extends number>(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 {
}