feat(auth): include subscription tier in token and session payload
- Adds subscription tier info to access/refresh tokens. - Stores tier details in session Redis record. - Returns tier data in login/register response. - Uses default tier when user has none.
This commit is contained in:
1 parent
7f42def478
commit
d27526e026
20 files changed
+2133
-40
No files matched your search
@@ -0,0 +1,210 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { requireAdminUser } from "@/lib/auth/middleware";
|
||||
import {
|
||||
getSubscriptionTierById,
|
||||
updateSubscriptionTier,
|
||||
deleteSubscriptionTier,
|
||||
countUsersInSubscriptionTier,
|
||||
} from "@/lib/prisma/queries/subscription-tiers";
|
||||
import { updateSubscriptionTierSchema } from "@/lib/validation-schemas";
|
||||
import { secureLogger } from "@/lib/secure-logger";
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
const auth = await requireAdminUser(request);
|
||||
if (auth instanceof NextResponse) {
|
||||
return auth;
|
||||
}
|
||||
|
||||
try {
|
||||
const { id } = await params;
|
||||
const tierId = Number.parseInt(id, 10);
|
||||
|
||||
if (Number.isNaN(tierId) || tierId <= 0) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: "ID de tier inválido" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const tier = await getSubscriptionTierById(tierId, true);
|
||||
|
||||
if (!tier) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: "Plano não encontrado" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
tier,
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
} catch (error) {
|
||||
secureLogger.error("❌ Erro ao consultar subscription tier", {
|
||||
error: error instanceof Error ? error.message : "Erro desconhecido",
|
||||
});
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: "Erro ao consultar plano de assinatura",
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function PATCH(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
const auth = await requireAdminUser(request);
|
||||
if (auth instanceof NextResponse) {
|
||||
return auth;
|
||||
}
|
||||
|
||||
try {
|
||||
const { id } = await params;
|
||||
const tierId = Number.parseInt(id, 10);
|
||||
|
||||
if (Number.isNaN(tierId) || tierId <= 0) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: "ID de tier inválido" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const parsed = updateSubscriptionTierSchema.parse(body);
|
||||
|
||||
const updatedTier = await updateSubscriptionTier(tierId, {
|
||||
name: parsed.name,
|
||||
slug: parsed.slug,
|
||||
description: parsed.description ?? undefined,
|
||||
level: parsed.level,
|
||||
price_monthly: parsed.priceMonthly ?? undefined,
|
||||
price_yearly: parsed.priceYearly ?? undefined,
|
||||
is_active: parsed.isActive,
|
||||
features: parsed.features?.map((feature, index) => ({
|
||||
feature_key: feature.featureKey,
|
||||
label: feature.label,
|
||||
description: feature.description ?? null,
|
||||
sort_order: feature.sortOrder ?? index,
|
||||
_delete: feature._delete ?? false,
|
||||
})),
|
||||
});
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
tier: updatedTier,
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
} catch (error) {
|
||||
if (error instanceof Error && "issues" in error) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: "Dados inválidos",
|
||||
details: (error as any).issues,
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
secureLogger.error("❌ Erro ao atualizar subscription tier", {
|
||||
error: error instanceof Error ? error.message : "Erro desconhecido",
|
||||
});
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: "Erro ao atualizar plano de assinatura",
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
const auth = await requireAdminUser(request);
|
||||
if (auth instanceof NextResponse) {
|
||||
return auth;
|
||||
}
|
||||
|
||||
try {
|
||||
const { id } = await params;
|
||||
const tierId = Number.parseInt(id, 10);
|
||||
|
||||
if (Number.isNaN(tierId) || tierId <= 0) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: "ID de tier inválido" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const tier = await getSubscriptionTierById(tierId, false);
|
||||
|
||||
if (!tier) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: "Plano não encontrado" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
if (tier.slug === "free") {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: "O plano padrão 'free' não pode ser removido.",
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const userCount = await countUsersInSubscriptionTier(tierId);
|
||||
|
||||
if (userCount > 0) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: "Plano não pode ser removido enquanto houver usuários associados.",
|
||||
users: userCount,
|
||||
},
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
await deleteSubscriptionTier(tierId);
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
} catch (error) {
|
||||
secureLogger.error("❌ Erro ao remover subscription tier", {
|
||||
error: error instanceof Error ? error.message : "Erro desconhecido",
|
||||
});
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: "Erro ao remover plano de assinatura",
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user