Skip to content

Commit cedaad3

Browse files
committed
refactor-fixes: coderrabbit fixes
1 parent 6baa3b1 commit cedaad3

File tree

9 files changed

+5326
-5327
lines changed

9 files changed

+5326
-5327
lines changed

backend/app/api/v1/integrations.py

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
IntegrationListResponse,
88
IntegrationStatusResponse
99
)
10-
from app.services.integration_service import integration_service
10+
from app.services.integration_service import integration_service, NotFoundError
1111
from app.core.dependencies import get_current_user
1212

1313
router = APIRouter()
@@ -24,10 +24,7 @@ async def create_integration(
2424
except ValueError as e:
2525
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
2626
except Exception as e:
27-
raise HTTPException(
28-
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
29-
detail=f"Failed to create integration: {str(e)}"
30-
)
27+
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)) from e
3128

3229

3330
@router.get("/", response_model=IntegrationListResponse)
@@ -37,10 +34,7 @@ async def list_integrations(user_id: UUID = Depends(get_current_user)):
3734
integrations = await integration_service.get_integrations(user_id)
3835
return IntegrationListResponse(integrations=integrations, total=len(integrations))
3936
except Exception as e:
40-
raise HTTPException(
41-
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
42-
detail=f"Failed to list integrations: {str(e)}"
43-
)
37+
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)) from e
4438

4539

4640
@router.get("/status/{platform}", response_model=IntegrationStatusResponse)
@@ -52,11 +46,7 @@ async def get_integration_status(
5246
try:
5347
return await integration_service.get_integration_status(user_id, platform)
5448
except Exception as e:
55-
raise HTTPException(
56-
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
57-
detail=f"Failed to get integration status: {str(e)}"
58-
)
59-
49+
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)) from e
6050

6151
@router.get("/{integration_id}", response_model=IntegrationResponse)
6252
async def get_integration(
@@ -74,14 +64,8 @@ async def get_integration(
7464
)
7565

7666
return integration
77-
except HTTPException:
78-
raise
7967
except Exception as e:
80-
raise HTTPException(
81-
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
82-
detail=f"Failed to get integration: {str(e)}"
83-
)
84-
68+
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)) from e
8569

8670
@router.put("/{integration_id}", response_model=IntegrationResponse)
8771
async def update_integration(
@@ -92,14 +76,15 @@ async def update_integration(
9276
"""Update an existing integration."""
9377
try:
9478
return await integration_service.update_integration(user_id, integration_id, request)
79+
except NotFoundError as e:
80+
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(e)) from e
9581
except HTTPException:
9682
raise
9783
except Exception as e:
9884
raise HTTPException(
9985
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
10086
detail=f"Failed to update integration: {str(e)}"
101-
)
102-
87+
) from e
10388

10489
@router.delete("/{integration_id}", status_code=status.HTTP_204_NO_CONTENT)
10590
async def delete_integration(
@@ -109,10 +94,12 @@ async def delete_integration(
10994
"""Delete an integration."""
11095
try:
11196
await integration_service.delete_integration(user_id, integration_id)
97+
except NotFoundError as e:
98+
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(e)) from e
11299
except HTTPException:
113100
raise
114101
except Exception as e:
115102
raise HTTPException(
116103
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
117104
detail=f"Failed to delete integration: {str(e)}"
118-
)
105+
) from e

backend/app/core/dependencies.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ async def get_current_user(authorization: str = Header(None)) -> UUID:
4747
try:
4848
supabase = get_supabase_client()
4949
# Verify the token and get user
50-
user_response = await supabase.auth.get_user(token)
50+
user_response = supabase.auth.get_user(token)
5151

5252
if not user_response or not user_response.user:
5353
raise HTTPException(
@@ -61,9 +61,9 @@ async def get_current_user(authorization: str = Header(None)) -> UUID:
6161
except HTTPException:
6262
raise
6363
except Exception as e:
64-
logger.error(f"Authentication error: {str(e)}")
64+
logger.exception("Authentication error")
6565
raise HTTPException(
6666
status_code=status.HTTP_401_UNAUTHORIZED,
6767
detail="Authentication failed",
6868
headers={"WWW-Authenticate": "Bearer"},
69-
)
69+
) from e

backend/app/services/integration_service.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
IntegrationCreateRequest,
88
IntegrationUpdateRequest,
99
IntegrationResponse,
10-
IntegrationStatusResponse
10+
IntegrationStatusResponse,
11+
NotFoundError
1112
)
1213

1314
logger = logging.getLogger(__name__)
@@ -64,7 +65,7 @@ async def create_integration(
6465
return IntegrationResponse(**result.data[0])
6566

6667
except Exception as e:
67-
logger.error(f"Error creating integration: {str(e)}")
68+
logger.exception("Error creating integration")
6869
raise
6970

7071
async def get_integrations(self, user_id: UUID) -> List[IntegrationResponse]:
@@ -78,7 +79,7 @@ async def get_integrations(self, user_id: UUID) -> List[IntegrationResponse]:
7879
return [IntegrationResponse(**item) for item in result.data]
7980

8081
except Exception as e:
81-
logger.error(f"Error getting integrations: {str(e)}")
82+
logger.exception("Error getting integrations")
8283
raise
8384

8485
async def get_integration(self, user_id: UUID, integration_id: UUID) -> Optional[IntegrationResponse]:
@@ -96,7 +97,7 @@ async def get_integration(self, user_id: UUID, integration_id: UUID) -> Optional
9697
return IntegrationResponse(**result.data[0])
9798

9899
except Exception as e:
99-
logger.error(f"Error getting integration: {str(e)}")
100+
logger.exception("Error getting integration")
100101
raise
101102

102103
async def get_integration_by_platform(
@@ -118,7 +119,7 @@ async def get_integration_by_platform(
118119
return IntegrationResponse(**result.data[0])
119120

120121
except Exception as e:
121-
logger.error(f"Error getting integration by platform: {str(e)}")
122+
logger.exception("Error getting integration by platform")
122123
raise
123124

124125
async def update_integration(
@@ -140,12 +141,14 @@ async def update_integration(
140141
if request.config is not None:
141142
update_data["config"] = request.config
142143

144+
existing = await self.get_integration(user_id, integration_id)
145+
if not existing:
146+
raise NotFoundError("Integration not found")
147+
143148
if request.organization_link is not None:
144-
if "config" not in update_data:
145-
# Get existing config first
146-
existing = await self.get_integration(user_id, integration_id)
147-
update_data["config"] = existing.config or {}
148-
update_data["config"]["organization_link"] = request.organization_link
149+
base_config = (update_data.get("config") or existing.config or {}).copy()
150+
base_config["organization_link"] = request.organization_link
151+
update_data["config"] = base_config
149152

150153
result = await self.supabase.table("organization_integrations")\
151154
.update(update_data)\
@@ -161,7 +164,7 @@ async def update_integration(
161164
return IntegrationResponse(**result.data[0])
162165

163166
except Exception as e:
164-
logger.error(f"Error updating integration: {str(e)}")
167+
logger.exception("Error updating integration")
165168
raise
166169

167170
async def delete_integration(self, user_id: UUID, integration_id: UUID) -> bool:
@@ -178,7 +181,7 @@ async def delete_integration(self, user_id: UUID, integration_id: UUID) -> bool:
178181
return True
179182

180183
except Exception as e:
181-
logger.error(f"Error deleting integration: {str(e)}")
184+
logger.exception("Error deleting integration")
182185
raise
183186

184187
async def get_integration_status(
@@ -204,7 +207,7 @@ async def get_integration_status(
204207
)
205208

206209
except Exception as e:
207-
logger.error(f"Error getting integration status: {str(e)}")
210+
logger.exception("Error getting integration status")
208211
raise
209212

210213
async def get_all_integrations_for_platform(self, platform: str) -> List[IntegrationResponse]:
@@ -219,7 +222,7 @@ async def get_all_integrations_for_platform(self, platform: str) -> List[Integra
219222
return [IntegrationResponse(**item) for item in result.data]
220223

221224
except Exception as e:
222-
logger.error(f"Error getting all integrations for platform: {str(e)}")
225+
logger.exception("Error getting all integrations for platform")
223226
raise
224227

225228

backend/database/01_create_integration_tables.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ CREATE POLICY "Users can create their own integrations"
5151
CREATE POLICY "Users can update their own integrations"
5252
ON organization_integrations
5353
FOR UPDATE
54-
USING (auth.uid() = user_id);
54+
USING (auth.uid() = user_id)
55+
WITH CHECK (auth.uid() = user_id);
5556

5657
CREATE POLICY "Users can delete their own integrations"
5758
ON organization_integrations

frontend/.env example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ VITE_SUPABASE_URL=YOUR SUPABASE URL
22
VITE_BASE_URL=http://localhost:5173/
33
VITE_SUPABASE_KEY=YOUR SUPABASE ANON KEY
44
VITE_BACKEND_URL=http://localhost:8000
5-
VITE_DISCORD_BOT_INVITE_URL=https://discord.com/oauth2/authorize?client_id==&permissions==&integration_type=0&scope=bot+applications.commands
5+
VITE_DISCORD_BOT_INVITE_URL=https://discord.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&permissions=YOUR_PERMISSIONS&integration_type=0&scope=bot+applications.commands

0 commit comments

Comments
 (0)