Ajustes e melhorias no Codex
This commit is contained in:
@@ -71,7 +71,9 @@ export interface ParecerDetalhe {
|
||||
user_email: string;
|
||||
titulo: string;
|
||||
area_id: string;
|
||||
prompt_id: string;
|
||||
/** Nome da área vindo da API (evita depender só do cache de áreas no front). */
|
||||
area_nome?: string | null;
|
||||
prompt_id?: string | null;
|
||||
prompt_conteudo: string;
|
||||
instrucao: string;
|
||||
conteudo_gerado: string;
|
||||
@@ -84,6 +86,61 @@ export interface ParecerDetalhe {
|
||||
atualizado_por: string | null;
|
||||
}
|
||||
|
||||
/** Extrai o objeto de detalhe quando a API devolve array, objeto com `data`, ou mistura. */
|
||||
function extrairParecerDetalheRaw(raw: unknown): Record<string, unknown> | null {
|
||||
if (raw == null) return null;
|
||||
|
||||
if (Array.isArray(raw)) {
|
||||
const first = raw[0];
|
||||
if (first == null) return null;
|
||||
if (typeof first === "object" && !Array.isArray(first)) {
|
||||
const o = first as Record<string, unknown>;
|
||||
if (Array.isArray(o.data)) {
|
||||
const row = o.data[0];
|
||||
return row && typeof row === "object" && !Array.isArray(row) ? (row as Record<string, unknown>) : null;
|
||||
}
|
||||
if ("id" in o) return o;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
if (typeof raw === "object") {
|
||||
const o = raw as Record<string, unknown>;
|
||||
if (Array.isArray(o.data)) {
|
||||
const row = o.data[0];
|
||||
return row && typeof row === "object" && !Array.isArray(row) ? (row as Record<string, unknown>) : null;
|
||||
}
|
||||
if (o.data != null && typeof o.data === "object" && !Array.isArray(o.data) && "id" in (o.data as object)) {
|
||||
return o.data as Record<string, unknown>;
|
||||
}
|
||||
if ("id" in o) return o;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function normalizarParecerDetalhe(row: Record<string, unknown>): ParecerDetalhe {
|
||||
return {
|
||||
id: String(row.id ?? ""),
|
||||
estabelecimento_id: Number(row.estabelecimento_id ?? 0),
|
||||
user_email: String(row.user_email ?? ""),
|
||||
titulo: String(row.titulo ?? ""),
|
||||
area_id: String(row.area_id ?? ""),
|
||||
area_nome: row.area_nome != null ? String(row.area_nome) : null,
|
||||
prompt_id: row.prompt_id != null ? String(row.prompt_id) : null,
|
||||
prompt_conteudo: String(row.prompt_conteudo ?? ""),
|
||||
instrucao: String(row.instrucao ?? ""),
|
||||
conteudo_gerado: String(row.conteudo_gerado ?? ""),
|
||||
anexo_url: row.anexo_url != null ? String(row.anexo_url) : null,
|
||||
anexo_nome: row.anexo_nome != null ? String(row.anexo_nome) : null,
|
||||
status: String(row.status ?? ""),
|
||||
created_at: String(row.created_at ?? ""),
|
||||
updated_at: String(row.updated_at ?? ""),
|
||||
criado_por: row.criado_por != null ? String(row.criado_por) : null,
|
||||
atualizado_por: row.atualizado_por != null ? String(row.atualizado_por) : null,
|
||||
};
|
||||
}
|
||||
|
||||
export interface EditarParecerTituloAreaBody {
|
||||
titulo: string;
|
||||
area_id: string;
|
||||
@@ -231,13 +288,12 @@ class ParecerService {
|
||||
if (!id?.trim()) {
|
||||
throw new Error("ID do parecer é obrigatório");
|
||||
}
|
||||
const response = await apiService.get<ParecerDetalhe[] | ParecerDetalhe>(PARECER_DETALHE_URL(id));
|
||||
const raw = response.data;
|
||||
const item = Array.isArray(raw) ? raw[0] : raw;
|
||||
if (!item || typeof item !== "object" || !("id" in item)) {
|
||||
const response = await apiService.get<unknown>(PARECER_DETALHE_URL(id));
|
||||
const row = extrairParecerDetalheRaw(response.data);
|
||||
if (!row?.id) {
|
||||
throw new Error("Parecer não encontrado");
|
||||
}
|
||||
return item as ParecerDetalhe;
|
||||
return normalizarParecerDetalhe(row);
|
||||
}
|
||||
|
||||
async enviarMensagemChat(
|
||||
|
||||
@@ -74,6 +74,16 @@ export interface EditarPromptErrorResponse {
|
||||
missing_fields?: string[];
|
||||
}
|
||||
|
||||
export interface DeletarPromptSuccessResponse {
|
||||
success: true;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export interface DeletarPromptErrorResponse {
|
||||
success: false;
|
||||
message?: string;
|
||||
}
|
||||
|
||||
class PromptsService {
|
||||
async listar(params: ListarPromptsParams): Promise<ListarPromptsResponseBody> {
|
||||
const { page, per_page, titulo, area_id } = params;
|
||||
@@ -181,6 +191,33 @@ class PromptsService {
|
||||
|
||||
return data as EditarPromptSuccessResponse;
|
||||
}
|
||||
|
||||
async deletar(id: string): Promise<DeletarPromptSuccessResponse> {
|
||||
if (!id?.trim()) {
|
||||
throw new Error("ID do prompt é obrigatório");
|
||||
}
|
||||
|
||||
const idEnc = encodeURIComponent(id.trim());
|
||||
const url = `https://prod-hgtx-intelligence-n8n.hgtx.com.br/webhook/4e6c2374-9c22-4c81-b558-45f0cfefa5c3/codex/parecer/prompt/deletar/${idEnc}`;
|
||||
|
||||
const response = await apiService.delete<
|
||||
DeletarPromptSuccessResponse | DeletarPromptErrorResponse | (DeletarPromptSuccessResponse | DeletarPromptErrorResponse)[]
|
||||
>(url);
|
||||
|
||||
const raw = response.data;
|
||||
const data = Array.isArray(raw) ? raw[0] : raw;
|
||||
|
||||
if (!data || typeof data !== "object" || !("success" in data)) {
|
||||
throw new Error("Resposta inválida ao excluir prompt");
|
||||
}
|
||||
|
||||
if (data.success === false) {
|
||||
const err = data as DeletarPromptErrorResponse;
|
||||
throw new Error(err.message ?? "Erro ao excluir prompt.");
|
||||
}
|
||||
|
||||
return data as DeletarPromptSuccessResponse;
|
||||
}
|
||||
}
|
||||
|
||||
export const promptsService = new PromptsService();
|
||||
|
||||
Reference in New Issue
Block a user