Initial commit
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { cookies } from 'next/headers';
|
||||
import { verifyToken } from '@/lib/auth';
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const cookieStore = await cookies();
|
||||
const token = cookieStore.get('hub_token')?.value;
|
||||
if (!token) return NextResponse.json({ error: 'No autorizado' }, { status: 401 });
|
||||
|
||||
const user = await verifyToken(token) as any;
|
||||
if (!user) return NextResponse.json({ error: 'Token inválido' }, { status: 401 });
|
||||
|
||||
const body = await request.json();
|
||||
const { title, description, urgency } = body;
|
||||
|
||||
const apiKey = process.env.PLANE_API_KEY;
|
||||
const apiUrl = process.env.PLANE_API_URL;
|
||||
const workspace = process.env.PLANE_WORKSPACE_SLUG;
|
||||
const projectId = process.env.PLANE_PROJECT_ID;
|
||||
|
||||
if (!apiKey || !apiUrl || !workspace || !projectId) {
|
||||
return NextResponse.json({ error: 'Faltan variables de entorno para la conexión con Plane' }, { status: 500 });
|
||||
}
|
||||
|
||||
const companyPrefix = user.companyName ? `[${user.companyName.toUpperCase()}] ` : '';
|
||||
const planeData = {
|
||||
name: `${companyPrefix}${title}`,
|
||||
description_html: `<p><strong>Reportado por:</strong> ${user.email}</p><p>${description.replace(/\n/g, '<br/>')}</p>`,
|
||||
priority: urgency
|
||||
};
|
||||
|
||||
const response = await fetch(`${apiUrl}/workspaces/${workspace}/projects/${projectId}/issues/`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-api-key': apiKey
|
||||
},
|
||||
body: JSON.stringify(planeData)
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
console.error('Plane API Error:', errorText);
|
||||
return NextResponse.json({ error: 'Error al crear el ticket en Plane' }, { status: response.status });
|
||||
}
|
||||
|
||||
const result = await response.json();
|
||||
return NextResponse.json({ success: true, ticket: result });
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error procesando ticket:', error);
|
||||
return NextResponse.json({ error: 'Error interno del servidor' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user