91 lines
2.9 KiB
TypeScript
91 lines
2.9 KiB
TypeScript
// server/api/events/index.get.ts
|
|
import { createNocoDBEventsClient, transformEventForCalendar } from '~/server/utils/nocodb-events';
|
|
import type { EventFilters } from '~/utils/types';
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
try {
|
|
const query = getQuery(event) as EventFilters & {
|
|
limit?: string;
|
|
offset?: string;
|
|
calendar_format?: string
|
|
};
|
|
|
|
// Get user session for role-based filtering
|
|
const session = await getUserSession(event);
|
|
if (!session || !session.user) {
|
|
throw createError({
|
|
statusCode: 401,
|
|
statusMessage: 'Authentication required'
|
|
});
|
|
}
|
|
|
|
const eventsClient = createNocoDBEventsClient();
|
|
|
|
// Build filters with user role
|
|
const filters: EventFilters & { limit?: number; offset?: number } = {
|
|
...query,
|
|
user_role: session.user.tier,
|
|
limit: query.limit ? parseInt(query.limit) : 50,
|
|
offset: query.offset ? parseInt(query.offset) : 0
|
|
};
|
|
|
|
// If no date range provided, default to current month + 2 months ahead
|
|
if (!filters.start_date || !filters.end_date) {
|
|
const now = new Date();
|
|
const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);
|
|
const endDate = new Date(now.getFullYear(), now.getMonth() + 3, 0); // 3 months ahead
|
|
|
|
filters.start_date = startOfMonth.toISOString();
|
|
filters.end_date = endDate.toISOString();
|
|
}
|
|
|
|
// Get events from database
|
|
const response = await eventsClient.findUserEvents(session.user.id, filters);
|
|
|
|
// Transform for FullCalendar if requested
|
|
if (query.calendar_format === 'true') {
|
|
const calendarEvents = response.list.map(transformEventForCalendar);
|
|
return {
|
|
success: true,
|
|
data: calendarEvents,
|
|
total: response.PageInfo?.totalRows || response.list.length
|
|
};
|
|
}
|
|
|
|
return {
|
|
success: true,
|
|
data: response.list,
|
|
total: response.PageInfo?.totalRows || response.list.length,
|
|
pagination: response.PageInfo
|
|
};
|
|
|
|
} catch (error) {
|
|
console.error('Error fetching events:', error);
|
|
throw createError({
|
|
statusCode: 500,
|
|
statusMessage: 'Failed to fetch events'
|
|
});
|
|
}
|
|
});
|
|
|
|
// Helper function to get user session (you may need to adjust this based on your auth implementation)
|
|
async function getUserSession(event: any) {
|
|
// This should be replaced with your actual session retrieval logic
|
|
// For now, assuming you have a session utility similar to your auth system
|
|
try {
|
|
const sessionCookie = getCookie(event, 'session') || getHeader(event, 'authorization');
|
|
if (!sessionCookie) return null;
|
|
|
|
// Decode session - adjust based on your session implementation
|
|
// This is a placeholder that should be replaced with your actual session logic
|
|
return {
|
|
user: {
|
|
id: 'user-id', // This should come from your session
|
|
tier: 'user' // This should come from your session
|
|
}
|
|
};
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|