Skip to content
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

feat/delete session api #329

Merged
merged 1 commit into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions backend/src/api/routes/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from src.utilities.exceptions.database import EntityDoesNotExist
from src.utilities.exceptions.http.exc_404 import http_404_exc_uuid_not_found_request
from src.config.settings.const import ANONYMOUS_USER
from src.config.manager import settings
from src.models.schemas.chat import (
ChatsWithTime,
ChatInMessage,
Expand Down Expand Up @@ -90,6 +91,53 @@ async def update_session(
return ChatUUIDResponse(sessionUuid=sessions.uuid)


@router.delete(
path="/session/{uuid}",
name="Session:delete-session-by-uuid",
status_code=fastapi.status.HTTP_200_OK,
)
async def delete_session(
uuid: str,
token: str = fastapi.Depends(oauth2_scheme),
session_repo: SessionCRUDRepository = fastapi.Depends(get_repository(repo_type=SessionCRUDRepository)),
account_repo: AccountCRUDRepository = fastapi.Depends(get_repository(repo_type=AccountCRUDRepository)),
jwt_payload: dict = fastapi.Depends(jwt_required),
) -> dict[str, str]:
"""
Delete a session by uuid

This endpoint deletes an existing session by its uuid.

It is **IRREVERSIBLE** and should be used with caution.

It requires an admin token to access.

```bash
curl -X 'DELETE' \
'http://127.0.0.1:8000/api/chat/session/fa250ff0-fb22-49f5-a7f9-2b057b7a7398' \
-H 'accept: application/json' \
-H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFiYyIsImVtYWlsIjoidXNlckBleGFtcGxlLmNvbSIsImV4cCI6MTcyMjY3MzU2OCwic3ViIjoiWU9VUi1KV1QtU1VCSkVDVCJ9.THYudCS7r0dwuW4s21QG9YmGNSc8-qkuumbtvhWychM'
```

Returns a dictionary:

{
"notification": "Session with uuid 'fa250ff0-fb22-49f5-a7f9-2b057b7a7398' is successfully deleted!"
}
"""
current_user = await account_repo.read_account_by_username(username=jwt_payload.username)
account_id=current_user.id
if current_user.username == settings.ADMIN_USERNAME:
account_id=0
try:
deletion_result = await session_repo.delete_session_by_uuid(uuid=uuid, account_id=account_id)

except EntityDoesNotExist:
raise await http_404_exc_uuid_not_found_request(uuid=uuid)

return {"notification": deletion_result}


@router.get(
"/seesionuuid",
name="chat:session-uuid",
Expand Down
17 changes: 17 additions & 0 deletions backend/src/repository/crud/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ async def update_sessions_by_uuid(self, session: SessionUpdate, account_id: int)
await self.async_session.refresh(instance=update_session)

return update_session # type: ignore

async def delete_session_by_uuid(self, uuid: str, account_id: int) -> Session:
if account_id == 0:
select_stmt = sqlalchemy.select(Session).where(Session.uuid == uuid)
else:
select_stmt = sqlalchemy.select(Session).where(Session.uuid == uuid, Session.account_id == account_id)
query = await self.async_session.execute(statement=select_stmt)
delete_session = query.scalar()
if delete_session is None:
raise EntityDoesNotExist(f"Session with uuid `{uuid}` does not exist!")

stmt = sqlalchemy.delete(table=Session).where(Session.uuid == uuid)

await self.async_session.execute(statement=stmt)
await self.async_session.commit()

return f"Session with uuid '{uuid}' is successfully deleted!"

async def append_ds_name_to_session(self, session_uuid: str, account_id: int, ds_name: str) -> Session:
"""
Expand Down