Compare commits
No commits in common. "70b77fbe9f9279eae15a1166feb55c16d8e5d038" and "287af29f6c4317b89b227f1fc00fe699b2b8dbe5" have entirely different histories.
70b77fbe9f
...
287af29f6c
|
|
@ -4,36 +4,25 @@ import { createSessionManager } from '~/server/utils/session';
|
|||
import type { EventFilters } from '~/utils/types';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
console.log('[api/events.get] =========================');
|
||||
console.log('[api/events.get] GET /api/events - List all events');
|
||||
console.log('[api/events.get] Request from:', getClientIP(event));
|
||||
|
||||
try {
|
||||
const query = getQuery(event) as EventFilters & {
|
||||
limit?: string;
|
||||
offset?: string;
|
||||
calendar_format?: string;
|
||||
force?: string;
|
||||
calendar_format?: string
|
||||
};
|
||||
|
||||
console.log('[api/events.get] Query parameters:', query);
|
||||
|
||||
// Get user session using the working session manager
|
||||
// Get user session using the proper SessionManager
|
||||
const sessionManager = createSessionManager();
|
||||
const cookieHeader = getHeader(event, 'cookie');
|
||||
const session = sessionManager.getSession(cookieHeader);
|
||||
|
||||
if (!session) {
|
||||
console.log('[api/events.get] ❌ No valid session found');
|
||||
if (!session || !session.user) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
statusMessage: 'Authentication required'
|
||||
});
|
||||
}
|
||||
|
||||
console.log('[api/events.get] ✅ Valid session found for user:', session.user.email);
|
||||
console.log('[api/events.get] User tier:', session.user.tier);
|
||||
|
||||
const eventsClient = createNocoDBEventsClient();
|
||||
|
||||
// Build filters with user role
|
||||
|
|
@ -54,13 +43,9 @@ export default defineEventHandler(async (event) => {
|
|||
filters.end_date = endDate.toISOString();
|
||||
}
|
||||
|
||||
console.log('[api/events.get] Fetching events with filters:', filters);
|
||||
|
||||
// Get events from database
|
||||
const response = await eventsClient.findUserEvents(session.user.id, filters);
|
||||
|
||||
console.log('[api/events.get] ✅ Successfully fetched', response.list.length, 'events');
|
||||
|
||||
// Transform for FullCalendar if requested
|
||||
if (query.calendar_format === 'true') {
|
||||
const calendarEvents = response.list.map(transformEventForCalendar);
|
||||
|
|
@ -79,10 +64,10 @@ export default defineEventHandler(async (event) => {
|
|||
};
|
||||
|
||||
} catch (error: any) {
|
||||
console.error('[api/events.get] ❌ Error fetching events:', error);
|
||||
console.error('Error fetching events:', error);
|
||||
|
||||
// Re-throw createError instances
|
||||
if (error.statusCode) {
|
||||
// Re-throw authentication errors as-is
|
||||
if (error.statusCode === 401) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,40 +4,23 @@ import { createSessionManager } from '~/server/utils/session';
|
|||
import type { EventCreateRequest } from '~/utils/types';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
console.log('[api/events.post] =========================');
|
||||
console.log('[api/events.post] POST /api/events - Create event');
|
||||
console.log('[api/events.post] Request from:', getClientIP(event));
|
||||
|
||||
try {
|
||||
const body = await readBody(event) as EventCreateRequest;
|
||||
|
||||
console.log('[api/events.post] Event data received:', {
|
||||
title: body.title,
|
||||
event_type: body.event_type,
|
||||
start_datetime: body.start_datetime,
|
||||
end_datetime: body.end_datetime,
|
||||
visibility: body.visibility
|
||||
});
|
||||
|
||||
// Get user session using the working session manager
|
||||
// Get user session using the proper SessionManager
|
||||
const sessionManager = createSessionManager();
|
||||
const cookieHeader = getHeader(event, 'cookie');
|
||||
const session = sessionManager.getSession(cookieHeader);
|
||||
|
||||
if (!session || !session.user) {
|
||||
console.log('[api/events.post] ❌ No valid session found');
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
statusMessage: 'Authentication required'
|
||||
});
|
||||
}
|
||||
|
||||
console.log('[api/events.post] ✅ Valid session found for user:', session.user.email);
|
||||
console.log('[api/events.post] User tier:', session.user.tier);
|
||||
|
||||
// Check if user has permission to create events (board or admin only)
|
||||
if (session.user.tier !== 'board' && session.user.tier !== 'admin') {
|
||||
console.log('[api/events.post] ❌ Insufficient permissions. User tier:', session.user.tier);
|
||||
throw createError({
|
||||
statusCode: 403,
|
||||
statusMessage: 'Only board members and administrators can create events'
|
||||
|
|
@ -46,7 +29,6 @@ export default defineEventHandler(async (event) => {
|
|||
|
||||
// Validate required fields
|
||||
if (!body.title || !body.start_datetime || !body.end_datetime) {
|
||||
console.log('[api/events.post] ❌ Missing required fields');
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Title, start date, and end date are required'
|
||||
|
|
@ -58,7 +40,6 @@ export default defineEventHandler(async (event) => {
|
|||
const endDate = new Date(body.end_datetime);
|
||||
|
||||
if (startDate >= endDate) {
|
||||
console.log('[api/events.post] ❌ Invalid date range');
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'End date must be after start date'
|
||||
|
|
@ -68,7 +49,6 @@ export default defineEventHandler(async (event) => {
|
|||
// Validate event type
|
||||
const validEventTypes = ['meeting', 'social', 'fundraiser', 'workshop', 'board-only'];
|
||||
if (!validEventTypes.includes(body.event_type)) {
|
||||
console.log('[api/events.post] ❌ Invalid event type:', body.event_type);
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Invalid event type'
|
||||
|
|
@ -78,7 +58,6 @@ export default defineEventHandler(async (event) => {
|
|||
// Validate visibility
|
||||
const validVisibilities = ['public', 'board-only', 'admin-only'];
|
||||
if (!validVisibilities.includes(body.visibility)) {
|
||||
console.log('[api/events.post] ❌ Invalid visibility:', body.visibility);
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Invalid visibility setting'
|
||||
|
|
@ -87,15 +66,12 @@ export default defineEventHandler(async (event) => {
|
|||
|
||||
// Admin-only visibility can only be set by admins
|
||||
if (body.visibility === 'admin-only' && session.user.tier !== 'admin') {
|
||||
console.log('[api/events.post] ❌ Admin-only event creation attempted by non-admin');
|
||||
throw createError({
|
||||
statusCode: 403,
|
||||
statusMessage: 'Only administrators can create admin-only events'
|
||||
});
|
||||
}
|
||||
|
||||
console.log('[api/events.post] ✅ Validation passed, creating event...');
|
||||
|
||||
const eventsClient = createNocoDBEventsClient();
|
||||
|
||||
// Prepare event data
|
||||
|
|
@ -114,18 +90,14 @@ export default defineEventHandler(async (event) => {
|
|||
cost_non_members: body.cost_non_members || '',
|
||||
member_pricing_enabled: body.member_pricing_enabled || 'true',
|
||||
visibility: body.visibility as 'public' | 'board-only' | 'admin-only',
|
||||
status: (body.status || 'active') as 'active' | 'cancelled' | 'completed' | 'draft',
|
||||
status: (body.status as 'active' | 'cancelled' | 'completed' | 'draft') || 'active',
|
||||
creator: session.user.id,
|
||||
current_attendees: '0'
|
||||
current_attendees: 0
|
||||
};
|
||||
|
||||
console.log('[api/events.post] Event data prepared:', Object.keys(eventData));
|
||||
|
||||
// Create the event
|
||||
const newEvent = await eventsClient.create(eventData);
|
||||
|
||||
console.log('[api/events.post] ✅ Event created successfully with ID:', newEvent.id);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: newEvent,
|
||||
|
|
@ -133,7 +105,7 @@ export default defineEventHandler(async (event) => {
|
|||
};
|
||||
|
||||
} catch (error: any) {
|
||||
console.error('[api/events.post] ❌ Error creating event:', error);
|
||||
console.error('Error creating event:', error);
|
||||
|
||||
// Re-throw createError instances
|
||||
if (error.statusCode) {
|
||||
|
|
|
|||
|
|
@ -1,387 +1,325 @@
|
|||
// server/utils/nocodb-events.ts
|
||||
import type { Event, EventRSVP, EventsResponse, EventFilters } from '~/utils/types';
|
||||
import { createSessionManager } from '~/server/utils/session';
|
||||
import { getEffectiveNocoDBConfig } from './admin-config';
|
||||
|
||||
// Import shared NocoDB utilities from the working members system
|
||||
import { getNocoDbConfiguration, setGlobalNocoDBConfig, handleNocoDbError } from '~/server/utils/nocodb';
|
||||
|
||||
// Define interfaces for API responses
|
||||
interface EventsListResponse {
|
||||
list: Event[];
|
||||
PageInfo: {
|
||||
pageSize: number;
|
||||
totalRows: number;
|
||||
isFirstPage: boolean;
|
||||
isLastPage: boolean;
|
||||
page: number;
|
||||
};
|
||||
}
|
||||
|
||||
// Table ID enumeration for Events (similar to Members table pattern)
|
||||
export enum EventTable {
|
||||
Events = "events-table-id", // Will be configured via admin panel
|
||||
EventRSVPs = "rsvps-table-id" // Separate table for RSVPs
|
||||
}
|
||||
|
||||
// Dynamic table ID getter - will use configured table ID from admin panel
|
||||
export const getEventTableId = (tableName: 'Events' | 'EventRSVPs'): string => {
|
||||
try {
|
||||
// Try to get effective configuration from admin config system first
|
||||
const effectiveConfig = getEffectiveNocoDBConfig();
|
||||
if (effectiveConfig?.tables) {
|
||||
const tableKey = tableName === 'Events' ? 'events' : 'event_rsvps';
|
||||
const tableId = effectiveConfig.tables[tableKey] || effectiveConfig.tables[tableName];
|
||||
if (tableId) {
|
||||
console.log(`[nocodb-events] Using admin config table ID for ${tableName}:`, tableId);
|
||||
return tableId;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(`[nocodb-events] Admin config not available, trying fallback for ${tableName}`);
|
||||
}
|
||||
|
||||
// Try to get table ID from global configuration
|
||||
const globalConfig = (global as any).globalNocoDBConfig;
|
||||
if (globalConfig?.tables) {
|
||||
const tableKey = tableName === 'Events' ? 'events' : 'event_rsvps';
|
||||
const tableId = globalConfig.tables[tableKey] || globalConfig.tables[tableName];
|
||||
if (tableId) {
|
||||
console.log(`[nocodb-events] Using global table ID for ${tableName}:`, tableId);
|
||||
return tableId;
|
||||
}
|
||||
}
|
||||
|
||||
// Try runtime config as fallback
|
||||
const config = useRuntimeConfig();
|
||||
if (tableName === 'Events' && config.nocodb?.eventsTableId) {
|
||||
console.log(`[nocodb-events] Using runtime config table ID for ${tableName}:`, config.nocodb.eventsTableId);
|
||||
return config.nocodb.eventsTableId;
|
||||
}
|
||||
|
||||
// Final fallback to default
|
||||
const defaultTableId = tableName === 'Events' ? 'mt1mx3vkcw0vbmh' : 'rsvps-table-id';
|
||||
console.log(`[nocodb-events] Using fallback table ID for ${tableName}:`, defaultTableId);
|
||||
return defaultTableId;
|
||||
};
|
||||
|
||||
export const createEventTableUrl = (table: EventTable | string) => {
|
||||
let tableId: string;
|
||||
|
||||
if (table === EventTable.Events || table === 'Events') {
|
||||
tableId = getEventTableId('Events');
|
||||
} else if (table === EventTable.EventRSVPs || table === 'EventRSVPs') {
|
||||
tableId = getEventTableId('EventRSVPs');
|
||||
} else {
|
||||
tableId = table.toString();
|
||||
}
|
||||
|
||||
const url = `${getNocoDbConfiguration().url}/api/v2/tables/${tableId}/records`;
|
||||
console.log('[nocodb-events] Event table URL:', url);
|
||||
return url;
|
||||
};
|
||||
|
||||
// Event field normalization (similar to member normalization)
|
||||
export const normalizeEventFieldsFromNocoDB = (data: any): Event => {
|
||||
console.log('[normalizeEventFieldsFromNocoDB] Input event keys:', Object.keys(data));
|
||||
|
||||
const normalized: any = { ...data };
|
||||
|
||||
// Field mapping for event data (adjust based on your actual NocoDB schema)
|
||||
const eventFieldMap: Record<string, string> = {
|
||||
'Title': 'title',
|
||||
'Description': 'description',
|
||||
'Event Type': 'event_type',
|
||||
'Start Date': 'start_datetime',
|
||||
'End Date': 'end_datetime',
|
||||
'Location': 'location',
|
||||
'Visibility': 'visibility',
|
||||
'Status': 'status',
|
||||
'Creator': 'creator',
|
||||
'Max Attendees': 'max_attendees',
|
||||
'Is Paid': 'is_paid',
|
||||
'Cost Members': 'cost_members',
|
||||
'Cost Non Members': 'cost_non_members',
|
||||
// Handle snake_case fields (in case they come in already normalized)
|
||||
'title': 'title',
|
||||
'description': 'description',
|
||||
'event_type': 'event_type',
|
||||
'start_datetime': 'start_datetime',
|
||||
'end_datetime': 'end_datetime',
|
||||
'location': 'location',
|
||||
'visibility': 'visibility',
|
||||
'status': 'status',
|
||||
'creator': 'creator',
|
||||
'max_attendees': 'max_attendees',
|
||||
'is_paid': 'is_paid',
|
||||
'cost_members': 'cost_members',
|
||||
'cost_non_members': 'cost_non_members'
|
||||
};
|
||||
|
||||
// Apply field mapping
|
||||
for (const [sourceKey, targetKey] of Object.entries(eventFieldMap)) {
|
||||
if (sourceKey in data && data[sourceKey] !== undefined && data[sourceKey] !== null) {
|
||||
normalized[targetKey] = data[sourceKey];
|
||||
console.log(`[normalizeEventFieldsFromNocoDB] Mapped "${sourceKey}" -> "${targetKey}":`, data[sourceKey]);
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure required fields exist with fallbacks
|
||||
normalized.title = normalized.title || normalized['Title'] || '';
|
||||
normalized.status = normalized.status || normalized['Status'] || 'active';
|
||||
normalized.visibility = normalized.visibility || normalized['Visibility'] || 'public';
|
||||
|
||||
console.log('[normalizeEventFieldsFromNocoDB] Final normalized event keys:', Object.keys(normalized));
|
||||
return normalized as Event;
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a client for interacting with the Events NocoDB table
|
||||
* Following the same pattern as the working members client
|
||||
* Provides CRUD operations and specialized queries for events and RSVPs
|
||||
*/
|
||||
export function createNocoDBEventsClient() {
|
||||
// Validate API token before using it (from incoming version)
|
||||
const config = getNocoDbConfiguration();
|
||||
const token = config.token;
|
||||
// Get effective configuration from admin config system
|
||||
const effectiveConfig = getEffectiveNocoDBConfig();
|
||||
|
||||
const baseUrl = effectiveConfig.url;
|
||||
const token = effectiveConfig.token;
|
||||
const eventsBaseId = effectiveConfig.baseId;
|
||||
const eventsTableId = effectiveConfig.tables.events || 'events';
|
||||
const rsvpTableId = effectiveConfig.tables.rsvps || 'event_rsvps';
|
||||
|
||||
if (!baseUrl || !token || !eventsBaseId) {
|
||||
throw new Error('Events NocoDB configuration is incomplete. Please check environment variables.');
|
||||
}
|
||||
|
||||
// Validate API token before using it
|
||||
if (token) {
|
||||
const cleanToken = token.trim();
|
||||
|
||||
// Check for non-ASCII characters that would cause ByteString errors
|
||||
if (!/^[\x00-\xFF]*$/.test(cleanToken)) {
|
||||
console.error('[nocodb-events] ❌ CRITICAL ERROR: API token contains invalid Unicode characters!');
|
||||
console.error('[nocodb-events] This will cause ByteString conversion errors in HTTP headers.');
|
||||
console.error('[nocodb-events] Please update the API token in the admin configuration.');
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Events system: NocoDB API token contains invalid characters. Please reconfigure the database connection.'
|
||||
statusMessage: 'Events system: NocoDB API token contains invalid characters. Please reconfigure the database connection in the admin panel with a valid API token.'
|
||||
});
|
||||
}
|
||||
|
||||
// Additional validation for common token issues
|
||||
if (cleanToken.includes('•') || cleanToken.includes('…') || cleanToken.includes('"') || cleanToken.includes('"')) {
|
||||
console.error('[nocodb-events] ❌ CRITICAL ERROR: API token contains formatting characters!');
|
||||
console.error('[nocodb-events] Found characters like bullets (•), quotes, etc. that break HTTP headers.');
|
||||
console.error('[nocodb-events] Please copy the raw API token from NocoDB without any formatting.');
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Events system: NocoDB API token contains formatting characters. Please reconfigure with the raw token from NocoDB.'
|
||||
statusMessage: 'Events system: NocoDB API token contains formatting characters (bullets, quotes, etc.). Please reconfigure with the raw token from NocoDB.'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const headers = {
|
||||
'xc-token': token,
|
||||
'Content-Type': 'application/json'
|
||||
};
|
||||
|
||||
const eventsClient = {
|
||||
/**
|
||||
* Find all events with optional filtering
|
||||
*/
|
||||
async findAll(filters?: EventFilters & { limit?: number; offset?: number }): Promise<EventsListResponse> {
|
||||
console.log('[nocodb-events] 🔍 DEBUG: Filters received:', filters);
|
||||
const startTime = Date.now();
|
||||
async findAll(filters?: EventFilters & { limit?: number; offset?: number }) {
|
||||
const queryParams = new URLSearchParams();
|
||||
|
||||
try {
|
||||
// Build query parameters like the members system
|
||||
const params: Record<string, any> = {
|
||||
limit: filters?.limit || 1000,
|
||||
};
|
||||
|
||||
if (filters?.offset) {
|
||||
params.offset = filters.offset;
|
||||
}
|
||||
|
||||
// Simple status filtering (like members system)
|
||||
if (filters?.status) {
|
||||
console.log('[nocodb-events] 🔍 Adding status filter:', filters.status);
|
||||
params.where = `(status,eq,${filters.status})`;
|
||||
} else {
|
||||
console.log('[nocodb-events] 🔍 No status filter - fetching all records');
|
||||
}
|
||||
|
||||
// TEMPORARILY DISABLE COMPLEX FILTERING TO ISOLATE ISSUE
|
||||
console.log('[nocodb-events] ⚠️ Temporarily skipping date/role/search filtering to isolate issue');
|
||||
|
||||
// TEMPORARILY DISABLE SORTING TO ISOLATE ISSUE
|
||||
console.log('[nocodb-events] ⚠️ Also temporarily skipping sorting to isolate issue');
|
||||
|
||||
const url = createEventTableUrl(EventTable.Events);
|
||||
|
||||
const result = await $fetch<EventsListResponse>(url, {
|
||||
headers: {
|
||||
"xc-token": getNocoDbConfiguration().token,
|
||||
},
|
||||
params
|
||||
});
|
||||
|
||||
console.log('[nocodb-events] ✅ Successfully fetched events, count:', result.list?.length || 0);
|
||||
console.log('[nocodb-events] Request duration:', Date.now() - startTime, 'ms');
|
||||
|
||||
// Apply field normalization like members system
|
||||
if (result.list) {
|
||||
result.list = result.list.map(normalizeEventFieldsFromNocoDB);
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (error: any) {
|
||||
console.error('[nocodb-events] ❌ Error fetching events:', error);
|
||||
handleNocoDbError(error, 'getEvents', 'Events');
|
||||
throw error;
|
||||
if (filters?.limit) queryParams.set('limit', filters.limit.toString());
|
||||
if (filters?.offset) queryParams.set('offset', filters.offset.toString());
|
||||
|
||||
// TEMPORARILY: Try different query approach to isolate the issue
|
||||
// Remove complex filtering until we can identify what works
|
||||
|
||||
console.log('[nocodb-events] 🔍 DEBUG: Filters received:', JSON.stringify(filters, null, 2));
|
||||
|
||||
// Try only status filter first (simplest case)
|
||||
if (filters?.status) {
|
||||
console.log('[nocodb-events] 🔍 Adding status filter:', filters.status);
|
||||
queryParams.set('where', `(status,eq,${filters.status})`);
|
||||
} else {
|
||||
// Try no status filter at all to see if that works
|
||||
console.log('[nocodb-events] 🔍 No status filter - fetching all records');
|
||||
}
|
||||
|
||||
// Skip date filtering completely for now
|
||||
console.log('[nocodb-events] ⚠️ Temporarily skipping date/role/search filtering to isolate issue');
|
||||
|
||||
// ALSO temporarily skip sorting to see if that's the issue
|
||||
console.log('[nocodb-events] ⚠️ Also temporarily skipping sorting to isolate issue');
|
||||
|
||||
const url = `${baseUrl}/api/v2/tables/${eventsTableId}/records?${queryParams.toString()}`;
|
||||
|
||||
const response = await $fetch(url, {
|
||||
method: 'GET',
|
||||
headers
|
||||
});
|
||||
|
||||
return response;
|
||||
},
|
||||
|
||||
/**
|
||||
* Find a single event by ID
|
||||
*/
|
||||
async findOne(id: string) {
|
||||
console.log('[nocodb-events] Fetching event ID:', id);
|
||||
const url = `${baseUrl}/api/v2/tables/${eventsTableId}/records/${id}`;
|
||||
|
||||
try {
|
||||
const result = await $fetch<Event>(`${createEventTableUrl(EventTable.Events)}/${id}`, {
|
||||
headers: {
|
||||
"xc-token": getNocoDbConfiguration().token,
|
||||
},
|
||||
});
|
||||
|
||||
console.log('[nocodb-events] Successfully retrieved event:', result.id || (result as any).Id);
|
||||
return normalizeEventFieldsFromNocoDB(result);
|
||||
} catch (error: any) {
|
||||
console.error('[nocodb-events] Error fetching event:', error);
|
||||
handleNocoDbError(error, 'getEventById', 'Event');
|
||||
throw error;
|
||||
}
|
||||
return await $fetch<Event>(url, {
|
||||
method: 'GET',
|
||||
headers
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Create a new event
|
||||
*/
|
||||
async create(eventData: Partial<Event>) {
|
||||
console.log('[nocodb-events] Creating event with fields:', Object.keys(eventData));
|
||||
const url = `${baseUrl}/api/v2/tables/${eventsTableId}/records`;
|
||||
|
||||
try {
|
||||
// Clean data like members system
|
||||
const cleanData: Record<string, any> = {};
|
||||
|
||||
// Only include allowed event fields
|
||||
const allowedFields = [
|
||||
"title", "description", "event_type", "start_datetime", "end_datetime",
|
||||
"location", "is_recurring", "recurrence_pattern", "max_attendees",
|
||||
"is_paid", "cost_members", "cost_non_members", "member_pricing_enabled",
|
||||
"visibility", "status", "creator", "current_attendees"
|
||||
];
|
||||
|
||||
for (const field of allowedFields) {
|
||||
if (field in eventData && eventData[field as keyof Event] !== undefined) {
|
||||
cleanData[field] = eventData[field as keyof Event];
|
||||
}
|
||||
}
|
||||
|
||||
// Set defaults
|
||||
cleanData.status = cleanData.status || 'active';
|
||||
cleanData.visibility = cleanData.visibility || 'public';
|
||||
cleanData.current_attendees = cleanData.current_attendees || '0';
|
||||
|
||||
console.log('[nocodb-events] Clean event data fields:', Object.keys(cleanData));
|
||||
|
||||
const result = await $fetch<Event>(createEventTableUrl(EventTable.Events), {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"xc-token": getNocoDbConfiguration().token,
|
||||
},
|
||||
body: cleanData,
|
||||
});
|
||||
|
||||
console.log('[nocodb-events] Created event with ID:', result.id || (result as any).Id);
|
||||
return normalizeEventFieldsFromNocoDB(result);
|
||||
} catch (error: any) {
|
||||
console.error('[nocodb-events] Create event failed:', error);
|
||||
handleNocoDbError(error, 'createEvent', 'Event');
|
||||
throw error;
|
||||
}
|
||||
// Set default values
|
||||
const data = {
|
||||
...eventData,
|
||||
status: eventData.status || 'active',
|
||||
created_at: new Date().toISOString(),
|
||||
updated_at: new Date().toISOString()
|
||||
};
|
||||
|
||||
return await $fetch<Event>(url, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: data
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Update an existing event
|
||||
*/
|
||||
async update(id: string, eventData: Partial<Event>) {
|
||||
console.log('[nocodb-events] Updating event:', id);
|
||||
const url = `${baseUrl}/api/v2/tables/${eventsTableId}/records`;
|
||||
|
||||
try {
|
||||
// Clean data like members system
|
||||
const cleanData: Record<string, any> = {};
|
||||
|
||||
const allowedFields = [
|
||||
"title", "description", "event_type", "start_datetime", "end_datetime",
|
||||
"location", "is_recurring", "recurrence_pattern", "max_attendees",
|
||||
"is_paid", "cost_members", "cost_non_members", "member_pricing_enabled",
|
||||
"visibility", "status", "creator", "current_attendees"
|
||||
];
|
||||
|
||||
for (const field of allowedFields) {
|
||||
if (field in eventData && eventData[field as keyof Event] !== undefined) {
|
||||
cleanData[field] = eventData[field as keyof Event];
|
||||
}
|
||||
}
|
||||
|
||||
// PATCH requires ID in the body (like members system)
|
||||
cleanData.Id = parseInt(id);
|
||||
|
||||
const result = await $fetch<Event>(createEventTableUrl(EventTable.Events), {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
"xc-token": getNocoDbConfiguration().token,
|
||||
},
|
||||
body: cleanData
|
||||
});
|
||||
|
||||
console.log('[nocodb-events] Update successful for ID:', id);
|
||||
return normalizeEventFieldsFromNocoDB(result);
|
||||
} catch (error: any) {
|
||||
console.error('[nocodb-events] Update event failed:', error);
|
||||
handleNocoDbError(error, 'updateEvent', 'Event');
|
||||
throw error;
|
||||
}
|
||||
const data = {
|
||||
Id: parseInt(id),
|
||||
...eventData,
|
||||
updated_at: new Date().toISOString()
|
||||
};
|
||||
|
||||
return await $fetch<Event>(url, {
|
||||
method: 'PATCH',
|
||||
headers,
|
||||
body: data
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Delete an event
|
||||
*/
|
||||
async delete(id: string) {
|
||||
console.log('[nocodb-events] Deleting event:', id);
|
||||
const url = `${baseUrl}/api/v2/tables/${eventsTableId}/records`;
|
||||
|
||||
try {
|
||||
const result = await $fetch(createEventTableUrl(EventTable.Events), {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
"xc-token": getNocoDbConfiguration().token,
|
||||
},
|
||||
body: {
|
||||
"Id": parseInt(id)
|
||||
}
|
||||
});
|
||||
|
||||
console.log('[nocodb-events] Delete successful for ID:', id);
|
||||
return result;
|
||||
} catch (error: any) {
|
||||
console.error('[nocodb-events] Delete event failed:', error);
|
||||
handleNocoDbError(error, 'deleteEvent', 'Event');
|
||||
throw error;
|
||||
}
|
||||
return await $fetch(url, {
|
||||
method: 'DELETE',
|
||||
headers,
|
||||
body: { Id: parseInt(id) }
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Get events for a specific user (simplified version)
|
||||
* Create an RSVP record for an event
|
||||
*/
|
||||
async createRSVP(rsvpData: Partial<EventRSVP>) {
|
||||
const url = `${baseUrl}/api/v2/tables/${rsvpTableId}/records`;
|
||||
|
||||
const data = {
|
||||
...rsvpData,
|
||||
created_time: new Date().toISOString(),
|
||||
updated_time: new Date().toISOString()
|
||||
};
|
||||
|
||||
return await $fetch<EventRSVP>(url, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: data
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Find RSVPs for a specific event
|
||||
*/
|
||||
async findEventRSVPs(eventId: string) {
|
||||
const queryParams = new URLSearchParams();
|
||||
queryParams.set('where', `(event_id,eq,${eventId})`);
|
||||
queryParams.set('sort', 'created_time');
|
||||
|
||||
const url = `${baseUrl}/api/v2/tables/${rsvpTableId}/records?${queryParams.toString()}`;
|
||||
|
||||
return await $fetch(url, {
|
||||
method: 'GET',
|
||||
headers
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Find a user's RSVP for a specific event
|
||||
*/
|
||||
async findUserRSVP(eventId: string, memberId: string) {
|
||||
const queryParams = new URLSearchParams();
|
||||
queryParams.set('where', `~and((event_id,eq,${eventId}),(member_id,eq,${memberId}))`);
|
||||
queryParams.set('limit', '1');
|
||||
|
||||
const url = `${baseUrl}/api/v2/tables/${rsvpTableId}/records?${queryParams.toString()}`;
|
||||
|
||||
const response = await $fetch<{ list?: EventRSVP[]; PageInfo?: any }>(url, {
|
||||
method: 'GET',
|
||||
headers
|
||||
});
|
||||
|
||||
return response?.list?.[0] || null;
|
||||
},
|
||||
|
||||
/**
|
||||
* Update an RSVP record
|
||||
*/
|
||||
async updateRSVP(id: string, rsvpData: Partial<EventRSVP>) {
|
||||
const url = `${baseUrl}/api/v2/tables/${rsvpTableId}/records`;
|
||||
|
||||
const data = {
|
||||
Id: parseInt(id),
|
||||
...rsvpData,
|
||||
updated_time: new Date().toISOString()
|
||||
};
|
||||
|
||||
return await $fetch<EventRSVP>(url, {
|
||||
method: 'PATCH',
|
||||
headers,
|
||||
body: data
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Update event attendance count (for optimization)
|
||||
*/
|
||||
async updateAttendeeCount(eventId: string, count: number) {
|
||||
const url = `${baseUrl}/api/v2/tables/${eventsTableId}/records`;
|
||||
|
||||
return await $fetch(url, {
|
||||
method: 'PATCH',
|
||||
headers,
|
||||
body: {
|
||||
Id: parseInt(eventId),
|
||||
current_attendees: count.toString(),
|
||||
updated_at: new Date().toISOString()
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Get events for a specific user with their RSVP status
|
||||
*/
|
||||
async findUserEvents(memberId: string, filters?: EventFilters) {
|
||||
console.log('[nocodb-events] Finding events for member:', memberId);
|
||||
// First get all visible events
|
||||
const events = await this.findAll(filters) as { list?: Event[]; PageInfo?: any };
|
||||
|
||||
try {
|
||||
// For now, just get all visible events (we'll add RSVP logic later)
|
||||
const events = await this.findAll(filters);
|
||||
|
||||
// TODO: Add RSVP lookup from separate table
|
||||
// For now, return events without RSVP status
|
||||
return {
|
||||
list: events.list || [],
|
||||
PageInfo: events.PageInfo
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.error('[nocodb-events] Error finding user events:', error);
|
||||
throw error;
|
||||
if (!events.list || events.list.length === 0) {
|
||||
return { list: [], PageInfo: events.PageInfo };
|
||||
}
|
||||
|
||||
// Get user's RSVPs for these events
|
||||
// Fix: Use 'Id' (capital I) as that's the actual field name from NocoDB
|
||||
const eventIds = events.list.map((e: any) => e.Id);
|
||||
|
||||
// Skip RSVP lookup if no valid event IDs
|
||||
if (!eventIds.length || eventIds.some(id => !id)) {
|
||||
console.log('[nocodb-events] ⚠️ No valid event IDs found, skipping RSVP lookup');
|
||||
return { list: events.list, PageInfo: events.PageInfo };
|
||||
}
|
||||
|
||||
const rsvpQueryParams = new URLSearchParams();
|
||||
const eventIdConditions = eventIds.map(id => `(event_id,eq,${id})`).join(',');
|
||||
rsvpQueryParams.set('where', `~and((member_id,eq,${memberId}),~or(${eventIdConditions}))`);
|
||||
|
||||
const rsvpUrl = `${baseUrl}/api/v2/tables/${rsvpTableId}/records?${rsvpQueryParams.toString()}`;
|
||||
|
||||
const rsvps = await $fetch<{ list?: EventRSVP[]; PageInfo?: any }>(rsvpUrl, {
|
||||
method: 'GET',
|
||||
headers
|
||||
});
|
||||
|
||||
// Map RSVPs to events
|
||||
const rsvpMap = new Map();
|
||||
if (rsvps.list) {
|
||||
rsvps.list.forEach((rsvp: EventRSVP) => {
|
||||
rsvpMap.set(rsvp.event_id, rsvp);
|
||||
});
|
||||
}
|
||||
|
||||
// Add RSVP information to events
|
||||
// Fix: Use 'Id' (capital I) as that's the actual field name from NocoDB
|
||||
const eventsWithRSVP = events.list.map((event: any) => ({
|
||||
...event,
|
||||
user_rsvp: rsvpMap.get(event.Id) || null
|
||||
}));
|
||||
|
||||
return {
|
||||
list: eventsWithRSVP,
|
||||
PageInfo: events.PageInfo
|
||||
};
|
||||
},
|
||||
|
||||
/**
|
||||
* Generate payment reference for RSVP
|
||||
*/
|
||||
generatePaymentReference(memberId: string, date?: Date): string {
|
||||
const referenceDate = date || new Date();
|
||||
const dateString = referenceDate.toISOString().split('T')[0]; // YYYY-MM-DD
|
||||
return `EVT-${memberId}-${dateString}`;
|
||||
},
|
||||
|
||||
/**
|
||||
* Check if event has reached capacity
|
||||
*/
|
||||
async isEventFull(eventId: string): Promise<boolean> {
|
||||
const event = await this.findOne(eventId);
|
||||
|
||||
if (!event.max_attendees) return false; // Unlimited capacity
|
||||
|
||||
const maxAttendees = parseInt(event.max_attendees);
|
||||
const currentAttendees = parseInt(String(event.current_attendees || 0));
|
||||
|
||||
return currentAttendees >= maxAttendees;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -390,8 +328,9 @@ export function createNocoDBEventsClient() {
|
|||
|
||||
/**
|
||||
* Utility function to transform Event data for FullCalendar
|
||||
* Updated to use correct field names from NocoDB (Id instead of id)
|
||||
*/
|
||||
export function transformEventForCalendar(event: Event): any {
|
||||
export function transformEventForCalendar(event: any): any {
|
||||
const eventTypeColors = {
|
||||
'meeting': { bg: '#2196f3', border: '#1976d2' },
|
||||
'social': { bg: '#4caf50', border: '#388e3c' },
|
||||
|
|
@ -404,8 +343,8 @@ export function transformEventForCalendar(event: Event): any {
|
|||
{ bg: '#757575', border: '#424242' };
|
||||
|
||||
return {
|
||||
id: event.id,
|
||||
title: event.title,
|
||||
id: event.Id, // Fix: Use 'Id' (capital I) from NocoDB
|
||||
title: event.title || 'Untitled Event',
|
||||
start: event.start_datetime,
|
||||
end: event.end_datetime,
|
||||
backgroundColor: colors.bg,
|
||||
|
|
|
|||
Loading…
Reference in New Issue