Supra_App/src/lib/core/client/server.ts

98 lines
2.5 KiB
TypeScript
Raw Normal View History

2026-02-17 14:30:02 +07:00
import { get } from 'svelte/store';
import { departmentStore } from '../stores/departments';
import { sendMessage } from '../handlers/ws_messageSender';
import { auth } from '../stores/auth';
import { extractCookieOnNonBrowser } from '$lib/helpers/cookie';
import { browser } from '$app/environment';
import { permission } from '../stores/permissions';
import { addNotification } from '../stores/noti';
import { recipeData, recipeOverviewData } from '../stores/recipeStore';
2026-02-17 14:30:02 +07:00
export async function getRecipes() {
if (browser && !get(departmentStore)) {
console.log('cannot get dep', get(departmentStore));
return [];
}
let countryTarget = get(departmentStore);
let country = '';
let countryPerm = `document.read.${countryTarget}`;
let user_perms = get(permission);
if (!user_perms.includes(countryPerm)) {
addNotification('ERR:Invalid permission');
return [];
}
2026-02-17 14:30:02 +07:00
// if (!countryTarget && !browser) {
// countryTarget = extractCookieOnNonBrowser()['department'];
// }
// construct path. fetch (GET) {server}/recipe/{countryTarget}/{version}
let idToken = await get(auth)?.getIdToken();
console.log('country target get recipe', countryTarget);
recipeData.set([]);
recipeOverviewData.set([]);
2026-02-17 14:30:02 +07:00
await sendMessage({
2026-02-17 14:30:02 +07:00
type: 'recipe',
payload: {
auth: idToken ?? '',
partial: false,
country: countryTarget ?? '',
version: -1,
parameters: ''
}
});
return [];
}
export async function getRecipeWithVersion(version: string) {
if (browser && !get(departmentStore)) {
console.log('cannot get dep', get(departmentStore));
return [];
}
let countryTarget = get(departmentStore);
let country = '';
let countryPerm = `document.read.${countryTarget}`;
let user_perms = get(permission);
if (!user_perms.includes(countryPerm)) {
addNotification('ERR:Invalid permission');
return [];
}
// if (!countryTarget && !browser) {
// countryTarget = extractCookieOnNonBrowser()['department'];
// }
// construct path. fetch (GET) {server}/recipe/{countryTarget}/{version}
let idToken = await get(auth)?.getIdToken();
console.log('country target get recipe', countryTarget);
recipeData.set([]);
recipeOverviewData.set([]);
// NOTE: although version is provided, actual version field is still need to be latest
// Just in case version is not found
await sendMessage({
type: 'recipe',
payload: {
auth: idToken ?? '',
partial: false,
country: countryTarget ?? '',
version: -1,
parameters: 'use_legacy_version=true,version=' + version
}
});
return [];
}