async function completePurchase(eventId, seatId, userId) {
try {
// 1. Get event details
const event = await fetch(
`http://localhost:50001/events/${eventId}/seatmap`
).then(r => r.json());
const seat = event.seats.find(s => s.id === seatId);
console.log('Event:', event.name);
console.log('Seat price:', seat.price);
// 2. Reserve seat
const reservation = await fetch(
'http://localhost:50002/reservations',
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
seatId: seatId,
customerId: userId
})
}
).then(r => r.json());
console.log('Reserved:', reservation.reservationId);
// 3. Wait for Kafka event processing
console.log('Waiting for event processing...');
await new Promise(resolve => setTimeout(resolve, 3000));
// 4. Add to cart
const order = await fetch('http://localhost:5003/cart/add', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
reservationId: reservation.reservationId,
seatId: seatId,
price: seat.price,
userId: userId
})
}).then(r => r.json());
console.log('Cart created:', order.id);
console.log('State:', order.state); // "draft"
// 5. Checkout
const finalOrder = await fetch(
'http://localhost:5003/orders/checkout',
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
orderId: order.id,
userId: userId
})
}
).then(r => r.json());
console.log('✅ Purchase complete!');
console.log('Order ID:', finalOrder.id);
console.log('State:', finalOrder.state); // "pending"
console.log('Total:', finalOrder.totalAmount);
return finalOrder;
} catch (error) {
console.error('❌ Purchase failed:', error.message);
throw error;
}
}
// Usage
completePurchase(
'550e8400-e29b-41d4-a716-446655440000', // eventId
'550e8400-e29b-41d4-a716-446655440002', // seatId
'user-123' // userId
);