136 lines
3.8 KiB
TypeScript
136 lines
3.8 KiB
TypeScript
const rawApiBase = import.meta.env.VITE_API_URL?.trim();
|
|
const DEPLOYED_BACKEND_ORIGIN = 'https://api.desineuron.in';
|
|
|
|
function getBrowserOrigin() {
|
|
if (typeof window !== 'undefined' && window.location?.origin) {
|
|
return window.location.origin;
|
|
}
|
|
return '';
|
|
}
|
|
|
|
export const API_URL = (
|
|
rawApiBase && rawApiBase.length > 0
|
|
? rawApiBase
|
|
: import.meta.env.DEV
|
|
? getBrowserOrigin()
|
|
: DEPLOYED_BACKEND_ORIGIN || getBrowserOrigin()
|
|
).replace(/\/$/, '');
|
|
|
|
export const WS_URL = API_URL.replace(/^http/, 'ws');
|
|
|
|
export interface ScatterDataPoint {
|
|
id: string;
|
|
name: string;
|
|
sentiment_score: number;
|
|
response_time_ms: number;
|
|
score: number;
|
|
qualification: string;
|
|
kanban_status: string;
|
|
}
|
|
|
|
export interface LeadRecord {
|
|
id: string;
|
|
name: string;
|
|
email?: string | null;
|
|
phone?: string | null;
|
|
source: string;
|
|
notes: string;
|
|
qualification: string;
|
|
score: number;
|
|
kanban_status: string;
|
|
stage: string;
|
|
budget: string;
|
|
unit_interest: string;
|
|
metadata: Record<string, unknown>;
|
|
created_at?: string | null;
|
|
updated_at?: string | null;
|
|
}
|
|
|
|
export interface LeadDemographics {
|
|
by_source: Array<{ source: string; lead_count: number; avg_score: number }>;
|
|
by_qualification: Array<{ qualification: string; lead_count: number }>;
|
|
}
|
|
|
|
export interface ChatLogRecord {
|
|
id: string;
|
|
lead_id: string;
|
|
sender: string;
|
|
channel: string;
|
|
content: string;
|
|
metadata: Record<string, unknown>;
|
|
created_at: string | null;
|
|
}
|
|
|
|
export interface MarketingCampaignSummary {
|
|
id: string;
|
|
name: string;
|
|
platform: 'meta' | 'google';
|
|
status: 'active' | 'paused' | 'completed';
|
|
budget: number;
|
|
spent: number;
|
|
impressions: number;
|
|
clicks: number;
|
|
conversions: number;
|
|
}
|
|
|
|
async function requestJson<T>(path: string): Promise<T> {
|
|
const response = await fetch(`${API_URL}${path}`, {
|
|
headers: { Accept: 'application/json' },
|
|
});
|
|
if (!response.ok) {
|
|
throw new Error(`Request failed: ${response.status}`);
|
|
}
|
|
return response.json() as Promise<T>;
|
|
}
|
|
|
|
async function requestWrappedData<T>(path: string): Promise<T> {
|
|
const payload = await requestJson<{ data: T }>(path);
|
|
return payload.data;
|
|
}
|
|
|
|
export async function getSentimentScatter(): Promise<ScatterDataPoint[]> {
|
|
return requestJson<ScatterDataPoint[]>('/api/analytics/sentiment-scatter');
|
|
}
|
|
|
|
export async function getCatalystCampaigns(): Promise<MarketingCampaignSummary[]> {
|
|
return requestWrappedData<MarketingCampaignSummary[]>('/api/catalyst/campaigns');
|
|
}
|
|
|
|
export async function getLeads(): Promise<LeadRecord[]> {
|
|
const payload = await requestJson<{ data: LeadRecord[] }>('/api/leads');
|
|
return payload.data;
|
|
}
|
|
|
|
export async function getLead(leadId: string): Promise<LeadRecord> {
|
|
return requestWrappedData<LeadRecord>(`/api/leads/${leadId}`);
|
|
}
|
|
|
|
export async function getKanbanBoard() {
|
|
return requestWrappedData<Array<{ status: string; stage: string; count: number; items: LeadRecord[] }>>('/api/kanban/board');
|
|
}
|
|
|
|
export async function getChatLogs(leadId?: string): Promise<ChatLogRecord[]> {
|
|
const suffix = leadId ? `?lead_id=${encodeURIComponent(leadId)}` : '';
|
|
return requestWrappedData<ChatLogRecord[]>(`/api/chat-logs${suffix}`);
|
|
}
|
|
|
|
export async function getLeadDemographics(): Promise<LeadDemographics> {
|
|
return requestWrappedData<LeadDemographics>('/api/leads/demographics');
|
|
}
|
|
|
|
export async function seedSyntheticLeads(count = 100): Promise<{ seeded: number; chat_logs_seeded: number; batch: string }> {
|
|
const response = await fetch(`${API_URL}/api/leads/seed-synthetic`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
Accept: 'application/json',
|
|
},
|
|
body: JSON.stringify({ count }),
|
|
});
|
|
if (!response.ok) {
|
|
throw new Error(`Seed request failed: ${response.status}`);
|
|
}
|
|
const payload = await response.json() as { data: { seeded: number; chat_logs_seeded: number; batch: string } };
|
|
return payload.data;
|
|
}
|