Files
tgex-frontend/lib/types/api.ts

372 lines
9.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// ============================================================================
// API Types - Generated from API_DOCUMENTATION.md
// ============================================================================
// ----------------------------------------------------------------------------
// User & Auth
// ----------------------------------------------------------------------------
export interface User {
id: string; // UUID
telegram_id: number;
username: string | null;
}
export interface AuthInitResponse {
bot_username: string;
start_param: string; // "login"
}
export interface AuthCompleteRequest {
token: string; // one-time token from bot
}
export interface AuthCompleteResponse {
access_token: string;
refresh_token: string;
user: User;
}
export interface AuthRefreshRequest {
refresh_token: string;
}
export interface AuthRefreshResponse {
access_token: string;
}
// ----------------------------------------------------------------------------
// Target Channel
// ----------------------------------------------------------------------------
// Основная структура как возвращает API
export interface TargetChannel {
id: string; // UUID
telegram_id: number;
title: string;
username: string | null;
is_active: boolean;
}
// Расширенная версия с дополнительными полями (пока не реализовано на бекенде)
export interface TargetChannelDetail extends TargetChannel {
// В будущем можно добавить статистику
}
export interface TargetChannelUpdateRequest {
is_active?: boolean;
}
export interface TargetChannelsListResponse {
target_channels: TargetChannel[];
}
// ----------------------------------------------------------------------------
// External Channel
// ----------------------------------------------------------------------------
// Основная структура как возвращает API
export interface ExternalChannel {
id: string; // UUID
telegram_id: number;
title: string;
username: string | null;
description: string | null;
subscribers_count: number | null;
}
export interface ExternalChannelCreateRequest {
telegram_id: number;
title: string;
username?: string | null;
description?: string | null;
subscribers_count?: number | null;
target_channel_ids: string[]; // UUID[]
}
export interface ExternalChannelUpdateRequest {
title?: string;
username?: string | null;
description?: string | null;
subscribers_count?: number | null;
}
export interface ExternalChannelLinksUpdateRequest {
add_target_channel_ids?: string[];
remove_target_channel_ids?: string[];
}
export interface ExternalChannelsListResponse {
external_channels: ExternalChannel[];
}
// ----------------------------------------------------------------------------
// Creative
// ----------------------------------------------------------------------------
export type CreativeStatus = "active" | "archived";
export interface Creative {
id: string;
name: string;
text: string;
target_channel_id: string;
target_channel_title: string;
created_at: string;
status: CreativeStatus;
placements_count: number;
}
export interface CreativesListResponse {
creatives: Creative[];
}
export interface CreativeCreateRequest {
name: string;
text: string;
target_channel_id: string;
}
export interface CreativeUpdateRequest {
name?: string;
text?: string;
status?: CreativeStatus;
}
// ----------------------------------------------------------------------------
// Placement (Purchase)
// ----------------------------------------------------------------------------
export type InviteLinkType = "public" | "approval";
export type PlacementStatus = "active" | "archived";
export type ViewsAvailability =
| "unknown"
| "available"
| "unavailable"
| "manual";
export interface Placement {
id: string;
target_channel_id: string;
target_channel_title: string;
external_channel_id: string;
external_channel_title: string;
creative_id: string;
creative_name: string;
placement_date: string;
cost: number | null;
comment: string | null;
ad_post_url: string | null;
invite_link_type: InviteLinkType;
invite_link: string;
status: PlacementStatus;
subscriptions_count: number;
views_count: number | null;
views_availability: ViewsAvailability;
last_views_fetch_at: string | null;
created_at: string;
}
export interface PlacementsListResponse {
placements: Placement[];
}
export interface PlacementCreateRequest {
target_channel_id: string;
external_channel_id: string;
creative_id: string;
placement_date: string;
cost?: number | null;
comment?: string | null;
ad_post_url?: string | null;
invite_link_type?: InviteLinkType;
}
export interface PlacementUpdateRequest {
placement_date?: string;
cost?: number | null;
comment?: string | null;
ad_post_url?: string | null;
status?: PlacementStatus;
}
// Legacy aliases for backwards compatibility
export type Purchase = Placement;
export type PurchaseCreateRequest = PlacementCreateRequest;
export type PurchaseUpdateRequest = PlacementUpdateRequest;
export interface ViewsFetchResponse {
placement_id: string;
views_count: number | null;
views_availability: ViewsAvailability;
fetched_at: string;
error_message: string | null;
}
export interface ViewsHistoryResponse {
histories: ViewsSnapshot[];
}
// ----------------------------------------------------------------------------
// Subscription
// ----------------------------------------------------------------------------
export interface Subscription {
id: string;
purchase_id: string;
telegram_user_id: number;
user_first_name: string;
user_last_name: string | null;
user_username: string | null;
subscribed_at: string; // ISO 8601
is_active: boolean;
event_type: "chat_member" | "join_request";
}
// ----------------------------------------------------------------------------
// Views Snapshot
// ----------------------------------------------------------------------------
export interface ViewsSnapshot {
id: string;
purchase_id: string;
views: number;
fetched_at: string; // ISO 8601
}
// ----------------------------------------------------------------------------
// Analytics
// ----------------------------------------------------------------------------
export type DateGrouping = "day" | "week" | "month" | "quarter" | "year";
// Spending Analytics
export interface SpendingDataPoint {
period: string;
cost: number;
subscriptions: number;
views: number;
cpf: number | null;
cpm: number | null;
}
export interface SpendingAnalytics {
total_cost: number;
total_subscriptions: number;
total_views: number;
avg_cpf: number | null;
avg_cpm: number | null;
chart_data: SpendingDataPoint[];
}
// Placements Analytics
export interface PlacementAnalytics {
id: string;
target_channel_id: string;
target_channel_title: string;
external_channel_id: string;
external_channel_title: string;
creative_id: string;
creative_name: string;
placement_date: string;
cost: number | null;
subscriptions_count: number;
views_count: number | null;
cpf: number | null;
cpm: number | null;
}
export interface PlacementsAnalytics {
placements: PlacementAnalytics[];
}
// Creatives Analytics
export interface CreativeAnalytics {
id: string;
name: string;
placements_count: number;
total_cost: number;
total_subscriptions: number;
total_views: number;
avg_cpf: number | null;
avg_cpm: number | null;
}
export interface CreativesAnalytics {
creatives: CreativeAnalytics[];
}
// External Channels Analytics
export interface ExternalChannelAnalytics {
id: string;
title: string;
username: string | null;
placements_count: number;
total_cost: number;
total_subscriptions: number;
total_views: number;
avg_cpf: number | null;
avg_cpm: number | null;
}
export interface ExternalChannelsAnalytics {
external_channels: ExternalChannelAnalytics[];
}
// ----------------------------------------------------------------------------
// Common API Responses
// ----------------------------------------------------------------------------
export interface ListResponse<T> {
data: T[];
total: number;
}
export interface SuccessResponse {
success: boolean;
}
export interface ApiError {
error: {
code: string;
message: string;
details?: any;
};
}
// ----------------------------------------------------------------------------
// Query Parameters
// ----------------------------------------------------------------------------
export interface TargetChannelQueryParams {
is_active?: boolean;
}
export interface ExternalChannelQueryParams {
target_channel_id?: string;
search?: string;
}
export interface CreativeQueryParams {
target_channel_id?: string;
is_archived?: boolean;
}
export interface PlacementQueryParams {
target_channel_id?: string;
external_channel_id?: string;
creative_id?: string;
is_archived?: boolean;
date_from?: string; // ISO 8601
date_to?: string; // ISO 8601
sort?: "date" | "cost" | "cpf" | "subscriptions";
order?: "asc" | "desc";
}
export interface AnalyticsOverviewQueryParams {
target_channel_id?: string;
date_from?: string;
date_to?: string;
}