diff --git a/admin/src/app/api/dashboard/route.ts b/admin/src/app/api/dashboard/route.ts index 8bdddfd..65c1c9d 100644 --- a/admin/src/app/api/dashboard/route.ts +++ b/admin/src/app/api/dashboard/route.ts @@ -15,9 +15,9 @@ export async function GET() { // Total MRR const mrrRes = await query(` - SELECT COALESCE(SUM(p.price), 0) as mrr + SELECT COALESCE(SUM(p.price_monthly), 0) as mrr FROM tenants t - JOIN plans p ON t.active_plan_id = p.id + JOIN plans p ON t.plan_id = p.id WHERE t.subscription_status = 'active' `); const mrr = mrrRes.rows[0].mrr; @@ -26,7 +26,7 @@ export async function GET() { const plansRes = await query(` SELECT p.name, COUNT(t.id) as count FROM plans p - LEFT JOIN tenants t ON t.active_plan_id = p.id AND t.subscription_status = 'active' + LEFT JOIN tenants t ON t.plan_id = p.id AND t.subscription_status = 'active' GROUP BY p.name ORDER BY count DESC `); @@ -34,9 +34,9 @@ export async function GET() { // Recent tenants const recentRes = await query(` SELECT t.id, t.name, COALESCE(p.name, 'N/A') as plan, t.subscription_status as status, - COALESCE(p.price, 0) as mrr, TO_CHAR(t.created_at, 'YYYY-MM-DD') as date + COALESCE(p.price_monthly, 0) as mrr, TO_CHAR(t.created_at, 'YYYY-MM-DD') as date FROM tenants t - LEFT JOIN plans p ON t.active_plan_id = p.id + LEFT JOIN plans p ON t.plan_id = p.id ORDER BY t.created_at DESC LIMIT 5 `); diff --git a/admin/src/app/api/tenants/route.ts b/admin/src/app/api/tenants/route.ts index 1cb5c66..4b5099c 100644 --- a/admin/src/app/api/tenants/route.ts +++ b/admin/src/app/api/tenants/route.ts @@ -8,11 +8,11 @@ export async function GET(req: NextRequest) { COALESCE(p.name, 'Starter') as plan, t.subscription_status as status, t.email, t.phone, - COALESCE(p.price, 0) as mrr, + COALESCE(p.price_monthly, 0) as mrr, TO_CHAR(t.created_at, 'YYYY-MM-DD') as "createdAt", TO_CHAR(t.subscription_ends_at, 'YYYY-MM-DD') as "trialEnds" FROM tenants t - LEFT JOIN plans p ON t.active_plan_id = p.id + LEFT JOIN plans p ON t.plan_id = p.id ORDER BY t.created_at DESC `); @@ -39,7 +39,7 @@ export async function POST(req: NextRequest) { const planId = planRes.rows.length > 0 ? planRes.rows[0].id : null; 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, 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'] ); @@ -71,7 +71,7 @@ export async function PUT(req: NextRequest) { const res = await query( `UPDATE tenants - SET name = $1, slug = $2, email = $3, phone = $4, active_plan_id = $5, subscription_status = $6, updated_at = NOW() + SET name = $1, slug = $2, email = $3, phone = $4, plan_id = $5, subscription_status = $6, updated_at = NOW() WHERE id = $7 RETURNING *`, [name, slug, email, phone, planId, status, id] );