diff --git a/src/database.py b/src/database.py index 607eac89..5d4f423b 100644 --- a/src/database.py +++ b/src/database.py @@ -227,16 +227,55 @@ class PostgresStore: def create_attachment(self, attachment: dict) -> dict: with get_db() as conn, conn.cursor() as cur: - cur.execute("SELECT 1 FROM chat_members WHERE chat_id=%s AND user_id=%s", (attachment["chat_id"], attachment["owner_id"])) + cur.execute( + "SELECT 1 FROM chat_members WHERE chat_id=%s AND user_id=%s", + (attachment["chat_id"], attachment["owner_id"]) + ) if not cur.fetchone(): - cur.execute("SELECT id FROM chats WHERE id=%s", (attachment["chat_id"],)) + cur.execute( + "SELECT id FROM chats WHERE id=%s", + (attachment["chat_id"],) + ) if cur.fetchone(): raise ValueError("owner_not_in_chat") raise ValueError("chat_not_found") + cur.execute( - "INSERT INTO attachments (id, chat_id, owner_id, filename, content_type, size_bytes, sha256, status, object_key, error_code, created_at, updated_at) VALUES (%s,%s,%s,%s,%s,%s,%s,'ready',%s,NULL,%s,%s)", - (attachment["id"], attachment["chat_id"], attachment["owner_id"], attachment["filename"], attachment["content_type"], attachment["size_bytes"], attachment["sha256"], attachment["object_key"], attachment["created_at"], attachment["updated_at"])) - return attachment + """ + INSERT INTO attachments ( + id, chat_id, owner_id, filename, content_type, + size_bytes, sha256, status, object_key, + error_code, created_at, updated_at + ) + VALUES (%s, %s, %s, %s, %s, %s, %s, 'ready', %s, NULL, %s, %s) + """, + ( + attachment["id"], + attachment["chat_id"], + attachment["owner_id"], + attachment["filename"], + attachment["content_type"], + attachment["size_bytes"], + attachment["sha256"], + attachment["object_key"], + attachment["created_at"], + attachment["updated_at"] + ) + ) + + cur.execute( + """ + SELECT id, chat_id, owner_id, filename, content_type, + size_bytes, sha256, status, error_code, + created_at, updated_at + FROM attachments + WHERE id = %s + """, + (attachment["id"],) + ) + row = cur.fetchone() + + return self._attachment_response(cur, row) def get_attachment(self, attachment_id: str, user_id: str) -> Optional[dict]: with get_db() as conn, conn.cursor() as cur: