This commit is contained in:
@@ -135,6 +135,15 @@ export default defineEventHandler(async (event) => {
|
||||
|
||||
const newRSVP = await eventsClient.createRSVP(rsvpData);
|
||||
|
||||
// Update event attendee count
|
||||
try {
|
||||
await updateEventAttendeeCount(eventId);
|
||||
console.log('[RSVP] ✅ Updated event attendee count for event:', eventId);
|
||||
} catch (countError) {
|
||||
console.log('[RSVP] ⚠️ Failed to update attendee count:', countError);
|
||||
// Don't fail the RSVP creation if count update fails
|
||||
}
|
||||
|
||||
// Include payment information in response for paid events
|
||||
let responseData: any = newRSVP;
|
||||
|
||||
@@ -191,3 +200,37 @@ async function getRegistrationConfig() {
|
||||
accountHolder: 'MonacoUSA Association'
|
||||
};
|
||||
}
|
||||
|
||||
async function updateEventAttendeeCount(eventId: string) {
|
||||
console.log('[updateEventAttendeeCount] Updating attendee count for event:', eventId);
|
||||
|
||||
try {
|
||||
const eventsClient = createNocoDBEventsClient();
|
||||
|
||||
// Get all confirmed RSVPs for this event
|
||||
const confirmedRSVPs = await eventsClient.getEventRSVPs(eventId, 'confirmed');
|
||||
|
||||
// Calculate total attendees (confirmed RSVPs + their guests)
|
||||
let totalAttendees = 0;
|
||||
|
||||
for (const rsvp of confirmedRSVPs) {
|
||||
totalAttendees += 1; // The member themselves
|
||||
const guestCount = parseInt(rsvp.extra_guests || '0');
|
||||
totalAttendees += guestCount; // Add their guests
|
||||
}
|
||||
|
||||
console.log('[updateEventAttendeeCount] Calculated total attendees:', totalAttendees, 'from', confirmedRSVPs.length, 'RSVPs');
|
||||
|
||||
// Update the event's current_attendees field
|
||||
await eventsClient.update(eventId, {
|
||||
current_attendees: totalAttendees.toString()
|
||||
});
|
||||
|
||||
console.log('[updateEventAttendeeCount] ✅ Successfully updated event attendee count to:', totalAttendees);
|
||||
|
||||
return totalAttendees;
|
||||
} catch (error) {
|
||||
console.error('[updateEventAttendeeCount] ❌ Error updating attendee count:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -253,6 +253,23 @@ export function createNocoDBEventsClient() {
|
||||
// Apply field normalization like members system
|
||||
if (result.list) {
|
||||
result.list = result.list.map(normalizeEventFieldsFromNocoDB);
|
||||
|
||||
// Update attendee counts for all events
|
||||
result.list = await Promise.all(
|
||||
result.list.map(async (event) => {
|
||||
try {
|
||||
const eventId = event.event_id || event.id || (event as any).Id;
|
||||
const updatedCount = await this.calculateEventAttendeeCount(eventId.toString());
|
||||
return {
|
||||
...event,
|
||||
current_attendees: updatedCount.toString()
|
||||
};
|
||||
} catch (error) {
|
||||
console.log('[nocodb-events] ⚠️ Failed to calculate attendee count for event:', event.title, error);
|
||||
return event; // Return original if calculation fails
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -490,6 +507,7 @@ export function createNocoDBEventsClient() {
|
||||
payment_reference: rsvpData.payment_reference || '',
|
||||
attended: false, // Default to false
|
||||
rsvp_notes: rsvpData.rsvp_notes || '',
|
||||
extra_guests: rsvpData.extra_guests || '0', // Include guest count
|
||||
is_member_pricing: rsvpData.is_member_pricing === 'true' || rsvpData.is_member_pricing === true
|
||||
};
|
||||
|
||||
@@ -651,6 +669,74 @@ export function createNocoDBEventsClient() {
|
||||
console.error('[nocodb-events] ❌ Error updating RSVP:', error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Get all RSVPs for an event with optional status filter
|
||||
*/
|
||||
async getEventRSVPs(eventId: string, rsvpStatus?: string) {
|
||||
console.log('[nocodb-events] Getting RSVPs for event:', eventId, 'with status:', rsvpStatus);
|
||||
|
||||
try {
|
||||
try {
|
||||
// Build where clause
|
||||
let whereClause = `(event_id,eq,${eventId})`;
|
||||
if (rsvpStatus) {
|
||||
whereClause += `~and(rsvp_status,eq,${rsvpStatus})`;
|
||||
}
|
||||
|
||||
// Try to get from RSVP table first
|
||||
const rsvps = await $fetch<{list: any[]}>(createEventTableUrl(EventTable.EventRSVPs), {
|
||||
headers: {
|
||||
"xc-token": getNocoDbConfiguration().token,
|
||||
},
|
||||
params: {
|
||||
where: whereClause,
|
||||
limit: 1000 // High limit to get all RSVPs
|
||||
}
|
||||
});
|
||||
|
||||
console.log('[nocodb-events] ✅ Found', rsvps.list?.length || 0, 'RSVPs for event:', eventId);
|
||||
return rsvps.list || [];
|
||||
|
||||
} catch (rsvpTableError: any) {
|
||||
console.log('[nocodb-events] ⚠️ RSVP table not available, returning empty array');
|
||||
|
||||
// Return empty array if table not available
|
||||
return [];
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error('[nocodb-events] ❌ Error getting event RSVPs:', error);
|
||||
return []; // Return empty array instead of throwing to prevent blocking
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Calculate total attendee count for an event (confirmed RSVPs + their guests)
|
||||
*/
|
||||
async calculateEventAttendeeCount(eventId: string): Promise<number> {
|
||||
console.log('[nocodb-events] Calculating attendee count for event:', eventId);
|
||||
|
||||
try {
|
||||
// Get all confirmed RSVPs for this event
|
||||
const confirmedRSVPs = await this.getEventRSVPs(eventId, 'confirmed');
|
||||
|
||||
// Calculate total attendees (confirmed RSVPs + their guests)
|
||||
let totalAttendees = 0;
|
||||
|
||||
for (const rsvp of confirmedRSVPs) {
|
||||
totalAttendees += 1; // The member themselves
|
||||
const guestCount = parseInt(rsvp.extra_guests || '0');
|
||||
totalAttendees += guestCount; // Add their guests
|
||||
}
|
||||
|
||||
console.log('[nocodb-events] ✅ Calculated total attendees:', totalAttendees, 'from', confirmedRSVPs.length, 'confirmed RSVPs');
|
||||
|
||||
return totalAttendees;
|
||||
} catch (error) {
|
||||
console.error('[nocodb-events] ❌ Error calculating attendee count for event:', eventId, error);
|
||||
return 0; // Return 0 if calculation fails
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user