From d82be93da2ad4c142ba6f5685392cc386618cc88 Mon Sep 17 00:00:00 2001 From: Kaj Kowalski Date: Wed, 5 Nov 2025 20:43:35 +0100 Subject: [PATCH] feat(data-integration): add Jumbo API setup tools Adds a management command and helper script to configure the Jumbo API external data source and fetch chat sessions. Ensures idempotent creation and linkage to the Jumbo company while reading API credentials from environment variables. Prints data source details and a post-fetch summary to ease setup and verification. --- .../management/commands/setup_jumbo_api.py | 69 +++++++++++++++++ dashboard_project/scripts/setup_jumbo_api.py | 77 +++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 dashboard_project/data_integration/management/commands/setup_jumbo_api.py create mode 100644 dashboard_project/scripts/setup_jumbo_api.py diff --git a/dashboard_project/data_integration/management/commands/setup_jumbo_api.py b/dashboard_project/data_integration/management/commands/setup_jumbo_api.py new file mode 100644 index 0000000..2819eca --- /dev/null +++ b/dashboard_project/data_integration/management/commands/setup_jumbo_api.py @@ -0,0 +1,69 @@ +""" +Management command to set up Jumbo API external data source and fetch data. +""" + +import os + +from accounts.models import Company +from data_integration.models import ChatSession, ExternalDataSource +from django.core.management.base import BaseCommand + + +class Command(BaseCommand): + help = "Set up Jumbo API external data source and fetch chat data" + + def handle(self, **options): # noqa: ARG002 + self.stdout.write("Setting up Jumbo API data source...") + + # Get Jumbo company + try: + jumbo = Company.objects.get(name="Jumbo") + self.stdout.write(f"✓ Found Jumbo company (ID: {jumbo.id})") + except Company.DoesNotExist: + self.stdout.write(self.style.ERROR("✗ Jumbo company not found. Run setup_jumbo command first.")) + return + + # Create or get Jumbo API external data source + source, created = ExternalDataSource.objects.get_or_create( + name="Jumbo API", + defaults={ + "company": jumbo, + "api_url": "https://proto.notso.ai/jumbo/chats", + "auth_username": os.environ.get("EXTERNAL_API_USERNAME", ""), + "auth_password": os.environ.get("EXTERNAL_API_PASSWORD", ""), + "is_active": True, + }, + ) + + # Ensure company is set if already existed + if not created and not source.company: + source.company = jumbo + source.save() + self.stdout.write(self.style.SUCCESS(f"✓ Linked existing Jumbo API source to {jumbo.name}")) + elif created: + self.stdout.write(self.style.SUCCESS("✓ Created Jumbo API external data source")) + else: + self.stdout.write("✓ Jumbo API source already exists") + + self.stdout.write( + f"\nData source details:" + f"\n ID: {source.id}" + f"\n Company: {source.company.name if source.company else 'None'}" + f"\n Endpoint: {source.api_url}" + f"\n Active: {source.is_active}" + ) + + # Fetch data (call the task synchronously) + self.stdout.write("\nFetching Jumbo chat data...") + try: + # Use .apply() or direct function call to run synchronously + from data_integration.utils import fetch_and_store_chat_data + + result = fetch_and_store_chat_data(source.id) + self.stdout.write(self.style.SUCCESS(f"✓ Data fetch completed: {result}")) + except Exception as e: + self.stdout.write(self.style.ERROR(f"✗ Error fetching data: {e}")) + + # Show summary + session_count = ChatSession.objects.filter(company=jumbo).count() + self.stdout.write(self.style.SUCCESS(f"\n✓ Setup complete!\n Total Jumbo chat sessions: {session_count}")) diff --git a/dashboard_project/scripts/setup_jumbo_api.py b/dashboard_project/scripts/setup_jumbo_api.py new file mode 100644 index 0000000..1b03709 --- /dev/null +++ b/dashboard_project/scripts/setup_jumbo_api.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python +""" +Script to create Jumbo API external data source and fetch data. +""" + +import os +import sys + +import django + +# Setup Django +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dashboard_project.settings") +django.setup() + +from accounts.models import Company # noqa: E402 +from data_integration.models import ExternalDataSource # noqa: E402 +from data_integration.tasks import refresh_specific_source # noqa: E402 + + +def main(): + print("Setting up Jumbo API data source...") + + # Get Jumbo company + try: + jumbo = Company.objects.get(name="Jumbo") + print(f"✓ Found Jumbo company (ID: {jumbo.id})") + except Company.DoesNotExist: + print("✗ Jumbo company not found. Run setup_jumbo command first.") + return + + # Create or get Jumbo API external data source + source, created = ExternalDataSource.objects.get_or_create( + name="Jumbo API", + defaults={ + "company": jumbo, + "api_endpoint": "https://mijn.jumbo.com/api/chat/sessions", + "api_username": os.environ.get("EXTERNAL_API_USERNAME", ""), + "api_password": os.environ.get("EXTERNAL_API_PASSWORD", ""), + "is_active": True, + }, + ) + + # Ensure company is set if already existed + if not created and not source.company: + source.company = jumbo + source.save() + print(f"✓ Linked existing Jumbo API source to {jumbo.name}") + elif created: + print("✓ Created Jumbo API external data source") + else: + print("✓ Jumbo API source already exists") + + print("\nData source details:") + print(f" ID: {source.id}") + print(f" Company: {source.company.name if source.company else 'None'}") + print(f" Endpoint: {source.api_endpoint}") + print(f" Active: {source.is_active}") + + # Fetch data + print("\nFetching Jumbo chat data...") + try: + result = refresh_specific_source(source.id) + print(f"✓ Data fetch completed: {result}") + except Exception as e: + print(f"✗ Error fetching data: {e}") + + # Show summary + from data_integration.models import ChatSession + + session_count = ChatSession.objects.filter(company=jumbo).count() + print("\n✓ Setup complete!") + print(f" Total Jumbo chat sessions: {session_count}") + + +if __name__ == "__main__": + main()