From b4c58e7c36d7d62cc135c0f2315691f4c39b7e19 Mon Sep 17 00:00:00 2001 From: Shahrukh khan <59122336+shahrukh802@users.noreply.github.com> Date: Wed, 5 Jun 2024 22:48:01 +0500 Subject: [PATCH] archive conversation api added (#1209) --- server/api/v1/conversations/conversations.py | 16 ++++++++++++++++ server/app/controllers/conversation.py | 16 ++++++++++++++++ server/app/controllers/workspace.py | 2 +- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/server/api/v1/conversations/conversations.py b/server/api/v1/conversations/conversations.py index eb139e4c9..a055d5641 100644 --- a/server/api/v1/conversations/conversations.py +++ b/server/api/v1/conversations/conversations.py @@ -45,3 +45,19 @@ async def conversation_messages( return APIResponse( data=response, message="User conversation messages returned successfully!" ) + +@conversation_router.delete("/{conv_id}") +async def delete_conversation( + conv_id: UUID = Path(..., description="ID of the conversation"), + conversation_controller: ConversationController = Depends( + Factory().get_conversation_controller + ), + user: UserInfo = Depends(get_current_user), + +): + response = await conversation_controller.archive_conversation( + conv_id, user.id + ) + return APIResponse( + data=response, message="Conversation archived successfully" + ) \ No newline at end of file diff --git a/server/app/controllers/conversation.py b/server/app/controllers/conversation.py index ec692f0fe..6c6320e13 100644 --- a/server/app/controllers/conversation.py +++ b/server/app/controllers/conversation.py @@ -1,3 +1,4 @@ +from fastapi import status, HTTPException from app.models import User, UserConversation from app.repositories import UserRepository from app.repositories.conversation import ConversationRepository @@ -38,3 +39,18 @@ async def get_conversation_messages( count = await self.conversation_repository.get_messages_count(conversation_id) return ConversationMessageList(count=count, messages=conversation_messages) + + async def archive_conversation( + self, conversation_id: str, user_id: str + ): + user_conversation = ( + await self.conversation_repository.get_by_id(conversation_id) + ) + if not user_conversation: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, + message=f"conversation with id: {conversation_id} was not found") + if user_conversation.user_id != user_id: + raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, + message="Unauthorized to update conversation for this user") + user_conversation.valid = False + await self.conversation_repository.session.commit() \ No newline at end of file diff --git a/server/app/controllers/workspace.py b/server/app/controllers/workspace.py index 21a397b23..4b206f2c0 100644 --- a/server/app/controllers/workspace.py +++ b/server/app/controllers/workspace.py @@ -48,7 +48,7 @@ async def get_workspace_users( workspace = await self.space_repository.get_by_id(id=workspace_id) if not workspace: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, - detail=f"Workspace with id: {workspace_id} was not found") + message=f"Workspace with id: {workspace_id} was not found") users = await self.space_repository.get_users_by_workspace_id(workspace_id) return WorkspaceUsersResponse(users=users) \ No newline at end of file