128 lines
3.7 KiB
TypeScript
128 lines
3.7 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
|
|
const API_URL = 'http://aggios-backend:8080';
|
|
|
|
export async function GET(
|
|
request: NextRequest,
|
|
context: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const { id } = await context.params;
|
|
const token = request.headers.get('authorization');
|
|
const subdomain = request.headers.get('host')?.split('.')[0] || '';
|
|
|
|
if (!token) {
|
|
return NextResponse.json(
|
|
{ error: 'Unauthorized' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
const response = await fetch(`${API_URL}/api/crm/customers/${id}`, {
|
|
cache: 'no-store',
|
|
headers: {
|
|
'Authorization': token,
|
|
'X-Tenant-Subdomain': subdomain,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const error = await response.json();
|
|
return NextResponse.json(error, { status: response.status });
|
|
}
|
|
|
|
const data = await response.json();
|
|
return NextResponse.json(data);
|
|
} catch (error) {
|
|
console.error('Error fetching customer:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch customer' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function PUT(
|
|
request: NextRequest,
|
|
context: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const { id } = await context.params;
|
|
const token = request.headers.get('authorization');
|
|
const subdomain = request.headers.get('host')?.split('.')[0] || '';
|
|
const body = await request.json();
|
|
|
|
if (!token) {
|
|
return NextResponse.json(
|
|
{ error: 'Unauthorized' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
const response = await fetch(`${API_URL}/api/crm/customers/${id}`, {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Authorization': token,
|
|
'X-Tenant-Subdomain': subdomain,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(body),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const error = await response.json();
|
|
return NextResponse.json(error, { status: response.status });
|
|
}
|
|
|
|
const data = await response.json();
|
|
return NextResponse.json(data);
|
|
} catch (error) {
|
|
console.error('Error updating customer:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to update customer' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function DELETE(
|
|
request: NextRequest,
|
|
context: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const { id } = await context.params;
|
|
const token = request.headers.get('authorization');
|
|
const subdomain = request.headers.get('host')?.split('.')[0] || '';
|
|
|
|
if (!token) {
|
|
return NextResponse.json(
|
|
{ error: 'Unauthorized' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
const response = await fetch(`${API_URL}/api/crm/customers/${id}`, {
|
|
method: 'DELETE',
|
|
headers: {
|
|
'Authorization': token,
|
|
'X-Tenant-Subdomain': subdomain,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const error = await response.json();
|
|
return NextResponse.json(error, { status: response.status });
|
|
}
|
|
|
|
return NextResponse.json({ success: true });
|
|
} catch (error) {
|
|
console.error('Error deleting customer:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to delete customer' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|