274 lines
6.5 KiB
TypeScript
274 lines
6.5 KiB
TypeScript
import { sendCommandRequest, sendMessage } from '../handlers/ws_messageSender';
|
|
import { get } from 'svelte/store';
|
|
import { auth } from '../stores/auth';
|
|
import {
|
|
productCodesLoading,
|
|
hasSheetPriceBeenSent,
|
|
markSheetPriceAsSent,
|
|
sheetPriceLoading,
|
|
streamingRawData,
|
|
setPendingProductCodesCountry
|
|
} from '../stores/sheetStore';
|
|
import { setGenLayoutGenerating } from '../stores/genLayoutStore';
|
|
|
|
export function requestCatalogs(country: string): boolean {
|
|
return sendCommandRequest('sheet', {
|
|
country: country,
|
|
param: 'catalogs'
|
|
});
|
|
}
|
|
|
|
export function requestPriceSlots(country: string): boolean {
|
|
return sendCommandRequest('sheet', {
|
|
country: country,
|
|
param: 'priceslot'
|
|
});
|
|
}
|
|
|
|
export function updatePriceSlot(
|
|
country: string,
|
|
content: {
|
|
slot: number;
|
|
name: string;
|
|
description: string;
|
|
products: { product_code: string; price: number | null; row_index?: number }[];
|
|
}
|
|
): boolean {
|
|
return sendCommandRequest('sheet', {
|
|
country: country,
|
|
content: content,
|
|
param: 'update/priceslot'
|
|
});
|
|
}
|
|
|
|
export function enterRoom(country: string, catalog: string): boolean {
|
|
return sendCommandRequest('sheet', {
|
|
country: country,
|
|
catalog: catalog,
|
|
param: 'enter'
|
|
});
|
|
}
|
|
|
|
export function sendHeartbeat(country: string, catalog: string): boolean {
|
|
return sendCommandRequest('sheet', {
|
|
country: country,
|
|
catalog: catalog,
|
|
param: 'heartbeat'
|
|
});
|
|
}
|
|
|
|
export function exitRoom(country: string, catalog: string): boolean {
|
|
return sendCommandRequest('sheet', {
|
|
country: country,
|
|
catalog: catalog,
|
|
param: 'exit'
|
|
});
|
|
}
|
|
|
|
export function requestCatalogMenu(country: string, catalog: string): boolean {
|
|
return sendCommandRequest('sheet', {
|
|
country: country,
|
|
catalog: catalog,
|
|
param: 'catalog/menu'
|
|
});
|
|
}
|
|
|
|
export function updateMenu(country: string, catalog: string, content: any[]): boolean {
|
|
return sendCommandRequest('sheet', {
|
|
country: country,
|
|
catalog: catalog,
|
|
content: content,
|
|
param: 'update/menu'
|
|
});
|
|
}
|
|
|
|
export function addMenu(country: string, catalog: string, content: any[]): boolean {
|
|
console.log('[sheetService] Adding menu:', { country, catalog, content });
|
|
const sent = sendCommandRequest('sheet', {
|
|
country: country,
|
|
catalog: catalog,
|
|
content: content,
|
|
param: 'add/menu'
|
|
});
|
|
console.log('[sheetService] Add menu sent:', sent);
|
|
return sent;
|
|
}
|
|
|
|
export function deleteMenu(country: string, catalog: string, targetIds: number[]): boolean {
|
|
const content = targetIds.map((id) => ({ target_id: id }));
|
|
return sendCommandRequest('sheet', {
|
|
country: country,
|
|
catalog: catalog,
|
|
content: content,
|
|
param: 'delete/menu'
|
|
});
|
|
}
|
|
|
|
export function swapMenu(
|
|
country: string,
|
|
catalog: string,
|
|
swaps: { source_id: number; target_id: number }[]
|
|
): boolean {
|
|
return sendCommandRequest('sheet', {
|
|
country: country,
|
|
catalog: catalog,
|
|
content: swaps,
|
|
param: 'swap/menu'
|
|
});
|
|
}
|
|
|
|
export function requestListMenu(country: string, boxid?: string): boolean {
|
|
const curr_user = get(auth);
|
|
|
|
let user_info: any = {};
|
|
if (curr_user) {
|
|
user_info = {
|
|
displayName: curr_user.displayName,
|
|
email: curr_user.email,
|
|
uid: curr_user.uid
|
|
};
|
|
}
|
|
|
|
productCodesLoading.set(true);
|
|
setPendingProductCodesCountry(country);
|
|
|
|
console.log('[sheetService] Sending list_menu request for country:', country, 'boxid:', boxid);
|
|
|
|
return sendMessage({
|
|
type: 'list_menu',
|
|
payload: {
|
|
user_info,
|
|
country,
|
|
boxid: boxid || undefined
|
|
}
|
|
});
|
|
}
|
|
|
|
export function requestGenLayout(country: string): boolean {
|
|
const curr_user = get(auth);
|
|
|
|
let user_info: any = {};
|
|
if (curr_user) {
|
|
user_info = {
|
|
displayName: curr_user.displayName,
|
|
email: curr_user.email,
|
|
uid: curr_user.uid
|
|
};
|
|
}
|
|
|
|
setGenLayoutGenerating();
|
|
|
|
console.log('[sheetService] Sending gen-layout request for country:', country);
|
|
|
|
return sendMessage({
|
|
type: 'command',
|
|
payload: {
|
|
user_info,
|
|
srv_name: 'gen-service',
|
|
values: {
|
|
file_layout: 'sheet',
|
|
file_desc: 'sheet',
|
|
country: country,
|
|
param: 'new-inter-v3-multi-promotion-other_catalog-supra_app'
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Request price data from sheet for specific product codes
|
|
* NOTE: Can only send once per type (price). Use hasSheetPriceBeenSent to check.
|
|
*/
|
|
export function requestSheetPrice(country: string, productCodes: string[]): boolean {
|
|
// Check if already sent
|
|
if (hasSheetPriceBeenSent('price')) {
|
|
console.warn('[sheetService] Price request already sent, skipping');
|
|
return false;
|
|
}
|
|
|
|
if (!productCodes || productCodes.length === 0) {
|
|
console.warn('[sheetService] No product codes to request price for');
|
|
return false;
|
|
}
|
|
|
|
// Generate request_id (UUID v4)
|
|
const request_id = crypto.randomUUID();
|
|
|
|
// Store request_id and country in streamingRawData for tracking
|
|
streamingRawData.update((data) => ({
|
|
...data,
|
|
price: {
|
|
request_id,
|
|
country,
|
|
chunks: [],
|
|
rawParts: []
|
|
}
|
|
}));
|
|
|
|
sheetPriceLoading.set(true);
|
|
|
|
// Convert to array of objects (backend expects objects, not strings)
|
|
const content = productCodes.map((code) => ({ product_code: code }));
|
|
|
|
console.log('[sheetService] Sending sheet price request for country:', country, 'codes:', productCodes.length, 'request_id:', request_id);
|
|
|
|
const sent = sendCommandRequest('sheet', {
|
|
country: country,
|
|
content: content,
|
|
param: 'price',
|
|
stream: true,
|
|
request_id
|
|
});
|
|
|
|
if (sent) {
|
|
markSheetPriceAsSent('price');
|
|
} else {
|
|
sheetPriceLoading.set(false);
|
|
}
|
|
|
|
return sent;
|
|
}
|
|
|
|
/**
|
|
* Update price data in sheet
|
|
* content: [{ row_index: number, cells: [{ value: string, coord: { row: number, col: number } }] }]
|
|
*/
|
|
export function updateSheetPrice(
|
|
country: string,
|
|
content: { row_index: number; cells: { value: string; coord: { row: number; col: number } }[] }[]
|
|
): boolean {
|
|
if (!content || content.length === 0) {
|
|
console.warn('[sheetService] No content to update');
|
|
return false;
|
|
}
|
|
|
|
console.log('[sheetService] Updating sheet price for country:', country, 'items:', content.length);
|
|
|
|
return sendCommandRequest('sheet', {
|
|
country: country,
|
|
content: content,
|
|
param: 'update/price'
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Add new price rows to sheet (for product codes that don't exist in price sheet)
|
|
* content: [{ cells: [product_code, name_en, name_th, ..., price, ...] }]
|
|
*/
|
|
export function addSheetPrice(
|
|
country: string,
|
|
content: { cells: string[] }[]
|
|
): boolean {
|
|
if (!content || content.length === 0) {
|
|
console.warn('[sheetService] No content to add');
|
|
return false;
|
|
}
|
|
|
|
console.log('[sheetService] Adding price rows for country:', country, 'items:', content.length, content);
|
|
|
|
return sendCommandRequest('sheet', {
|
|
country: country,
|
|
content: content,
|
|
param: 'add/price'
|
|
});
|
|
}
|