File size: 3,704 Bytes
b273c57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
116
117
118
119
import { LegalProfessional, ChatSession } from '../types';

export const initDB = async (): Promise<boolean> => {
  return true;
};

export const addProfessionals = async (data: LegalProfessional[], type: 'lawyer' | 'notary'): Promise<void> => {
  const response = await fetch('/api/db/professionals', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ data, type }),
  });

  if (!response.ok) {
    const error = await response.json();
    throw new Error(error.error || 'Failed to add professionals');
  }
};

export const getAllProfessionals = async (type?: 'lawyer' | 'notary'): Promise<LegalProfessional[]> => {
  const url = type ? `/api/db/professionals?type=${type}` : '/api/db/professionals';
  const response = await fetch(url);
  if (!response.ok) {
    const error = await response.json();
    throw new Error(error.error || 'Failed to fetch professionals');
  }
  return response.json();
};

export const clearAllProfessionals = async (type?: 'lawyer' | 'notary'): Promise<void> => {
  const url = type ? `/api/db/professionals?type=${type}` : '/api/db/professionals';
  const response = await fetch(url, {
    method: 'DELETE',
  });
  if (!response.ok) {
    const error = await response.json();
    throw new Error(error.error || 'Failed to clear professionals');
  }
};

// Legacy names for compatibility if needed, but we'll update the components
export const addLawyers = (lawyers: LegalProfessional[]) => addProfessionals(lawyers, 'lawyer');
export const getAllLawyers = () => getAllProfessionals('lawyer');
export const clearAllLawyers = () => clearAllProfessionals('lawyer');

// --- CHAT SESSIONS ---
export const saveChatSession = async (chat: ChatSession): Promise<void> => {
  const response = await fetch('/api/db/chats', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(chat),
  });

  if (!response.ok) {
    const error = await response.json();
    throw new Error(error.error || 'Failed to save chat session');
  }
};

export const getAllChatSessions = async (): Promise<ChatSession[]> => {
  const response = await fetch('/api/db/chats');
  if (!response.ok) {
    const error = await response.json();
    throw new Error(error.error || 'Failed to fetch chat sessions');
  }
  return response.json();
};

export const deleteChatSession = async (id: string): Promise<void> => {
  const response = await fetch(`/api/db/chats?id=${id}`, {
    method: 'DELETE',
  });
  if (!response.ok) {
    const error = await response.json();
    throw new Error(error.error || 'Failed to delete chat session');
  }
};

// --- CHECKPOINTS ---
import { Checkpoint } from '../types';

export const saveCheckpoint = async (ckpt: Checkpoint): Promise<void> => {
  const response = await fetch('/api/db/checkpoints', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(ckpt),
  });

  if (!response.ok) {
    const error = await response.json();
    throw new Error(error.error || 'Failed to save checkpoint');
  }
};

export const getAllCheckpoints = async (): Promise<Checkpoint[]> => {
  const response = await fetch('/api/db/checkpoints');
  if (!response.ok) {
    const error = await response.json();
    throw new Error(error.error || 'Failed to fetch checkpoints');
  }
  return response.json();
};

export const deleteCheckpoint = async (id: string): Promise<void> => {
  const response = await fetch(`/api/db/checkpoints?id=${id}`, {
    method: 'DELETE',
  });
  if (!response.ok) {
    const error = await response.json();
    throw new Error(error.error || 'Failed to delete checkpoint');
  }
};