File size: 3,226 Bytes
b8e6434
 
1c92e64
b8e6434
 
 
 
 
 
e79f2ff
b8e6434
 
 
 
 
 
 
 
 
 
 
 
 
 
e79f2ff
b8e6434
 
 
 
 
 
 
e79f2ff
b8e6434
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c92e64
 
 
 
 
 
 
 
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
113
114
115
import type { AnalysisHistoryItem, AnalysisResult, CompanyProfile, Tender } from "./types";

const API_BASE = process.env.NEXT_PUBLIC_API_BASE ?? "";

const jsonHeaders = {
  "Content-Type": "application/json",
};

export async function healthCheck() {
  const res = await fetch(`${API_BASE}/api/health`);
  if (!res.ok) {
    throw new Error("Health check failed");
  }
  return res.json();
}

export async function fetchDbStatus() {
  const res = await fetch(`${API_BASE}/api/health/db-status`);
  if (!res.ok) return null;
  return res.json();
}

export async function searchTenders(params: { 
  keyword?: string; 
  buyer?: string; 
  provider_code?: string; 
  date?: string;
  skip?: number;
  limit?: number;
}): Promise<Tender[]> {
  const query = new URLSearchParams();
  if (params.keyword) query.append("keyword", params.keyword);
  if (params.buyer) query.append("buyer", params.buyer);
  if (params.provider_code) query.append("provider_code", params.provider_code);
  if (params.date) query.append("date", params.date);
  if (params.skip !== undefined) query.append("skip", params.skip.toString());
  if (params.limit !== undefined) query.append("limit", params.limit.toString());
  
  const res = await fetch(`${API_BASE}/api/tenders?${query.toString()}`);
  if (!res.ok) {
    throw new Error("Error searching tenders");
  }
  return res.json();
}

export async function analyzeTender(
  tender: Tender, 
  companyProfile: CompanyProfile, 
  documentText?: string
): Promise<AnalysisResult> {
  const res = await fetch(`${API_BASE}/api/analyze`, {
    method: "POST",
    headers: jsonHeaders,
    body: JSON.stringify({ 
      tender, 
      company_profile: companyProfile, 
      document_text: documentText 
    }),
  });
  if (!res.ok) {
    throw new Error("Error analyzing tender");
  }
  return res.json();
}

export async function uploadDocument(file: File): Promise<{ text: string; filename: string }> {
  const formData = new FormData();
  formData.append("file", file);

  const res = await fetch(`${API_BASE}/api/upload-document`, {
    method: "POST",
    body: formData,
  });
  if (!res.ok) {
    throw new Error("Error uploading document");
  }
  return res.json();
}

export async function saveCompanyProfile(profile: CompanyProfile): Promise<CompanyProfile> {
  const res = await fetch(`${API_BASE}/api/company-profile`, {
    method: "POST",
    headers: jsonHeaders,
    body: JSON.stringify(profile),
  });
  if (!res.ok) {
    throw new Error("Error saving company profile");
  }
  return res.json();
}

export async function fetchCompanyProfile(): Promise<CompanyProfile> {
  const res = await fetch(`${API_BASE}/api/company-profile`);
  if (!res.ok) {
    throw new Error("No company profile available");
  }
  return res.json();
}

export async function fetchAnalysisHistory(): Promise<AnalysisHistoryItem[]> {
  const res = await fetch(`${API_BASE}/api/analysis-history`);
  if (!res.ok) {
    throw new Error("Error fetching analysis history");
  }
  return res.json();
}

export async function syncDatabase() {
  const res = await fetch(`${API_BASE}/api/tenders/sync`, { method: "POST" });
  if (!res.ok) {
    throw new Error("Error syncing database");
  }
  return res.json();
}