2026-03-06 16:08:19 +03:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
import httpx
|
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
|
|
|
|
|
EVOTOR_API_BASE = "https://api.evotor.ru"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def fetch_stores(access_token: str) -> list[dict]:
|
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
|
resp = await client.get(
|
|
|
|
|
f"{EVOTOR_API_BASE}/stores",
|
|
|
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
|
|
|
timeout=15,
|
|
|
|
|
)
|
|
|
|
|
resp.raise_for_status()
|
|
|
|
|
data = resp.json()
|
|
|
|
|
items = data.get("items", data) if isinstance(data, dict) else data
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
"id": s.get("uuid") or s.get("id"),
|
|
|
|
|
"name": s.get("name"),
|
|
|
|
|
"address": s.get("address"),
|
|
|
|
|
}
|
|
|
|
|
for s in items
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def fetch_groups(access_token: str, store_id: str) -> list[dict]:
|
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
|
resp = await client.get(
|
|
|
|
|
f"{EVOTOR_API_BASE}/stores/{store_id}/product-groups",
|
|
|
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
|
|
|
timeout=15,
|
|
|
|
|
)
|
2026-03-09 18:04:13 +03:00
|
|
|
if resp.status_code == 402:
|
|
|
|
|
return []
|
2026-03-06 16:08:19 +03:00
|
|
|
resp.raise_for_status()
|
|
|
|
|
data = resp.json()
|
|
|
|
|
items = data.get("items", data) if isinstance(data, dict) else data
|
|
|
|
|
return [{"id": g.get("uuid") or g.get("id"), "name": g.get("name")} for g in items]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def fetch_products(access_token: str, store_id: str) -> list[dict]:
|
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
|
resp = await client.get(
|
|
|
|
|
f"{EVOTOR_API_BASE}/stores/{store_id}/products",
|
|
|
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
|
|
|
timeout=15,
|
|
|
|
|
)
|
2026-03-09 18:04:13 +03:00
|
|
|
if resp.status_code == 402:
|
|
|
|
|
return []
|
2026-03-06 16:08:19 +03:00
|
|
|
resp.raise_for_status()
|
|
|
|
|
data = resp.json()
|
|
|
|
|
items = data.get("items", data) if isinstance(data, dict) else data
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
"id": p.get("uuid") or p.get("id"),
|
|
|
|
|
"name": p.get("name"),
|
|
|
|
|
"parent_id": p.get("parentUuid") or p.get("parent_id"),
|
|
|
|
|
"price": p.get("price"),
|
|
|
|
|
"quantity": p.get("quantity"),
|
|
|
|
|
"measure_name": p.get("measureName") or p.get("measure_name"),
|
|
|
|
|
"article_number": p.get("code") or p.get("article_number"),
|
|
|
|
|
"allow_to_sell": p.get("allowToSell") if p.get("allowToSell") is not None else p.get("allow_to_sell"),
|
|
|
|
|
}
|
|
|
|
|
for p in items
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def refresh_catalog_cache(user_id: int, access_token: str, db: Session) -> None:
|
|
|
|
|
from web.models import CachedStore, CachedGroup, CachedProduct
|
|
|
|
|
|
|
|
|
|
now = datetime.utcnow()
|
|
|
|
|
|
|
|
|
|
# Delete old cache for user
|
|
|
|
|
db.query(CachedProduct).filter(CachedProduct.user_id == user_id).delete()
|
|
|
|
|
db.query(CachedGroup).filter(CachedGroup.user_id == user_id).delete()
|
|
|
|
|
db.query(CachedStore).filter(CachedStore.user_id == user_id).delete()
|
|
|
|
|
db.commit()
|
|
|
|
|
|
|
|
|
|
stores = await fetch_stores(access_token)
|
|
|
|
|
for store in stores:
|
|
|
|
|
db.add(CachedStore(
|
|
|
|
|
user_id=user_id,
|
|
|
|
|
evotor_id=store["id"],
|
|
|
|
|
name=store["name"] or "",
|
|
|
|
|
address=store.get("address"),
|
|
|
|
|
fetched_at=now,
|
|
|
|
|
))
|
|
|
|
|
db.commit()
|
|
|
|
|
|
|
|
|
|
for store in stores:
|
|
|
|
|
groups = await fetch_groups(access_token, store["id"])
|
|
|
|
|
for group in groups:
|
|
|
|
|
db.add(CachedGroup(
|
|
|
|
|
user_id=user_id,
|
|
|
|
|
evotor_id=group["id"],
|
|
|
|
|
store_evotor_id=store["id"],
|
|
|
|
|
name=group["name"] or "",
|
|
|
|
|
fetched_at=now,
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
products = await fetch_products(access_token, store["id"])
|
|
|
|
|
for product in products:
|
|
|
|
|
db.add(CachedProduct(
|
|
|
|
|
user_id=user_id,
|
|
|
|
|
evotor_id=product["id"],
|
|
|
|
|
store_evotor_id=store["id"],
|
|
|
|
|
group_evotor_id=product.get("parent_id"),
|
|
|
|
|
name=product["name"] or "",
|
|
|
|
|
price=product.get("price"),
|
|
|
|
|
quantity=product.get("quantity"),
|
|
|
|
|
measure_name=product.get("measure_name"),
|
|
|
|
|
article_number=product.get("article_number"),
|
|
|
|
|
allow_to_sell=product.get("allow_to_sell"),
|
|
|
|
|
fetched_at=now,
|
|
|
|
|
))
|
|
|
|
|
db.commit()
|