Rudraaaa76 commited on
Commit
82b7c1e
·
1 Parent(s): 13773e4
api/webhook_routes.py CHANGED
@@ -18,13 +18,7 @@ async def receive_jira_webhook(
18
  Receive webhook events from Jira and sync data using the user's stored credentials
19
  """
20
  try:
21
- try:
22
- payload = await request.json()
23
- except Exception as e:
24
- logger.warning(f"Failed to parse webhook JSON: {str(e)}")
25
- body = await request.body()
26
- logger.warning(f"Raw body: {body.decode('utf-8', errors='ignore')}")
27
- return {"status": "error", "reason": "invalid_json"}
28
  logger.info(f"Received Jira webhook: {x_atlassian_webhook_identifier}")
29
 
30
  # 1. Extract Jira Account ID from payload
 
18
  Receive webhook events from Jira and sync data using the user's stored credentials
19
  """
20
  try:
21
+ payload = await request.json()
 
 
 
 
 
 
22
  logger.info(f"Received Jira webhook: {x_atlassian_webhook_identifier}")
23
 
24
  # 1. Extract Jira Account ID from payload
services/supabase_service.py CHANGED
@@ -23,28 +23,20 @@ class SupabaseService:
23
  """
24
  Find user's firebase_id by email from the Users table
25
  """
26
- user = self.get_user_by_email(email)
27
- if user:
28
- return user.get("firebase_id")
29
- return None
30
-
31
- def get_user_by_email(self, email: str) -> Optional[Dict[str, Any]]:
32
- """
33
- Get full user details by email
34
- """
35
  if not self.client:
36
  logger.warning("Supabase client not initialized")
37
  return None
38
 
39
  try:
40
- response = self.client.table("Users").select("*").eq("jira_email", email).execute()
 
41
 
42
  if response.data and len(response.data) > 0:
43
- return response.data[0]
44
  return None
45
 
46
  except Exception as e:
47
- logger.warning(f"Error fetching user by email {email}: {str(e)}")
48
  return None
49
 
50
  def save_user_credentials(self, email: str, api_token: str, server_url: str, account_id: str) -> bool:
@@ -56,39 +48,22 @@ class SupabaseService:
56
  return False
57
 
58
  try:
59
- # We use upsert to create the user if they don't exist, or update if they do.
60
- # We match on "jira_email" which is the primary identifier for us here.
61
- # Note: For upsert to work on a non-id column, that column must be UNIQUE in Supabase.
62
- # If jira_email is not unique, we should check first.
63
-
64
- # First check if user exists
65
- existing_user = self.get_user_by_email(email)
66
 
67
  data = {
68
- "jira_email": email,
69
  "jira_api_token": api_token,
70
  "jira_server_url": server_url,
71
  "jira_account_id": account_id
72
  }
73
 
74
- if existing_user:
75
- # Update existing user
76
- response = self.client.table("Users").update(data).eq("jira_email", email).execute()
77
- logger.info(f"Updated credentials for existing user {email}")
78
- else:
79
- # Create new user (Insert)
80
- # We might need a generic firebase_id if it's required and not nullable
81
- # For now let's hope it's nullable or auto-generated, or unrelated here.
82
- # If firebase_id is required, we cannot create a user without it.
83
- # Assuming this flow is for existing firebase users who are adding Jira...
84
- logger.warning(f"User {email} not found. Attempting to create new record...")
85
- response = self.client.table("Users").insert(data).execute()
86
 
87
  if response.data:
88
  logger.info(f"Successfully saved credentials for user {email}")
89
  return True
90
  else:
91
- logger.warning(f"Failed to save credentials for {email}")
92
  return False
93
 
94
  except Exception as e:
 
23
  """
24
  Find user's firebase_id by email from the Users table
25
  """
 
 
 
 
 
 
 
 
 
26
  if not self.client:
27
  logger.warning("Supabase client not initialized")
28
  return None
29
 
30
  try:
31
+ # Assuming the table name is "Users" and it has "jira_email" and "firebase_id" columns
32
+ response = self.client.table("Users").select("firebase_id").eq("jira_email", email).execute()
33
 
34
  if response.data and len(response.data) > 0:
35
+ return response.data[0]["firebase_id"]
36
  return None
37
 
38
  except Exception as e:
39
+ logger.error(f"Error fetching user by email {email}: {str(e)}")
40
  return None
41
 
42
  def save_user_credentials(self, email: str, api_token: str, server_url: str, account_id: str) -> bool:
 
48
  return False
49
 
50
  try:
51
+ # We need to find the user first to update them, or we assume email is unique and we can update based on it.
52
+ # Supabase update requires a match.
 
 
 
 
 
53
 
54
  data = {
 
55
  "jira_api_token": api_token,
56
  "jira_server_url": server_url,
57
  "jira_account_id": account_id
58
  }
59
 
60
+ response = self.client.table("Users").update(data).eq("jira_email", email).execute()
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  if response.data:
63
  logger.info(f"Successfully saved credentials for user {email}")
64
  return True
65
  else:
66
+ logger.warning(f"No user found with email {email} to update credentials")
67
  return False
68
 
69
  except Exception as e: