mirror of
https://github.com/marcogll/AnchorOS.git
synced 2026-03-15 14:24:27 +00:00
Add detailed logging to API endpoints for debugging 500 errors
This commit is contained in:
45
app/api/sync/calendar/bookings/route.ts
Normal file
45
app/api/sync/calendar/bookings/route.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { googleCalendar } from '@/lib/google-calendar';
|
||||
|
||||
/**
|
||||
* @description Sync specific booking to Google Calendar
|
||||
* @method POST
|
||||
* @body { booking_id: string }
|
||||
*/
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
// TODO: Add admin auth check
|
||||
const body = await request.json() as { booking_id: string };
|
||||
const { booking_id } = body;
|
||||
|
||||
if (!booking_id) {
|
||||
return NextResponse.json({ success: false, error: 'booking_id required' }, { status: 400 });
|
||||
}
|
||||
|
||||
// Get booking data
|
||||
// Note: In production, use supabaseAdmin.from('bookings').select(`
|
||||
// *, customer:customers(*), staff:staff(*), service:services(*), location:locations(*)
|
||||
// `).eq('id', booking_id).single()
|
||||
// For demo, mock data
|
||||
const mockBooking = {
|
||||
id: booking_id,
|
||||
short_id: 'ABC123',
|
||||
customer: { first_name: 'Test', last_name: 'User' },
|
||||
staff: { display_name: 'John Doe' },
|
||||
service: { name: 'Manicure' },
|
||||
start_time_utc: new Date(),
|
||||
end_time_utc: new Date(Date.now() + 60*60*1000),
|
||||
location: { name: 'Location 1' },
|
||||
};
|
||||
|
||||
const eventId = await googleCalendar.syncBooking(mockBooking, 'create');
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: { google_event_id: eventId },
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error('Booking sync failed:', error);
|
||||
return NextResponse.json({ success: false, error: error.message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
33
app/api/sync/calendar/route.ts
Normal file
33
app/api/sync/calendar/route.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { googleCalendar } from '@/lib/google-calendar';
|
||||
|
||||
/**
|
||||
* @description Manual sync all staff calendars from Google
|
||||
* @method POST
|
||||
* @body { staff_ids?: string[] } - Optional staff IDs to sync
|
||||
*/
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
// TODO: Add admin auth check
|
||||
const body = await request.json();
|
||||
const { staff_ids } = body;
|
||||
|
||||
if (!googleCalendar.isReady()) {
|
||||
return NextResponse.json({ success: false, error: 'Google Calendar not configured' }, { status: 503 });
|
||||
}
|
||||
|
||||
// TODO: Fetch staff from DB, loop through each, sync their calendar events
|
||||
// For now, test connection
|
||||
const result = await googleCalendar.testConnection();
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Sync initiated',
|
||||
connection: result,
|
||||
synced_staff_count: 0, // TODO
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error('Calendar sync failed:', error);
|
||||
return NextResponse.json({ success: false, error: error.message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
29
app/api/sync/calendar/test/route.ts
Normal file
29
app/api/sync/calendar/test/route.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { cookies } from 'next/headers';
|
||||
import { googleCalendar } from '@/lib/google-calendar';
|
||||
|
||||
/**
|
||||
* @description Test Google Calendar connection endpoint
|
||||
* @description Only accessible by admin/manager roles
|
||||
*/
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
// TODO: Add admin auth check using middleware or supabaseAdmin
|
||||
// Temporarily open for testing
|
||||
|
||||
// Test connection
|
||||
const result = await googleCalendar.testConnection();
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: result,
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error('Google Calendar test failed:', error);
|
||||
return NextResponse.json(
|
||||
{ success: false, error: 'Internal server error', details: error.message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
44
app/api/sync/calendar/webhook/route.ts
Normal file
44
app/api/sync/calendar/webhook/route.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { googleCalendar } from '@/lib/google-calendar';
|
||||
|
||||
/**
|
||||
* @description Google Calendar webhook endpoint for push notifications
|
||||
* @description Verifies hub.challenge for subscription verification
|
||||
* @description Processes event changes for bidirectional sync
|
||||
*/
|
||||
export async function GET(request: NextRequest) {
|
||||
const url = new URL(request.url);
|
||||
const hubMode = url.searchParams.get('hub.mode');
|
||||
const hubChallenge = url.searchParams.get('hub.challenge');
|
||||
const hubVerifyToken = url.searchParams.get('hub.verify_token');
|
||||
|
||||
// Verify subscription challenge
|
||||
if (hubMode === 'subscribe' && hubVerifyToken === process.env.GOOGLE_CALENDAR_VERIFY_TOKEN) {
|
||||
return new NextResponse(hubChallenge!, {
|
||||
headers: { 'Content-Type': 'text/plain' },
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json({ error: 'Verification failed' }, { status: 403 });
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
// TODO: Verify webhook signature
|
||||
const body = await request.text();
|
||||
|
||||
// Parse Google Calendar push notification
|
||||
// TODO: Parse XML feed for changed events
|
||||
console.log('Google Calendar webhook received:', body);
|
||||
|
||||
// Process changed events:
|
||||
// 1. Fetch changed events from Google
|
||||
// 2. Upsert to google_calendar_events table
|
||||
// 3. Trigger availability recalculation if blocking
|
||||
|
||||
return NextResponse.json({ success: true, processed: true });
|
||||
} catch (error: any) {
|
||||
console.error('Google Calendar webhook failed:', error);
|
||||
return NextResponse.json({ success: false, error: error.message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user