Compare commits
4 Commits
ae629d1dc2
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| aa74c984d0 | |||
| 887e95c6ca | |||
| 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() })),
|
||||
@@ -608,7 +611,8 @@ class ChatController {
|
||||
if (user.length > 0) {
|
||||
const nomeUser = (user[0].USU_NOME || '').trim();
|
||||
if (nomeUser && textoFinal) {
|
||||
textoFinal = nomeUser + ': ' + textoFinal;
|
||||
// Padrão WhatsApp: *Nome:* em negrito + quebra de linha + mensagem
|
||||
textoFinal = '*' + nomeUser + ':*\n' + textoFinal;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -662,6 +666,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 +713,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.' });
|
||||
|
||||
@@ -440,10 +440,10 @@ class EvolutionController {
|
||||
// Reaproveita a funcao de atualizar foto
|
||||
var numero = conv[0].CON_NUMERO || '';
|
||||
var empresaId = conv[0].CON_EMPRESA_ID;
|
||||
// A funcao atualizarFotoContato esta no escopo do processWebhook, entao recriamos a logica aqui
|
||||
await EvolutionController.atualizarFotoContato(alias, conv[0].CON_CLIENTE_ID, numero, empresaId);
|
||||
// Ação explícita do usuário: força a busca (ignora a flag CFE_FOTO_CELULAR)
|
||||
const atualizou = await EvolutionController.atualizarFotoContato(alias, conv[0].CON_CLIENTE_ID, numero, empresaId, true);
|
||||
|
||||
res.json({ success: true, message: 'Foto atualizada.' });
|
||||
res.json({ success: true, atualizada: !!atualizou, message: atualizou ? 'Foto atualizada.' : 'Nenhuma foto encontrada para este contato.' });
|
||||
} catch (err) {
|
||||
res.status(500).json({ success: false, error: err.message });
|
||||
}
|
||||
@@ -452,12 +452,14 @@ class EvolutionController {
|
||||
/**
|
||||
* Versão estática de atualizarFotoContato para uso externo
|
||||
*/
|
||||
static async atualizarFotoContato(alias, clienteId, numero, empresaId) {
|
||||
if (!clienteId) return;
|
||||
static async atualizarFotoContato(alias, clienteId, numero, empresaId, forcar) {
|
||||
if (!clienteId) return false;
|
||||
try {
|
||||
const cfg = await db.query(alias,
|
||||
"SELECT CFE_FOTO_CELULAR FROM CHATC2_CONFIGURACOES_EMPRESA WHERE CFE_EMPRESA_ID = ?", [empresaId]);
|
||||
if (cfg.length === 0 || cfg[0].CFE_FOTO_CELULAR !== 'S') return;
|
||||
if (!forcar) {
|
||||
const cfg = await db.query(alias,
|
||||
"SELECT CFE_FOTO_CELULAR FROM CHATC2_CONFIGURACOES_EMPRESA WHERE CFE_EMPRESA_ID = ?", [empresaId]);
|
||||
if (cfg.length === 0 || cfg[0].CFE_FOTO_CELULAR !== 'S') return false;
|
||||
}
|
||||
|
||||
const inst = await db.query(alias,
|
||||
"SELECT * FROM CHATC2_INSTANCIAS WHERE INS_EMPRESA_ID = ? AND INS_SITUACAO = 'A' FETCH FIRST 1 ROWS ONLY", [empresaId]);
|
||||
@@ -468,29 +470,34 @@ class EvolutionController {
|
||||
const instanceName = (inst[0].INS_INSTANCE_NAME || '').trim();
|
||||
if (!url || !apiKey || !instanceName) return;
|
||||
|
||||
// Evolution API 2.4.0 NAO possui endpoint getProfile (disponivel apenas a partir da v2.5+)
|
||||
console.log('[Foto] Evolution API ' + url + ' - getProfile nao disponivel nesta versao. A foto do WhatsApp sera usada quando atualizar a Evolution.');
|
||||
// Busca a foto de perfil via Evolution: POST <URL>/chat/fetchProfile/<instancia> { number }
|
||||
|
||||
// Mesmo sem o endpoint, tentamos alguns formatos conhecidos
|
||||
var profileEndpoints = [
|
||||
'/chat/getProfile/' + encodeURIComponent(instanceName) + '?number=' + numero,
|
||||
'/chat/getProfile/' + encodeURIComponent(instanceName) + '/' + numero,
|
||||
'/contact/getProfile/' + encodeURIComponent(instanceName) + '?number=' + numero,
|
||||
];
|
||||
|
||||
var profile = null;
|
||||
for (var ep of profileEndpoints) {
|
||||
try {
|
||||
profile = await evolutionRequest(url, apiKey, ep, 'GET');
|
||||
if (profile) break;
|
||||
} catch(e) {
|
||||
profile = null;
|
||||
// Normaliza o numero para o padrao WhatsApp BR: 55(DDI) + DDD + 9XXXXXXXX
|
||||
function normalizarNumero(n) {
|
||||
var d = String(n || '').replace(/\D/g, '');
|
||||
if (!d) return '';
|
||||
if (!d.startsWith('55')) d = '55' + d; // garante DDI Brasil
|
||||
var resto = d.slice(2); // DDD + assinante
|
||||
if (resto.length === 10) { // DDD(2) + 8 digitos -> insere o 9 (celular)
|
||||
resto = resto.slice(0, 2) + '9' + resto.slice(2);
|
||||
}
|
||||
return '55' + resto;
|
||||
}
|
||||
var numeroFmt = normalizarNumero(numero);
|
||||
if (!numeroFmt) return false;
|
||||
|
||||
var profile = null;
|
||||
try {
|
||||
profile = await evolutionRequest(url, apiKey,
|
||||
'/chat/fetchProfile/' + encodeURIComponent(instanceName), 'POST', { number: numeroFmt });
|
||||
} catch (e) {
|
||||
console.log('[Foto] fetchProfile falhou:', (e.message || '').substring(0, 120));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!profile) {
|
||||
console.log('[Foto] Nenhum endpoint getProfile disponivel. Evolution API precisa ser atualizada.');
|
||||
return;
|
||||
console.log('[Foto] Nenhum perfil retornado para', numeroFmt);
|
||||
return false;
|
||||
}
|
||||
|
||||
let fotoUrl = null;
|
||||
@@ -505,31 +512,32 @@ class EvolutionController {
|
||||
fotoUrl = profile.response.profilePicUrl || profile.response.picUrl || null;
|
||||
}
|
||||
|
||||
if (fotoUrl && fotoUrl.startsWith('http')) {
|
||||
console.log('[Foto] Baixando foto de:', fotoUrl.substring(0, 80));
|
||||
if (fotoUrl && String(fotoUrl).startsWith('http')) {
|
||||
console.log('[Foto] Baixando foto de:', String(fotoUrl).substring(0, 80));
|
||||
try {
|
||||
var http = require('http');
|
||||
var https = require('https');
|
||||
var fotoBuffer = await new Promise(function(resolve, reject) {
|
||||
var u = new URL(fotoUrl);
|
||||
var lib = u.protocol === 'https:' ? https : http;
|
||||
lib.get(fotoUrl, { timeout: 15000, headers: { 'apikey': apiKey } }, function(imgRes) {
|
||||
lib.get(fotoUrl, { timeout: 15000 }, function(imgRes) {
|
||||
var chunks = [];
|
||||
imgRes.on('data', function(c) { chunks.push(c); });
|
||||
imgRes.on('end', function() { resolve(Buffer.concat(chunks)); });
|
||||
}).on('error', reject).on('timeout', function() { this.destroy(); reject(new Error('Timeout')); });
|
||||
});
|
||||
if (fotoBuffer && fotoBuffer.length > 100) {
|
||||
var imgData = fotoBuffer.toString('base64');
|
||||
await db.execute(alias,
|
||||
'UPDATE CLIENTES SET CLI_FOTO = ? WHERE CLI_CODIGO_ID = ?',
|
||||
[imgData, clienteId]);
|
||||
console.log('[Foto] Foto atualizada para cliente', clienteId, '- tamanho:', fotoBuffer.length);
|
||||
[fotoBuffer.toString('base64'), clienteId]);
|
||||
console.log('[Foto] Foto atualizada para cliente', clienteId, '- bytes:', fotoBuffer.length);
|
||||
return true;
|
||||
}
|
||||
} catch(imgErr) {
|
||||
console.error('[Foto] Erro ao baixar imagem:', imgErr.message.substring(0, 100));
|
||||
console.error('[Foto] Erro ao baixar imagem:', (imgErr.message || '').substring(0, 120));
|
||||
}
|
||||
} else {
|
||||
console.log('[Foto] Perfil sem foto disponivel para', numeroFmt);
|
||||
}
|
||||
return false;
|
||||
} catch(e) {
|
||||
console.error('[Foto] Erro:', e.message);
|
||||
}
|
||||
|
||||
+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',
|
||||
|
||||
+137
-7
@@ -594,6 +594,35 @@ body { background:#f3f4f6; display:flex; height:100vh; overflow:hidden; }
|
||||
}
|
||||
.empty-state .icon { font-size: 52px; opacity: 0.45; }
|
||||
.empty-state p { font-size: 15px; color: #6b7280; }
|
||||
|
||||
/* Botões só do mobile (voltar / info) */
|
||||
.mobile-only { display: none; }
|
||||
|
||||
/* ===== RESPONSIVO: navegação de painel único (lista → conversa → info) ===== */
|
||||
@media (max-width: 768px) {
|
||||
.sidebar-left { width: 100%; }
|
||||
.chat-center { display: none; }
|
||||
|
||||
/* Quando uma conversa está aberta, mostra o chat e esconde a lista */
|
||||
body.chat-aberto .sidebar-left { display: none; }
|
||||
body.chat-aberto .chat-center { display: flex; }
|
||||
|
||||
/* Painel de informações vira overlay deslizante (oculto por padrão) */
|
||||
.sidebar-right {
|
||||
position: fixed;
|
||||
top: 0; right: 0; bottom: 0;
|
||||
width: 88%; max-width: 340px;
|
||||
z-index: 1200;
|
||||
box-shadow: -4px 0 24px rgba(0,0,0,0.28);
|
||||
display: none !important;
|
||||
}
|
||||
body.info-aberto .sidebar-right { display: flex !important; }
|
||||
|
||||
.mobile-only { display: inline-flex !important; align-items: center; justify-content: center; }
|
||||
|
||||
.chat-header { gap: 8px; }
|
||||
.chat-header .info h3 { font-size: 14px; }
|
||||
}
|
||||
</style>
|
||||
<link rel="stylesheet" href="/css/dark-mode.css">
|
||||
<script>function darkModeToggle(){var e=document.body;if(!e)return;var a=localStorage.getItem('chatc2_dark_mode')!=='true';e.classList.toggle('dark-mode',a);localStorage.setItem('chatc2_dark_mode',a?'true':'false');document.querySelectorAll('.dark-mode-toggle').forEach(function(b){b.innerHTML=a?'☀️ Claro':'🌙 Escuro'});}
|
||||
@@ -652,11 +681,13 @@ window.darkModeIsDark=function(){return localStorage.getItem('chatc2_dark_mode')
|
||||
|
||||
<div class="chat-center">
|
||||
<div class="chat-header" id="chatHeader" style="display:none">
|
||||
<button class="mobile-only" onclick="voltarLista()" title="Voltar" style="background:none;border:none;font-size:20px;cursor:pointer;padding:4px 6px;color:#374151">←</button>
|
||||
<div class="info">
|
||||
<h3 id="chatNome"></h3>
|
||||
<span id="chatStatus"></span>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<button class="mobile-only" onclick="toggleInfo()" title="Informações">ℹ️</button>
|
||||
<button class="btn-finalizar" onclick="finalizarConversa()">✅ Finalizar</button>
|
||||
</div>
|
||||
</div>
|
||||
@@ -685,7 +716,20 @@ window.darkModeIsDark=function(){return localStorage.getItem('chatc2_dark_mode')
|
||||
<div class="header"><h3>📋 Informações</h3></div>
|
||||
<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 +790,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(); });
|
||||
@@ -845,6 +919,9 @@ function renderConversas(convs, busca) {
|
||||
// ===== ABRIR CONVERSA =====
|
||||
window.abrirConversa = async function(id) {
|
||||
conversaAtiva = id;
|
||||
// Mobile: alterna para a visão da conversa (e fecha o painel de info)
|
||||
document.body.classList.add('chat-aberto');
|
||||
document.body.classList.remove('info-aberto');
|
||||
document.querySelectorAll('.conv-item').forEach(function(el) { el.classList.remove('active'); });
|
||||
var item = document.querySelector('.conv-item[data-id="' + id + '"]');
|
||||
if (item) item.classList.add('active');
|
||||
@@ -884,6 +961,7 @@ function renderInfoConversa(conv) {
|
||||
}
|
||||
|
||||
window._convAtual = conv;
|
||||
aplicarPainelResolucao(conv);
|
||||
|
||||
// Info cliente
|
||||
var infoContainer = document.getElementById('clienteInfoContainer');
|
||||
@@ -908,7 +986,8 @@ function renderInfoConversa(conv) {
|
||||
(c.matricula ? '<div class="field"><div class="label">Matrícula</div><div class="value">' + esc(c.matricula) + '</div></div>' : '') +
|
||||
(c.plano ? '<div class="field"><div class="label">Plano</div><div class="value">' + esc(c.plano) + '</div></div>' : '') +
|
||||
'<div class="field"><div class="label">Contato</div><div class="value">' + esc(c.celular || c.telefone || conv.numero || '-') + '</div></div>' +
|
||||
'<div class="field"><div class="label ' + inadClass + '">' + inadLabel + '</div></div>';
|
||||
'<div class="field"><div class="label ' + inadClass + '">' + inadLabel + '</div></div>' +
|
||||
'<button id="btnAtualizarFoto" onclick="atualizarFotoCliente()" style="margin-top:8px;padding:6px 10px;border:1px solid #e5e7eb;border-radius:6px;background:#f9fafb;cursor:pointer;font-size:12px;color:#374151">🔄 Atualizar foto (WhatsApp)</button>';
|
||||
} else if (conv.dependente) {
|
||||
var dep = conv.dependente;
|
||||
fotoContainer.textContent = (dep.nome || '?').charAt(0).toUpperCase();
|
||||
@@ -1361,11 +1440,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,11 +1473,42 @@ 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) {}
|
||||
};
|
||||
|
||||
// ===== PRIVADA TOGGLE =====
|
||||
// ===== NAVEGAÇÃO MOBILE (painel único) =====
|
||||
window.voltarLista = function() {
|
||||
document.body.classList.remove('chat-aberto', 'info-aberto');
|
||||
};
|
||||
window.toggleInfo = function() {
|
||||
document.body.classList.toggle('info-aberto');
|
||||
};
|
||||
|
||||
// Busca a foto do cliente via Evolution (fetchProfile) e atualiza o painel
|
||||
window.atualizarFotoCliente = async function() {
|
||||
if (!conversaAtiva) return;
|
||||
var btn = document.getElementById('btnAtualizarFoto');
|
||||
if (btn) { btn.disabled = true; btn.textContent = '⏳ Buscando...'; }
|
||||
try {
|
||||
var r = await (await fetch('/api/' + alias + '/evolution/refresh-photo/' + conversaAtiva, {
|
||||
method: 'POST', headers: { 'Authorization': 'Bearer ' + token }
|
||||
})).json();
|
||||
if (r.success && r.atualizada) {
|
||||
abrirConversa(conversaAtiva); // recarrega para exibir a nova foto
|
||||
} else if (btn) {
|
||||
btn.disabled = false;
|
||||
btn.textContent = r.message || 'Nenhuma foto encontrada';
|
||||
setTimeout(function(){ btn.textContent = '🔄 Atualizar foto (WhatsApp)'; }, 2500);
|
||||
}
|
||||
} catch(e) {
|
||||
if (btn) { btn.disabled = false; btn.textContent = '🔄 Atualizar foto (WhatsApp)'; }
|
||||
}
|
||||
};
|
||||
|
||||
window.togglePrivada = function() {
|
||||
document.getElementById('privadaToggle').classList.toggle('ativa');
|
||||
var btn = document.getElementById('privadaToggle');
|
||||
@@ -1519,9 +1649,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() {
|
||||
|
||||
@@ -452,8 +452,8 @@ window.darkModeIsDark=function(){return localStorage.getItem('chatc2_dark_mode')
|
||||
} else {
|
||||
html += '<div id="conteudoCarnes_' + c.id + '" style="display:none"></div>';
|
||||
}
|
||||
// Conteudo Itens
|
||||
html += '<div id="conteudoItens_' + c.id + '" style="' + (temCarnes && !temItens ? 'display:none' : '') + ';overflow-x:auto">';
|
||||
// Conteudo Itens (oculto por padrão quando há boletos — Boletos é a aba ativa)
|
||||
html += '<div id="conteudoItens_' + c.id + '" style="' + (temCarnes ? 'display:none;' : '') + 'overflow-x:auto">';
|
||||
if (temItens) {
|
||||
html += '<table style="font-size:12px"><thead><tr>' +
|
||||
'<th>Produto</th><th style="text-align:center">Quantidade</th>' +
|
||||
@@ -492,19 +492,64 @@ window.darkModeIsDark=function(){return localStorage.getItem('chatc2_dark_mode')
|
||||
if (cItens) cItens.style.display = prefixo === 'itens' ? '' : 'none';
|
||||
};
|
||||
|
||||
// Edição INLINE do telefone do dependente (sem prompt/alert)
|
||||
window.editarTelDep = function(id) {
|
||||
var btn = document.querySelector('button[onclick="editarTelDep(' + id + ')"]');
|
||||
var telAtual = btn ? (btn.getAttribute('data-tel') || '') : '';
|
||||
var novo = prompt('Editar telefone do dependente:', telAtual || '');
|
||||
if (novo === null || novo.trim() === telAtual) return;
|
||||
var span = document.getElementById('depTel_' + id);
|
||||
if (!span || span.getAttribute('data-editing') === '1') return;
|
||||
var btnEdit = document.querySelector('button[onclick="editarTelDep(' + id + ')"]');
|
||||
var telAtual = btnEdit ? (btnEdit.getAttribute('data-tel') || '') : '';
|
||||
span.setAttribute('data-editing', '1');
|
||||
span.setAttribute('data-original', span.innerHTML);
|
||||
if (btnEdit) btnEdit.style.display = 'none';
|
||||
span.innerHTML =
|
||||
'<input type="text" id="depTelInput_' + id + '" value="' + telAtual.replace(/"/g, '"') + '" style="width:120px;padding:2px 6px;border:1px solid #667eea;border-radius:4px;font-size:12px;outline:none">' +
|
||||
' <button onclick="confirmarTelDep(' + id + ')" title="Confirmar" style="padding:1px 7px;border:1px solid #059669;border-radius:4px;background:#059669;color:#fff;cursor:pointer;font-size:11px">✔</button>' +
|
||||
' <button onclick="cancelarTelDep(' + id + ')" title="Cancelar" style="padding:1px 7px;border:1px solid #d1d5db;border-radius:4px;background:#fff;cursor:pointer;font-size:11px">✖</button>';
|
||||
var inp = document.getElementById('depTelInput_' + id);
|
||||
if (inp) {
|
||||
inp.focus();
|
||||
inp.addEventListener('keydown', function(e) {
|
||||
if (e.key === 'Enter') { e.preventDefault(); confirmarTelDep(id); }
|
||||
else if (e.key === 'Escape') { cancelarTelDep(id); }
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
window.cancelarTelDep = function(id) {
|
||||
var span = document.getElementById('depTel_' + id);
|
||||
if (!span) return;
|
||||
span.innerHTML = span.getAttribute('data-original') || '-';
|
||||
span.setAttribute('data-editing', '');
|
||||
var btnEdit = document.querySelector('button[onclick="editarTelDep(' + id + ')"]');
|
||||
if (btnEdit) btnEdit.style.display = '';
|
||||
};
|
||||
|
||||
window.confirmarTelDep = function(id) {
|
||||
var inp = document.getElementById('depTelInput_' + id);
|
||||
var span = document.getElementById('depTel_' + id);
|
||||
if (!inp || !span) return;
|
||||
var novo = (inp.value || '').trim();
|
||||
var btnEdit = document.querySelector('button[onclick="editarTelDep(' + id + ')"]');
|
||||
inp.disabled = true;
|
||||
fetch('/api/' + alias + '/dependents/' + id + '/phone', {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
|
||||
body: JSON.stringify({ telefone: novo.trim() })
|
||||
body: JSON.stringify({ telefone: novo })
|
||||
}).then(function(r) { return r.json(); }).then(function(d) {
|
||||
if (d.success) { document.getElementById('depTel_' + id).textContent = novo.trim(); alert('Telefone atualizado!'); }
|
||||
else alert('Erro: ' + d.error);
|
||||
}).catch(function(e) { alert('Erro: ' + e.message); });
|
||||
if (d.success) {
|
||||
span.setAttribute('data-editing', '');
|
||||
span.textContent = novo || '-';
|
||||
if (btnEdit) { btnEdit.setAttribute('data-tel', novo); btnEdit.style.display = ''; }
|
||||
} else {
|
||||
inp.disabled = false;
|
||||
inp.style.borderColor = '#ef4444';
|
||||
inp.title = d.error || 'Erro ao salvar';
|
||||
}
|
||||
}).catch(function(e) {
|
||||
inp.disabled = false;
|
||||
inp.style.borderColor = '#ef4444';
|
||||
inp.title = e.message;
|
||||
});
|
||||
};
|
||||
|
||||
// ============================================================
|
||||
|
||||
@@ -646,3 +646,87 @@ tr:last-child td { border-bottom: none; }
|
||||
::-webkit-scrollbar-track { background: transparent; }
|
||||
::-webkit-scrollbar-thumb { background: #d1d5db; border-radius: 3px; }
|
||||
::-webkit-scrollbar-thumb:hover { background: #9ca3af; }
|
||||
|
||||
/* =====================================================
|
||||
RESPONSIVO (tablet / celular)
|
||||
===================================================== */
|
||||
@media (max-width: 1024px) {
|
||||
.container { padding: 18px; }
|
||||
.search-bar select { min-width: 150px; }
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
/* A sidebar lateral vira uma barra horizontal no topo */
|
||||
body { flex-direction: column; }
|
||||
.sidebar {
|
||||
width: 100%;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
overflow-x: auto;
|
||||
}
|
||||
.sidebar-brand {
|
||||
border-bottom: none;
|
||||
padding: 10px 14px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
.sidebar-brand span { display: none; }
|
||||
.sidebar-brand h2 { font-size: 15px; }
|
||||
.sidebar-nav {
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
padding: 6px;
|
||||
gap: 2px;
|
||||
overflow-x: auto;
|
||||
overflow-y: hidden;
|
||||
flex: 1;
|
||||
}
|
||||
.sidebar-nav .nav-label { display: none; }
|
||||
.sidebar-nav a {
|
||||
white-space: nowrap;
|
||||
padding: 8px 12px;
|
||||
margin-bottom: 0;
|
||||
font-size: 13px;
|
||||
}
|
||||
.sidebar-footer {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
border-top: none;
|
||||
padding: 6px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
.sidebar-footer .dark-mode-toggle {
|
||||
width: auto;
|
||||
margin: 0 4px 0 0;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.sidebar-footer a { padding: 8px 10px; white-space: nowrap; }
|
||||
|
||||
/* Conteúdo principal */
|
||||
.container { padding: 14px; }
|
||||
.topbar { padding: 12px 16px; flex-wrap: wrap; gap: 8px; }
|
||||
.topbar-title { font-size: 15px; }
|
||||
.user-info { gap: 8px; }
|
||||
.card { padding: 16px; border-radius: var(--radius-md); }
|
||||
|
||||
/* Tabelas: rolagem horizontal em vez de espremer */
|
||||
.table-wrapper { overflow-x: auto; -webkit-overflow-scrolling: touch; }
|
||||
.table-wrapper table { min-width: 560px; }
|
||||
|
||||
/* Busca/filtros empilham e ocupam a largura */
|
||||
.search-bar { gap: 8px; }
|
||||
.search-bar input,
|
||||
.search-bar select,
|
||||
.search-bar button { width: 100%; min-width: 0; }
|
||||
.search-bar .total-info { margin-left: 0; }
|
||||
|
||||
/* Modal quase tela cheia */
|
||||
.modal-box { width: 94%; padding: 20px; max-height: 90vh; overflow-y: auto; }
|
||||
}
|
||||
|
||||
@media (max-width: 480px) {
|
||||
.container { padding: 10px; }
|
||||
.card { padding: 13px; }
|
||||
.btn { padding: 9px 14px; font-size: 13px; }
|
||||
.info-grid { grid-template-columns: 1fr; }
|
||||
}
|
||||
|
||||
@@ -36,6 +36,12 @@ body { background:#f3f4f6; display:flex; min-height:100vh; }
|
||||
.user-name { font-size:14px; font-weight:500; color:#374151; }
|
||||
.status-badge { display:inline-flex; align-items:center; gap:5px; padding:4px 12px; border-radius:20px; font-size:12px; font-weight:600; }
|
||||
.status-badge.online { background:#d1fae5; color:#065f46; }
|
||||
|
||||
/* Responsivo: abas rolam horizontalmente no celular */
|
||||
@media (max-width: 768px) {
|
||||
.tabs { overflow-x: auto; -webkit-overflow-scrolling: touch; }
|
||||
.tabs button { flex: 0 0 auto; white-space: nowrap; padding: 12px 14px; font-size: 13px; }
|
||||
}
|
||||
</style>
|
||||
<link rel="stylesheet" href="/css/dark-mode.css">
|
||||
<script>function darkModeToggle(){var e=document.body;if(!e)return;var a=localStorage.getItem('chatc2_dark_mode')!=='true';e.classList.toggle('dark-mode',a);localStorage.setItem('chatc2_dark_mode',a?'true':'false');document.querySelectorAll('.dark-mode-toggle').forEach(function(b){b.innerHTML=a?'☀️ Claro':'🌙 Escuro'});}
|
||||
@@ -85,6 +91,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 +753,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 +929,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