- Atualiza cores das badges para padrão cyan do projeto - Adiciona degradê no header do card de perfil - Implementa upload de foto de perfil via API Convex - Integra notificações do Convex com preferências do usuário - Cria API /api/notifications/send para verificar preferências - Melhora layout das páginas de login/recuperação com degradê - Adiciona badge "Helpdesk" e título "Raven" consistente 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
88 lines
2.5 KiB
TypeScript
88 lines
2.5 KiB
TypeScript
/**
|
|
* API de Upload de Avatar
|
|
* POST - Faz upload de uma nova foto de perfil
|
|
*/
|
|
|
|
import { NextRequest, NextResponse } from "next/server"
|
|
|
|
import { getServerSession } from "@/lib/auth-server"
|
|
import { prisma } from "@/lib/prisma"
|
|
import { createConvexClient } from "@/server/convex-client"
|
|
import { api } from "@/convex/_generated/api"
|
|
import type { Id } from "@/convex/_generated/dataModel"
|
|
|
|
const MAX_FILE_SIZE = 5 * 1024 * 1024 // 5MB
|
|
const ALLOWED_TYPES = ["image/jpeg", "image/png", "image/webp", "image/gif"]
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const session = await getServerSession()
|
|
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: "Não autorizado" }, { status: 401 })
|
|
}
|
|
|
|
const formData = await request.formData()
|
|
const file = formData.get("file") as File | null
|
|
|
|
if (!file) {
|
|
return NextResponse.json({ error: "Nenhum arquivo enviado" }, { status: 400 })
|
|
}
|
|
|
|
// Valida tipo
|
|
if (!ALLOWED_TYPES.includes(file.type)) {
|
|
return NextResponse.json(
|
|
{ error: "Tipo de arquivo não permitido. Use JPG, PNG, WebP ou GIF." },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Valida tamanho
|
|
if (file.size > MAX_FILE_SIZE) {
|
|
return NextResponse.json(
|
|
{ error: "Arquivo muito grande. Máximo 5MB." },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const convex = createConvexClient()
|
|
|
|
// Gera URL de upload
|
|
const uploadUrl = await convex.action(api.files.generateUploadUrl, {})
|
|
|
|
// Faz upload do arquivo
|
|
const uploadResponse = await fetch(uploadUrl, {
|
|
method: "POST",
|
|
headers: { "Content-Type": file.type },
|
|
body: file,
|
|
})
|
|
|
|
if (!uploadResponse.ok) {
|
|
console.error("[profile/avatar] Erro no upload:", await uploadResponse.text())
|
|
return NextResponse.json({ error: "Erro ao fazer upload" }, { status: 500 })
|
|
}
|
|
|
|
const { storageId } = (await uploadResponse.json()) as { storageId: Id<"_storage"> }
|
|
|
|
// Obtém URL pública do arquivo
|
|
const avatarUrl = await convex.action(api.files.getUrl, { storageId })
|
|
|
|
if (!avatarUrl) {
|
|
return NextResponse.json({ error: "Erro ao obter URL do avatar" }, { status: 500 })
|
|
}
|
|
|
|
// Atualiza o usuário no banco
|
|
await prisma.authUser.update({
|
|
where: { id: session.user.id },
|
|
data: { image: avatarUrl },
|
|
})
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
avatarUrl,
|
|
})
|
|
} catch (error) {
|
|
console.error("[profile/avatar] Erro:", error)
|
|
return NextResponse.json({ error: "Erro interno do servidor" }, { status: 500 })
|
|
}
|
|
}
|