File size: 2,083 Bytes
7ad8558
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type {
  CadastroBaseSearchResponse,
  Property,
  PropertyDraft,
  SpreadsheetPreview
} from "./types";

const API_URL = window.location.port === "5173" ? "http://localhost:8001" : "";

export async function fetchProperties(): Promise<Property[]> {
  const response = await fetch(`${API_URL}/properties`);
  if (!response.ok) {
    throw new Error("Falha ao carregar imoveis.");
  }
  return response.json();
}

export async function createProperty(payload: PropertyDraft): Promise<Property> {
  const response = await fetch(`${API_URL}/properties`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json"
    },
    body: JSON.stringify(payload)
  });

  if (!response.ok) {
    const errorText = await response.text();
    throw new Error(errorText || "Falha ao salvar imovel.");
  }

  return response.json();
}

export async function deleteProperty(propertyId: number): Promise<void> {
  const response = await fetch(`${API_URL}/properties/${propertyId}`, {
    method: "DELETE"
  });

  if (!response.ok) {
    throw new Error("Falha ao excluir registro.");
  }
}

export async function previewSpreadsheet(file: File): Promise<SpreadsheetPreview> {
  const formData = new FormData();
  formData.append("file", file);

  const response = await fetch(`${API_URL}/properties/import-preview`, {
    method: "POST",
    body: formData
  });

  if (!response.ok) {
    const errorText = await response.text();
    throw new Error(errorText || "Falha ao ler planilha.");
  }

  return response.json();
}

export async function searchCadastroBase(
  mode: "inscricao" | "endereco",
  query: string
): Promise<CadastroBaseSearchResponse> {
  const params = new URLSearchParams({ mode, q: query, limit: "20" });
  const response = await fetch(`${API_URL}/cadastro-base/search?${params.toString()}`);

  if (!response.ok) {
    const errorText = await response.text();
    throw new Error(errorText || "Falha ao consultar base cadastral.");
  }

  return response.json();
}

export function getExportUrl(): string {
  return `${API_URL}/properties/export`;
}