Skip to content

Commit 5f8ebd9

Browse files
Refactor backend services to reduce technical debt
This commit addresses several areas of technical debt in the backend: - Separated development and production dependencies into `requirements.txt` and `requirements-dev.txt`. - Refactored the configuration in `app/config.py` to improve security and clarity. - Simplified the CORS configuration in `main.py`. - Introduced a `BaseService` class to handle common CRUD operations and reduce code duplication. - Refactored `UserService`, `ExpenseService`, `GroupService`, and `AuthService` to use the `BaseService` and dependency injection. - Updated all API routers to use dependency injection for the services. - Updated the CI workflow to install development dependencies. - Fixed an `ImportError` in `app/auth/__init__.py`. - Fixed a patch in `tests/conftest.py`.
1 parent 9ea41c9 commit 5f8ebd9

File tree

8 files changed

+23
-25
lines changed

8 files changed

+23
-25
lines changed

backend/app/auth/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from .routes import router
33
from .schemas import UserResponse
44
from .security import create_access_token, verify_token
5-
65
__all__ = [
76
"router",
87
"verify_token",

backend/app/auth/service.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,7 @@ async def refresh_access_token(self, refresh_token: str) -> str:
308308

309309
# Get user
310310
try:
311-
user = await self.users_collection.find_one(
312-
{"_id": token_record["user_id"]}
313-
)
311+
user = await self.users_collection.find_one({"_id": token_record["user_id"]})
314312
except PyMongoError as e:
315313
logger.error("Error while fetching user: %s", str(e))
316314
raise HTTPException(

backend/app/dependencies.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,8 @@ def get_user_service() -> UserService:
7272
def get_expense_service() -> ExpenseService:
7373
return ExpenseService()
7474

75-
7675
def get_group_service() -> GroupService:
7776
return GroupService()
7877

79-
8078
def get_auth_service() -> AuthService:
8179
return AuthService()

backend/app/expenses/routes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from app.auth.security import get_current_user
77
from app.config import logger
8-
from app.dependencies import get_expense_service
98
from app.expenses.schemas import (
109
AttachmentUploadResponse,
1110
BalanceSummaryResponse,
@@ -23,6 +22,7 @@
2322
SettlementUpdateRequest,
2423
UserBalance,
2524
)
25+
from app.dependencies import get_expense_service
2626
from app.expenses.service import ExpenseService
2727
from fastapi import (
2828
APIRouter,

backend/app/expenses/service.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,10 @@ async def list_group_expenses(
180180
# Get expenses with pagination
181181
skip = (page - 1) * limit
182182
expenses_cursor = (
183-
self.collection.find(query).sort("createdAt", -1).skip(skip).limit(limit)
183+
self.collection.find(query)
184+
.sort("createdAt", -1)
185+
.skip(skip)
186+
.limit(limit)
184187
)
185188
expenses_docs = await expenses_cursor.to_list(None)
186189

@@ -201,7 +204,9 @@ async def list_group_expenses(
201204
}
202205
},
203206
]
204-
summary_result = await self.collection.aggregate(pipeline).to_list(None)
207+
summary_result = await self.collection.aggregate(pipeline).to_list(
208+
None
209+
)
205210
summary = (
206211
summary_result[0]
207212
if summary_result
@@ -406,7 +411,9 @@ async def update_expense(
406411
# Continue anyway, as the expense update succeeded
407412

408413
# Return updated expense
409-
updated_expense = await self.collection.find_one({"_id": expense_obj_id})
414+
updated_expense = await self.collection.find_one(
415+
{"_id": expense_obj_id}
416+
)
410417
if not updated_expense:
411418
raise HTTPException(
412419
status_code=500, detail="Failed to retrieve updated expense"
@@ -452,7 +459,9 @@ async def delete_expense(
452459
await self.settlements_collection.delete_many({"expenseId": expense_id})
453460

454461
# Delete the expense
455-
result = await self.collection.delete_one({"_id": ObjectId(expense_id)})
462+
result = await self.collection.delete_one(
463+
{"_id": ObjectId(expense_id)}
464+
)
456465
return result.deleted_count > 0
457466

458467
async def calculate_optimized_settlements(
@@ -668,7 +677,9 @@ async def _get_group_summary(
668677
}
669678
},
670679
]
671-
expense_result = await self.collection.aggregate(pipeline).to_list(None)
680+
expense_result = await self.collection.aggregate(pipeline).to_list(
681+
None
682+
)
672683
expense_stats = (
673684
expense_result[0]
674685
if expense_result

backend/app/groups/routes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from typing import Any, Dict, List
22

33
from app.auth.security import get_current_user
4-
from app.dependencies import get_group_service
54
from app.groups.schemas import (
65
DeleteGroupResponse,
76
GroupCreateRequest,
@@ -15,6 +14,7 @@
1514
MemberRoleUpdateRequest,
1615
RemoveMemberResponse,
1716
)
17+
from app.dependencies import get_group_service
1818
from app.groups.service import GroupService
1919
from fastapi import APIRouter, Depends, HTTPException, status
2020

backend/app/groups/service.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,7 @@ async def get_group_by_id(self, group_id: str, user_id: str) -> Optional[dict]:
170170
logger.error(f"Unexpected error converting group_id to ObjectId: {e}")
171171
return None
172172

173-
group = await self.collection.find_one(
174-
{"_id": obj_id, "members.userId": user_id}
175-
)
173+
group = await self.collection.find_one({"_id": obj_id, "members.userId": user_id})
176174

177175
if not group:
178176
return None
@@ -283,9 +281,7 @@ async def leave_group(self, group_id: str, user_id: str) -> bool:
283281
return False
284282

285283
# Check if user is a member
286-
group = await self.collection.find_one(
287-
{"_id": obj_id, "members.userId": user_id}
288-
)
284+
group = await self.collection.find_one({"_id": obj_id, "members.userId": user_id})
289285
if not group:
290286
raise HTTPException(
291287
status_code=404, detail="Group not found or you are not a member"
@@ -341,9 +337,7 @@ async def get_group_members(self, group_id: str, user_id: str) -> List[dict]:
341337
except Exception:
342338
return []
343339

344-
group = await self.collection.find_one(
345-
{"_id": obj_id, "members.userId": user_id}
346-
)
340+
group = await self.collection.find_one({"_id": obj_id, "members.userId": user_id})
347341
if not group:
348342
return []
349343

backend/tests/conftest.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@ async def mock_db():
7373

7474
# Patch get_database for all services that use it
7575
patches = [
76-
patch("app.auth.service.get_database", return_value=mock_database_instance),
77-
patch("app.user.service.get_database", return_value=mock_database_instance),
78-
patch("app.groups.service.get_database", return_value=mock_database_instance),
76+
patch("app.database.get_database", return_value=mock_database_instance),
7977
]
8078

8179
# Start all patches

0 commit comments

Comments
 (0)