From 11f76f4e75794076b6dae0c08988ab07284fd0dc Mon Sep 17 00:00:00 2001 From: Stan Girard Date: Tue, 6 Feb 2024 19:30:20 -0800 Subject: [PATCH] fix(prompts): can now be removed (#2154) # Description Please include a summary of the changes and the related issue. Please also include relevant motivation and context. ## Checklist before requesting a review Please delete options that are not relevant. - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my code - [ ] I have commented hard-to-understand areas - [ ] I have ideally added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged ## Screenshots (if appropriate): --- backend/modules/brain/controller/brain_routes.py | 14 +++++++++----- backend/modules/brain/service/brain_service.py | 1 - backend/modules/prompt/repository/prompts.py | 14 +++++++++++++- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/backend/modules/brain/controller/brain_routes.py b/backend/modules/brain/controller/brain_routes.py index cef9bcf2f..da7dbca5b 100644 --- a/backend/modules/brain/controller/brain_routes.py +++ b/backend/modules/brain/controller/brain_routes.py @@ -137,17 +137,21 @@ async def update_existing_brain( if existing_brain is None: raise HTTPException(status_code=404, detail="Brain not found") - brain_service.update_brain_by_id(brain_id, brain_update_data) - if brain_update_data.prompt_id is None and existing_brain.prompt_id: prompt = prompt_service.get_prompt_by_id(existing_brain.prompt_id) if prompt and prompt.status == "private": prompt_service.delete_prompt_by_id(existing_brain.prompt_id) - if brain_update_data.status == "private" and existing_brain.status == "public": - brain_user_service.delete_brain_users(brain_id) + return {"message": f"Prompt {brain_id} has been updated."} - return {"message": f"Brain {brain_id} has been updated."} + elif brain_update_data.status == "private" and existing_brain.status == "public": + brain_user_service.delete_brain_users(brain_id) + return {"message": f"Brain {brain_id} has been deleted."} + + else: + brain_service.update_brain_by_id(brain_id, brain_update_data) + + return {"message": f"Brain {brain_id} has been updated."} @brain_router.put( diff --git a/backend/modules/brain/service/brain_service.py b/backend/modules/brain/service/brain_service.py index 3b42f1503..0f2475021 100644 --- a/backend/modules/brain/service/brain_service.py +++ b/backend/modules/brain/service/brain_service.py @@ -268,7 +268,6 @@ class BrainService: status_code=404, detail=f"Brain with id {brain_id} not found", ) - brain_update_answer = self.brain_repository.update_brain_by_id( brain_id, brain=BrainUpdatableProperties( diff --git a/backend/modules/prompt/repository/prompts.py b/backend/modules/prompt/repository/prompts.py index 9a87ee078..b31739e0b 100644 --- a/backend/modules/prompt/repository/prompts.py +++ b/backend/modules/prompt/repository/prompts.py @@ -1,10 +1,10 @@ from fastapi import HTTPException +from models.settings import get_supabase_client from modules.prompt.entity.prompt import Prompt from modules.prompt.repository.prompts_interface import ( DeletePromptResponse, PromptsInterface, ) -from models.settings import get_supabase_client class Prompts(PromptsInterface): @@ -30,6 +30,18 @@ class Prompts(PromptsInterface): Returns: A dictionary containing the status of the delete and prompt_id of the deleted prompt """ + + # Update brains where prompt_id is equal to the value to NULL + self.db.from_("brains").update({"prompt_id": None}).filter( + "prompt_id", "eq", prompt_id + ).execute() + + # Update chat_history where prompt_id is equal to the value to NULL + self.db.from_("chat_history").update({"prompt_id": None}).filter( + "prompt_id", "eq", prompt_id + ).execute() + + # Delete the prompt response = ( self.db.from_("prompts") .delete()