change: wip testing price from sheet

Signed-off-by: pakintada@gmail.com <Pakin>
This commit is contained in:
pakintada@gmail.com 2026-05-26 17:08:44 +07:00
parent 3b70cc9fe8
commit 07977ce896
9 changed files with 268 additions and 18 deletions

View file

@ -2,6 +2,7 @@ import { get, writable } from 'svelte/store';
import { addNotification, notiStore } from '../stores/noti';
import {
currentRecipeVersionsSelector,
lastRequestSheetPrice,
materialFromServerQuery,
priceRecipeData,
recipeData,
@ -9,6 +10,8 @@ import {
recipeLoading,
recipeOverviewData,
recipeStreamMeta,
streamingRawData,
streamingRawMeta,
toppingGroupFromServerQuery,
toppingListFromServerQuery
} from '../stores/recipeStore';
@ -18,13 +21,16 @@ import { type RecipeVersion } from '$lib/models/recipe_version.model';
import { goto } from '$app/navigation';
import { socketAlreadySendHeartbeat, socketConnectionOfflineCount } from '../stores/websocketStore';
import type { RecipePrice } from '$lib/models/price.model';
import { sendMessage } from './ws_messageSender';
import { sendCommandRequest, sendMessage } from './ws_messageSender';
import { auth as authStore } from '../stores/auth';
import { v4 as uuidv4 } from 'uuid';
import { handleSheetResponseFromNoti } from './sheetNotiHandler';
export const messages = writable<string[]>([]);
type WSMessage = { type: string; payload: any };
// MAXIMUM LIMIT = 1814355
const handlers: Record<string, (payload: any) => void> = {
chat: (p) => messages.update((m) => [...m, p]),
ping: (p) => console.log('ping from server'),
@ -206,6 +212,9 @@ const handlers: Record<string, (payload: any) => void> = {
let msg = p.msg ?? `Notify from ${p.from}`;
let target = p.to;
let from_service = p.from ?? '';
let ref_service = p.ref ?? '';
if (target) {
//
let currentUsername = auth.currentUser?.displayName;
@ -249,11 +258,91 @@ const handlers: Record<string, (payload: any) => void> = {
console.log('get price length: ', content.length);
let current_price = get(priceRecipeData);
let lastRequestPriceInstance = get(lastRequestSheetPrice);
let saved_product_code_to_get_from_sheet = [];
let current_meta = get(recipeStreamMeta);
lastRequestPriceInstance[current_meta?.country ?? 'unknown'] = {};
for (const c of content) {
current_price[c.ProductCode] = c.NewPrice + (c.StringParam ? `,${c.StringParam}` : '');
lastRequestPriceInstance[current_meta?.country ?? 'unknown'][c.ProductCode] = '';
saved_product_code_to_get_from_sheet.push({
product_code: c.ProductCode
});
}
priceRecipeData.set(current_price);
console.log('check length', saved_product_code_to_get_from_sheet.length);
// set command request to stream mode so
let request_id = uuidv4();
lastRequestPriceInstance[request_id] = current_meta?.country ?? '';
let current_streaming_instance = get(streamingRawData);
current_streaming_instance[request_id] = '';
streamingRawData.set(current_streaming_instance);
sendCommandRequest('sheet', {
country: current_meta?.country ?? '',
content: saved_product_code_to_get_from_sheet,
param: 'price',
stream: true,
request_id
});
lastRequestSheetPrice.set(lastRequestPriceInstance);
},
raw_stream: (p) => {
let streamRawInstance = get(streamingRawData);
let sub_type = p.sub_type;
let request_id = p.request_id;
let size_per_chunk = p.size_per_chunk;
let total_chunks = p.total_chunks;
let idx = p.idx;
switch (sub_type) {
case 'price':
streamingRawMeta.set({
id: request_id,
total_size: total_chunks,
chunk_size: size_per_chunk,
progress: 0
});
break;
case 'chunk_price':
streamingRawMeta.set({
id: request_id,
total_size: total_chunks,
chunk_size: size_per_chunk,
progress: idx
});
let raw_payload = p.raw ?? '';
streamRawInstance[request_id] += raw_payload;
streamingRawData.set(streamRawInstance);
break;
case 'end_price':
let lastRequestPriceInstance = get(lastRequestSheetPrice);
let country = lastRequestPriceInstance[request_id];
try {
let raw_payload = JSON.parse(streamRawInstance[request_id]);
let ref_from_raw = raw_payload.ref ?? '';
let from_service_raw = raw_payload.from ?? '';
let parsed_payload = raw_payload.payload ?? '';
if (from_service_raw == 'sheet-service') {
handleSheetResponseFromNoti(parsed_payload, ref_from_raw, country);
delete streamRawInstance[request_id];
streamingRawData.set(streamRawInstance);
}
} catch (e) {
console.log(`end price process error: ${e}`);
}
break;
default:
}
},
heartbeat: (p) => {
socketConnectionOfflineCount.set(0);
@ -270,5 +359,14 @@ export function handleIncomingMessages(raw: string) {
addNotification('ERR:No response from server');
return;
}
// raw streaming type
if (msg.type.startsWith('raw_stream')) {
// convert
let sub_type = msg.type.replace('raw_stream_', '');
msg.payload.sub_type = sub_type;
msg.type = 'raw_stream';
}
handlers[msg.type]?.(msg.payload);
}