Taobin-Recipe-Manager/client/src/app/core/services/material.service.ts

127 lines
3.7 KiB
TypeScript
Raw Normal View History

2023-10-06 15:33:10 +07:00
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { MaterialCode, MaterialSetting } from '../models/recipe.model';
import { environment } from 'src/environments/environment';
2024-01-19 14:59:21 +07:00
import { Observable, count } from 'rxjs';
2024-01-17 17:38:23 +07:00
import { ActivatedRoute } from '@angular/router';
import { getCountryMapSwitcher } from 'src/app/shared/helpers/recipe';
2024-01-18 16:59:06 +07:00
import { AsyncStorage } from 'src/app/shared/helpers/asyncStorage';
2023-10-06 15:33:10 +07:00
@Injectable({ providedIn: 'root' })
export class MaterialService {
2024-01-17 17:38:23 +07:00
private department = this._route.snapshot.paramMap.get('department')
constructor(private _httpClient: HttpClient, private _route: ActivatedRoute) {}
2023-10-06 15:33:10 +07:00
2024-01-18 16:59:06 +07:00
async getMaterialCodes(
2023-10-06 15:33:10 +07:00
matIds?: number[],
2023-10-24 18:01:52 +07:00
country?: string,
filename?: string
2024-01-18 16:59:06 +07:00
): Promise<Observable<MaterialCode[]>> {
// async country
let asyncCountry = await AsyncStorage.getItem<string>('currentRecipeCountry');
2023-10-06 15:33:10 +07:00
return this._httpClient.get<MaterialCode[]>(
`${environment.api}/materials/code`,
{
params: {
2024-01-18 16:59:06 +07:00
country: country || asyncCountry,
2023-10-27 16:13:04 +07:00
filename: filename || this.getCurrentFile(),
2023-10-06 15:33:10 +07:00
mat_ids: matIds?.join(',') || '',
},
withCredentials: true,
}
);
}
2024-01-18 16:59:06 +07:00
async getFullMaterialDetail(
2023-12-29 16:10:57 +07:00
country?: string,
filename?: string
2024-01-18 16:59:06 +07:00
): Promise<Observable<{
2023-12-29 16:10:57 +07:00
"materialId": number,
"name": string,
2024-01-24 11:58:39 +07:00
"nameEN": string,
2023-12-29 16:10:57 +07:00
"type": string
2024-01-18 16:59:06 +07:00
}[] | null>>{
2024-01-19 14:59:21 +07:00
console.log("getFullMaterialDetail", country, "where filename = ",filename, "department.short = ", this.department!);
2023-12-29 16:10:57 +07:00
2024-01-19 14:59:21 +07:00
let currentCountryWithoutDepartment = await this.getCurrentCountry();
let asyncCountry = await this.getCurrentCountry(this.department!);
2024-01-18 16:59:06 +07:00
2024-01-19 14:59:21 +07:00
console.log("[FullMatFetchService] get current country = ", currentCountryWithoutDepartment, " do switch tuple = ", getCountryMapSwitcher(currentCountryWithoutDepartment));
country = getCountryMapSwitcher(currentCountryWithoutDepartment);
2024-01-08 10:37:31 +07:00
filename = filename || this.getCurrentFile();
2024-01-17 17:38:23 +07:00
// finalize fetch from what?
console.log("country, filename", country, filename);
2023-12-29 16:10:57 +07:00
return this._httpClient.get<{
"materialId": number,
"name": string,
2024-01-24 11:58:39 +07:00
"nameEN": string,
2023-12-29 16:10:57 +07:00
"type": string
}[]>(`${environment.api}/materials/full/${country}/${filename}`, {
withCredentials: true,
});
}
2024-01-18 16:59:06 +07:00
async getMaterialSettingById(
2023-10-06 15:33:10 +07:00
id: number,
2023-10-24 18:01:52 +07:00
country?: string,
filename?: string
2024-01-18 16:59:06 +07:00
): Promise<Observable<MaterialSetting>> {
let asyncCountry = await AsyncStorage.getItem<string>('currentRecipeCountry');
2023-10-06 15:33:10 +07:00
return this._httpClient.get<MaterialSetting>(
`${environment.api}/materials/setting/${id}`,
{
params: {
2024-01-18 16:59:06 +07:00
country: country || asyncCountry,
2023-10-27 16:13:04 +07:00
filename: filename || this.getCurrentFile(),
2023-10-06 15:33:10 +07:00
},
withCredentials: true,
}
);
}
2023-10-27 16:13:04 +07:00
getCurrentFile(): string {
const currentRecipeFile = localStorage.getItem('currentRecipeFile');
if (currentRecipeFile) {
return currentRecipeFile;
}
2024-01-17 17:38:23 +07:00
return 'default';
2023-10-27 16:13:04 +07:00
}
2024-01-18 16:59:06 +07:00
async getCurrentCountry(department? : string): Promise<string> {
2024-01-17 17:38:23 +07:00
// fetch by using department
if(department){
// translate back to full name
let fullname = getCountryMapSwitcher(department);
console.log('Material.service::fullname: ', fullname);
2024-01-19 14:59:21 +07:00
await AsyncStorage.setItem('currentRecipeCountry', fullname);
2024-01-17 17:38:23 +07:00
// localStorage.setItem('currentRecipeCountry', fullname);
return fullname;
}
2024-01-18 16:59:06 +07:00
// const currentRecipeCountry = localStorage.getItem('currentRecipeCountry');
const currentRecipeCountry = await AsyncStorage.getItem<string>('currentRecipeCountry');
2023-10-27 16:13:04 +07:00
if (currentRecipeCountry) {
return currentRecipeCountry;
}
return 'Thailand';
}
2023-10-06 15:33:10 +07:00
}