File size: 3,838 Bytes
2f1409f
76089f2
3a19693
 
 
 
76089f2
3a19693
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76089f2
 
 
 
 
 
 
 
3c4a809
 
 
 
 
 
 
 
76089f2
 
 
 
 
 
 
 
 
3a19693
 
76089f2
3a19693
76089f2
 
 
 
3c4a809
76089f2
 
 
3c4a809
76089f2
 
3c4a809
76089f2
 
 
3c4a809
76089f2
 
3c4a809
76089f2
 
3c4a809
76089f2
 
 
 
 
 
3a19693
 
76089f2
 
 
 
3c4a809
76089f2
 
3a19693
76089f2
3a19693
76089f2
 
 
 
3a19693
76089f2
3a19693
76089f2
 
 
 
 
 
 
3c4a809
 
 
 
76089f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const BASE = import.meta.env.DEV ? '/api' : ''

function getToken() {
  return localStorage.getItem('leadgen_token')
}

async function request(path, options = {}) {
  const token = getToken()
  const headers = { ...(options.headers || {}) }
  if (token) headers['Authorization'] = `Bearer ${token}`
  if (options.body && !(options.body instanceof FormData)) {
    headers['Content-Type'] = headers['Content-Type'] || 'application/json'
  }

  const res = await fetch(`${BASE}${path}`, { ...options, headers })

  if (res.status === 401) {
    localStorage.removeItem('leadgen_token')
    localStorage.removeItem('leadgen_user')
    window.location.href = '/'
    return
  }

  if (!res.ok) {
    const err = await res.json().catch(() => ({ detail: res.statusText }))
    throw new Error(err.detail || 'Request failed')
  }
  if (res.status === 204) return null
  return res.json()
}

// Projects
export const projectsApi = {
  list: () => request('/projects'),
  create: (data) => request('/projects', { method: 'POST', body: JSON.stringify(data) }),
  update: (id, data) => request(`/projects/${id}`, { method: 'PUT', body: JSON.stringify(data) }),
  delete: (id) => request(`/projects/${id}`, { method: 'DELETE' }),
}

// Leads
export const leadsApi = {
  list: (params = {}) => {
    const qs = new URLSearchParams(
      Object.fromEntries(Object.entries(params).filter(([, v]) => v != null && v !== ''))
    ).toString()
    return request(`/leads${qs ? '?' + qs : ''}`)
  },
  get: (id) => request(`/leads/${id}`),
  create: (data) => request('/leads', { method: 'POST', body: JSON.stringify(data) }),
  update: (id, data) => request(`/leads/${id}`, { method: 'PUT', body: JSON.stringify(data) }),
  delete: (id) => request(`/leads/${id}`, { method: 'DELETE' }),
  bulkDelete: (ids) => request('/leads', { method: 'DELETE', body: JSON.stringify(ids) }),
}

// Import
export const importApi = {
  csv: (file, sourceLabel = 'csv', projectId = null) => {
    const fd = new FormData()
    fd.append('file', file)
    fd.append('source_label', sourceLabel)
    if (projectId) fd.append('project_id', projectId)
    return request('/import/csv', { method: 'POST', body: fd })
  },
  excel: (file, sourceLabel = 'excel', projectId = null) => {
    const fd = new FormData()
    fd.append('file', file)
    fd.append('source_label', sourceLabel)
    if (projectId) fd.append('project_id', projectId)
    return request('/import/excel', { method: 'POST', body: fd })
  },
  linkedin: (file, projectId = null) => {
    const fd = new FormData()
    fd.append('file', file)
    if (projectId) fd.append('project_id', projectId)
    return request('/import/linkedin', { method: 'POST', body: fd })
  },
}

// Scraping
export const scrapingApi = {
  scrapeUrl: (config) => request('/scrape/url', { method: 'POST', body: JSON.stringify(config) }),
  scrapeBulk: (urls) => request('/scrape/bulk', { method: 'POST', body: JSON.stringify(urls) }),
  listJobs: () => request('/scrape/jobs'),
  getJob: (id) => request(`/scrape/jobs/${id}`),
}

// Export — direct download links with token
export const exportApi = {
  csvUrl: (params = {}) => {
    const token = getToken()
    const qs = new URLSearchParams(
      Object.fromEntries(Object.entries({ ...params, token }).filter(([, v]) => v != null && v !== ''))
    ).toString()
    return `${BASE}/export/csv${qs ? '?' + qs : ''}`
  },
  excelUrl: (params = {}) => {
    const token = getToken()
    const qs = new URLSearchParams(
      Object.fromEntries(Object.entries({ ...params, token }).filter(([, v]) => v != null && v !== ''))
    ).toString()
    return `${BASE}/export/excel${qs ? '?' + qs : ''}`
  },
}

// Analytics
export const analyticsApi = {
  get: (projectId = null) => {
    const qs = projectId ? `?project_id=${projectId}` : ''
    return request(`/analytics${qs}`)
  },
}