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'; 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 []; } // 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([]); await sendMessage({ 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 []; }