feat: adicionar campo de senha na criacao e edicao de inquilinos pelo admin
Build and Deploy (Gitea Actions) / build-and-deploy (push) Successful in 2m36s Details

This commit is contained in:
Sidney 2026-06-07 20:32:09 -03:00
parent 3d2b41bf56
commit 9d13e6626d
2 changed files with 20 additions and 20 deletions

View File

@ -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 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; const planId = planRes.rows.length > 0 ? planRes.rows[0].id : null;
// Start transaction
await query('BEGIN');
// 1. Create Tenant
const res = await query( const res = await query(
`INSERT INTO tenants (name, slug, email, phone, active_plan_id, subscription_status, subscription_ends_at) `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 *`, VALUES ($1, $2, $3, $4, $5, $6, NOW() + INTERVAL '30 days') RETURNING *`,
[name, slug, email, phone, planId, status || 'trial'] [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 (password && email) {
if (email && password) {
const salt = await bcrypt.genSalt(10); const salt = await bcrypt.genSalt(10);
const hash = await bcrypt.hash(password, salt); const hash = await bcrypt.hash(password, salt);
await query( await query(`
`INSERT INTO users (tenant_id, name, email, password_hash, role, phone) INSERT INTO users (tenant_id, name, email, password_hash, role)
VALUES ($1, $2, $3, $4, 'owner', $5)`, VALUES ($1, $2, $3, $4, 'owner')
[tenant.id, name, email, hash, phone] `, [newTenant.id, name, email, hash]);
);
} }
await query('COMMIT'); return NextResponse.json(newTenant);
return NextResponse.json(tenant);
} catch (error: any) { } catch (error: any) {
await query('ROLLBACK');
return NextResponse.json({ error: error.message }, { status: 500 }); return NextResponse.json({ error: error.message }, { status: 500 });
} }
} }
export async function PUT(req: NextRequest) { export async function PUT(req: NextRequest) {
try { 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 }); 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']); 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] [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]); return NextResponse.json(res.rows[0]);
} catch (error: any) { } catch (error: any) {
return NextResponse.json({ error: error.message }, { status: 500 }); return NextResponse.json({ error: error.message }, { status: 500 });

View File

@ -168,12 +168,12 @@ export default function Tenants() {
</div> </div>
</div> </div>
<div className="grid-2"> <div className="grid-2">
<div className="form-group"><label className="form-label">E-mail</label><input className="form-input" type="email" value={form.email || ''} onChange={e => setForm(p => ({ ...p, email: e.target.value }))} /></div> <div className="form-group"><label className="form-label">E-mail (Login) *</label><input type="email" required className="form-input" value={form.email || ''} onChange={e => setForm(p => ({ ...p, email: e.target.value }))} /></div>
<div className="form-group"><label className="form-label">Telefone</label><input className="form-input" value={form.phone || ''} onChange={e => setForm(p => ({ ...p, phone: e.target.value }))} /></div> <div className="form-group"><label className="form-label">Telefone</label><input className="form-input" value={form.phone || ''} onChange={e => setForm(p => ({ ...p, phone: e.target.value }))} /></div>
</div> </div>
{!editing && ( <div className="grid-2">
<div className="form-group"><label className="form-label">Senha Inicial do Administrador *</label><input type="password" required className="form-input" value={form.password || ''} onChange={e => setForm(p => ({ ...p, password: e.target.value }))} placeholder="Mínimo 6 caracteres" /></div> <div className="form-group"><label className="form-label">Senha de Acesso {editing ? '(Deixe em branco para manter)' : '*'}</label><input type="text" className="form-input" placeholder={editing ? '••••••••' : 'Senha do Inquilino'} value={form.password || ''} onChange={e => setForm(p => ({ ...p, password: e.target.value }))} /></div>
)} </div>
<div className="grid-2"> <div className="grid-2">
<div className="form-group"><label className="form-label">Plano</label> <div className="form-group"><label className="form-label">Plano</label>
<select className="form-select" value={form.plan || ''} onChange={e => setForm(p => ({ ...p, plan: e.target.value }))}> <select className="form-select" value={form.plan || ''} onChange={e => setForm(p => ({ ...p, plan: e.target.value }))}>