Implementação do CRUD de modelos de relatório e menu de emissão de relatórios compilados com filtros e exportação DOCX/PDF
This commit is contained in:
+488
@@ -2481,6 +2481,494 @@ const initApp = () => {
|
||||
if (e.target === modal) modal.remove();
|
||||
});
|
||||
};
|
||||
|
||||
// ============================================================
|
||||
// CRUD DE MODELOS DE RELATÓRIO
|
||||
// ============================================================
|
||||
const modelosRelatorioModal = document.getElementById('modelosRelatorioModal');
|
||||
const btnModelosRelatorio = document.getElementById('btnModelosRelatorio');
|
||||
const btnCloseModelosModal = document.getElementById('btnCloseModelosModal');
|
||||
const templatesList = document.getElementById('templatesList');
|
||||
const btnCreateTemplate = document.getElementById('btnCreateTemplate');
|
||||
const btnCancelTemplate = document.getElementById('btnCancelTemplate');
|
||||
const btnSaveTemplate = document.getElementById('btnSaveTemplate');
|
||||
|
||||
const templateFormId = document.getElementById('templateFormId');
|
||||
const templateFormNome = document.getElementById('templateFormNome');
|
||||
const templateFormFinalidade = document.getElementById('templateFormFinalidade');
|
||||
const templateFormPeriodicidade = document.getElementById('templateFormPeriodicidade');
|
||||
const templateFormEstrutura = document.getElementById('templateFormEstrutura');
|
||||
const templateFormTitle = document.getElementById('templateFormTitle');
|
||||
|
||||
let allTemplates = [];
|
||||
|
||||
// Abrir modal de modelos
|
||||
if (btnModelosRelatorio) {
|
||||
btnModelosRelatorio.addEventListener('click', async () => {
|
||||
modelosRelatorioModal.style.display = 'flex';
|
||||
clearTemplateForm();
|
||||
await fetchTemplates();
|
||||
});
|
||||
}
|
||||
|
||||
// Fechar modal de modelos
|
||||
if (btnCloseModelosModal) {
|
||||
btnCloseModelosModal.addEventListener('click', () => {
|
||||
modelosRelatorioModal.style.display = 'none';
|
||||
});
|
||||
}
|
||||
if (modelosRelatorioModal) {
|
||||
modelosRelatorioModal.addEventListener('click', (e) => {
|
||||
if (e.target === modelosRelatorioModal) modelosRelatorioModal.style.display = 'none';
|
||||
});
|
||||
}
|
||||
|
||||
// Buscar modelos do backend
|
||||
async function fetchTemplates() {
|
||||
templatesList.innerHTML = '<div style="color: var(--text-secondary); text-align: center; padding: 20px;">Carregando...</div>';
|
||||
try {
|
||||
const res = await fetch('/api/modelos');
|
||||
allTemplates = await res.json();
|
||||
renderTemplatesList();
|
||||
} catch (err) {
|
||||
console.error('Erro ao buscar modelos:', err);
|
||||
templatesList.innerHTML = '<div style="color: #ff6b6b; text-align: center; padding: 20px;">Erro ao carregar modelos.</div>';
|
||||
}
|
||||
}
|
||||
|
||||
// Renderizar a lista de modelos na esquerda
|
||||
function renderTemplatesList() {
|
||||
if (allTemplates.length === 0) {
|
||||
templatesList.innerHTML = '<div style="color: var(--text-secondary); text-align: center; padding: 20px;">Nenhum modelo cadastrado.</div>';
|
||||
return;
|
||||
}
|
||||
templatesList.innerHTML = allTemplates.map(t => `
|
||||
<div class="obs-item" style="padding: 10px; margin: 0 0 8px 0; cursor: pointer; border-left: 3px solid var(--brand-green); display: flex; flex-direction: column; gap: 4px; background: var(--bg-secondary); border-radius: 8px;" data-id="${t.id}">
|
||||
<div style="font-weight: 600; color: var(--text-primary); font-size: 0.9rem;">${t.nome}</div>
|
||||
<div style="font-size: 0.75rem; color: var(--text-secondary);">${t.periodicidade} • ${t.finalidade || 'Sem finalidade'}</div>
|
||||
<div style="display: flex; gap: 8px; justify-content: flex-end; margin-top: 4px;">
|
||||
<button class="template-edit-btn" data-id="${t.id}" style="background: none; border: 1px solid var(--border-light); color: var(--brand-green); padding: 2px 8px; border-radius: 4px; font-size: 0.72rem; cursor: pointer;">Editar</button>
|
||||
<button class="template-delete-btn" data-id="${t.id}" style="background: none; border: 1px solid var(--border-light); color: #ff6b6b; padding: 2px 8px; border-radius: 4px; font-size: 0.72rem; cursor: pointer;">Excluir</button>
|
||||
</div>
|
||||
</div>
|
||||
`).join('');
|
||||
|
||||
// Eventos de clique na lista para editar/deletar
|
||||
templatesList.querySelectorAll('.obs-item').forEach(item => {
|
||||
item.addEventListener('click', (e) => {
|
||||
if (e.target.tagName === 'BUTTON') return;
|
||||
const id = item.dataset.id;
|
||||
const template = allTemplates.find(t => t.id == id);
|
||||
if (template) fillTemplateForm(template);
|
||||
});
|
||||
});
|
||||
|
||||
templatesList.querySelectorAll('.template-edit-btn').forEach(btn => {
|
||||
btn.addEventListener('click', (e) => {
|
||||
e.stopPropagation();
|
||||
const id = btn.dataset.id;
|
||||
const template = allTemplates.find(t => t.id == id);
|
||||
if (template) fillTemplateForm(template);
|
||||
});
|
||||
});
|
||||
|
||||
templatesList.querySelectorAll('.template-delete-btn').forEach(btn => {
|
||||
btn.addEventListener('click', async (e) => {
|
||||
e.stopPropagation();
|
||||
const id = btn.dataset.id;
|
||||
const template = allTemplates.find(t => t.id == id);
|
||||
if (!template) return;
|
||||
if (confirm(`Tem certeza que deseja excluir o modelo "${template.nome}"?`)) {
|
||||
try {
|
||||
const res = await fetch(`/api/modelos/${id}`, { method: 'DELETE' });
|
||||
if (res.ok) {
|
||||
alert('Modelo excluído com sucesso!');
|
||||
clearTemplateForm();
|
||||
await fetchTemplates();
|
||||
} else {
|
||||
const err = await res.json();
|
||||
alert('Erro ao excluir: ' + err.error);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Erro ao excluir modelo:', err);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Preencher formulário de edição
|
||||
function fillTemplateForm(template) {
|
||||
templateFormId.value = template.id;
|
||||
templateFormNome.value = template.nome;
|
||||
templateFormFinalidade.value = template.finalidade || '';
|
||||
templateFormPeriodicidade.value = template.periodicidade || 'Semanal';
|
||||
templateFormEstrutura.value = template.estrutura || '';
|
||||
templateFormTitle.textContent = 'Editar Modelo';
|
||||
}
|
||||
|
||||
// Limpar formulário
|
||||
function clearTemplateForm() {
|
||||
templateFormId.value = '';
|
||||
templateFormNome.value = '';
|
||||
templateFormFinalidade.value = '';
|
||||
templateFormPeriodicidade.value = 'Semanal';
|
||||
templateFormEstrutura.value = '';
|
||||
templateFormTitle.textContent = 'Novo Modelo';
|
||||
}
|
||||
|
||||
// Botão "Novo Modelo"
|
||||
if (btnCreateTemplate) {
|
||||
btnCreateTemplate.addEventListener('click', clearTemplateForm);
|
||||
}
|
||||
|
||||
// Botão "Cancelar"
|
||||
if (btnCancelTemplate) {
|
||||
btnCancelTemplate.addEventListener('click', clearTemplateForm);
|
||||
}
|
||||
|
||||
// Botão "Salvar Modelo"
|
||||
if (btnSaveTemplate) {
|
||||
btnSaveTemplate.addEventListener('click', async () => {
|
||||
const id = templateFormId.value;
|
||||
const nome = templateFormNome.value.trim();
|
||||
const finalidade = templateFormFinalidade.value.trim();
|
||||
const periodicidade = templateFormPeriodicidade.value;
|
||||
const estrutura = templateFormEstrutura.value.trim();
|
||||
|
||||
if (!nome || !estrutura) {
|
||||
alert('Nome e estrutura do modelo são obrigatórios.');
|
||||
return;
|
||||
}
|
||||
|
||||
const body = { nome, finalidade, periodicidade, estrutura };
|
||||
const url = id ? `/api/modelos/${id}` : '/api/modelos';
|
||||
const method = id ? 'PUT' : 'POST';
|
||||
|
||||
try {
|
||||
const res = await fetch(url, {
|
||||
method,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body)
|
||||
});
|
||||
if (res.ok) {
|
||||
alert('Modelo salvo com sucesso!');
|
||||
clearTemplateForm();
|
||||
await fetchTemplates();
|
||||
} else {
|
||||
const err = await res.json();
|
||||
alert('Erro ao salvar modelo: ' + err.error);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Erro ao salvar modelo:', err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// GERAÇÃO / EMISSÃO DE RELATÓRIO PEDAGÓGICO
|
||||
// ============================================================
|
||||
const emitirRelatorioModal = document.getElementById('emitirRelatorioModal');
|
||||
const btnEmitirRelatorio = document.getElementById('btnEmitirRelatorio');
|
||||
const btnCloseEmitirModal = document.getElementById('btnCloseEmitirModal');
|
||||
|
||||
const emitirFilterCrianca = document.getElementById('emitirFilterCrianca');
|
||||
const emitirFilterDataInicio = document.getElementById('emitirFilterDataInicio');
|
||||
const emitirFilterDataFim = document.getElementById('emitirFilterDataFim');
|
||||
const emitirFilterTag = document.getElementById('emitirFilterTag');
|
||||
const emitirFilterTemplate = document.getElementById('emitirFilterTemplate');
|
||||
|
||||
const emitirObsCountInfo = document.getElementById('emitirObsCountInfo');
|
||||
const btnGenerateCompiledReport = document.getElementById('btnGenerateCompiledReport');
|
||||
const emitirReportPreviewArea = document.getElementById('emitirReportPreviewArea');
|
||||
|
||||
const btnExportDocx = document.getElementById('btnExportDocx');
|
||||
const btnExportPdf = document.getElementById('btnExportPdf');
|
||||
|
||||
let activeCompiledReport = null; // Guarda o texto do relatório compilado ativo
|
||||
|
||||
// Abrir modal de emissão
|
||||
if (btnEmitirRelatorio) {
|
||||
btnEmitirRelatorio.addEventListener('click', async () => {
|
||||
emitirRelatorioModal.style.display = 'flex';
|
||||
activeCompiledReport = null;
|
||||
emitirReportPreviewArea.innerHTML = `
|
||||
<div style="color: var(--text-secondary); text-align: center; margin-top: 50px;">
|
||||
Escolha uma criança e um modelo de relatório para começar.
|
||||
</div>
|
||||
`;
|
||||
btnExportDocx.disabled = true;
|
||||
btnExportDocx.style.cursor = 'not-allowed';
|
||||
btnExportDocx.style.borderColor = 'var(--border-light)';
|
||||
btnExportDocx.style.color = 'var(--text-secondary)';
|
||||
|
||||
btnExportPdf.disabled = true;
|
||||
btnExportPdf.style.cursor = 'not-allowed';
|
||||
btnExportPdf.style.borderColor = 'var(--border-light)';
|
||||
btnExportPdf.style.color = 'var(--text-secondary)';
|
||||
|
||||
emitirFilterDataInicio.value = '';
|
||||
emitirFilterDataFim.value = '';
|
||||
emitirFilterTag.value = '';
|
||||
|
||||
await loadFiltersData();
|
||||
});
|
||||
}
|
||||
|
||||
// Fechar modal de emissão
|
||||
if (btnCloseEmitirModal) {
|
||||
btnCloseEmitirModal.addEventListener('click', () => {
|
||||
emitirRelatorioModal.style.display = 'none';
|
||||
});
|
||||
}
|
||||
if (emitirRelatorioModal) {
|
||||
emitirRelatorioModal.addEventListener('click', (e) => {
|
||||
if (e.target === emitirRelatorioModal) emitirRelatorioModal.style.display = 'none';
|
||||
});
|
||||
}
|
||||
|
||||
// Carregar dados de filtros (crianças e templates)
|
||||
async function loadFiltersData() {
|
||||
try {
|
||||
const res = await fetch('/api/observacoes/criancas');
|
||||
const criancas = await res.json();
|
||||
emitirFilterCrianca.innerHTML = '<option value="">Selecione a criança</option>' +
|
||||
criancas.map(c => `<option value="${c}">${c}</option>`).join('');
|
||||
} catch (err) {
|
||||
console.error('Erro ao buscar crianças para filtros:', err);
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch('/api/modelos');
|
||||
const modelos = await res.json();
|
||||
emitirFilterTemplate.innerHTML = '<option value="">Selecione o modelo</option>' +
|
||||
modelos.map(m => `<option value="${m.id}">${m.nome}</option>`).join('');
|
||||
} catch (err) {
|
||||
console.error('Erro ao buscar modelos para filtros:', err);
|
||||
}
|
||||
|
||||
updateObservationsCount();
|
||||
}
|
||||
|
||||
// Ouvintes para atualizar contagem
|
||||
[emitirFilterCrianca, emitirFilterDataInicio, emitirFilterDataFim, emitirFilterTag, emitirFilterTemplate].forEach(el => {
|
||||
if (el) el.addEventListener('change', updateObservationsCount);
|
||||
});
|
||||
if (emitirFilterCrianca) {
|
||||
emitirFilterCrianca.addEventListener('input', updateObservationsCount);
|
||||
}
|
||||
|
||||
// Recalcular quantidade de observações elegíveis
|
||||
async function updateObservationsCount() {
|
||||
const crianca = emitirFilterCrianca ? emitirFilterCrianca.value : '';
|
||||
const dataInicio = emitirFilterDataInicio ? emitirFilterDataInicio.value : '';
|
||||
const dataFim = emitirFilterDataFim ? emitirFilterDataFim.value : '';
|
||||
const tag = emitirFilterTag ? emitirFilterTag.value : '';
|
||||
const modeloId = emitirFilterTemplate ? emitirFilterTemplate.value : '';
|
||||
|
||||
if (!crianca || !modeloId) {
|
||||
emitirObsCountInfo.textContent = 'Escolha uma criança e um modelo para prosseguir.';
|
||||
btnGenerateCompiledReport.disabled = true;
|
||||
btnGenerateCompiledReport.style.opacity = '0.6';
|
||||
btnGenerateCompiledReport.style.cursor = 'not-allowed';
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
let url = `/api/observacoes/contar?crianca=${encodeURIComponent(crianca)}`;
|
||||
if (dataInicio) url += `&dataInicio=${dataInicio}`;
|
||||
if (dataFim) url += `&dataFim=${dataFim}`;
|
||||
if (tag) url += `&tags=${encodeURIComponent(tag)}`;
|
||||
|
||||
const res = await fetch(url);
|
||||
const data = await res.json();
|
||||
const count = data.count || 0;
|
||||
|
||||
if (count === 0) {
|
||||
emitirObsCountInfo.innerHTML = `⚠️ <span style="color: #ff6b6b; font-weight: 600;">Nenhuma observação</span> encontrada para esses filtros.`;
|
||||
btnGenerateCompiledReport.disabled = true;
|
||||
btnGenerateCompiledReport.style.opacity = '0.6';
|
||||
btnGenerateCompiledReport.style.cursor = 'not-allowed';
|
||||
} else {
|
||||
emitirObsCountInfo.innerHTML = `✨ <strong>${count}</strong> observações selecionadas para compilação.`;
|
||||
btnGenerateCompiledReport.disabled = false;
|
||||
btnGenerateCompiledReport.style.opacity = '1';
|
||||
btnGenerateCompiledReport.style.cursor = 'pointer';
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Erro ao contar observações:', err);
|
||||
}
|
||||
}
|
||||
|
||||
// Gerar relatório consolidado com IA
|
||||
if (btnGenerateCompiledReport) {
|
||||
btnGenerateCompiledReport.addEventListener('click', async () => {
|
||||
const crianca = emitirFilterCrianca.value;
|
||||
const dataInicio = emitirFilterDataInicio.value;
|
||||
const dataFim = emitirFilterDataFim.value;
|
||||
const tag = emitirFilterTag.value;
|
||||
const modeloId = emitirFilterTemplate.value;
|
||||
|
||||
if (!crianca || !modeloId) return;
|
||||
|
||||
emitirReportPreviewArea.innerHTML = `
|
||||
<div style="display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100%; gap: 12px; margin-top: 50px;">
|
||||
<div style="width: 32px; height: 32px; border: 3px solid var(--border-light); border-top-color: var(--brand-green); border-radius: 50%; animation: spin 1s linear infinite;"></div>
|
||||
<div style="color: var(--text-secondary); font-size: 0.9rem; font-weight: 500;">Analisando observações e gerando relatório pedagógico...</div>
|
||||
</div>
|
||||
`;
|
||||
btnGenerateCompiledReport.disabled = true;
|
||||
btnGenerateCompiledReport.style.opacity = '0.6';
|
||||
|
||||
try {
|
||||
const res = await fetch('/api/modelos/gerar-relatorio', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
crianca,
|
||||
dataInicio,
|
||||
dataFim,
|
||||
tags: tag || undefined,
|
||||
modeloId
|
||||
})
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const errData = await res.json();
|
||||
throw new Error(errData.error || 'Erro desconhecido');
|
||||
}
|
||||
|
||||
const data = await res.json();
|
||||
activeCompiledReport = data;
|
||||
|
||||
// Renderizar prévia do relatório em markdown
|
||||
emitirReportPreviewArea.innerHTML = `
|
||||
<div style="background: var(--bg-primary); padding: 12px; border-radius: 8px; margin-bottom: 16px; border: 1px solid var(--border-light); font-size: 0.85rem;">
|
||||
<div><strong>Estudante:</strong> ${data.crianca}</div>
|
||||
<div><strong>Período:</strong> ${data.periodo}</div>
|
||||
<div><strong>Modelo Aplicado:</strong> ${data.templateNome}</div>
|
||||
<div><strong>Quantidade de Observações Consolidadas:</strong> ${data.totalObservacoes}</div>
|
||||
</div>
|
||||
<div class="report-content-body" style="color: var(--text-primary);">
|
||||
${marked.parse(data.report)}
|
||||
</div>
|
||||
`;
|
||||
|
||||
// Ativar botões de exportação
|
||||
btnExportDocx.disabled = false;
|
||||
btnExportDocx.style.cursor = 'pointer';
|
||||
btnExportDocx.style.borderColor = 'var(--brand-green)';
|
||||
btnExportDocx.style.color = 'var(--brand-green)';
|
||||
|
||||
btnExportPdf.disabled = false;
|
||||
btnExportPdf.style.cursor = 'pointer';
|
||||
btnExportPdf.style.borderColor = 'var(--brand-green)';
|
||||
btnExportPdf.style.color = 'var(--brand-green)';
|
||||
|
||||
} catch (err) {
|
||||
console.error('Erro ao gerar relatório compilado:', err);
|
||||
emitirReportPreviewArea.innerHTML = `
|
||||
<div style="color: #ff6b6b; text-align: center; margin-top: 50px; font-weight: 500;">
|
||||
Erro ao gerar relatório: ${err.message}
|
||||
</div>
|
||||
`;
|
||||
} finally {
|
||||
btnGenerateCompiledReport.disabled = false;
|
||||
btnGenerateCompiledReport.style.opacity = '1';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Exportar para DOCX (Formato HTML-DOCX)
|
||||
if (btnExportDocx) {
|
||||
btnExportDocx.addEventListener('click', () => {
|
||||
if (!activeCompiledReport) return;
|
||||
|
||||
const htmlContent = `
|
||||
<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:office:word" xmlns="http://www.w3.org/TR/REC-html40">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Relatório Pedagógico - ${activeCompiledReport.crianca}</title>
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; line-height: 1.5; color: #333; }
|
||||
h1, h2, h3 { color: #1f2937; }
|
||||
.header-info { background-color: #f3f4f6; padding: 12px; border-radius: 6px; margin-bottom: 20px; border: 1px solid #e5e7eb; }
|
||||
.header-info div { margin-bottom: 4px; font-size: 11pt; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="header-info">
|
||||
<div><b>Estudante:</b> ${activeCompiledReport.crianca}</div>
|
||||
<div><b>Período:</b> ${activeCompiledReport.periodo}</div>
|
||||
<div><b>Modelo:</b> ${activeCompiledReport.templateNome}</div>
|
||||
<div><b>Quantidade de observações consolidadas:</b> ${activeCompiledReport.totalObservacoes}</div>
|
||||
</div>
|
||||
<hr/>
|
||||
${marked.parse(activeCompiledReport.report)}
|
||||
</body>
|
||||
</html>
|
||||
`;
|
||||
|
||||
const blob = new Blob(['\ufeff' + htmlContent], { type: 'application/msword' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = `relatorio_${activeCompiledReport.crianca.replace(/\s+/g, '_')}_${Date.now()}.doc`;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
URL.revokeObjectURL(url);
|
||||
});
|
||||
}
|
||||
|
||||
// Exportar para PDF (Abre janela de impressão do navegador)
|
||||
if (btnExportPdf) {
|
||||
btnExportPdf.addEventListener('click', () => {
|
||||
if (!activeCompiledReport) return;
|
||||
|
||||
const printWindow = window.open('', '_blank');
|
||||
printWindow.document.write(`
|
||||
<html>
|
||||
<head>
|
||||
<title>Relatório Pedagógico - ${activeCompiledReport.crianca}</title>
|
||||
<style>
|
||||
body { font-family: 'Outfit', 'Inter', -apple-system, sans-serif; padding: 40px; color: #333; line-height: 1.6; }
|
||||
h1, h2, h3 { color: #1e293b; margin-top: 24px; margin-bottom: 12px; }
|
||||
h1 { border-bottom: 2px solid #e2e8f0; padding-bottom: 10px; font-size: 22px; }
|
||||
h2 { font-size: 18px; border-bottom: 1px solid #f1f5f9; padding-bottom: 6px; }
|
||||
.meta-box { background: #f8fafc; padding: 16px; border-radius: 8px; margin-bottom: 24px; font-size: 14px; border: 1px solid #e2e8f0; }
|
||||
.meta-item { margin-bottom: 6px; display: flex; }
|
||||
.meta-label { font-weight: bold; color: #475569; width: 220px; }
|
||||
p { margin-bottom: 14px; text-align: justify; }
|
||||
ul, ol { margin-bottom: 14px; padding-left: 20px; }
|
||||
li { margin-bottom: 6px; }
|
||||
@media print {
|
||||
body { padding: 0; }
|
||||
button { display: none; }
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Relatório Pedagógico de Desenvolvimento</h1>
|
||||
<div class="meta-box">
|
||||
<div class="meta-item"><span class="meta-label">Estudante:</span> <span>${activeCompiledReport.crianca}</span></div>
|
||||
<div class="meta-item"><span class="meta-label">Período:</span> <span>${activeCompiledReport.periodo}</span></div>
|
||||
<div class="meta-item"><span class="meta-label">Modelo Aplicado:</span> <span>${activeCompiledReport.templateNome}</span></div>
|
||||
<div class="meta-item"><span class="meta-label">Quantidade de Observações:</span> <span>${activeCompiledReport.totalObservacoes}</span></div>
|
||||
</div>
|
||||
<div>${marked.parse(activeCompiledReport.report)}</div>
|
||||
<script>
|
||||
window.onload = function() {
|
||||
window.print();
|
||||
setTimeout(() => { window.close(); }, 500);
|
||||
};
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
`);
|
||||
printWindow.document.close();
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// Player de áudio personalizado global para as mídias geradas
|
||||
|
||||
@@ -73,6 +73,22 @@
|
||||
<span>Minhas Observações</span>
|
||||
</button>
|
||||
|
||||
<!-- Botão: Emitir Relatório -->
|
||||
<button class="btn-observacoes" id="btnEmitirRelatorio" style="border-color: var(--brand-green); color: var(--brand-green); background: rgba(16, 163, 127, 0.05);">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M19.5 14.25v-2.625a3.375 3.375 0 0 0-3.375-3.375h-1.5A1.125 1.125 0 0 1 13.5 7.125v-1.5a3.375 3.375 0 0 0-3.375-3.375H8.25m0 12.75h7.5m-7.5 3H12M10.5 2.25H5.625c-.621 0-1.125.504-1.125 1.125v17.25c0 .621.504 1.125 1.125 1.125h12.75c.621 0 1.125-.504 1.125-1.125V11.25a9 9 0 0 0-9-9Z" />
|
||||
</svg>
|
||||
<span>Emitir Relatório</span>
|
||||
</button>
|
||||
|
||||
<!-- Botão: Modelos de Relatório -->
|
||||
<button class="btn-observacoes" id="btnModelosRelatorio">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
|
||||
</svg>
|
||||
<span>Modelos de Relatório</span>
|
||||
</button>
|
||||
|
||||
<!-- Histórico de Conversas -->
|
||||
<div class="chat-history" id="chatHistory">
|
||||
<!-- Histórico filtrado/busca -->
|
||||
@@ -449,6 +465,155 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Modal: Modelos de Relatório (CRUD Templates) -->
|
||||
<div id="modelosRelatorioModal" class="settings-modal" style="display: none;">
|
||||
<div class="settings-modal-content" style="max-width: 800px; width: 95%; max-height: 90vh; display: flex; flex-direction: column;">
|
||||
<div class="settings-modal-header">
|
||||
<h3>📋 Modelos de Relatório</h3>
|
||||
<button id="btnCloseModelosModal" class="btn-close-modal">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18 18 6M6 6l12 12" />
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
<div class="settings-modal-body" style="padding: 16px; overflow: hidden; display: flex; flex-direction: row; gap: 16px; flex: 1; min-height: 420px;">
|
||||
<!-- Lado Esquerdo: Lista de Modelos -->
|
||||
<div style="flex: 1; border-right: 1px solid var(--border-light); padding-right: 16px; display: flex; flex-direction: column; gap: 12px; overflow-y: auto;">
|
||||
<div style="display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid var(--border-light); padding-bottom: 8px;">
|
||||
<h4 style="margin: 0; color: var(--text-primary);">Modelos Salvos</h4>
|
||||
<button id="btnCreateTemplate" style="background: var(--brand-green); color: white; border: none; padding: 6px 12px; border-radius: 6px; font-size: 0.85rem; font-weight: 600; cursor: pointer; transition: opacity 0.2s;">+ Novo Modelo</button>
|
||||
</div>
|
||||
<div id="templatesList" style="display: flex; flex-direction: column; gap: 8px;">
|
||||
<!-- Carregado via JS -->
|
||||
<div style="color: var(--text-secondary); text-align: center; padding: 20px;">Carregando modelos...</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Lado Direito: Formulário de Cadastro/Edição -->
|
||||
<div id="templateFormContainer" style="flex: 1.2; display: flex; flex-direction: column; gap: 12px; overflow-y: auto; padding-left: 4px;">
|
||||
<h4 id="templateFormTitle" style="margin: 0; color: var(--text-primary); border-bottom: 1px solid var(--border-light); padding-bottom: 8px;">Editar Modelo</h4>
|
||||
<input type="hidden" id="templateFormId">
|
||||
|
||||
<div class="settings-group" style="gap: 4px;">
|
||||
<label style="color: var(--text-secondary); font-size: 0.8rem; font-weight: 500;">Nome do Modelo</label>
|
||||
<input type="text" id="templateFormNome" placeholder="Ex: Relatório Semanal de Desenvolvimento" class="obs-input" style="width: 100%;">
|
||||
</div>
|
||||
|
||||
<div class="settings-group" style="gap: 4px;">
|
||||
<label style="color: var(--text-secondary); font-size: 0.8rem; font-weight: 500;">Finalidade</label>
|
||||
<input type="text" id="templateFormFinalidade" placeholder="Ex: Avaliar a evolução socioemocional..." class="obs-input" style="width: 100%;">
|
||||
</div>
|
||||
|
||||
<div class="settings-group" style="gap: 4px;">
|
||||
<label style="color: var(--text-secondary); font-size: 0.8rem; font-weight: 500;">Periodicidade / Frequência</label>
|
||||
<select id="templateFormPeriodicidade" class="obs-select" style="width: 100%;">
|
||||
<option value="Semanal">Semanal</option>
|
||||
<option value="Mensal">Mensal</option>
|
||||
<option value="Semestral">Semestral</option>
|
||||
<option value="Anual">Anual</option>
|
||||
<option value="Outro">Outro</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="settings-group" style="flex: 1; display: flex; flex-direction: column; gap: 4px;">
|
||||
<label style="color: var(--text-secondary); font-size: 0.8rem; font-weight: 500;">Estrutura do Relatório (Markdown / Diretrizes)</label>
|
||||
<textarea id="templateFormEstrutura" placeholder="## Seção 1\n- Detalhe o comportamento...\n\n## Seção 2\n- Descreva a autonomia..." class="obs-input" style="width: 100%; flex: 1; min-height: 160px; font-family: monospace; resize: vertical; padding: 10px; line-height: 1.4;"></textarea>
|
||||
</div>
|
||||
|
||||
<div style="display: flex; gap: 8px; justify-content: flex-end; border-top: 1px solid var(--border-light); padding-top: 12px; margin-top: auto;">
|
||||
<button id="btnCancelTemplate" style="background: none; border: 1px solid var(--border-light); color: var(--text-secondary); padding: 8px 16px; border-radius: 8px; cursor: pointer; font-size: 0.9rem;">Cancelar</button>
|
||||
<button id="btnSaveTemplate" style="background: var(--brand-green); border: none; color: white; padding: 8px 16px; border-radius: 8px; font-weight: 600; cursor: pointer; font-size: 0.9rem;">Salvar Modelo</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Modal: Emitir Relatório (Geração com filtros e exportação) -->
|
||||
<div id="emitirRelatorioModal" class="settings-modal" style="display: none;">
|
||||
<div class="settings-modal-content" style="max-width: 800px; width: 95%; max-height: 90vh; display: flex; flex-direction: column;">
|
||||
<div class="settings-modal-header">
|
||||
<h3>📄 Emitir Relatório Pedagógico</h3>
|
||||
<button id="btnCloseEmitirModal" class="btn-close-modal">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18 18 6M6 6l12 12" />
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
<div class="settings-modal-body" style="padding: 16px; overflow: hidden; display: flex; flex-direction: column; gap: 12px; flex: 1;">
|
||||
<!-- Filtros e Controles -->
|
||||
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; background: var(--bg-secondary); padding: 12px; border-radius: 12px; border: 1px solid var(--border-light);">
|
||||
<div class="settings-group" style="gap: 4px;">
|
||||
<label style="color: var(--text-secondary); font-size: 0.78rem; font-weight: 600;">Criança</label>
|
||||
<select id="emitirFilterCrianca" class="obs-select" style="padding: 6px 10px;">
|
||||
<option value="">Selecione a criança</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="settings-group" style="gap: 4px;">
|
||||
<label style="color: var(--text-secondary); font-size: 0.78rem; font-weight: 600;">Data Início</label>
|
||||
<input type="date" id="emitirFilterDataInicio" class="obs-input" style="padding: 5px 8px;">
|
||||
</div>
|
||||
|
||||
<div class="settings-group" style="gap: 4px;">
|
||||
<label style="color: var(--text-secondary); font-size: 0.78rem; font-weight: 600;">Data Fim</label>
|
||||
<input type="date" id="emitirFilterDataFim" class="obs-input" style="padding: 5px 8px;">
|
||||
</div>
|
||||
|
||||
<div class="settings-group" style="gap: 4px;">
|
||||
<label style="color: var(--text-secondary); font-size: 0.78rem; font-weight: 600;">Tags</label>
|
||||
<select id="emitirFilterTag" class="obs-select" style="padding: 6px 10px;">
|
||||
<option value="">Todas as tags</option>
|
||||
<option value="social">Social</option>
|
||||
<option value="cognitivo">Cognitivo</option>
|
||||
<option value="motor">Motor</option>
|
||||
<option value="linguagem">Linguagem</option>
|
||||
<option value="emocional">Emocional</option>
|
||||
<option value="colaborativo">Colaborativo</option>
|
||||
<option value="autônomo">Autônomo</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="settings-group" style="gap: 4px;">
|
||||
<label style="color: var(--text-secondary); font-size: 0.78rem; font-weight: 600;">Modelo / Template</label>
|
||||
<select id="emitirFilterTemplate" class="obs-select" style="padding: 6px 10px;">
|
||||
<option value="">Selecione o modelo</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Estatísticas e Botão de Ação -->
|
||||
<div style="display: flex; justify-content: space-between; align-items: center; padding: 0 4px;">
|
||||
<span id="emitirObsCountInfo" style="color: var(--text-secondary); font-size: 0.88rem; font-weight: 500;">Selecione uma criança para buscar observações...</span>
|
||||
<button id="btnGenerateCompiledReport" disabled style="background: var(--brand-green); color: white; border: none; padding: 8px 18px; border-radius: 8px; font-weight: 600; cursor: pointer; display: flex; align-items: center; gap: 8px; transition: opacity 0.2s; opacity: 0.6;">
|
||||
<span>Gerar Relatório com IA</span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- Visualizador / Pré-visualização do Relatório Gerado -->
|
||||
<div style="flex: 1; border: 1px solid var(--border-light); border-radius: 12px; overflow: hidden; display: flex; flex-direction: column; background: var(--bg-secondary);">
|
||||
<div style="padding: 8px 16px; border-bottom: 1px solid var(--border-light); background: var(--bg-primary); display: flex; justify-content: space-between; align-items: center;">
|
||||
<span style="font-weight: 600; font-size: 0.88rem; color: var(--text-primary);">Visualização do Relatório</span>
|
||||
<div style="display: flex; gap: 8px;">
|
||||
<button id="btnExportDocx" disabled style="background: none; border: 1px solid var(--border-light); color: var(--text-secondary); padding: 5px 12px; border-radius: 6px; font-size: 0.8rem; cursor: not-allowed; display: flex; align-items: center; gap: 4px; font-weight: 500; transition: all 0.2s;">
|
||||
💾 DOCX
|
||||
</button>
|
||||
<button id="btnExportPdf" disabled style="background: none; border: 1px solid var(--border-light); color: var(--text-secondary); padding: 5px 12px; border-radius: 6px; font-size: 0.8rem; cursor: not-allowed; display: flex; align-items: center; gap: 4px; font-weight: 500; transition: all 0.2s;">
|
||||
🖨️ PDF
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="emitirReportPreviewArea" style="flex: 1; padding: 16px; overflow-y: auto; color: var(--text-primary); line-height: 1.6; font-size: 0.95rem;">
|
||||
<div style="color: var(--text-secondary); text-align: center; margin-top: 50px;">
|
||||
Escolha uma criança e um modelo de relatório para começar.
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Bibliotecas externas para renderizar markdown e blocos de código -->
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/12.0.1/marked.min.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
|
||||
|
||||
@@ -238,6 +238,37 @@ function writeObsIndex(idx) {
|
||||
// Inicializar banco de dados e migrar dados se estiver vazio
|
||||
async function initDatabase() {
|
||||
try {
|
||||
// 0. Garantir que a tabela de modelos exista e tenha dados padrão
|
||||
await dbPool.query(`
|
||||
CREATE TABLE IF NOT EXISTS escola.modelos (
|
||||
id SERIAL PRIMARY KEY,
|
||||
nome VARCHAR(255) NOT NULL,
|
||||
finalidade TEXT,
|
||||
periodicidade VARCHAR(100),
|
||||
estrutura TEXT,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
`);
|
||||
|
||||
const checkModelos = await dbPool.query("SELECT COUNT(*) FROM escola.modelos;");
|
||||
if (parseInt(checkModelos.rows[0].count) === 0) {
|
||||
console.log('[Database Init] Inserindo modelos/templates padrão de relatório...');
|
||||
await dbPool.query(`
|
||||
INSERT INTO escola.modelos (nome, finalidade, periodicidade, estrutura) VALUES
|
||||
($1, $2, $3, $4),
|
||||
($5, $6, $7, $8);
|
||||
`, [
|
||||
"Relatório Semanal de Desenvolvimento",
|
||||
"Acompanhar a evolução cognitiva e social das crianças semanalmente",
|
||||
"Semanal",
|
||||
`# Relatório Semanal de Desenvolvimento Pedagógico\n\n## 1. Aspectos Cognitivos e Linguagem\n- Como a criança interagiu com os materiais e desafios propostos esta semana.\n- Expressão verbal, argumentação e participação em rodas de conversa.\n\n## 2. Aspectos Socioemocionais e Autonomia\n- Colaboração com os colegas e resolução de pequenos conflitos.\n- Nível de independência na realização das atividades diárias.\n\n## 3. Aspectos Motores e Brincar\n- Coordenação motora ampla e fina durante as brincadeiras e atividades direcionadas.`,
|
||||
"Avaliação Semestral Individual",
|
||||
"Avaliar de forma global o progresso do aluno ao fim do semestre",
|
||||
"Semestral",
|
||||
`# Avaliação Semestral Individual - Educação Infantil\n\n## Introdução\nContextualização geral da adaptação e participação do aluno no semestre.\n\n## I. O Eu, o Outro e o Nós\n- Relações sociais, respeito às regras e empatia.\n\n## II. Corpo, Gestos e Movimentos\n- Expressividade, habilidades motoras e exploração do espaço.\n\n## III. Traços, Sons, Cores e Formas\n- Sensibilidade estética, linguagem artística e musical.\n\n## IV. Escuta, Fala, Pensamento e Imaginação\n- Oralidade, repertório de histórias e início da escrita/grafismo.\n\n## V. Espaços, Tempos, Quantidades, Relações e Transformações\n- Raciocínio lógico-matemático e curiosidade científica.`
|
||||
]);
|
||||
}
|
||||
|
||||
// 1. Sincronizar cache de configurações do agente ou migrar
|
||||
const configRes = await dbPool.query("SELECT agent_name, kemily_avatar_url, camila_avatar_url, temperature_preset FROM escola.config WHERE key = 'current';");
|
||||
if (configRes.rows.length > 0) {
|
||||
@@ -660,6 +691,317 @@ app.post('/api/observacao/salvar', requireAuth, async (req, res) => {
|
||||
res.json({ success: true, filename, id });
|
||||
});
|
||||
|
||||
// ============================================================
|
||||
// CRUD DE MODELOS DE RELATÓRIO (TEMPLATES) & COMPILAÇÃO
|
||||
// ============================================================
|
||||
|
||||
// Listar todos os modelos
|
||||
app.get('/api/modelos', requireAuth, async (req, res) => {
|
||||
try {
|
||||
const dbRes = await dbPool.query("SELECT * FROM escola.modelos ORDER BY nome ASC;");
|
||||
res.json(dbRes.rows);
|
||||
} catch (err) {
|
||||
console.error('Erro ao buscar modelos:', err);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Criar novo modelo
|
||||
app.post('/api/modelos', requireAuth, async (req, res) => {
|
||||
const { nome, finalidade, periodicidade, estrutura } = req.body;
|
||||
if (!nome || !estrutura) return res.status(400).json({ error: 'Nome e estrutura são obrigatórios' });
|
||||
try {
|
||||
const dbRes = await dbPool.query(
|
||||
"INSERT INTO escola.modelos (nome, finalidade, periodicidade, estrutura) VALUES ($1, $2, $3, $4) RETURNING *;",
|
||||
[nome, finalidade, periodicidade, estrutura]
|
||||
);
|
||||
res.status(201).json(dbRes.rows[0]);
|
||||
} catch (err) {
|
||||
console.error('Erro ao criar modelo:', err);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Atualizar modelo existente
|
||||
app.put('/api/modelos/:id', requireAuth, async (req, res) => {
|
||||
const { id } = req.params;
|
||||
const { nome, finalidade, periodicidade, estrutura } = req.body;
|
||||
if (!nome || !estrutura) return res.status(400).json({ error: 'Nome e estrutura são obrigatórios' });
|
||||
try {
|
||||
const dbRes = await dbPool.query(
|
||||
"UPDATE escola.modelos SET nome = $1, finalidade = $2, periodicidade = $3, estrutura = $4 WHERE id = $5 RETURNING *;",
|
||||
[nome, finalidade, periodicidade, estrutura, id]
|
||||
);
|
||||
if (dbRes.rows.length === 0) return res.status(404).json({ error: 'Modelo não encontrado' });
|
||||
res.json(dbRes.rows[0]);
|
||||
} catch (err) {
|
||||
console.error('Erro ao atualizar modelo:', err);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Excluir modelo
|
||||
app.delete('/api/modelos/:id', requireAuth, async (req, res) => {
|
||||
const { id } = req.params;
|
||||
try {
|
||||
const dbRes = await dbPool.query("DELETE FROM escola.modelos WHERE id = $1 RETURNING *;", [id]);
|
||||
if (dbRes.rows.length === 0) return res.status(404).json({ error: 'Modelo não encontrado' });
|
||||
res.json({ success: true, message: 'Modelo excluído com sucesso' });
|
||||
} catch (err) {
|
||||
console.error('Erro ao excluir modelo:', err);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Obter lista única de crianças já observadas
|
||||
app.get('/api/observacoes/criancas', requireAuth, async (req, res) => {
|
||||
try {
|
||||
const dbRes = await dbPool.query(`
|
||||
SELECT DISTINCT jsonb_array_elements_text(criancas) as nome
|
||||
FROM escola.observacoes
|
||||
WHERE criancas IS NOT NULL AND jsonb_array_length(criancas) > 0
|
||||
ORDER BY nome;
|
||||
`);
|
||||
res.json(dbRes.rows.map(r => r.nome));
|
||||
} catch (err) {
|
||||
console.error('Erro ao buscar crianças únicas:', err);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Contar observações baseadas em filtros
|
||||
app.get('/api/observacoes/contar', requireAuth, async (req, res) => {
|
||||
const { crianca, dataInicio, dataFim, tags } = req.query;
|
||||
try {
|
||||
let sql = `SELECT COUNT(*) FROM escola.observacoes WHERE 1=1`;
|
||||
const params = [];
|
||||
let pCount = 1;
|
||||
|
||||
if (crianca) {
|
||||
sql += ` AND EXISTS (
|
||||
SELECT 1 FROM jsonb_array_elements_text(criancas) AS c
|
||||
WHERE LOWER(c) LIKE $${pCount}
|
||||
)`;
|
||||
params.push(`%${crianca.toLowerCase()}%`);
|
||||
pCount++;
|
||||
}
|
||||
|
||||
if (dataInicio) {
|
||||
sql += ` AND date >= $${pCount}`;
|
||||
params.push(dataInicio);
|
||||
pCount++;
|
||||
}
|
||||
|
||||
if (dataFim) {
|
||||
sql += ` AND date <= $${pCount}`;
|
||||
params.push(dataFim);
|
||||
pCount++;
|
||||
}
|
||||
|
||||
if (tags) {
|
||||
const tagList = tags.split(',').map(t => t.trim().toLowerCase());
|
||||
sql += ` AND EXISTS (
|
||||
SELECT 1 FROM jsonb_array_elements_text(tags) AS t
|
||||
WHERE LOWER(t) = ANY($${pCount})
|
||||
)`;
|
||||
params.push(tagList);
|
||||
pCount++;
|
||||
}
|
||||
|
||||
const dbRes = await dbPool.query(sql, params);
|
||||
res.json({ count: parseInt(dbRes.rows[0].count) });
|
||||
} catch (err) {
|
||||
console.error('Erro ao contar observações:', err);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Gerar relatório consolidado com IA baseado em observações e um modelo
|
||||
app.post('/api/modelos/gerar-relatorio', requireAuth, async (req, res) => {
|
||||
const { crianca, dataInicio, dataFim, tags, modeloId } = req.body;
|
||||
if (!modeloId) return res.status(400).json({ error: 'O ID do modelo é obrigatório' });
|
||||
|
||||
try {
|
||||
// 1. Buscar o modelo/template
|
||||
const templateRes = await dbPool.query("SELECT * FROM escola.modelos WHERE id = $1;", [modeloId]);
|
||||
if (templateRes.rows.length === 0) return res.status(404).json({ error: 'Modelo de relatório não encontrado' });
|
||||
const template = templateRes.rows[0];
|
||||
|
||||
// 2. Buscar as observações filtradas
|
||||
let sql = `
|
||||
SELECT date, time, report, criancas, tags
|
||||
FROM escola.observacoes
|
||||
WHERE 1=1
|
||||
`;
|
||||
const params = [];
|
||||
let pCount = 1;
|
||||
|
||||
if (crianca) {
|
||||
sql += ` AND EXISTS (
|
||||
SELECT 1 FROM jsonb_array_elements_text(criancas) AS c
|
||||
WHERE LOWER(c) LIKE $${pCount}
|
||||
)`;
|
||||
params.push(`%${crianca.toLowerCase()}%`);
|
||||
pCount++;
|
||||
}
|
||||
|
||||
if (dataInicio) {
|
||||
sql += ` AND date >= $${pCount}`;
|
||||
params.push(dataInicio);
|
||||
pCount++;
|
||||
}
|
||||
|
||||
if (dataFim) {
|
||||
sql += ` AND date <= $${pCount}`;
|
||||
params.push(dataFim);
|
||||
pCount++;
|
||||
}
|
||||
|
||||
if (tags) {
|
||||
const tagList = Array.isArray(tags) ? tags : [tags];
|
||||
sql += ` AND EXISTS (
|
||||
SELECT 1 FROM jsonb_array_elements_text(tags) AS t
|
||||
WHERE LOWER(t) = ANY($${pCount})
|
||||
)`;
|
||||
params.push(tagList.map(t => t.toLowerCase()));
|
||||
pCount++;
|
||||
}
|
||||
|
||||
sql += ` ORDER BY date ASC`;
|
||||
const dbRes = await dbPool.query(sql, params);
|
||||
const observations = dbRes.rows;
|
||||
|
||||
if (observations.length === 0) {
|
||||
return res.status(400).json({ error: 'Nenhuma observação encontrada para os filtros selecionados.' });
|
||||
}
|
||||
|
||||
// 3. Montar prompt para IA usando a estrutura do template
|
||||
const combinedObsText = observations.map((o, idx) => {
|
||||
const dateText = o.date ? `Data: ${o.date}` : '';
|
||||
const tagsText = o.tags ? `Tags: ${Array.isArray(o.tags) ? o.tags.join(', ') : o.tags}` : '';
|
||||
return `[Observação #${idx + 1} ${dateText} ${tagsText}]\n${o.report}`;
|
||||
}).join('\n\n---\n\n');
|
||||
|
||||
const agentConfig = readAgentConfig();
|
||||
const agentName = agentConfig.agentName || "Kemily";
|
||||
|
||||
const systemPrompt = `Você é a ${agentName}, uma assistente pedagógica e especialista em educação infantil.
|
||||
Seu objetivo é analisar o histórico de observações de uma criança e redigir um relatório pedagógico consolidado e profissional.
|
||||
|
||||
Você DEVE estruturar o relatório seguindo exatamente as seções e diretrizes fornecidas no modelo de relatório selecionado.
|
||||
|
||||
MODELO DE RELATÓRIO: "${template.nome}"
|
||||
FINALIDADE: "${template.finalidade || 'Não informada'}"
|
||||
PERIODICIDADE: "${template.periodicidade || 'Não informada'}"
|
||||
|
||||
ESTRUTURA/DIRETRIZES DO MODELO:
|
||||
"""
|
||||
${template.estrutura}
|
||||
"""
|
||||
|
||||
HISTÓRICO DE OBSERVAÇÕES REGISTRADAS:
|
||||
"""
|
||||
${combinedObsText.slice(0, 8000)}
|
||||
"""
|
||||
|
||||
Orientações adicionais:
|
||||
- Redija o relatório em português brasileiro formal e adequado para pedagogia.
|
||||
- Preencha cada seção da estrutura de forma rica e detalhada, baseando-se estritamente nos fatos reais descritos nas observações.
|
||||
- Não invente acontecimentos que não estejam descritos nas observações, mas faça as conexões pedagógicas pertinentes de acordo com as evidências apresentadas.
|
||||
- Retorne apenas o relatório em formato Markdown estruturado.`;
|
||||
|
||||
let generatedReport = '';
|
||||
|
||||
// Tenta OpenRouter
|
||||
try {
|
||||
console.log('[Compilar Relatório] Chamando OpenRouter...');
|
||||
const response = await fetch('https://openrouter.ai/api/v1/chat/completions', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${process.env.OPENROUTER_API_KEY}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: process.env.OPENROUTER_MODEL || 'openai/gpt-4.1-nano',
|
||||
messages: [{ role: 'user', content: systemPrompt }],
|
||||
temperature: 0.5,
|
||||
max_tokens: 2500
|
||||
})
|
||||
});
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
generatedReport = data.choices?.[0]?.message?.content || '';
|
||||
} else {
|
||||
console.warn('[Compilar Relatório] Falha no OpenRouter status:', response.status);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[Compilar Relatório] Erro no OpenRouter:', err.message);
|
||||
}
|
||||
|
||||
// Fallback para Google Gemini
|
||||
if (!generatedReport && process.env.GOOGLE_AI_API_KEY) {
|
||||
try {
|
||||
console.log('[Compilar Relatório] Fallback para Google Gemini...');
|
||||
const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/${process.env.GOOGLE_AI_MODEL || 'gemini-2.5-flash'}:generateContent?key=${process.env.GOOGLE_AI_API_KEY}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
contents: [{ parts: [{ text: systemPrompt }] }],
|
||||
generationConfig: { temperature: 0.5, maxOutputTokens: 2500 }
|
||||
})
|
||||
});
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
generatedReport = data.candidates?.[0]?.content?.parts?.[0]?.text || '';
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[Compilar Relatório] Erro no Google Gemini:', err.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback para Groq
|
||||
if (!generatedReport && process.env.GROQ_API_KEY) {
|
||||
try {
|
||||
console.log('[Compilar Relatório] Fallback para Groq...');
|
||||
const response = await fetch('https://api.groq.com/openai/v1/chat/completions', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${process.env.GROQ_API_KEY}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: process.env.GROQ_MODEL || 'llama-3.3-70b-versatile',
|
||||
messages: [{ role: 'user', content: systemPrompt }],
|
||||
temperature: 0.5,
|
||||
max_tokens: 2500
|
||||
})
|
||||
});
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
generatedReport = data.choices?.[0]?.message?.content || '';
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[Compilar Relatório] Erro no Groq:', err.message);
|
||||
}
|
||||
}
|
||||
|
||||
if (!generatedReport) {
|
||||
throw new Error('Falha ao compilar relatório pedagógico com todos os provedores de IA.');
|
||||
}
|
||||
|
||||
res.json({
|
||||
report: generatedReport,
|
||||
templateNome: template.nome,
|
||||
crianca: crianca || 'Todos os alunos',
|
||||
periodo: (dataInicio || '') + (dataInicio && dataFim ? ' a ' : '') + (dataFim || 'Todo o período'),
|
||||
totalObservacoes: observations.length
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('Erro geral ao compilar relatório:', err);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Rota: GET /api/observacoes — lista todas as observações (índice)
|
||||
app.get('/api/observacoes', requireAuth, async (req, res) => {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user