Ver Fonte

feat: Use soft delete for order messages instead of physical deletion

unknown há 2 meses atrás
pai
commit
eb3dbe4b2f
2 ficheiros alterados com 5 adições e 18 exclusões
  1. 4 18
      backend/routers/chat.py
  2. 1 0
      backend/schema.sql

+ 4 - 18
backend/routers/chat.py

@@ -35,7 +35,8 @@ async def get_order_messages(order_id: int, user: dict = Depends(get_current_use
         SELECT m.id, m.is_from_admin, m.message, m.created_at, u.first_name, u.email 
         FROM order_messages m
         LEFT JOIN users u ON m.user_id = u.id
-        WHERE m.order_id = %s ORDER BY m.created_at ASC
+        WHERE m.order_id = %s AND m.deleted = FALSE 
+        ORDER BY m.created_at ASC
     """
     messages = db.execute_query(query, (order_id,))
     for msg in messages:
@@ -165,23 +166,8 @@ async def delete_order_message(order_id: int, msg_id: int, user: dict = Depends(
     if not is_admin and msg[0]['user_id'] != user_id:
         raise HTTPException(status_code=403, detail="Not authorized to delete this message")
 
-    # Try to delete associated file if any
-    try:
-        import json
-        import os
-        import config
-        msg_data = json.loads(msg[0]['message'])
-        image_url = msg_data.get("image_url")
-        if image_url:
-            # image_url looks like "uploads/chats/filename"
-            file_path = os.path.join(config.BASE_DIR, image_url)
-            if os.path.exists(file_path):
-                os.remove(file_path)
-    except Exception as e:
-        print(f"Error deleting chat image: {e}")
-
-    # Delete message from database
-    db.execute_commit("DELETE FROM order_messages WHERE id = %s", (msg_id,))
+    # Soft delete message from database
+    db.execute_commit("UPDATE order_messages SET deleted = TRUE WHERE id = %s", (msg_id,))
 
     # Broadcast deletion
     await manager.broadcast_to_order(order_id, {

+ 1 - 0
backend/schema.sql

@@ -167,6 +167,7 @@ CREATE TABLE IF NOT EXISTS `order_messages` (
   `message` text NOT NULL,
   `created_at` timestamp NULL DEFAULT current_timestamp(),
   `is_read` tinyint(1) DEFAULT 0,
+  `deleted` tinyint(1) DEFAULT 0,
   PRIMARY KEY (`id`),
   KEY `order_id` (`order_id`),
   CONSTRAINT `fk_msg_order` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`) ON DELETE CASCADE