diff --git a/admin/src/app/api/tenants/route.ts b/admin/src/app/api/tenants/route.ts index 2a71df9..1cb5c66 100644 --- a/admin/src/app/api/tenants/route.ts +++ b/admin/src/app/api/tenants/route.ts @@ -38,40 +38,32 @@ export async function POST(req: NextRequest) { const planRes = await query('SELECT id FROM plans WHERE name = $1 LIMIT 1', [plan || 'Starter']); const planId = planRes.rows.length > 0 ? planRes.rows[0].id : null; - // Start transaction - await query('BEGIN'); - - // 1. Create Tenant const res = await query( `INSERT INTO tenants (name, slug, email, phone, active_plan_id, subscription_status, subscription_ends_at) VALUES ($1, $2, $3, $4, $5, $6, NOW() + INTERVAL '30 days') RETURNING *`, [name, slug, email, phone, planId, status || 'trial'] ); - const tenant = res.rows[0]; + const newTenant = res.rows[0]; - // 2. Create Owner User if password provided - if (email && password) { + if (password && email) { const salt = await bcrypt.genSalt(10); const hash = await bcrypt.hash(password, salt); - await query( - `INSERT INTO users (tenant_id, name, email, password_hash, role, phone) - VALUES ($1, $2, $3, $4, 'owner', $5)`, - [tenant.id, name, email, hash, phone] - ); + await query(` + INSERT INTO users (tenant_id, name, email, password_hash, role) + VALUES ($1, $2, $3, $4, 'owner') + `, [newTenant.id, name, email, hash]); } - await query('COMMIT'); - return NextResponse.json(tenant); + return NextResponse.json(newTenant); } catch (error: any) { - await query('ROLLBACK'); return NextResponse.json({ error: error.message }, { status: 500 }); } } export async function PUT(req: NextRequest) { try { - const { id, name, slug, email, phone, plan, status } = await req.json(); + const { id, name, slug, email, phone, plan, status, password } = await req.json(); if (!id) return NextResponse.json({ error: 'Missing id' }, { status: 400 }); const planRes = await query('SELECT id FROM plans WHERE name = $1 LIMIT 1', [plan || 'Starter']); @@ -84,6 +76,14 @@ export async function PUT(req: NextRequest) { [name, slug, email, phone, planId, status, id] ); + if (password) { + const salt = await bcrypt.genSalt(10); + const hash = await bcrypt.hash(password, salt); + await query(` + UPDATE users SET password_hash = $1 WHERE tenant_id = $2 AND role = 'owner' + `, [hash, id]); + } + return NextResponse.json(res.rows[0]); } catch (error: any) { return NextResponse.json({ error: error.message }, { status: 500 }); diff --git a/admin/src/components/Tenants.tsx b/admin/src/components/Tenants.tsx index d55be80..fbc4282 100644 --- a/admin/src/components/Tenants.tsx +++ b/admin/src/components/Tenants.tsx @@ -168,12 +168,12 @@ export default function Tenants() {
-
setForm(p => ({ ...p, email: e.target.value }))} />
+
setForm(p => ({ ...p, email: e.target.value }))} />
setForm(p => ({ ...p, phone: e.target.value }))} />
- {!editing && ( -
setForm(p => ({ ...p, password: e.target.value }))} placeholder="Mínimo 6 caracteres" />
- )} +
+
setForm(p => ({ ...p, password: e.target.value }))} />
+