[sheet-service] updated price slot option

This commit is contained in:
Ittipat Lusuk 2026-06-18 14:49:44 +07:00
parent 79a5630c7e
commit 77fe041792
2 changed files with 94 additions and 48 deletions

View file

@ -76,7 +76,18 @@ PUBLISH sheet-service/swap/menu '{"type": "sheet","payload": {"user_info": {"uid
### Get price payload by productCode
```
PUBLISH sheet-service/price '{"type": "sheet","payload": {"user_info": {"uid": "xxxxxxxxxxxxxxxxxxxxxxxxxx"},"srv_name": "sheet-service","values": {"country": "tha","content": [{"product_code": "12-99-02-99"}, {"product_code": "12-01-01-0009"}]}}}'
PUBLISH sheet-service/price '{"type": "sheet","payload": {"user_info": {"uid": "xxxxxxxxxxxxxxxxxxxxxxxxxx"},"srv_name": "sheet-service","values": {"country": "tha","option": "All","content": [{"product_code": "12-99-02-99"}, {"product_code": "12-01-01-0009"}]}}}'
```
### Get price slot list available
```
PUBLISH sheet-service/price '{"type": "sheet","payload": {"user_info": {"uid": "xxxxxxxxxxxxxxxxxxxxxxxxxx"},"srv_name": "sheet-service","values": {"country": "tha","option": "PriceSlot"}}}'
```
### Get price slot payload by sheet name Ex. PriceSlot5
```
PUBLISH sheet-service/price '{"type": "sheet","payload": {"user_info": {"uid": "xxxxxxxxxxxxxxxxxxxxxxxxxx"},"srv_name": "sheet-service","values": {"country": "tha","option": "PriceSlot5"}}}'
```
## PUBLISH sheet-service/add/price

129
main.py
View file

@ -524,10 +524,14 @@ def redis_message_handler():
elif channel == GET_PRICE_CHANNEL:
try:
if (option and option == "PriceSlot"):
handle_get_price_slot(country, user_id)
else:
if not (content) and option != "All":
if not (option):
raise Exception (f"[{SERVICE_NAME}] Missing required parameters | Channel: {channel}")
if option.startswith("PriceSlot"):
handle_get_price_slot(country, user_id, option)
elif (option == "All"):
if not (content):
raise Exception (f"[{SERVICE_NAME}] Missing required parameters | Channel: {channel}")
handle_get_price(country, user_id, content)
@ -1851,9 +1855,7 @@ def handle_get_price(country: str, user_id: str, content: list):
raise Exception(f"[{SERVICE_NAME}] Get price error: {e}")
def handle_get_price_slot(country: str, user_id: str):
batch_id = str(uuid.uuid4())
def handle_get_price_slot(country: str, user_id: str, option: str = ""):
try:
config = COUNTRY_MAPPING.get(country)
if not config:
@ -1863,41 +1865,27 @@ def handle_get_price_slot(country: str, user_id: str):
doc_price = grist_docs.get("price")
if not doc_price:
raise Exception(f"Price doc not found for country {country}")
raise Exception(f"[{SERVICE_NAME}] Price doc not found for country {country}")
# 1. ดึงรายชื่อ Table ทั้งหมดใน Document Price
all_tables = get_all_grist_tables(doc_price)
# 2. ค้นหา Table ที่เข้าเงื่อนไข PriceSlot[index]
price_slot_pattern = re.compile(r"^PriceSlot[1-9]\d*$")
matching_tables = []
for t_id in all_tables:
table_name = reconstruct_table_name(t_id)
if price_slot_pattern.match(table_name) or price_slot_pattern.match(t_id):
matching_tables.append({
"id": t_id,
"name": table_name if price_slot_pattern.match(table_name) else t_id
})
result_content = None
total_tables = len(matching_tables)
send_stream_notification(
"start",
{"message": f"Start fetching {total_tables} price slots"},
batch_id, 0, total_tables, total_tables, user_id, "price"
)
if total_tables == 0:
send_stream_notification("end", {"message": "No PriceSlot tables found"}, batch_id, 0, 0, 0, user_id, "price")
return
headers_grist = {"Authorization": f"Bearer {GRIST_API_KEY}"}
for current_idx, table_info in enumerate(matching_tables, start=1):
t_id = table_info["id"]
t_name = table_info["name"]
if option == "PriceSlot":
all_tables = get_all_grist_tables(doc_price)
price_slot_pattern = re.compile(r"^PriceSlot[1-9]\d*$")
table_list = []
for t_id in all_tables:
table_name = reconstruct_table_name(t_id)
if price_slot_pattern.match(table_name) or price_slot_pattern.match(t_id):
table_list.append(table_name if price_slot_pattern.match(table_name) else t_id)
result_content = {"sheet": table_list}
elif option.startswith("PriceSlot"):
t_id = option
t_name = option
headers_grist = {"Authorization": f"Bearer {GRIST_API_KEY}"}
meta_url = f"{GRIST_URL.rstrip('/')}/api/docs/{doc_price}/tables/{t_id}/columns"
resp_meta = request_with_retry("GET", meta_url, headers=headers_grist)
@ -1929,23 +1917,70 @@ def handle_get_price_slot(country: str, user_id: str):
"cells": cells
})
chunk_data = [{
result_content = [{
"sheet": t_name,
"header": header,
"payload": payload
}]
send_stream_notification("chunk", chunk_data, batch_id, current_idx, total_tables, total_tables, user_id, "price")
else:
raise Exception(f"[{SERVICE_NAME}] PriceSlot Invalid option: {option}")
send_stream_notification(
"end",
{"message": "All price slots sent successfully"},
batch_id, total_tables, total_tables, total_tables, user_id, "price"
)
if FRONTEND_NOTIFY_URL:
notify_payload = {
"type": "notify",
"payload": {
"from": SERVICE_NAME,
"level": "content",
"to": user_id,
"ref": "price",
"content": result_content
}
}
res = request_with_retry(
"POST",
FRONTEND_NOTIFY_URL,
headers=None,
json=notify_payload,
)
if res.status_code == 200:
print(f"[{SERVICE_NAME}] Price slot data sent to user: {user_id} | Option: {option}")
else:
error_detail = res.text
try:
error_detail = res.json()
except:
pass
print(f"[{SERVICE_NAME}] Notify server response: {res.status_code} | {error_detail}")
return result_content
except Exception as e:
print(f"[{SERVICE_NAME}] Get price slot error: {e}")
traceback.print_exc()
send_stream_notification("error", {"error_detail": str(e)}, batch_id, 0, 0, 0, user_id, "price")
# กรณีพัง ให้ยิง Error กลับไปบอก Frontend ด้วย
if FRONTEND_NOTIFY_URL:
error_payload = {
"type": "notify",
"payload": {
"from": SERVICE_NAME,
"level": "content",
"to": user_id,
"ref": "price",
"content": {"error": str(e)}
}
}
request_with_retry(
"POST",
FRONTEND_NOTIFY_URL,
headers=None,
json=error_payload,
)
raise Exception(f"[{SERVICE_NAME}] Get price slot error: {e}")
def handle_add_price(country: str, content: list, option: str = ""):