2023-12-06 10:05:16 +07:00
|
|
|
import { HttpClient } from '@angular/common/http';
|
|
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
import { Observable, tap } from 'rxjs';
|
|
|
|
|
import {
|
|
|
|
|
Recipe,
|
|
|
|
|
Recipe01,
|
|
|
|
|
RecipeDetail,
|
|
|
|
|
RecipeDetailMat,
|
|
|
|
|
RecipeOverview,
|
|
|
|
|
RecipeOverviewList,
|
|
|
|
|
RecipesDashboard,
|
|
|
|
|
} from '../models/recipe.model';
|
|
|
|
|
import { environment } from 'src/environments/environment';
|
|
|
|
|
import { RecipeMetaData } from 'src/app/shared/types/recipe';
|
2024-01-17 17:38:23 +07:00
|
|
|
import { getCountryMapSwitcher } from 'src/app/shared/helpers/recipe';
|
|
|
|
|
import { ActivatedRoute } from '@angular/router';
|
2024-01-18 16:59:06 +07:00
|
|
|
import { AsyncStorage } from 'src/app/shared/helpers/asyncStorage';
|
2024-04-11 16:59:37 +07:00
|
|
|
import { SocketIOService } from 'src/app/shared/services/websocket.service';
|
|
|
|
|
import { UserService } from './user.service';
|
2023-12-06 10:05:16 +07:00
|
|
|
|
|
|
|
|
type RecipeOverviewParams = {
|
|
|
|
|
filename: string;
|
|
|
|
|
country: string;
|
|
|
|
|
materialIds: number[];
|
|
|
|
|
offset: number;
|
|
|
|
|
take: number;
|
|
|
|
|
search: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type RecipeDashboardParams = {
|
|
|
|
|
filename: string;
|
|
|
|
|
country: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
interface RecipeFiles {
|
|
|
|
|
[key: string]: string[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Injectable({ providedIn: 'root' })
|
|
|
|
|
export class RecipeService {
|
|
|
|
|
private countries: string[] = [];
|
|
|
|
|
private recipeFiles: RecipeFiles = {};
|
|
|
|
|
|
|
|
|
|
private tmp_files: string[] = [];
|
|
|
|
|
|
2024-01-17 17:38:23 +07:00
|
|
|
private department = this._route.snapshot.paramMap.get('department');
|
|
|
|
|
|
2023-12-06 10:05:16 +07:00
|
|
|
private get tmpfiles(): string[] {
|
|
|
|
|
return this.tmp_files;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-22 16:04:34 +07:00
|
|
|
constructor(
|
|
|
|
|
private _httpClient: HttpClient,
|
2024-04-11 16:59:37 +07:00
|
|
|
private _route: ActivatedRoute,
|
|
|
|
|
private _user: UserService,
|
|
|
|
|
// private _socket: SocketIOService
|
|
|
|
|
) {
|
|
|
|
|
// this._socket.checkIn(this._user.getCurrentUser()!.name, 'recipe:response');
|
|
|
|
|
}
|
2023-12-06 10:05:16 +07:00
|
|
|
|
2024-04-11 16:59:37 +07:00
|
|
|
shareHttpClient() {
|
2024-04-01 09:38:34 +07:00
|
|
|
return this._httpClient;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-06 10:05:16 +07:00
|
|
|
getRecipesDashboard(
|
2024-01-18 16:59:06 +07:00
|
|
|
params: any = {
|
2024-01-17 17:38:23 +07:00
|
|
|
country: this.getCurrentCountry(this.department!),
|
2023-12-06 10:05:16 +07:00
|
|
|
filename: this.getCurrentFile(),
|
|
|
|
|
}
|
|
|
|
|
): Observable<RecipesDashboard> {
|
2024-04-11 16:59:37 +07:00
|
|
|
// test socket service
|
|
|
|
|
|
|
|
|
|
// this._socket.getPayload(`${this._user.getCurrentUser()!.name}:recipe:response:cache`)?.subscribe({
|
|
|
|
|
// next: (c: any)=> {
|
|
|
|
|
// console.log("cache from server", c);
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// this._socket.sendTopic('recipe:dashboard', {
|
|
|
|
|
// room: 'recipe:response',
|
|
|
|
|
// topic: 'recipe:dashboard',
|
|
|
|
|
// user: this._user.getCurrentUser()!.name,
|
|
|
|
|
// country: params.country,
|
|
|
|
|
// filename: params.filename,
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// return this._socket.getPayload(
|
|
|
|
|
// `recipe:dashboard:${this._user.getCurrentUser()!.name}:payload`
|
|
|
|
|
// )!;
|
|
|
|
|
|
2023-12-06 10:05:16 +07:00
|
|
|
return this._httpClient.get<RecipesDashboard>(
|
|
|
|
|
environment.api + '/recipes/dashboard',
|
|
|
|
|
{
|
|
|
|
|
params: {
|
|
|
|
|
country: params.country,
|
|
|
|
|
filename: params.filename,
|
|
|
|
|
},
|
|
|
|
|
withCredentials: true,
|
|
|
|
|
responseType: 'json',
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 16:59:06 +07:00
|
|
|
async getRecipeOverview(
|
|
|
|
|
params: any = {
|
2024-01-17 17:38:23 +07:00
|
|
|
country: this.getCurrentCountry(this.department!),
|
2023-12-06 10:05:16 +07:00
|
|
|
filename: this.getCurrentFile(),
|
|
|
|
|
materialIds: [],
|
|
|
|
|
offset: 0,
|
|
|
|
|
take: 20,
|
|
|
|
|
search: '',
|
|
|
|
|
}
|
2024-01-18 16:59:06 +07:00
|
|
|
): Promise<Observable<RecipeOverviewList>> {
|
2024-04-11 16:59:37 +07:00
|
|
|
|
|
|
|
|
console.log("overview: ", params);
|
|
|
|
|
|
|
|
|
|
// this._socket.sendTopic('recipe:overview', {
|
|
|
|
|
// room: 'recipe:response',
|
|
|
|
|
// topic: 'recipe:overview',
|
|
|
|
|
// user: this._user.getCurrentUser()!.name,
|
|
|
|
|
// ...params
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// return this._socket.getPayload(
|
|
|
|
|
// `recipe:overview:${this._user.getCurrentUser()!.name}:payload`
|
|
|
|
|
// )!;
|
|
|
|
|
|
2023-12-06 10:05:16 +07:00
|
|
|
return this._httpClient.get<RecipeOverviewList>(
|
|
|
|
|
environment.api + '/recipes/overview',
|
|
|
|
|
{
|
|
|
|
|
params: {
|
|
|
|
|
country: params.country,
|
|
|
|
|
filename: params.filename,
|
|
|
|
|
materialIds: params.materialIds.join(','),
|
|
|
|
|
offset: params.offset.toString(),
|
|
|
|
|
take: params.take.toString(),
|
|
|
|
|
search: params.search,
|
|
|
|
|
},
|
|
|
|
|
withCredentials: true,
|
|
|
|
|
responseType: 'json',
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-22 16:04:34 +07:00
|
|
|
async getRecipeDetail(
|
|
|
|
|
productCode: string
|
|
|
|
|
): Promise<Observable<RecipeDetail>> {
|
2024-01-19 14:59:21 +07:00
|
|
|
let asyncCountry = await this.getCurrentCountry(this.department!);
|
2024-04-01 09:38:34 +07:00
|
|
|
// console.log('get detail by asyncCountry', asyncCountry);
|
2024-01-19 14:59:21 +07:00
|
|
|
|
2023-12-06 10:05:16 +07:00
|
|
|
return this._httpClient.get<RecipeDetail>(
|
|
|
|
|
environment.api + '/recipes/' + productCode,
|
|
|
|
|
{
|
|
|
|
|
params: {
|
|
|
|
|
filename: this.getCurrentFile(),
|
2024-01-19 14:59:21 +07:00
|
|
|
country: asyncCountry,
|
2023-12-06 10:05:16 +07:00
|
|
|
},
|
|
|
|
|
withCredentials: true,
|
|
|
|
|
responseType: 'json',
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 16:59:06 +07:00
|
|
|
async getRecipeDetailMat(
|
2023-12-06 10:05:16 +07:00
|
|
|
productCode: string
|
2024-02-22 16:04:34 +07:00
|
|
|
): Promise<Observable<{ result: RecipeDetailMat[] }>> {
|
2024-01-19 14:59:21 +07:00
|
|
|
let asyncCountry = await this.getCurrentCountry(this.department!);
|
|
|
|
|
|
2023-12-06 10:05:16 +07:00
|
|
|
return this._httpClient.get<{ result: RecipeDetailMat[] }>(
|
|
|
|
|
environment.api + '/recipes/' + productCode + '/mat',
|
|
|
|
|
{
|
|
|
|
|
params: {
|
|
|
|
|
filename: this.getCurrentFile(),
|
2024-01-19 14:59:21 +07:00
|
|
|
country: asyncCountry,
|
2023-12-06 10:05:16 +07:00
|
|
|
},
|
|
|
|
|
withCredentials: true,
|
|
|
|
|
responseType: 'json',
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getCurrentFile(): string {
|
2024-03-04 11:19:11 +07:00
|
|
|
// get default from server
|
2024-01-17 17:38:23 +07:00
|
|
|
|
2023-12-06 10:05:16 +07:00
|
|
|
const currentRecipeFile = localStorage.getItem('currentRecipeFile');
|
|
|
|
|
if (currentRecipeFile) {
|
|
|
|
|
return currentRecipeFile;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-17 17:38:23 +07:00
|
|
|
return 'default';
|
2023-12-06 10:05:16 +07:00
|
|
|
}
|
|
|
|
|
|
2023-12-27 08:38:14 +07:00
|
|
|
setCurrentFile(filename: string) {
|
|
|
|
|
localStorage.setItem('currentRecipeFile', filename);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 16:59:06 +07:00
|
|
|
async getCurrentCountry(department?: string): Promise<string> {
|
2024-02-22 16:04:34 +07:00
|
|
|
if (department) {
|
2024-01-17 17:38:23 +07:00
|
|
|
// translate back to full name
|
|
|
|
|
let fullname = getCountryMapSwitcher(department);
|
|
|
|
|
|
|
|
|
|
console.log('fullname: ', fullname);
|
|
|
|
|
|
|
|
|
|
// localStorage.setItem('currentRecipeCountry', fullname);
|
2024-01-18 16:59:06 +07:00
|
|
|
|
|
|
|
|
await AsyncStorage.setItem('currentRecipeCountry', fullname);
|
|
|
|
|
|
2024-01-17 17:38:23 +07:00
|
|
|
return fullname;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 16:59:06 +07:00
|
|
|
// const currentRecipeCountry = localStorage.getItem('currentRecipeCountry');
|
|
|
|
|
|
2024-02-22 16:04:34 +07:00
|
|
|
const currentRecipeCountry = await AsyncStorage.getItem<string>(
|
|
|
|
|
'currentRecipeCountry'
|
|
|
|
|
);
|
2023-12-06 10:05:16 +07:00
|
|
|
if (currentRecipeCountry) {
|
|
|
|
|
return currentRecipeCountry;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 'Thailand';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getRecipesById(id: string): Observable<{
|
|
|
|
|
recipe: Recipe01;
|
|
|
|
|
recipeMetaData: RecipeMetaData;
|
|
|
|
|
}> {
|
|
|
|
|
return this._httpClient.get<{
|
|
|
|
|
recipe: Recipe01;
|
|
|
|
|
recipeMetaData: RecipeMetaData;
|
|
|
|
|
}>(environment.api + '/recipes/' + id, {
|
|
|
|
|
withCredentials: true,
|
|
|
|
|
responseType: 'json',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getRecipeCountries(): Observable<string[]> {
|
|
|
|
|
return this._httpClient
|
2023-12-07 11:09:20 +07:00
|
|
|
.get<string[]>(environment.api + '/recipes/countries', {
|
2023-12-06 10:05:16 +07:00
|
|
|
withCredentials: true,
|
|
|
|
|
responseType: 'json',
|
|
|
|
|
})
|
|
|
|
|
.pipe(tap((countries) => (this.countries = countries)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getRecipeFiles(country: string): Observable<string[]> {
|
|
|
|
|
return this._httpClient
|
2023-12-07 11:09:20 +07:00
|
|
|
.get<string[]>(environment.api + '/recipes/' + country + '/versions', {
|
2023-12-06 10:05:16 +07:00
|
|
|
withCredentials: true,
|
|
|
|
|
responseType: 'json',
|
|
|
|
|
})
|
|
|
|
|
.pipe(tap((files) => (this.recipeFiles[country] = files)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getRecipeFileCountries(): string[] {
|
|
|
|
|
return this.countries;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getRecipeFileNames(country: string): string[] | null {
|
|
|
|
|
return this.recipeFiles[country] ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
editChanges(country: string, filename: string, change: any) {
|
|
|
|
|
console.log('target version = ', filename);
|
|
|
|
|
console.log('change in edit: ', change);
|
|
|
|
|
return this._httpClient
|
|
|
|
|
.post<{
|
|
|
|
|
status: string;
|
|
|
|
|
}>(
|
|
|
|
|
environment.api + ('/recipes/edit/' + country + '/' + filename),
|
|
|
|
|
JSON.stringify(change),
|
|
|
|
|
{
|
|
|
|
|
withCredentials: true,
|
|
|
|
|
responseType: 'json',
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
.subscribe({
|
|
|
|
|
next(value) {
|
2024-01-15 11:48:25 +07:00
|
|
|
console.log(value);
|
2023-12-06 10:05:16 +07:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-11 16:59:37 +07:00
|
|
|
upgradeRecipe(country: string, filename: string, ctx: any) {
|
2024-03-04 11:19:11 +07:00
|
|
|
return this._httpClient.post<any>(
|
|
|
|
|
environment.api + ('/recipes/upgrade/' + country + '/' + filename),
|
|
|
|
|
ctx,
|
|
|
|
|
{
|
|
|
|
|
withCredentials: true,
|
|
|
|
|
responseType: 'json',
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-15 11:48:25 +07:00
|
|
|
getSavedTmp(country: string, filename: string) {
|
|
|
|
|
console.log('loading saved .tmp* file', country, filename);
|
2023-12-06 10:05:16 +07:00
|
|
|
|
|
|
|
|
// do split filename
|
|
|
|
|
filename = filename.split('_')[1];
|
|
|
|
|
|
|
|
|
|
// this._user.getCurrentUser().subscribe((user) => {
|
|
|
|
|
// this.user = user.user;
|
|
|
|
|
// })
|
2024-01-15 11:48:25 +07:00
|
|
|
return this._httpClient.get<string[]>(
|
|
|
|
|
environment.api + ('/recipes/saved/' + country + '/' + filename),
|
|
|
|
|
{
|
|
|
|
|
withCredentials: true,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
// .subscribe({
|
|
|
|
|
// next(value) {
|
|
|
|
|
// console.log( value);
|
|
|
|
|
// },
|
|
|
|
|
// });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getSubMenus(country: string, filename: string, productCode: string) {
|
2024-01-19 14:59:21 +07:00
|
|
|
console.log('getSubMenus', country, filename, productCode);
|
2024-01-15 11:48:25 +07:00
|
|
|
return this._httpClient.get<Recipe01[]>(
|
|
|
|
|
environment.api +
|
|
|
|
|
'/recipes/' +
|
|
|
|
|
country +
|
|
|
|
|
'/' +
|
|
|
|
|
filename +
|
|
|
|
|
'/' +
|
|
|
|
|
productCode +
|
|
|
|
|
'/submenus',
|
|
|
|
|
{
|
|
|
|
|
withCredentials: true,
|
|
|
|
|
responseType: 'json',
|
|
|
|
|
}
|
|
|
|
|
);
|
2023-12-06 10:05:16 +07:00
|
|
|
}
|
2024-01-21 15:18:33 +07:00
|
|
|
|
2024-02-22 16:04:34 +07:00
|
|
|
async getRawRecipeOfProductCode(
|
|
|
|
|
country: string,
|
|
|
|
|
filename: string,
|
|
|
|
|
productCode: string
|
|
|
|
|
): Promise<Observable<{}>> {
|
2024-01-21 15:18:33 +07:00
|
|
|
return this._httpClient.get<{}>(
|
2024-02-22 16:04:34 +07:00
|
|
|
environment.api +
|
|
|
|
|
'/recipes/' +
|
|
|
|
|
country +
|
|
|
|
|
'/' +
|
|
|
|
|
filename +
|
|
|
|
|
'/' +
|
|
|
|
|
productCode +
|
|
|
|
|
'/raw_full',
|
2024-01-21 15:18:33 +07:00
|
|
|
{
|
|
|
|
|
withCredentials: true,
|
|
|
|
|
responseType: 'json',
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-02-22 16:04:34 +07:00
|
|
|
|
2024-03-01 14:33:48 +07:00
|
|
|
getPatchListOfCurrentFile(
|
2024-02-22 16:04:34 +07:00
|
|
|
country: string,
|
|
|
|
|
filename: string
|
2024-03-01 14:33:48 +07:00
|
|
|
): Observable<any> {
|
2024-04-11 16:59:37 +07:00
|
|
|
console.log('try get patches', country, filename);
|
2024-02-22 16:04:34 +07:00
|
|
|
|
|
|
|
|
return this._httpClient.get<any>(
|
2024-04-11 16:59:37 +07:00
|
|
|
environment.api + '/recipes/patch/get/' + country + '/' + filename,
|
2024-02-22 16:04:34 +07:00
|
|
|
{ withCredentials: true, responseType: 'json' }
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-03-01 00:22:28 +07:00
|
|
|
|
|
|
|
|
async sortRecipe(
|
|
|
|
|
country: string,
|
|
|
|
|
filename: string,
|
|
|
|
|
sortKey: string,
|
2024-03-18 11:27:57 +07:00
|
|
|
ascending: boolean
|
2024-04-11 16:59:37 +07:00
|
|
|
): Promise<Observable<any>> {
|
|
|
|
|
return this._httpClient.post<any>(
|
|
|
|
|
environment.api + '/recipes/sort/' + country + '/' + filename,
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
sortKey: sortKey,
|
|
|
|
|
ascending: ascending,
|
|
|
|
|
}),
|
|
|
|
|
{ withCredentials: true, responseType: 'json' }
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-12-06 10:05:16 +07:00
|
|
|
}
|