11import json
2- import uuid
32
43import httpx
54import structlog
65from fastapi import APIRouter , Depends , HTTPException , Request , Response
6+ from sqlalchemy import select
77from sqlalchemy .ext .asyncio import AsyncSession
88
99from backend .config .lexicon import SLACK_UI
1010from backend .config .settings import settings
11- from backend .models .enums import Platform
12- from backend .models .schemas import DraftUpdate
11+ from backend .models .db_models import User
12+ from backend .models .enums import DraftStatus , Platform
13+ from backend .models .schemas import DraftCreate , DraftUpdate
1314from backend .repositories .draft_repository import DraftRepository
1415from backend .workers .dependencies import get_db_session
1516from backend .workers .tasks .generate_draft import generate_draft_task
@@ -148,6 +149,13 @@ async def slack_interactions(
148149
149150 async with httpx .AsyncClient () as client :
150151 if action_id == "action_publish_draft" :
152+ # ДОДАНО: Змінюємо статус на Опубліковано
153+ if draft_id .isdigit ():
154+ repo = DraftRepository (session )
155+ await repo .update (
156+ int (draft_id ), DraftUpdate (status = DraftStatus .PUBLISHED )
157+ )
158+
151159 await publish_post_task .kiq (
152160 post_id = draft_id , platform = platform , content = draft_text
153161 )
@@ -210,20 +218,25 @@ async def slack_interactions(
210218 callback_id = view .get ("callback_id" )
211219 state_values = view .get ("state" , {}).get ("values" , {})
212220
221+ # --- СЦЕНАРІЙ 1: Генерація нового драфту ---
213222 # --- СЦЕНАРІЙ 1: Генерація нового драфту ---
214223 if callback_id == "modal_generate_draft" :
215- channel_id = view .get ("private_metadata" ) # Дістаємо збережений канал
224+ channel_id = view .get ("private_metadata" )
216225 topic = (
217226 state_values .get ("block_topic_input" , {})
218227 .get ("input_topic" , {})
219228 .get ("value" , "" )
220229 .strip ()
221230 )
222- platform = (
223- state_values .get ("block_platform_select" , {})
224- .get ("input_platform_select" , {})
225- .get ("selected_option" , {})
226- .get ("value" , "telegram" )
231+
232+ block_state = state_values .get ("block_platform_select" , {}).get (
233+ "input_platform_select" , {}
234+ )
235+ selected_option = block_state .get ("selected_option" )
236+ platform = Platform (
237+ selected_option .get ("value" , "telegram" )
238+ if selected_option
239+ else "telegram"
227240 )
228241
229242 logger .info (
@@ -233,16 +246,34 @@ async def slack_interactions(
233246 platform = platform ,
234247 )
235248
236- new_draft_id = str (uuid .uuid4 ()) # ГЕНЕРУЄМО УНІКАЛЬНИЙ ID
249+ # --- ДОДАНО: СТВОРЕННЯ КОРИСТУВАЧА ТА ДРАФТУ В БД ---
250+ # 1. Знаходимо або створюємо користувача (Slack ID)
251+ user_query = await session .execute (
252+ select (User ).where (User .username == user_id )
253+ )
254+ db_user = user_query .scalar_one_or_none ()
255+ if not db_user :
256+ db_user = User (username = user_id )
257+ session .add (db_user )
258+ await session .flush ()
259+
260+ # 2. Створюємо драфт (статус pending)
261+ repo = DraftRepository (session )
262+ new_draft = await repo .create (
263+ DraftCreate (topic = topic , platform = platform , user_id = db_user .id )
264+ )
265+ real_draft_id = str (new_draft .id )
266+ # ---------------------------------------------------
267+
268+ # Передаємо РЕАЛЬНИЙ ID з бази замість UUID
237269 await generate_draft_task .kiq ( # type: ignore[call-overload]
238270 topic = topic ,
239- platform = platform ,
271+ platform = platform . value ,
240272 user_id = user_id ,
241273 channel_id = channel_id ,
242- draft_id = new_draft_id ,
274+ draft_id = real_draft_id ,
243275 )
244276
245- # Відправляємо підтвердження в канал
246277 async with httpx .AsyncClient () as client :
247278 await client .post (
248279 "https://slack.com/api/chat.postMessage" ,
0 commit comments