diff --git a/components/dialog-add-channel.tsx b/components/dialog-add-channel.tsx index 6ed1c7f..cbc5594 100644 --- a/components/dialog-add-channel.tsx +++ b/components/dialog-add-channel.tsx @@ -66,14 +66,34 @@ export function AddChannelDialog({ try { setLoading(true); - // Step 1: Verify/add channel via channels API - await channelsApi.list({ username }); + const username = parseChannelInput(inputValue); - // Step 2: Create empty placement + if (!username) { + setError("Не удалось распознать username из ссылки"); + return; + } + + if (existingUsernames.has(username.toLowerCase())) { + setError("Этот канал уже добавлен в проект"); + return; + } + + // Step 1: Find channel by username to get channel_id + const channelsResponse = await channelsApi.list({ username }); + const channel = channelsResponse.items.find( + (c) => c.username?.toLowerCase() === username.toLowerCase() + ); + + if (!channel) { + setError("Канал не найден в каталоге"); + return; + } + + // Step 2: Create empty placement with channel_id await placementsApi.createBulk(workspaceId, projectId, { channels: [ { - username, + channel_id: channel.id, status: "Без статуса", }, ], diff --git a/components/dialog-create-placements.tsx b/components/dialog-create-placements.tsx index 4b284ae..28dfb1b 100644 --- a/components/dialog-create-placements.tsx +++ b/components/dialog-create-placements.tsx @@ -24,12 +24,12 @@ import { } from "@/components/ui/select"; import { cn } from "@/lib/utils"; import { parseChannelInput } from "@/lib/utils/channel-parser"; -import { placementsApi, creativesApi } from "@/lib/api"; -import type { Placement, Creative } from "@/lib/types/api"; +import { placementsApi, creativesApi, channelsApi } from "@/lib/api"; +import type { Placement, Creative, Channel } from "@/lib/types/api"; interface ChannelInput { username: string; - isNew: boolean; + channelId: string; } interface CreatePlacementsDialogProps { @@ -108,7 +108,7 @@ export function CreatePlacementsDialog({ } }; - const addChannel = () => { + const addChannel = async () => { const username = parseChannelInput(newChannelInput); if (!username) { setError("Не удалось распознать username"); @@ -125,9 +125,25 @@ export function CreatePlacementsDialog({ return; } - setSelectedChannels([...selectedChannels, { username, isNew: true }]); - setNewChannelInput(""); - setError(null); + // Look up channel by username to get channel_id + try { + const channelsResponse = await channelsApi.list({ username }); + const channel = channelsResponse.items.find( + (c) => c.username?.toLowerCase() === lowerUsername + ); + + if (!channel) { + setError("Канал не найден в каталоге"); + return; + } + + setSelectedChannels([...selectedChannels, { username, channelId: channel.id }]); + setNewChannelInput(""); + setError(null); + } catch (err) { + console.error("Failed to look up channel:", err); + setError("Не удалось найти канал"); + } }; const removeChannel = (username: string) => { @@ -158,7 +174,7 @@ export function CreatePlacementsDialog({ await placementsApi.createBulk(workspaceId, projectId, { creative_id: creativeId || undefined, channels: selectedChannels.map((c) => ({ - username: c.username, + channel_id: c.channelId, status: DEFAULT_STATUS, })), details: Object.keys(details).length > 0 ? details : undefined, diff --git a/lib/api/placements.ts b/lib/api/placements.ts index 24a9ecd..d9dcc8d 100644 --- a/lib/api/placements.ts +++ b/lib/api/placements.ts @@ -52,7 +52,7 @@ export const placementsApi = { data: { creative_id?: string; channels: Array<{ - username: string; + channel_id: string; status?: string; comment?: string; details?: {