Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from app.routers import ga4_router
from app.routers import intercom_router
from app.routers import github_analytics_router
from app.routers import shopify_router
from app.models import User
from app.auth import hash_password, GUEST_USER_ID, ADMIN_USER_ID

Expand Down Expand Up @@ -253,6 +254,7 @@ async def httpx_connect_error_handler(_request: Request, exc: httpx.ConnectError
app.include_router(ga4_router.router, prefix=prefix)
app.include_router(intercom_router.router, prefix=prefix)
app.include_router(github_analytics_router.router, prefix=prefix)
app.include_router(shopify_router.router, prefix=prefix)
app.include_router(pipedrive_router.router, prefix=prefix)


Expand Down
17 changes: 17 additions & 0 deletions backend/app/routers/ask.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from app.scripts.ask_pipedrive import ask_pipedrive
from app.scripts.ask_ga4 import ask_ga4
from app.scripts.ask_github_analytics import ask_github_analytics
from app.scripts.ask_shopify import ask_shopify
from app.scripts.ask_intercom import ask_intercom
from app.scripts.ask_salesforce import ask_salesforce
from app.scripts.ask_sql import ask_sql
Expand Down Expand Up @@ -584,6 +585,22 @@ async def dispatch_question(
history=history,
channel=channel,
)
elif source.type == "shopify":
meta = source.metadata_ or {}
sh_store = meta.get("store", "")
sh_token = meta.get("accessToken", "")
if not sh_store or not sh_token:
raise HTTPException(400, "Shopify source missing store or accessToken in metadata")
result = await ask_shopify(
store=sh_store,
access_token=sh_token,
question=question,
agent_description=agent.description or "",
source_name=source.name,
llm_overrides=llm_overrides,
history=history,
channel=channel,
)
else:
raise HTTPException(400, f"Unsupported source type: {source.type}")

Expand Down
2 changes: 1 addition & 1 deletion backend/app/routers/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ async def create_source(
user: User = Depends(require_user),
):
"""Create a non-file source (BigQuery, Google Sheets, SQL). Credentials stored locally in metadata."""
valid_types = ("bigquery", "google_sheets", "sql_database", "firebase", "mongodb", "snowflake", "notion", "excel_online", "s3", "rest_api", "jira", "hubspot", "stripe", "pipedrive", "salesforce", "ga4", "intercom", "github_analytics")
valid_types = ("bigquery", "google_sheets", "sql_database", "firebase", "mongodb", "snowflake", "notion", "excel_online", "s3", "rest_api", "jira", "hubspot", "stripe", "pipedrive", "salesforce", "ga4", "intercom", "github_analytics", "shopify")
if body.type not in valid_types:
raise HTTPException(400, f"type must be one of: {', '.join(valid_types)}")
source_id = str(uuid.uuid4())
Expand Down
79 changes: 79 additions & 0 deletions backend/app/routers/shopify_router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
"""
Shopify store discovery and connection testing.
"""
import asyncio
from fastapi import APIRouter, Depends, HTTPException
from app.auth import require_user
from app.models import User, Source
from app.database import get_db
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.routers.crud import _sanitize_for_json

router = APIRouter(prefix="/shopify", tags=["shopify"])


@router.post("/test-connection")
async def test_connection(body: dict, user: User = Depends(require_user)):
"""Test Shopify API connection with the provided store name and access token."""
store = body.get("store", "")
access_token = body.get("accessToken", "")
if not store or not access_token:
raise HTTPException(400, "store and accessToken are required")

from app.scripts.ask_shopify import _test_connection_sync
loop = asyncio.get_event_loop()
try:
result = await loop.run_in_executor(None, lambda: _test_connection_sync(store, access_token))
except Exception as e:
raise HTTPException(400, f"Connection failed: {e}")
return result


@router.post("/discover")
async def discover_objects(body: dict, user: User = Depends(require_user)):
"""Discover available Shopify resources and their counts."""
store = body.get("store", "")
access_token = body.get("accessToken", "")
if not store or not access_token:
raise HTTPException(400, "store and accessToken are required")

from app.scripts.ask_shopify import _discover_objects_sync
loop = asyncio.get_event_loop()
try:
result = await loop.run_in_executor(None, lambda: _discover_objects_sync(store, access_token))
except Exception as e:
raise HTTPException(400, f"Discovery failed: {e}")
return _sanitize_for_json(result)


@router.post("/sources/{source_id}/refresh-metadata")
async def refresh_source_metadata(
source_id: str, db: AsyncSession = Depends(get_db), user: User = Depends(require_user),
):
r = await db.execute(select(Source).where(Source.id == source_id, Source.user_id == user.id))
source = r.scalar_one_or_none()
if not source:
raise HTTPException(404, "Source not found")
if source.type != "shopify":
raise HTTPException(400, "Source is not Shopify")

meta = dict(source.metadata_ or {})
store = meta.get("store", "")
access_token = meta.get("accessToken", "")
if not store or not access_token:
raise HTTPException(400, "Source missing store or accessToken")

from app.scripts.ask_shopify import _discover_objects_sync, REPORT_TEMPLATES
loop = asyncio.get_event_loop()
try:
result = await loop.run_in_executor(None, lambda: _discover_objects_sync(store, access_token))
except Exception as e:
raise HTTPException(400, f"Refresh failed: {e}")

meta["resourceCounts"] = result.get("resourceCounts", {})
meta["report_templates"] = REPORT_TEMPLATES
source.metadata_ = _sanitize_for_json(meta)
await db.commit()
await db.refresh(source)
return {"metaJSON": source.metadata_}
Loading
Loading