Gå til innhold

Data Sync & Cache

Overview

Data synchronisation is handled by sync_worker.py. It runs as a scheduled Windows task (nightly) and builds the JSON cache that the FastAPI backend uses to answer questions.

SQL Server master DB (Clubs table)
    ├── boolChatbot = 1  →  Club included in sync
Per club: query 44 allowed tables
cache/{clubFolder}_context.json

sync_worker.py – Detailed Flow

  1. Connects to the master database with pyodbc parameters from .env
  2. Reads the Clubs table – fetches all rows where boolChatbot = 1
  3. For each club:
  4. Connects to the club's own database
  5. Queries 44 pre-approved tables (news, activities, course info, price lists, etc.)
  6. Aggregates the results into a single Python dict
  7. Writes cache/{clubFolder}_context.json

Allowed Tables (44 total)

The sync worker uses an explicit allowlist (ALLOWED_TABLES) to prevent unintended data from being exposed to the AI model. Examples:

Table Content
tblNews News articles
tblActivities Activities and events
tblCourseInfo Course information
tblPrices Price list
tblContacts Contact information
tblOpeningHours Opening hours
AI_Chatbot_Settings Per-club chatbot settings

Cache Format

The file cache/{clubFolder}_context.json has the following top-level structure:

{
  "club_id": "SKI",
  "synced_at": "2026-05-02T03:00:00Z",
  "tables": {
    "tblNews": [
      {"id": 1, "title": "Summer open!", "body": "..."},
      ...
    ],
    "tblActivities": [...],
    ...
  }
}

Scheduled Execution (Windows Task Scheduler)

Parameter Value
Script API\chatbot\sync_worker.py
Executor Python venv: API\python\venv\Scripts\python.exe
Trigger Daily at 03:00
Working directory C:\ftp\sites\clubsite-4\API\chatbot

Run manually:

cd C:\ftp\sites\clubsite-4\API\python
.\venv\Scripts\python.exe ..\chatbot\sync_worker.py

Adding a New Tenant to Sync

  1. Set boolChatbot = 1 for the club in the Clubs table in the master database
  2. Verify the clubFolder field (used as the cache filename)
  3. Run sync_worker.py manually
  4. Confirm that cache/{clubFolder}_context.json has been created
  5. Update ALLOWED_TENANT_ID in app.py (temporary – removed in v2.1)

Future improvement

The v2.1 plan removes the ALLOWED_TENANT_ID restriction. The API will then automatically load the cache for any tenant with a valid cache file.