feat: implement advanced funnel management with multiple funnels and team assignments
All checks were successful
Build and Deploy / build-and-push (push) Successful in 2m32s
All checks were successful
Build and Deploy / build-and-push (push) Successful in 2m32s
- Updated DB schema to support multiple funnels (funnels table) and their stages (funnel_stages table).
- Added funnel_id to teams table to link teams to specific funnels.
- Redesigned /admin/funnels page ('Meus Funis') to allow creating multiple funnels, managing their stages, and assigning them to teams.
- Updated Dashboard, UserDetail, and AttendanceDetail to dynamically load the correct funnel based on the selected team or user's assigned team.
This commit is contained in:
143
backend/index.js
143
backend/index.js
@@ -570,11 +570,14 @@ apiRouter.get('/funnels', async (req, res) => {
|
||||
const effectiveTenantId = req.user.role === 'super_admin' ? tenantId : req.user.tenant_id;
|
||||
if (!effectiveTenantId || effectiveTenantId === 'all') return res.json([]);
|
||||
|
||||
const [rows] = await pool.query('SELECT * FROM tenant_funnels WHERE tenant_id = ? ORDER BY order_index ASC', [effectiveTenantId]);
|
||||
const [funnels] = await pool.query('SELECT * FROM funnels WHERE tenant_id = ? ORDER BY created_at ASC', [effectiveTenantId]);
|
||||
|
||||
// If no funnels exist for this tenant, seed the default ones
|
||||
if (rows.length === 0) {
|
||||
const defaultFunnels = [
|
||||
// Seed default funnel if none exists
|
||||
if (funnels.length === 0) {
|
||||
const fid = `funnel_${crypto.randomUUID().split('-')[0]}`;
|
||||
await pool.query('INSERT INTO funnels (id, tenant_id, name) VALUES (?, ?, ?)', [fid, effectiveTenantId, 'Funil Padrão']);
|
||||
|
||||
const defaultStages = [
|
||||
{ name: 'Sem atendimento', color: 'bg-zinc-100 text-zinc-700 border-zinc-200 dark:bg-dark-input dark:text-dark-muted dark:border-dark-border', order: 0 },
|
||||
{ name: 'Identificação', color: 'bg-blue-100 text-blue-700 border-blue-200 dark:bg-blue-900/30 dark:text-blue-400 dark:border-blue-800', order: 1 },
|
||||
{ name: 'Negociação', color: 'bg-purple-100 text-purple-700 border-purple-200 dark:bg-purple-900/30 dark:text-purple-400 dark:border-purple-800', order: 2 },
|
||||
@@ -582,51 +585,61 @@ apiRouter.get('/funnels', async (req, res) => {
|
||||
{ name: 'Perdidos', color: 'bg-red-100 text-red-700 border-red-200 dark:bg-red-900/30 dark:text-red-400 dark:border-red-800', order: 4 }
|
||||
];
|
||||
|
||||
for (const f of defaultFunnels) {
|
||||
const fid = `funnel_${crypto.randomUUID().split('-')[0]}`;
|
||||
for (const s of defaultStages) {
|
||||
const sid = `stage_${crypto.randomUUID().split('-')[0]}`;
|
||||
await pool.query(
|
||||
'INSERT INTO tenant_funnels (id, tenant_id, name, color_class, order_index) VALUES (?, ?, ?, ?, ?)',
|
||||
[fid, effectiveTenantId, f.name, f.color, f.order]
|
||||
'INSERT INTO funnel_stages (id, funnel_id, name, color_class, order_index) VALUES (?, ?, ?, ?, ?)',
|
||||
[sid, fid, s.name, s.color, s.order]
|
||||
);
|
||||
}
|
||||
|
||||
const [newRows] = await pool.query('SELECT * FROM tenant_funnels WHERE tenant_id = ? ORDER BY order_index ASC', [effectiveTenantId]);
|
||||
return res.json(newRows);
|
||||
// Update all teams of this tenant to use this funnel if they have none
|
||||
await pool.query('UPDATE teams SET funnel_id = ? WHERE tenant_id = ? AND funnel_id IS NULL', [fid, effectiveTenantId]);
|
||||
|
||||
funnels.push({ id: fid, tenant_id: effectiveTenantId, name: 'Funil Padrão' });
|
||||
}
|
||||
|
||||
res.json(rows);
|
||||
|
||||
const [stages] = await pool.query('SELECT * FROM funnel_stages WHERE funnel_id IN (?) ORDER BY order_index ASC', [funnels.map(f => f.id)]);
|
||||
const [teams] = await pool.query('SELECT id, funnel_id FROM teams WHERE tenant_id = ? AND funnel_id IS NOT NULL', [effectiveTenantId]);
|
||||
|
||||
const result = funnels.map(f => ({
|
||||
...f,
|
||||
stages: stages.filter(s => s.funnel_id === f.id),
|
||||
teamIds: teams.filter(t => t.funnel_id === f.id).map(t => t.id)
|
||||
}));
|
||||
|
||||
res.json(result);
|
||||
} catch (error) {
|
||||
console.error("GET /funnels error:", error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
apiRouter.post('/funnels', requireRole(['admin', 'owner', 'manager', 'super_admin']), async (req, res) => {
|
||||
const { name, color_class, order_index, tenantId } = req.body;
|
||||
const { name, tenantId } = req.body;
|
||||
const effectiveTenantId = req.user.role === 'super_admin' ? tenantId : req.user.tenant_id;
|
||||
try {
|
||||
const fid = `funnel_${crypto.randomUUID().split('-')[0]}`;
|
||||
await pool.query(
|
||||
'INSERT INTO tenant_funnels (id, tenant_id, name, color_class, order_index) VALUES (?, ?, ?, ?, ?)',
|
||||
[fid, effectiveTenantId, name, color_class, order_index || 0]
|
||||
);
|
||||
res.status(201).json({ id: fid, message: 'Etapa do funil criada com sucesso.' });
|
||||
await pool.query('INSERT INTO funnels (id, tenant_id, name) VALUES (?, ?, ?)', [fid, effectiveTenantId, name]);
|
||||
res.status(201).json({ id: fid });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
apiRouter.put('/funnels/:id', requireRole(['admin', 'owner', 'manager', 'super_admin']), async (req, res) => {
|
||||
const { name, color_class, order_index } = req.body;
|
||||
const { name, teamIds } = req.body;
|
||||
try {
|
||||
const [existing] = await pool.query('SELECT * FROM tenant_funnels WHERE id = ?', [req.params.id]);
|
||||
if (existing.length === 0) return res.status(404).json({ error: 'Etapa não encontrada' });
|
||||
if (req.user.role !== 'super_admin' && existing[0].tenant_id !== req.user.tenant_id) return res.status(403).json({ error: 'Acesso negado' });
|
||||
|
||||
await pool.query(
|
||||
'UPDATE tenant_funnels SET name = ?, color_class = ?, order_index = ? WHERE id = ?',
|
||||
[name || existing[0].name, color_class || existing[0].color_class, order_index !== undefined ? order_index : existing[0].order_index, req.params.id]
|
||||
);
|
||||
res.json({ message: 'Etapa do funil atualizada.' });
|
||||
if (name) {
|
||||
await pool.query('UPDATE funnels SET name = ? WHERE id = ?', [name, req.params.id]);
|
||||
}
|
||||
if (teamIds && Array.isArray(teamIds)) {
|
||||
await pool.query('UPDATE teams SET funnel_id = NULL WHERE funnel_id = ?', [req.params.id]);
|
||||
if (teamIds.length > 0) {
|
||||
await pool.query('UPDATE teams SET funnel_id = ? WHERE id IN (?)', [req.params.id, teamIds]);
|
||||
}
|
||||
}
|
||||
res.json({ message: 'Funnel updated.' });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
@@ -634,12 +647,49 @@ apiRouter.put('/funnels/:id', requireRole(['admin', 'owner', 'manager', 'super_a
|
||||
|
||||
apiRouter.delete('/funnels/:id', requireRole(['admin', 'owner', 'manager', 'super_admin']), async (req, res) => {
|
||||
try {
|
||||
const [existing] = await pool.query('SELECT * FROM tenant_funnels WHERE id = ?', [req.params.id]);
|
||||
if (existing.length === 0) return res.status(404).json({ error: 'Etapa não encontrada' });
|
||||
if (req.user.role !== 'super_admin' && existing[0].tenant_id !== req.user.tenant_id) return res.status(403).json({ error: 'Acesso negado' });
|
||||
await pool.query('DELETE FROM funnel_stages WHERE funnel_id = ?', [req.params.id]);
|
||||
await pool.query('UPDATE teams SET funnel_id = NULL WHERE funnel_id = ?', [req.params.id]);
|
||||
await pool.query('DELETE FROM funnels WHERE id = ?', [req.params.id]);
|
||||
res.json({ message: 'Funnel deleted.' });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
await pool.query('DELETE FROM tenant_funnels WHERE id = ?', [req.params.id]);
|
||||
res.json({ message: 'Etapa do funil excluída.' });
|
||||
apiRouter.post('/funnels/:id/stages', requireRole(['admin', 'owner', 'manager', 'super_admin']), async (req, res) => {
|
||||
const { name, color_class, order_index } = req.body;
|
||||
try {
|
||||
const sid = `stage_${crypto.randomUUID().split('-')[0]}`;
|
||||
await pool.query(
|
||||
'INSERT INTO funnel_stages (id, funnel_id, name, color_class, order_index) VALUES (?, ?, ?, ?, ?)',
|
||||
[sid, req.params.id, name, color_class, order_index || 0]
|
||||
);
|
||||
res.status(201).json({ id: sid });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
apiRouter.put('/funnel_stages/:id', requireRole(['admin', 'owner', 'manager', 'super_admin']), async (req, res) => {
|
||||
const { name, color_class, order_index } = req.body;
|
||||
try {
|
||||
const [existing] = await pool.query('SELECT * FROM funnel_stages WHERE id = ?', [req.params.id]);
|
||||
if (existing.length === 0) return res.status(404).json({ error: 'Stage not found' });
|
||||
|
||||
await pool.query(
|
||||
'UPDATE funnel_stages SET name = ?, color_class = ?, order_index = ? WHERE id = ?',
|
||||
[name || existing[0].name, color_class || existing[0].color_class, order_index !== undefined ? order_index : existing[0].order_index, req.params.id]
|
||||
);
|
||||
res.json({ message: 'Stage updated.' });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
apiRouter.delete('/funnel_stages/:id', requireRole(['admin', 'owner', 'manager', 'super_admin']), async (req, res) => {
|
||||
try {
|
||||
await pool.query('DELETE FROM funnel_stages WHERE id = ?', [req.params.id]);
|
||||
res.json({ message: 'Stage deleted.' });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
@@ -1043,20 +1093,39 @@ const provisionSuperAdmin = async (retries = 10, delay = 10000) => {
|
||||
console.log('Schema update note (funnel_stage):', err.message);
|
||||
}
|
||||
|
||||
// Create tenant_funnels table
|
||||
// Create funnels table
|
||||
await connection.query(`
|
||||
CREATE TABLE IF NOT EXISTS tenant_funnels (
|
||||
CREATE TABLE IF NOT EXISTS funnels (
|
||||
id varchar(36) NOT NULL,
|
||||
tenant_id varchar(36) NOT NULL,
|
||||
name varchar(255) NOT NULL,
|
||||
color_class varchar(255) DEFAULT 'bg-zinc-100 text-zinc-800 border-zinc-200',
|
||||
order_index int DEFAULT 0,
|
||||
created_at timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (id),
|
||||
KEY tenant_id (tenant_id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
`);
|
||||
|
||||
// Create funnel_stages table
|
||||
await connection.query(`
|
||||
CREATE TABLE IF NOT EXISTS funnel_stages (
|
||||
id varchar(36) NOT NULL,
|
||||
funnel_id varchar(36) NOT NULL,
|
||||
name varchar(255) NOT NULL,
|
||||
color_class varchar(255) DEFAULT 'bg-zinc-100 text-zinc-800 border-zinc-200',
|
||||
order_index int DEFAULT 0,
|
||||
created_at timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (id),
|
||||
KEY funnel_id (funnel_id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
`);
|
||||
|
||||
// Add funnel_id to teams
|
||||
try {
|
||||
await connection.query("ALTER TABLE teams ADD COLUMN funnel_id VARCHAR(36) DEFAULT NULL");
|
||||
} catch (err) {
|
||||
if (err.code !== 'ER_DUP_FIELDNAME') console.log('Schema update note (teams.funnel_id):', err.message);
|
||||
}
|
||||
|
||||
connection.release();
|
||||
// Ensure system tenant exists
|
||||
await pool.query('INSERT IGNORE INTO tenants (id, name, slug, admin_email, status) VALUES (?, ?, ?, ?, ?)', ['system', 'System Admin', 'system', email, 'active']);
|
||||
|
||||
Reference in New Issue
Block a user