-
Notifications
You must be signed in to change notification settings - Fork 0
Complaint Logging Backend #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fb63a52
feat: Add complaint logging module and tests to backend
aLEGEND21 405d33a
refactor: Replace party_id with location_id in complaint service and …
aLEGEND21 e40e8e1
fix: Fix lazy loading issues in location entity so that tests pass in…
aLEGEND21 ca7df25
refactor: Better error reporting and data organization for complaint …
aLEGEND21 54915e0
Merge branch 'main' into arnav/complaint-logging-backend
aLEGEND21 87e5acb
fix: Use correct complaint data model in service tests
aLEGEND21 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| from datetime import datetime | ||
| from typing import TYPE_CHECKING, Self | ||
|
|
||
| from sqlalchemy import DateTime, ForeignKey, Integer, String | ||
| from sqlalchemy.orm import Mapped, mapped_column, relationship | ||
| from src.core.database import EntityBase | ||
|
|
||
| from .complaint_model import Complaint | ||
|
|
||
| if TYPE_CHECKING: | ||
| from ..location.location_entity import LocationEntity | ||
|
|
||
|
|
||
| class ComplaintEntity(EntityBase): | ||
| __tablename__ = "complaints" | ||
|
|
||
| id: Mapped[int] = mapped_column(Integer, primary_key=True) | ||
| location_id: Mapped[int] = mapped_column( | ||
| Integer, ForeignKey("locations.id", ondelete="CASCADE"), nullable=False | ||
| ) | ||
| complaint_datetime: Mapped[datetime] = mapped_column(DateTime, nullable=False) | ||
| description: Mapped[str] = mapped_column(String, nullable=False, default="") | ||
|
|
||
| # Relationships | ||
| location: Mapped["LocationEntity"] = relationship( | ||
| "LocationEntity", passive_deletes=True | ||
| ) | ||
|
|
||
| @classmethod | ||
| def from_model(cls, data: Complaint) -> Self: | ||
| return cls( | ||
| location_id=data.location_id, | ||
| complaint_datetime=data.complaint_datetime, | ||
| description=data.description, | ||
| ) | ||
|
|
||
| def to_model(self) -> Complaint: | ||
| """Convert entity to model.""" | ||
| return Complaint( | ||
| id=self.id, | ||
| location_id=self.location_id, | ||
| complaint_datetime=self.complaint_datetime, | ||
| description=self.description, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| from datetime import datetime | ||
|
|
||
| from pydantic import BaseModel | ||
|
|
||
|
|
||
| class ComplaintData(BaseModel): | ||
| """Data DTO for a complaint without id.""" | ||
|
|
||
| location_id: int | ||
| complaint_datetime: datetime | ||
| description: str = "" | ||
|
|
||
|
|
||
| class Complaint(ComplaintData): | ||
| """Output DTO for a complaint.""" | ||
|
|
||
| id: int | ||
|
|
||
|
|
||
| class ComplaintCreate(BaseModel): | ||
| """Input DTO for creating a complaint for a location.""" | ||
|
|
||
| location_id: int | ||
| complaint_datetime: datetime | ||
| description: str = "" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| from fastapi import APIRouter, Depends, status | ||
| from src.core.authentication import authenticate_admin, authenticate_staff_or_admin | ||
| from src.modules.account.account_model import Account | ||
|
|
||
| from .complaint_model import Complaint, ComplaintCreate | ||
| from .complaint_service import ComplaintService | ||
|
|
||
| complaint_router = APIRouter(prefix="/api/locations", tags=["complaints"]) | ||
|
|
||
|
|
||
| @complaint_router.get( | ||
| "/{location_id}/complaints", | ||
| response_model=list[Complaint], | ||
| status_code=status.HTTP_200_OK, | ||
| summary="Get all complaints for a location", | ||
| description="Returns all complaints associated with a given location. Staff or admin only.", | ||
| ) | ||
| async def get_complaints_by_location( | ||
| location_id: int, | ||
| complaint_service: ComplaintService = Depends(), | ||
| _: Account = Depends(authenticate_staff_or_admin), | ||
| ) -> list[Complaint]: | ||
| """Get all complaints for a location.""" | ||
| return await complaint_service.get_complaints_by_location(location_id) | ||
|
|
||
|
|
||
| @complaint_router.post( | ||
| "/{location_id}/complaints", | ||
| response_model=Complaint, | ||
| status_code=status.HTTP_201_CREATED, | ||
| summary="Create a complaint for a location", | ||
| description="Creates a new complaint associated with a location. Admin only.", | ||
| ) | ||
| async def create_complaint( | ||
| location_id: int, | ||
| complaint_data: ComplaintCreate, | ||
| complaint_service: ComplaintService = Depends(), | ||
| _: Account = Depends(authenticate_admin), | ||
| ) -> Complaint: | ||
| """Create a complaint for a location.""" | ||
| return await complaint_service.create_complaint(location_id, complaint_data) | ||
|
|
||
|
|
||
| @complaint_router.put( | ||
| "/{location_id}/complaints/{complaint_id}", | ||
| response_model=Complaint, | ||
| status_code=status.HTTP_200_OK, | ||
| summary="Update a complaint", | ||
| description="Updates an existing complaint. Admin only.", | ||
| ) | ||
| async def update_complaint( | ||
| location_id: int, | ||
| complaint_id: int, | ||
| complaint_data: ComplaintCreate, | ||
| complaint_service: ComplaintService = Depends(), | ||
| _: Account = Depends(authenticate_admin), | ||
| ) -> Complaint: | ||
| """Update a complaint.""" | ||
| return await complaint_service.update_complaint( | ||
| complaint_id, location_id, complaint_data | ||
| ) | ||
|
|
||
|
|
||
| @complaint_router.delete( | ||
| "/{location_id}/complaints/{complaint_id}", | ||
| response_model=Complaint, | ||
| status_code=status.HTTP_200_OK, | ||
| summary="Delete a complaint", | ||
| description="Deletes a complaint. Admin only.", | ||
| ) | ||
| async def delete_complaint( | ||
| location_id: int, | ||
| complaint_id: int, | ||
| complaint_service: ComplaintService = Depends(), | ||
| _: Account = Depends(authenticate_admin), | ||
| ) -> Complaint: | ||
| """Delete a complaint.""" | ||
| return await complaint_service.delete_complaint(complaint_id) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| from fastapi import Depends | ||
| from sqlalchemy import select | ||
| from sqlalchemy.exc import IntegrityError | ||
| from sqlalchemy.ext.asyncio import AsyncSession | ||
| from src.core.database import get_session | ||
| from src.core.exceptions import ConflictException, NotFoundException | ||
|
|
||
| from .complaint_entity import ComplaintEntity | ||
| from .complaint_model import Complaint, ComplaintCreate | ||
|
|
||
|
|
||
| class ComplaintNotFoundException(NotFoundException): | ||
| def __init__(self, complaint_id: int): | ||
| super().__init__(f"Complaint with ID {complaint_id} not found") | ||
|
|
||
|
|
||
| class ComplaintConflictException(ConflictException): | ||
| def __init__(self, message: str): | ||
| super().__init__(message) | ||
|
|
||
|
|
||
| class ComplaintService: | ||
| def __init__( | ||
| self, | ||
| session: AsyncSession = Depends(get_session), | ||
| ): | ||
| self.session = session | ||
|
|
||
| async def _get_complaint_entity_by_id(self, complaint_id: int) -> ComplaintEntity: | ||
| result = await self.session.execute( | ||
| select(ComplaintEntity).where(ComplaintEntity.id == complaint_id) | ||
| ) | ||
| complaint_entity = result.scalar_one_or_none() | ||
| if complaint_entity is None: | ||
| raise ComplaintNotFoundException(complaint_id) | ||
| return complaint_entity | ||
|
|
||
| async def get_complaints_by_location(self, location_id: int) -> list[Complaint]: | ||
| """Get all complaints for a given location.""" | ||
| result = await self.session.execute( | ||
| select(ComplaintEntity).where(ComplaintEntity.location_id == location_id) | ||
| ) | ||
| complaints = result.scalars().all() | ||
| return [complaint.to_model() for complaint in complaints] | ||
|
|
||
| async def get_complaint_by_id(self, complaint_id: int) -> Complaint: | ||
| """Get a single complaint by ID.""" | ||
| complaint_entity = await self._get_complaint_entity_by_id(complaint_id) | ||
| return complaint_entity.to_model() | ||
|
|
||
| async def create_complaint( | ||
| self, location_id: int, data: ComplaintCreate | ||
| ) -> Complaint: | ||
| """Create a new complaint.""" | ||
| new_complaint = ComplaintEntity( | ||
| location_id=location_id, | ||
| complaint_datetime=data.complaint_datetime, | ||
| description=data.description, | ||
| ) | ||
| try: | ||
| self.session.add(new_complaint) | ||
| await self.session.commit() | ||
| except IntegrityError as e: | ||
| raise ComplaintConflictException(f"Failed to create complaint: {str(e)}") | ||
aLEGEND21 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| await self.session.refresh(new_complaint) | ||
| return new_complaint.to_model() | ||
|
|
||
| async def update_complaint( | ||
| self, complaint_id: int, location_id: int, data: ComplaintCreate | ||
| ) -> Complaint: | ||
| """Update an existing complaint.""" | ||
| complaint_entity = await self._get_complaint_entity_by_id(complaint_id) | ||
|
|
||
| complaint_entity.location_id = location_id | ||
| complaint_entity.complaint_datetime = data.complaint_datetime | ||
| complaint_entity.description = data.description | ||
|
|
||
| try: | ||
| self.session.add(complaint_entity) | ||
| await self.session.commit() | ||
| except IntegrityError as e: | ||
| raise ComplaintConflictException(f"Failed to update complaint: {str(e)}") | ||
| await self.session.refresh(complaint_entity) | ||
| return complaint_entity.to_model() | ||
|
|
||
| async def delete_complaint(self, complaint_id: int) -> Complaint: | ||
| """Delete a complaint.""" | ||
| complaint_entity = await self._get_complaint_entity_by_id(complaint_id) | ||
| complaint = complaint_entity.to_model() | ||
| await self.session.delete(complaint_entity) | ||
| await self.session.commit() | ||
| return complaint | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| """ | ||
| Tests for the complaint module. | ||
| """ | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.