63 lines
1.9 KiB
TypeScript
63 lines
1.9 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
|
|
export async function POST(req: Request) {
|
|
try {
|
|
console.log('Adding inventory item...');
|
|
|
|
const { receiptId, userId, rewardId } = await req.json();
|
|
console.log('Add inventory request:', { receiptId, userId, rewardId });
|
|
|
|
if (!receiptId || !userId || !rewardId) {
|
|
console.error('Missing required parameters:', { receiptId, userId, rewardId });
|
|
return NextResponse.json(
|
|
{ error: 'Missing required parameters' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Call the external inventory API
|
|
const url = `https://vps.playpoolstudios.com/boxy/api/add_inventory_item.php?id=${receiptId}&user_id=${userId}&reward_id=${rewardId}`;
|
|
const response = await fetch(
|
|
url,
|
|
{
|
|
method: 'GET',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
}
|
|
);
|
|
console.log(`${url}:${response}`);
|
|
|
|
if (!response.ok) {
|
|
console.error('Failed to add inventory item:', response.status, response.statusText);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to add inventory item' },
|
|
{ status: response.status }
|
|
);
|
|
}
|
|
|
|
const result = await response.json();
|
|
console.log('Inventory API response:', result);
|
|
|
|
// Check if the item was successfully added
|
|
if (result.success === false) {
|
|
console.error('Inventory item already exists or failed to add:', result);
|
|
return NextResponse.json(
|
|
{ error: 'Something went wrong - item may already exist in inventory' },
|
|
{ status: 409 }
|
|
);
|
|
}
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: 'Item added to inventory successfully',
|
|
data: result,
|
|
});
|
|
} catch (error) {
|
|
console.error('Error adding inventory item:', error);
|
|
return NextResponse.json(
|
|
{ error: error instanceof Error ? error.message : 'Error adding inventory item' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|