Spaces:
Sleeping
Sleeping
| from supabase import create_client, Client | |
| from config.settings import settings | |
| import logging | |
| from typing import Optional, Dict, Any | |
| logger = logging.getLogger(__name__) | |
| class SupabaseService: | |
| """Service to handle Supabase database operations""" | |
| def __init__(self): | |
| self.client: Optional[Client] = None | |
| if settings.supabase_url and settings.supabase_key: | |
| try: | |
| self.client = create_client(settings.supabase_url, settings.supabase_key) | |
| logger.info("Supabase client initialized successfully") | |
| except Exception as e: | |
| logger.error(f"Failed to initialize Supabase client: {str(e)}") | |
| else: | |
| logger.warning("Supabase URL or Key not found in settings. Supabase integration disabled.") | |
| def get_user_id_by_email(self, email: str) -> Optional[str]: | |
| """ | |
| Find user's firebase_id by email from the Users table | |
| """ | |
| if not self.client: | |
| logger.warning("Supabase client not initialized") | |
| return None | |
| try: | |
| # Assuming the table name is "Users" and it has "jira_email" and "firebase_id" columns | |
| response = self.client.table("Users").select("firebase_id").eq("jira_email", email).execute() | |
| if response.data and len(response.data) > 0: | |
| return response.data[0]["firebase_id"] | |
| return None | |
| except Exception as e: | |
| logger.error(f"Error fetching user by email {email}: {str(e)}") | |
| return None | |
| def save_user_credentials(self, email: str, api_token: str, server_url: str, account_id: str) -> bool: | |
| """ | |
| Save user's Jira credentials to the Users table | |
| """ | |
| if not self.client: | |
| logger.warning("Supabase client not initialized") | |
| return False | |
| try: | |
| # We need to find the user first to update them, or we assume email is unique and we can update based on it. | |
| # Supabase update requires a match. | |
| data = { | |
| "jira_api_token": api_token, | |
| "jira_server_url": server_url, | |
| "jira_account_id": account_id | |
| } | |
| response = self.client.table("Users").update(data).eq("jira_email", email).execute() | |
| if response.data: | |
| logger.info(f"Successfully saved credentials for user {email}") | |
| return True | |
| else: | |
| logger.warning(f"No user found with email {email} to update credentials") | |
| return False | |
| except Exception as e: | |
| logger.error(f"Error saving credentials for {email}: {str(e)}") | |
| return False | |
| def get_user_credentials_by_account_id(self, account_id: str) -> Optional[Dict[str, Any]]: | |
| """ | |
| Get user credentials by Jira Account ID | |
| """ | |
| if not self.client: | |
| logger.warning("Supabase client not initialized") | |
| return None | |
| try: | |
| response = self.client.table("Users").select("firebase_id, jira_api_token, jira_server_url, jira_email").eq("jira_account_id", account_id).execute() | |
| if response.data and len(response.data) > 0: | |
| return response.data[0] | |
| return None | |
| except Exception as e: | |
| logger.error(f"Error fetching credentials for account {account_id}: {str(e)}") | |
| return None | |
| def upsert_jira_data(self, firebase_id: str, data: Dict[str, Any]) -> bool: | |
| """ | |
| Insert or update Jira data for a user | |
| """ | |
| if not self.client: | |
| logger.warning("Supabase client not initialized") | |
| return False | |
| try: | |
| # Prepare payload | |
| payload = { | |
| "user_id": firebase_id, | |
| "jira_payload": data, | |
| "synced_at": "now()" | |
| } | |
| # Upsert into jira_data table | |
| # Note: The table needs to conflict on user_id if we want 1 row per user, | |
| # or we just insert a new log every time. | |
| # Given "store all the data", let's assume we want a log of events. | |
| # But the user also said "data gets updated". | |
| # Let's insert a new record for every event for now to be safe (Audit Log style). | |
| # AND/OR update a current state record. | |
| # For simplicity let's insert a new record. | |
| self.client.table("jira_data").insert(payload).execute() | |
| logger.info(f"Successfully stored Jira data for user {firebase_id}") | |
| return True | |
| except Exception as e: | |
| logger.error(f"Error storing Jira data for {firebase_id}: {str(e)}") | |
| return False | |
| # Create singleton | |
| supabase_service = SupabaseService() | |