126 lines
3.7 KiB
TypeScript
126 lines
3.7 KiB
TypeScript
import { HttpClient } from '@angular/common/http';
|
|
import { Injectable } from '@angular/core';
|
|
import { MaterialCode, MaterialSetting } from '../models/recipe.model';
|
|
import { environment } from 'src/environments/environment';
|
|
import { Observable, count } from 'rxjs';
|
|
import { ActivatedRoute } from '@angular/router';
|
|
import { getCountryMapSwitcher } from 'src/app/shared/helpers/recipe';
|
|
import { AsyncStorage } from 'src/app/shared/helpers/asyncStorage';
|
|
|
|
@Injectable({ providedIn: 'root' })
|
|
export class MaterialService {
|
|
|
|
private department = this._route.snapshot.paramMap.get('department')
|
|
|
|
constructor(private _httpClient: HttpClient, private _route: ActivatedRoute) {}
|
|
|
|
async getMaterialCodes(
|
|
matIds?: number[],
|
|
country?: string,
|
|
filename?: string
|
|
): Promise<Observable<MaterialCode[]>> {
|
|
|
|
// async country
|
|
let asyncCountry = await AsyncStorage.getItem<string>('currentRecipeCountry');
|
|
|
|
return this._httpClient.get<MaterialCode[]>(
|
|
`${environment.api}/materials/code`,
|
|
{
|
|
params: {
|
|
country: country || asyncCountry,
|
|
filename: filename || this.getCurrentFile(),
|
|
mat_ids: matIds?.join(',') || '',
|
|
},
|
|
withCredentials: true,
|
|
}
|
|
);
|
|
}
|
|
|
|
async getFullMaterialDetail(
|
|
country?: string,
|
|
filename?: string
|
|
): Promise<Observable<{
|
|
"materialId": number,
|
|
"name": string,
|
|
"nameEN": string,
|
|
"type": string
|
|
}[] | null>>{
|
|
console.log("getFullMaterialDetail", country, "where filename = ",filename, "department.short = ", this.department!);
|
|
|
|
let currentCountryWithoutDepartment = await this.getCurrentCountry();
|
|
let asyncCountry = await this.getCurrentCountry(this.department!);
|
|
|
|
console.log("[FullMatFetchService] get current country = ", currentCountryWithoutDepartment, " do switch tuple = ", getCountryMapSwitcher(currentCountryWithoutDepartment));
|
|
country = getCountryMapSwitcher(currentCountryWithoutDepartment);
|
|
filename = filename || this.getCurrentFile();
|
|
|
|
// finalize fetch from what?
|
|
console.log("country, filename", country, filename);
|
|
|
|
return this._httpClient.get<{
|
|
"materialId": number,
|
|
"name": string,
|
|
"nameEN": string,
|
|
"type": string
|
|
}[]>(`${environment.api}/materials/full/${country}/${filename}`, {
|
|
withCredentials: true,
|
|
});
|
|
}
|
|
|
|
async getMaterialSettingById(
|
|
id: number,
|
|
country?: string,
|
|
filename?: string
|
|
): Promise<Observable<MaterialSetting>> {
|
|
|
|
let asyncCountry = await AsyncStorage.getItem<string>('currentRecipeCountry');
|
|
|
|
return this._httpClient.get<MaterialSetting>(
|
|
`${environment.api}/materials/setting/${id}`,
|
|
{
|
|
params: {
|
|
country: country || asyncCountry,
|
|
filename: filename || this.getCurrentFile(),
|
|
},
|
|
withCredentials: true,
|
|
}
|
|
);
|
|
}
|
|
|
|
getCurrentFile(): string {
|
|
const currentRecipeFile = localStorage.getItem('currentRecipeFile');
|
|
if (currentRecipeFile) {
|
|
return currentRecipeFile;
|
|
}
|
|
|
|
return 'default';
|
|
}
|
|
|
|
async getCurrentCountry(department? : string): Promise<string> {
|
|
|
|
// fetch by using department
|
|
if(department){
|
|
|
|
// translate back to full name
|
|
let fullname = getCountryMapSwitcher(department);
|
|
|
|
console.log('Material.service::fullname: ', fullname);
|
|
|
|
await AsyncStorage.setItem('currentRecipeCountry', fullname);
|
|
|
|
// localStorage.setItem('currentRecipeCountry', fullname);
|
|
return fullname;
|
|
}
|
|
|
|
|
|
// const currentRecipeCountry = localStorage.getItem('currentRecipeCountry');
|
|
|
|
const currentRecipeCountry = await AsyncStorage.getItem<string>('currentRecipeCountry');
|
|
|
|
if (currentRecipeCountry) {
|
|
return currentRecipeCountry;
|
|
}
|
|
|
|
return 'Thailand';
|
|
}
|
|
}
|