Compare commits
2 Commits
ae629d1dc2
...
144ca322f5
| Author | SHA1 | Date | |
|---|---|---|---|
| 144ca322f5 | |||
| 89edb6f9a9 |
@@ -0,0 +1,51 @@
|
||||
# ============================================================
|
||||
# Configuracao do Chatc2
|
||||
# ============================================================
|
||||
|
||||
# Driver padrao para novas conexoes (postgres | firebird)
|
||||
DB_DRIVER=postgres
|
||||
|
||||
# ------------------------------------------------------------
|
||||
# Banco principal: PostgreSQL (alias "novo_local")
|
||||
# ------------------------------------------------------------
|
||||
PG_HOST=127.0.0.1
|
||||
PG_PORT=15433
|
||||
PG_USER=postgres
|
||||
PG_PASSWORD=postgres
|
||||
PG_DATABASE=novo_local
|
||||
# Schema a ser lido (ex.: lajedo, novo, dev). Use "public" se as tabelas
|
||||
# estiverem no schema padrao.
|
||||
PG_SCHEMA=public
|
||||
|
||||
# ------------------------------------------------------------
|
||||
# Banco Firebird (alias "firebird_local" / conexoes legadas)
|
||||
# ------------------------------------------------------------
|
||||
DB_HOST=localhost
|
||||
DB_PORT=3050
|
||||
DB_USER=SYSDBA
|
||||
DB_PASSWORD=masterkey
|
||||
DB_ENCODING=UTF-8
|
||||
# Caminho do arquivo .FDB. Se vazio, usa ../NOVO.FDB (raiz do projeto).
|
||||
# Ex. Windows: DB_DATABASE=C:\caminho\para\NOVO.FDB
|
||||
# Ex. Linux: DB_DATABASE=/opt/chatc2/db/NOVO.FDB
|
||||
DB_DATABASE=
|
||||
|
||||
# Servidor
|
||||
PORT=3000
|
||||
JWT_SECRET=CHATc2_1781527593_87c0a20ff1606d3e2aa0900eda4ecda9
|
||||
JWT_EXPIRES_IN=1h
|
||||
|
||||
# URLs de acesso
|
||||
LOCAL_URL=http://10.0.0.88:3000
|
||||
EXTERNAL_URL=https://atendchat.assantos.app.br
|
||||
|
||||
# ------------------------------------------------------------
|
||||
# Seguranca
|
||||
# ------------------------------------------------------------
|
||||
# CORS: origens permitidas (separadas por virgula). Se vazio, usa
|
||||
# LOCAL_URL + EXTERNAL_URL. Requisicoes same-origin nao sao afetadas.
|
||||
# CORS_ORIGINS=https://atendchat.assantos.app.br,http://10.0.0.88:3000
|
||||
|
||||
# Token de verificacao do webhook Evolution. Se definido, a Evolution deve
|
||||
# enviar este valor no header apikey (ou x-webhook-token). Vazio = sem checagem.
|
||||
# WEBHOOK_TOKEN=
|
||||
@@ -14,3 +14,5 @@ relatorio_migracao_*.json
|
||||
_inspect*.js
|
||||
whisper/
|
||||
.claude
|
||||
.gitignore
|
||||
CONTEXTO.md
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const db = require('../database');
|
||||
const { garantirEstrutura } = require('../resolucaoSetup');
|
||||
const ffmpegPath = require('ffmpeg-static');
|
||||
const { execFile } = require('child_process');
|
||||
const path = require('path');
|
||||
@@ -446,6 +447,8 @@ class ChatController {
|
||||
saudacaoEnviada: (row.CON_SAUDACAO_ENVIADA || 'N').trim(),
|
||||
csatEnviado: (row.CON_CSAT_ENVIADO || 'N').trim(),
|
||||
primeiraMsg: row.CON_PRIMEIRA_MSG,
|
||||
motivoId: row.CON_MOTIVO_ID || null,
|
||||
resolucao: row.CON_RESOLUCAO || null,
|
||||
cliente: clienteInfo,
|
||||
dependente: dependenteInfo,
|
||||
labels: labels.map(l => ({ id: l.ETI_CODIGO_ID, nome: (l.ETI_NOME || '').trim(), cor: (l.ETI_COR || '#667eea').trim() })),
|
||||
@@ -662,6 +665,23 @@ class ChatController {
|
||||
if (c.CON_EMPRESA_ID && !((req.user && req.user.empresas) || []).includes(c.CON_EMPRESA_ID))
|
||||
return res.status(403).json({ success: false, error: 'Sem permissão.' });
|
||||
|
||||
// ===== Fluxo de Resolução: valida motivo/resolução conforme config =====
|
||||
await garantirEstrutura(alias);
|
||||
const motivoId = (req.body && req.body.motivoId) ? parseInt(req.body.motivoId, 10) : null;
|
||||
const resolucao = (req.body && req.body.resolucao != null) ? String(req.body.resolucao).trim() : '';
|
||||
|
||||
const flagsRows = await db.query(alias,
|
||||
'SELECT CFE_MOTIVO_VISUALIZAR, CFE_MOTIVO_OBRIGATORIO, CFE_RESOLUCAO_VISUALIZAR, CFE_RESOLUCAO_OBRIGATORIO FROM CHATC2_CONFIGURACOES_EMPRESA WHERE CFE_EMPRESA_ID = ?',
|
||||
[c.CON_EMPRESA_ID]);
|
||||
const fl = flagsRows[0] || {};
|
||||
const isS = (v) => String(v || 'N').trim() === 'S';
|
||||
if (isS(fl.CFE_MOTIVO_VISUALIZAR) && isS(fl.CFE_MOTIVO_OBRIGATORIO) && !motivoId) {
|
||||
return res.status(400).json({ success: false, error: 'Selecione o motivo do atendimento para finalizar.' });
|
||||
}
|
||||
if (isS(fl.CFE_RESOLUCAO_VISUALIZAR) && isS(fl.CFE_RESOLUCAO_OBRIGATORIO) && !resolucao) {
|
||||
return res.status(400).json({ success: false, error: 'Preencha a resolução do atendimento para finalizar.' });
|
||||
}
|
||||
|
||||
// Busca nome do usuário que atendeu
|
||||
let usuarioNome = null;
|
||||
if (c.CON_USUARIO_ID) {
|
||||
@@ -692,9 +712,11 @@ class ChatController {
|
||||
CON_DT_FINAL = CURRENT_TIMESTAMP,
|
||||
CON_USUARIO_NOME = ?,
|
||||
CON_EQUIPE_NOME = ?,
|
||||
CON_ETIQUETAS_DESC = ?
|
||||
CON_ETIQUETAS_DESC = ?,
|
||||
CON_MOTIVO_ID = ?,
|
||||
CON_RESOLUCAO = ?
|
||||
WHERE CON_CODIGO_ID = ?
|
||||
`, [usuarioNome, equipeNome, etiquetasDesc, id]);
|
||||
`, [usuarioNome, equipeNome, etiquetasDesc, motivoId, resolucao || null, id]);
|
||||
|
||||
// CSAT
|
||||
const config = await db.query(alias,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const db = require('../database');
|
||||
const { isGerente } = require('../middlewares/roles');
|
||||
const { garantirEstrutura } = require('../resolucaoSetup');
|
||||
|
||||
class ConfigController {
|
||||
// ==================== CHATC2_EQUIPES ====================
|
||||
@@ -152,12 +153,16 @@ class ConfigController {
|
||||
const { alias } = req.params;
|
||||
const empresaId = parseInt(req.query.empresaId) || req.user?.empresas?.[0];
|
||||
|
||||
await garantirEstrutura(alias);
|
||||
|
||||
let config = await db.query(alias,
|
||||
'SELECT * FROM CHATC2_CONFIGURACOES_EMPRESA WHERE CFE_EMPRESA_ID = ?', [empresaId]
|
||||
);
|
||||
|
||||
const flagsPadrao = { motivoVisualizar: 'N', motivoObrigatorio: 'N', resolucaoVisualizar: 'N', resolucaoObrigatorio: 'N' };
|
||||
|
||||
if (config.length === 0) {
|
||||
const cfg = { empresaId, fotoCelular: 'N', saudacaoAtiva: 'S', saudacaoMensagem: '', csatAtivo: 'N', csatMensagem: '', enviarNomeUsuario: 'N', triagemAtiva: 'N', triagemMsgWelcome: '', triagemMsgAfter: '', triagemBoletoNumero: '0', instanciaPadraoId: null };
|
||||
const cfg = Object.assign({ empresaId, fotoCelular: 'N', saudacaoAtiva: 'S', saudacaoMensagem: '', csatAtivo: 'N', csatMensagem: '', enviarNomeUsuario: 'N', triagemAtiva: 'N', triagemMsgWelcome: '', triagemMsgAfter: '', triagemBoletoNumero: '0', instanciaPadraoId: null }, flagsPadrao);
|
||||
res.json({ success: true, data: cfg });
|
||||
} else {
|
||||
const c = config[0];
|
||||
@@ -174,11 +179,84 @@ class ConfigController {
|
||||
triagemMsgWelcome: c.CFE_TRIAGEM_MSG_WELCOME || '',
|
||||
triagemMsgAfter: c.CFE_TRIAGEM_MSG_AFTER || '',
|
||||
triagemBoletoNumero: (c.CFE_TRIAGEM_BOLETO_NUMERO || '0').trim(),
|
||||
// Fluxo de Resolução
|
||||
motivoVisualizar: (c.CFE_MOTIVO_VISUALIZAR || 'N').trim(),
|
||||
motivoObrigatorio: (c.CFE_MOTIVO_OBRIGATORIO || 'N').trim(),
|
||||
resolucaoVisualizar: (c.CFE_RESOLUCAO_VISUALIZAR || 'N').trim(),
|
||||
resolucaoObrigatorio: (c.CFE_RESOLUCAO_OBRIGATORIO || 'N').trim(),
|
||||
}});
|
||||
}
|
||||
} catch (err) { res.status(500).json({ success: false, error: err.message }); }
|
||||
}
|
||||
|
||||
// ==================== FLUXO DE RESOLUÇÃO ====================
|
||||
/** Salva as 4 flags do Fluxo de Resolução (sem tocar nas demais configs). */
|
||||
static async saveResolucaoConfig(req, res) {
|
||||
try {
|
||||
if (!(await isGerente(req))) return res.status(403).json({ success: false, error: 'Apenas gerentes podem alterar o fluxo de resolução.' });
|
||||
const { alias } = req.params;
|
||||
await garantirEstrutura(alias);
|
||||
const empresaId = req.body.empresaId || req.user?.empresas?.[0];
|
||||
const sn = (v) => (v === 'S' || v === true ? 'S' : 'N');
|
||||
const mv = sn(req.body.motivoVisualizar), mo = sn(req.body.motivoObrigatorio);
|
||||
const rv = sn(req.body.resolucaoVisualizar), ro = sn(req.body.resolucaoObrigatorio);
|
||||
|
||||
const exists = await db.query(alias, 'SELECT COUNT(*) AS T FROM CHATC2_CONFIGURACOES_EMPRESA WHERE CFE_EMPRESA_ID = ?', [empresaId]);
|
||||
if (exists[0].T > 0) {
|
||||
await db.execute(alias,
|
||||
`UPDATE CHATC2_CONFIGURACOES_EMPRESA SET CFE_MOTIVO_VISUALIZAR = ?, CFE_MOTIVO_OBRIGATORIO = ?,
|
||||
CFE_RESOLUCAO_VISUALIZAR = ?, CFE_RESOLUCAO_OBRIGATORIO = ? WHERE CFE_EMPRESA_ID = ?`,
|
||||
[mv, mo, rv, ro, empresaId]);
|
||||
} else {
|
||||
await db.execute(alias,
|
||||
`INSERT INTO CHATC2_CONFIGURACOES_EMPRESA (CFE_EMPRESA_ID, CFE_MOTIVO_VISUALIZAR, CFE_MOTIVO_OBRIGATORIO, CFE_RESOLUCAO_VISUALIZAR, CFE_RESOLUCAO_OBRIGATORIO)
|
||||
VALUES (?, ?, ?, ?, ?)`,
|
||||
[empresaId, mv, mo, rv, ro]);
|
||||
}
|
||||
res.json({ success: true });
|
||||
} catch (err) { res.status(500).json({ success: false, error: err.message }); }
|
||||
}
|
||||
|
||||
/** Lista os motivos de atendimento (leitura aberta — usada no chat). */
|
||||
static async listMotivos(req, res) {
|
||||
try {
|
||||
const { alias } = req.params;
|
||||
await garantirEstrutura(alias);
|
||||
const empresaId = parseInt(req.query.empresaId) || req.user?.empresas?.[0];
|
||||
const rows = await db.query(alias,
|
||||
`SELECT MOT_CODIGO_ID, MOT_DESCRICAO FROM "CHATC2_MOTIVOS_ATENDIMENTO"
|
||||
WHERE MOT_EMPRESA_ID = ? AND MOT_SITUACAO = 'A' ORDER BY MOT_DESCRICAO`, [empresaId]);
|
||||
res.json({ success: true, data: rows.map((m) => ({ id: m.MOT_CODIGO_ID, descricao: (m.MOT_DESCRICAO || '').trim() })) });
|
||||
} catch (err) { res.status(500).json({ success: false, error: err.message }); }
|
||||
}
|
||||
|
||||
static async createMotivo(req, res) {
|
||||
try {
|
||||
if (!(await isGerente(req))) return res.status(403).json({ success: false, error: 'Apenas gerentes podem cadastrar motivos.' });
|
||||
const { alias } = req.params;
|
||||
await garantirEstrutura(alias);
|
||||
const descricao = (req.body.descricao || '').trim();
|
||||
if (!descricao) return res.status(400).json({ success: false, error: 'Descrição obrigatória.' });
|
||||
const empresaId = req.body.empresaId || req.user?.empresas?.[0];
|
||||
const maxId = await db.query(alias, 'SELECT MAX(MOT_CODIGO_ID) AS ID FROM "CHATC2_MOTIVOS_ATENDIMENTO"');
|
||||
const newId = (maxId[0]?.ID || 0) + 1;
|
||||
await db.execute(alias,
|
||||
`INSERT INTO "CHATC2_MOTIVOS_ATENDIMENTO" (MOT_CODIGO_ID, MOT_EMPRESA_ID, MOT_DESCRICAO, MOT_SITUACAO)
|
||||
VALUES (?, ?, ?, 'A')`, [newId, empresaId, descricao]);
|
||||
res.json({ success: true, data: { id: newId } });
|
||||
} catch (err) { res.status(500).json({ success: false, error: err.message }); }
|
||||
}
|
||||
|
||||
static async deleteMotivo(req, res) {
|
||||
try {
|
||||
if (!(await isGerente(req))) return res.status(403).json({ success: false, error: 'Apenas gerentes podem remover motivos.' });
|
||||
const { alias, id } = req.params;
|
||||
await garantirEstrutura(alias);
|
||||
await db.execute(alias, `UPDATE "CHATC2_MOTIVOS_ATENDIMENTO" SET MOT_SITUACAO = 'I' WHERE MOT_CODIGO_ID = ?`, [id]);
|
||||
res.json({ success: true });
|
||||
} catch (err) { res.status(500).json({ success: false, error: err.message }); }
|
||||
}
|
||||
|
||||
static async saveCompanyConfig(req, res) {
|
||||
try {
|
||||
if (!(await isGerente(req))) return res.status(403).json({ success: false, error: 'Apenas gerentes podem alterar as configurações da empresa.' });
|
||||
|
||||
+10
-9
@@ -66,23 +66,24 @@ const DRIVER_DEFAULTS = {
|
||||
* Sobrescrevíveis pelo .env (PG_* para Postgres, DB_* para Firebird).
|
||||
*/
|
||||
const databases = {
|
||||
novo_local: {
|
||||
novo: {
|
||||
driver: 'postgres',
|
||||
host: process.env.PG_HOST || '127.0.0.1',
|
||||
port: parseInt(process.env.PG_PORT, 10) || 15433,
|
||||
user: process.env.PG_USER || 'postgres',
|
||||
password: process.env.PG_PASSWORD || 'postgres',
|
||||
database: process.env.PG_DATABASE || 'novo_local',
|
||||
schema: process.env.PG_SCHEMA || 'public',
|
||||
host: process.env.PG_HOST || 'db.assantos.app.br',
|
||||
port: parseInt(process.env.PG_PORT, 10) || 443,
|
||||
user: process.env.PG_USER || 'atendimento_user',
|
||||
password: process.env.PG_PASSWORD || 'PSSQDfPHilIZF1mbGzuR2RrEh0cTEGaF',
|
||||
database: process.env.PG_DATABASE || 'atendimento',
|
||||
schema: process.env.PG_SCHEMA || 'atendi',
|
||||
ssl: process.env.PG_SSL === 'true' ? true : process.env.PG_SSL === 'false' ? false : (parseInt(process.env.PG_PORT, 10) === 443 ? { rejectUnauthorized: false } : false),
|
||||
},
|
||||
|
||||
firebird_local: {
|
||||
novo_local: {
|
||||
driver: 'firebird',
|
||||
host: process.env.DB_HOST || 'localhost',
|
||||
port: parseInt(process.env.DB_PORT, 10) || 3050,
|
||||
// CAMINHO do arquivo .FDB: defina DB_DATABASE no .env (absoluto) ou ajuste
|
||||
// o path.resolve abaixo. Ex.: path.resolve(__dirname, '../db/NOVO.FDB').
|
||||
database: process.env.DB_DATABASE || path.resolve(__dirname, '../NOVO.FDB'),
|
||||
database: process.env.DB_DATABASE || path.resolve(__dirname, '../db/NOVO.FDB'),
|
||||
user: process.env.DB_USER || 'SYSDBA',
|
||||
password: process.env.DB_PASSWORD || 'masterkey',
|
||||
encoding: process.env.DB_ENCODING || 'UTF-8',
|
||||
|
||||
+71
-5
@@ -686,6 +686,19 @@ window.darkModeIsDark=function(){return localStorage.getItem('chatc2_dark_mode')
|
||||
<div class="cliente-foto" id="clienteFoto">?</div>
|
||||
<div class="info-section" id="clienteInfoContainer"></div>
|
||||
|
||||
<!-- Fluxo de Resolução (aparece conforme configuração da empresa) -->
|
||||
<div class="info-section" id="resolucaoPanel" style="display:none">
|
||||
<div class="label" style="font-size:10px;text-transform:uppercase;color:#9ca3af;margin-bottom:6px">RESOLUÇÃO DO ATENDIMENTO</div>
|
||||
<div id="motivoWrap" style="display:none;margin-bottom:8px">
|
||||
<select id="selectMotivo" style="width:100%;padding:6px 8px;border:1px solid #e5e7eb;border-radius:6px;font-size:12px;outline:none">
|
||||
<option value="">Selecione o motivo...</option>
|
||||
</select>
|
||||
</div>
|
||||
<div id="resolucaoWrap" style="display:none">
|
||||
<textarea id="campoResolucao" rows="3" placeholder="Descreva a resolução do atendimento..." style="width:100%;padding:8px;border:1px solid #e5e7eb;border-radius:6px;font-size:12px;outline:none;resize:vertical;font-family:inherit"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="info-section">
|
||||
<div class="label" style="font-size:10px;text-transform:uppercase;color:#9ca3af;margin-bottom:4px">ATENDENTE</div>
|
||||
<select id="selectAtendente" style="width:100%;padding:6px 8px;border:1px solid #e5e7eb;border-radius:6px;font-size:12px;outline:none" onchange="mudarAtendente(this)">
|
||||
@@ -746,6 +759,36 @@ function esc(s){ return String(s == null ? '' : s)
|
||||
.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>')
|
||||
.replace(/"/g,'"').replace(/'/g,'''); }
|
||||
|
||||
// ===== FLUXO DE RESOLUÇÃO =====
|
||||
var cfgResolucao = { motivoVisualizar:'N', motivoObrigatorio:'N', resolucaoVisualizar:'N', resolucaoObrigatorio:'N' };
|
||||
var motivosList = [];
|
||||
async function carregarConfigResolucao() {
|
||||
try {
|
||||
var cd = await (await fetch('/api/' + alias + '/company/config?empresaId=' + empresaId, { headers: { 'Authorization': 'Bearer ' + token } })).json();
|
||||
if (cd.success && cd.data) cfgResolucao = cd.data;
|
||||
var md = await (await fetch('/api/' + alias + '/motivos?empresaId=' + empresaId, { headers: { 'Authorization': 'Bearer ' + token } })).json();
|
||||
if (md.success) motivosList = md.data || [];
|
||||
} catch(e) {}
|
||||
}
|
||||
function aplicarPainelResolucao(conv) {
|
||||
var panel = document.getElementById('resolucaoPanel');
|
||||
if (!panel) return;
|
||||
var verMot = cfgResolucao.motivoVisualizar === 'S';
|
||||
var verRes = cfgResolucao.resolucaoVisualizar === 'S';
|
||||
panel.style.display = (verMot || verRes) ? 'block' : 'none';
|
||||
document.getElementById('motivoWrap').style.display = verMot ? 'block' : 'none';
|
||||
document.getElementById('resolucaoWrap').style.display = verRes ? 'block' : 'none';
|
||||
if (verMot) {
|
||||
var sel = document.getElementById('selectMotivo');
|
||||
sel.innerHTML = '<option value="">Selecione o motivo...</option>' +
|
||||
motivosList.map(function(m){ return '<option value="' + m.id + '">' + esc(m.descricao) + '</option>'; }).join('');
|
||||
sel.value = (conv && conv.motivoId) ? String(conv.motivoId) : '';
|
||||
}
|
||||
if (verRes) {
|
||||
document.getElementById('campoResolucao').value = (conv && conv.resolucao) ? conv.resolucao : '';
|
||||
}
|
||||
}
|
||||
|
||||
// ===== NAVEGAÇÃO =====
|
||||
document.querySelectorAll('.sidebar-left .nav-tabs a').forEach(function(a) {
|
||||
a.addEventListener('click', function(e) { e.preventDefault(); });
|
||||
@@ -884,6 +927,7 @@ function renderInfoConversa(conv) {
|
||||
}
|
||||
|
||||
window._convAtual = conv;
|
||||
aplicarPainelResolucao(conv);
|
||||
|
||||
// Info cliente
|
||||
var infoContainer = document.getElementById('clienteInfoContainer');
|
||||
@@ -1361,11 +1405,31 @@ dropArea.addEventListener('drop', function(e) {
|
||||
|
||||
// ===== FINALIZAR =====
|
||||
window.finalizarConversa = async function() {
|
||||
if (!conversaAtiva || !confirm('Finalizar esta conversa?')) return;
|
||||
if (!conversaAtiva) return;
|
||||
|
||||
// Coleta motivo/resolução (se visíveis) e valida obrigatoriedade
|
||||
var motivoId = null, resolucao = '';
|
||||
if (cfgResolucao.motivoVisualizar === 'S') {
|
||||
var sel = document.getElementById('selectMotivo');
|
||||
motivoId = sel && sel.value ? parseInt(sel.value, 10) : null;
|
||||
if (cfgResolucao.motivoObrigatorio === 'S' && !motivoId) {
|
||||
alert('Selecione o motivo do atendimento para finalizar.'); return;
|
||||
}
|
||||
}
|
||||
if (cfgResolucao.resolucaoVisualizar === 'S') {
|
||||
var txt = document.getElementById('campoResolucao');
|
||||
resolucao = txt ? (txt.value || '').trim() : '';
|
||||
if (cfgResolucao.resolucaoObrigatorio === 'S' && !resolucao) {
|
||||
alert('Preencha a resolução do atendimento para finalizar.'); return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!confirm('Finalizar esta conversa?')) return;
|
||||
try {
|
||||
var res = await fetch('/api/' + alias + '/conversations/' + conversaAtiva + '/finalize', {
|
||||
method: 'POST',
|
||||
headers: { 'Authorization': 'Bearer ' + token }
|
||||
headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ motivoId: motivoId, resolucao: resolucao })
|
||||
});
|
||||
var data = await res.json();
|
||||
if (data.success) {
|
||||
@@ -1374,6 +1438,8 @@ window.finalizarConversa = async function() {
|
||||
document.querySelector('.btn-finalizar').style.opacity = '0.6';
|
||||
carregarConversas();
|
||||
abrirConversa(conversaAtiva);
|
||||
} else {
|
||||
alert(data.error || 'Não foi possível finalizar a conversa.');
|
||||
}
|
||||
} catch(e) {}
|
||||
};
|
||||
@@ -1519,9 +1585,9 @@ window.mudarFiltro = function(filtro, el) {
|
||||
|
||||
// ===== INICIAR =====
|
||||
carregarConversas();
|
||||
if (conversaId) {
|
||||
setTimeout(function() { abrirConversa(parseInt(conversaId)); }, 300);
|
||||
}
|
||||
carregarConfigResolucao().then(function() {
|
||||
if (conversaId) abrirConversa(parseInt(conversaId));
|
||||
});
|
||||
|
||||
// ===== LOGOUT =====
|
||||
window.logout = function() {
|
||||
|
||||
@@ -85,6 +85,28 @@ window.darkModeIsDark=function(){return localStorage.getItem('chatc2_dark_mode')
|
||||
</p>
|
||||
<div id="menusList"><p style="color:#9ca3af">Carregando...</p></div>
|
||||
</div>
|
||||
|
||||
<div class="card" id="cardResolucao">
|
||||
<h3 style="margin:0 0 8px">🧩 Fluxo de Resolução</h3>
|
||||
<p style="color:#6b7280;font-size:13px;margin-bottom:16px">
|
||||
Defina os motivos de atendimento e como o atendente registra a resolução ao finalizar a conversa.
|
||||
</p>
|
||||
|
||||
<h4 style="margin:8px 0">Motivo do Atendimento</h4>
|
||||
<div style="display:flex;gap:8px;margin-bottom:8px">
|
||||
<input type="text" id="novoMotivo" placeholder="Cadastrar novo motivo..." style="flex:1;padding:8px;border:2px solid #e5e7eb;border-radius:8px;font-size:13px" onkeydown="if(event.key==='Enter')adicionarMotivo()">
|
||||
<button class="btn btn-primary btn-sm" onclick="adicionarMotivo()">+ Adicionar</button>
|
||||
</div>
|
||||
<div id="motivosList" style="margin-bottom:12px"><p style="color:#9ca3af;font-size:13px">Carregando...</p></div>
|
||||
<div class="form-group"><div class="toggle"><input type="checkbox" id="flgMotivoVis"> <label for="flgMotivoVis">Visualizar Motivo na tela de conversa</label></div></div>
|
||||
<div class="form-group"><div class="toggle"><input type="checkbox" id="flgMotivoObr"> <label for="flgMotivoObr">Obrigatório preencher (só finaliza com motivo)</label></div></div>
|
||||
|
||||
<h4 style="margin:16px 0 8px">Resolução do Atendimento</h4>
|
||||
<div class="form-group"><div class="toggle"><input type="checkbox" id="flgResolVis"> <label for="flgResolVis">Visualizar Resolução na tela de conversa</label></div></div>
|
||||
<div class="form-group"><div class="toggle"><input type="checkbox" id="flgResolObr"> <label for="flgResolObr">Obrigatório preencher (só finaliza com resolução)</label></div></div>
|
||||
|
||||
<button class="btn btn-primary" onclick="salvarResolucao()">💾 Salvar Fluxo de Resolução</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Aba Equipe -->
|
||||
@@ -725,6 +747,57 @@ window.salvarConfig = async function() {
|
||||
|
||||
// O addEventListener do cfgTriagem é adicionado dentro do carregarConfig() após criar o HTML
|
||||
|
||||
// ===== FLUXO DE RESOLUÇÃO =====
|
||||
function escc(s){ return String(s == null ? '' : s).replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>'); }
|
||||
|
||||
async function carregarResolucao() {
|
||||
var md = await api('/motivos?empresaId=' + empresaId);
|
||||
var div = document.getElementById('motivosList');
|
||||
if (div && md.success) {
|
||||
div.innerHTML = (md.data && md.data.length)
|
||||
? md.data.map(function(m) {
|
||||
return '<div style="display:flex;justify-content:space-between;align-items:center;padding:6px 10px;border:1px solid #eee;border-radius:6px;margin-bottom:4px">' +
|
||||
'<span style="font-size:13px">' + escc(m.descricao) + '</span>' +
|
||||
'<button class="btn btn-sm" style="color:#dc2626;background:none;border:none;cursor:pointer" onclick="removerMotivo(' + m.id + ')">🗑️</button></div>';
|
||||
}).join('')
|
||||
: '<p style="color:#9ca3af;font-size:13px">Nenhum motivo cadastrado.</p>';
|
||||
}
|
||||
var cd = await api('/company/config?empresaId=' + empresaId);
|
||||
if (cd.success) {
|
||||
var s = function(id, v) { var el = document.getElementById(id); if (el) el.checked = v === 'S'; };
|
||||
s('flgMotivoVis', cd.data.motivoVisualizar);
|
||||
s('flgMotivoObr', cd.data.motivoObrigatorio);
|
||||
s('flgResolVis', cd.data.resolucaoVisualizar);
|
||||
s('flgResolObr', cd.data.resolucaoObrigatorio);
|
||||
}
|
||||
}
|
||||
|
||||
window.adicionarMotivo = async function() {
|
||||
var inp = document.getElementById('novoMotivo');
|
||||
var d = (inp.value || '').trim();
|
||||
if (!d) return;
|
||||
var r = await api('/motivos', { method: 'POST', body: JSON.stringify({ descricao: d, empresaId: empresaId }) });
|
||||
if (r.success) { inp.value = ''; carregarResolucao(); } else alert(r.error || 'Erro ao adicionar motivo');
|
||||
};
|
||||
|
||||
window.removerMotivo = async function(id) {
|
||||
if (!confirm('Remover este motivo?')) return;
|
||||
var r = await api('/motivos/' + id, { method: 'DELETE' });
|
||||
if (r.success) carregarResolucao(); else alert(r.error || 'Erro ao remover');
|
||||
};
|
||||
|
||||
window.salvarResolucao = async function() {
|
||||
var body = {
|
||||
empresaId: empresaId,
|
||||
motivoVisualizar: document.getElementById('flgMotivoVis').checked ? 'S' : 'N',
|
||||
motivoObrigatorio: document.getElementById('flgMotivoObr').checked ? 'S' : 'N',
|
||||
resolucaoVisualizar: document.getElementById('flgResolVis').checked ? 'S' : 'N',
|
||||
resolucaoObrigatorio: document.getElementById('flgResolObr').checked ? 'S' : 'N',
|
||||
};
|
||||
var r = await api('/company/resolucao-config', { method: 'POST', body: JSON.stringify(body) });
|
||||
if (r.success) alert('Fluxo de Resolução salvo!'); else alert(r.error || 'Erro ao salvar');
|
||||
};
|
||||
|
||||
// ===== CONEXÕES =====
|
||||
async function carregarConexoes() {
|
||||
var data = await api('/evolution/instances?empresaId=' + empresaId);
|
||||
@@ -850,6 +923,7 @@ carregarMenus();
|
||||
carregarEtiquetas();
|
||||
carregarConexoes();
|
||||
carregarConfig();
|
||||
carregarResolucao();
|
||||
|
||||
})();
|
||||
</script>
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* Estrutura do "Fluxo de Resolução" (motivos + resolução do atendimento).
|
||||
* Cria de forma idempotente, em Postgres ou Firebird:
|
||||
* - Tabela CHATC2_MOTIVOS_ATENDIMENTO
|
||||
* - Flags em CHATC2_CONFIGURACOES_EMPRESA (CFE_MOTIVO_*, CFE_RESOLUCAO_*)
|
||||
* - Colunas CON_MOTIVO_ID / CON_RESOLUCAO em CHATC2_CONVERSAS
|
||||
*
|
||||
* Usa DDL no subconjunto que funciona nos dois bancos (INTEGER, VARCHAR, CHAR,
|
||||
* ALTER TABLE ... ADD). A tabela nova é criada com nome MAIÚSCULO entre aspas
|
||||
* para manter a convenção do schema migrado.
|
||||
*/
|
||||
const db = require('./database');
|
||||
|
||||
const prontos = {};
|
||||
|
||||
async function tentar(alias, sql) {
|
||||
// DDL idempotente: ignora erros de "já existe" (e similares entre dialetos)
|
||||
try { await db.execute(alias, sql); } catch (e) { /* noop */ }
|
||||
}
|
||||
|
||||
async function garantirEstrutura(alias) {
|
||||
if (prontos[alias]) return;
|
||||
|
||||
await tentar(alias, `CREATE TABLE "CHATC2_MOTIVOS_ATENDIMENTO" (
|
||||
MOT_CODIGO_ID INTEGER NOT NULL PRIMARY KEY,
|
||||
MOT_EMPRESA_ID INTEGER,
|
||||
MOT_DESCRICAO VARCHAR(150),
|
||||
MOT_SITUACAO CHAR(1) DEFAULT 'A'
|
||||
)`);
|
||||
|
||||
await tentar(alias, `ALTER TABLE CHATC2_CONFIGURACOES_EMPRESA ADD CFE_MOTIVO_VISUALIZAR CHAR(1) DEFAULT 'N'`);
|
||||
await tentar(alias, `ALTER TABLE CHATC2_CONFIGURACOES_EMPRESA ADD CFE_MOTIVO_OBRIGATORIO CHAR(1) DEFAULT 'N'`);
|
||||
await tentar(alias, `ALTER TABLE CHATC2_CONFIGURACOES_EMPRESA ADD CFE_RESOLUCAO_VISUALIZAR CHAR(1) DEFAULT 'N'`);
|
||||
await tentar(alias, `ALTER TABLE CHATC2_CONFIGURACOES_EMPRESA ADD CFE_RESOLUCAO_OBRIGATORIO CHAR(1) DEFAULT 'N'`);
|
||||
|
||||
await tentar(alias, `ALTER TABLE CHATC2_CONVERSAS ADD CON_MOTIVO_ID INTEGER`);
|
||||
await tentar(alias, `ALTER TABLE CHATC2_CONVERSAS ADD CON_RESOLUCAO VARCHAR(4000)`);
|
||||
|
||||
prontos[alias] = true;
|
||||
}
|
||||
|
||||
module.exports = { garantirEstrutura };
|
||||
@@ -27,6 +27,12 @@ router.delete('/:alias/labels/:id', ConfigController.deleteLabel);
|
||||
router.get('/:alias/company/config', ConfigController.getCompanyConfig);
|
||||
router.post('/:alias/company/config', ConfigController.saveCompanyConfig);
|
||||
|
||||
// Fluxo de Resolução (motivos + flags)
|
||||
router.post('/:alias/company/resolucao-config', ConfigController.saveResolucaoConfig);
|
||||
router.get('/:alias/motivos', ConfigController.listMotivos);
|
||||
router.post('/:alias/motivos', ConfigController.createMotivo);
|
||||
router.delete('/:alias/motivos/:id', ConfigController.deleteMotivo);
|
||||
|
||||
// Evolution API
|
||||
router.get('/:alias/evolution/instances', EvolutionController.listInstances);
|
||||
router.post('/:alias/evolution/connect', EvolutionController.connect);
|
||||
|
||||
Reference in New Issue
Block a user