122 lines
3 KiB
TypeScript
122 lines
3 KiB
TypeScript
// import { Models } from "@/lib/appwrite"; // Removido - não usamos mais Appwrite
|
|
|
|
export interface CadastroProdutoEtapa1Input {
|
|
codigoBarras: string;
|
|
nome: string;
|
|
precoBase: number;
|
|
descricao?: string;
|
|
laboratorioId: string;
|
|
categoriaId: string;
|
|
}
|
|
|
|
export interface CadastroProdutoEtapa2Input {
|
|
codigoBarras: string;
|
|
dataValidade: string;
|
|
precoVenda: number;
|
|
observacoes?: string;
|
|
referenciaCatalogoId?: string;
|
|
}
|
|
|
|
export interface CadastroProdutoEtapa3Input {
|
|
codigoBarras: string;
|
|
estoque: number;
|
|
referenciaCatalogoId?: string;
|
|
}
|
|
|
|
export interface ApiResponse<T = any> {
|
|
success: boolean;
|
|
data?: T;
|
|
error?: string;
|
|
details?: unknown;
|
|
}
|
|
|
|
class CadastroProdutoService {
|
|
private readonly apiBaseUrl = "";
|
|
|
|
private async request<T = any>(endpoint: string, options?: RequestInit): Promise<ApiResponse<T>> {
|
|
try {
|
|
const response = await fetch(`${this.apiBaseUrl}${endpoint}`, {
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
...(options?.headers || {}),
|
|
},
|
|
...options,
|
|
});
|
|
|
|
const payload = await response.json().catch(() => ({}));
|
|
|
|
if (!response.ok) {
|
|
return {
|
|
success: false,
|
|
error: (payload && (payload.error || payload.message)) || "Erro ao processar requisicao",
|
|
details: payload,
|
|
};
|
|
}
|
|
|
|
return {
|
|
success: true,
|
|
data: payload?.data ?? payload,
|
|
};
|
|
} catch (error) {
|
|
console.error("[CadastroProdutoService] Erro de requisicao", error);
|
|
return {
|
|
success: false,
|
|
error: "Erro de conexao com o servidor",
|
|
};
|
|
}
|
|
}
|
|
|
|
async buscarPorCodigoBarras(codigo: string): Promise<ApiResponse<{ document: any | null; found: boolean }>> {
|
|
if (!codigo) {
|
|
return { success: false, error: "Codigo de barras obrigatorio" };
|
|
}
|
|
|
|
const params = new URLSearchParams({ codigo });
|
|
const response = await fetch(`/api/produtos-catalogo?${params.toString()}`, {
|
|
method: "GET",
|
|
cache: "no-store",
|
|
});
|
|
|
|
try {
|
|
const data = await response.json();
|
|
if (!response.ok) {
|
|
return {
|
|
success: false,
|
|
error: data?.error || "Erro ao buscar produto",
|
|
details: data,
|
|
};
|
|
}
|
|
|
|
const document = Array.isArray(data?.documents) && data.documents.length > 0 ? data.documents[0] : null;
|
|
|
|
return {
|
|
success: true,
|
|
data: {
|
|
document,
|
|
found: Boolean(document),
|
|
},
|
|
};
|
|
} catch (error) {
|
|
console.error("[CadastroProdutoService] Erro ao interpretar resposta de busca", error);
|
|
return {
|
|
success: false,
|
|
error: "Erro ao interpretar resposta da busca",
|
|
};
|
|
}
|
|
}
|
|
|
|
async enviarEtapa<T extends object>(etapa: 1 | 2 | 3, payload: T): Promise<ApiResponse> {
|
|
const body = {
|
|
etapa,
|
|
...payload,
|
|
};
|
|
|
|
return this.request("/api/cadastro-produto", {
|
|
method: "POST",
|
|
body: JSON.stringify(body),
|
|
});
|
|
}
|
|
}
|
|
|
|
export const cadastroProdutoService = new CadastroProdutoService();
|
|
|