Files
tgex-frontend/lib/api/placements.ts
2026-01-26 12:13:38 +03:00

145 lines
3.8 KiB
TypeScript

// ============================================================================
// Placements API
// ============================================================================
import { api } from "./client";
import type {
Placement,
PlacementWithStats,
PlacementCreateRequest,
PlacementUpdateRequest,
PlacementsQueryParams,
ViewsHistoryItem,
ViewsHistoryQueryParams,
PaginatedResponse,
} from "@/lib/types/api";
type UpdatePlacementInProjectRequest = Partial<{
status: string;
comment: string | null;
creative_id: string | null;
placement_at: string | null;
payment_at: string | null;
cost: { type: "fixed"; value: number } | null;
cost_before_bargain: number | null;
placement_type: string | null;
format: string | null;
}>;
export const placementsApi = {
/**
* Get list of placements for workspace
*/
list: (workspaceId: string, params?: PlacementsQueryParams) =>
api.get<PaginatedResponse<Placement>>(
`/workspaces/${workspaceId}/placements`,
{ params }
),
/**
* Get placements for a specific project (grouped by channel UI)
*/
getByProject: (workspaceId: string, projectId: string) =>
api.get<{ placements: Placement[] }>(`/workspaces/${workspaceId}/projects/${projectId}/placements`)
.then((response) => response.placements),
/**
* Get single placement
*/
get: (workspaceId: string, placementId: string) =>
api.get<Placement>(`/workspaces/${workspaceId}/placements/${placementId}`),
/**
* Create new placement
*/
create: (workspaceId: string, data: PlacementCreateRequest) =>
api.post<Placement>(`/workspaces/${workspaceId}/placements`, data),
/**
* Create placements for multiple channels (bulk creation)
*/
createBulk: (
workspaceId: string,
projectId: string,
data: {
creative_id?: string;
channels: Array<{
channel_id: string;
status?: string;
comment?: string;
details?: {
placement_at?: string;
payment_at?: string;
cost?: { type: string; value: number };
cost_before_bargain?: number;
placement_type?: string;
format?: string;
comment?: string;
creative_id?: string;
};
}>;
details?: {
placement_at?: string;
payment_at?: string;
cost?: { type: string; value: number };
cost_before_bargain?: number;
placement_type?: string;
format?: string;
comment?: string;
creative_id?: string;
};
}
) =>
api.post<{ placements: Placement[] }>(
`/workspaces/${workspaceId}/projects/${projectId}/placements`,
data
),
/**
* Update placement
*/
update: (
workspaceId: string,
placementId: string,
data: PlacementUpdateRequest
) =>
api.patch<Placement>(
`/workspaces/${workspaceId}/placements/${placementId}`,
data
),
/**
* Update placement within project (Purchase Plans UI)
* PATCH /workspaces/{workspace_id}/projects/{project_id}/placements/{placement_id}
*/
updateInProject: (
workspaceId: string,
projectId: string,
placementId: string,
data: UpdatePlacementInProjectRequest
) =>
api.patch<Placement>(
`/workspaces/${workspaceId}/projects/${projectId}/placements/${placementId}`,
data
),
/**
* Delete (archive) placement
*/
delete: (workspaceId: string, placementId: string) =>
api.delete<void>(`/workspaces/${workspaceId}/placements/${placementId}`),
/**
* Get views history for placement
*/
getViewsHistory: (
workspaceId: string,
placementId: string,
params?: ViewsHistoryQueryParams
) =>
api.get<PaginatedResponse<ViewsHistoryItem>>(
`/workspaces/${workspaceId}/placements/${placementId}/views/history`,
{ params }
),
};