feat: implement real tenant creation and listing
All checks were successful
Build and Deploy / build-and-push (push) Successful in 2m34s

This commit is contained in:
Cauê Faleiros
2026-02-24 11:44:28 -03:00
parent 113ea4abfb
commit 2742bafb00
3 changed files with 108 additions and 8 deletions

View File

@@ -123,13 +123,56 @@ app.get('/api/attendances/:id', async (req, res) => {
// --- Rotas de Tenants (Super Admin) ---
app.get('/api/tenants', async (req, res) => {
try {
const [rows] = await pool.query('SELECT * FROM tenants');
// Buscar tenants e contar usuários/atendimentos
const query = `
SELECT t.*,
(SELECT COUNT(*) FROM users u WHERE u.tenant_id = t.id) as user_count,
(SELECT COUNT(*) FROM attendances a WHERE a.tenant_id = t.id) as attendance_count
FROM tenants t
`;
const [rows] = await pool.query(query);
res.json(rows);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Criar Tenant
app.post('/api/tenants', async (req, res) => {
const { name, slug, admin_email, status } = req.body;
const crypto = require('crypto');
const connection = await pool.getConnection();
try {
await connection.beginTransaction();
const tenantId = `tenant_${crypto.randomUUID().split('-')[0]}`; // Simple ID generation
// 1. Criar Tenant
await connection.query(
'INSERT INTO tenants (id, name, slug, admin_email, status) VALUES (?, ?, ?, ?, ?)',
[tenantId, name, slug, admin_email, status || 'active']
);
// 2. Criar Usuário Admin Default
const userId = `u_${crypto.randomUUID().split('-')[0]}`;
await connection.query(
'INSERT INTO users (id, tenant_id, name, email, role, status) VALUES (?, ?, ?, ?, ?, ?)',
[userId, tenantId, 'Admin', admin_email, 'admin', 'active']
);
await connection.commit();
res.status(201).json({ message: 'Tenant created successfully', id: tenantId });
} catch (error) {
await connection.rollback();
console.error('Erro ao criar tenant:', error);
res.status(500).json({ error: error.message });
} finally {
connection.release();
}
});
// Serve index.html for any unknown routes (for client-side routing)
if (process.env.NODE_ENV === 'production') {
app.get('*', (req, res) => {