diff --git a/app/pages/events/[id].vue b/app/pages/events/[id].vue index 6d98cae..2a2d7e7 100644 --- a/app/pages/events/[id].vue +++ b/app/pages/events/[id].vue @@ -98,10 +98,20 @@ async function saveChanges() { // Update local event data event.value = { ...editForm.value } + + //update map center if location changed + if (editForm.value.location.address !== event.value.location.address) { + center.value = [editForm.value.location.longitude, editForm.value.location.latitude] + } + isEditMode.value = false console.log('✅ Event updated successfully') } catch (error) { + if(error.status === 500){ + alert('Failed to geocode the provided location. Please check the address and try again.') + return + } console.error('❌ Error updating event:', error) alert('Error updating event. Please try again.') } diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index d0bc1d6..34cdb94 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -1,3 +1,8 @@ -onlyBuiltDependencies: - - '@prisma/client' - - better-sqlite3 +allowBuilds: + "@parcel/watcher": true + "@prisma/engines": true + better-sqlite3: true + esbuild: true + prisma: true + unrs-resolver: true + vue-demi: true diff --git a/server/api/events/[id]/index.patch.ts b/server/api/events/[id]/index.patch.ts index 0bbe926..071a259 100644 --- a/server/api/events/[id]/index.patch.ts +++ b/server/api/events/[id]/index.patch.ts @@ -1,118 +1,115 @@ -import prisma from '~~/server/utils/prisma' +import prisma from "~~/server/utils/prisma"; // Geocode location using Nominatim -async function geocodeLocation( location: string) { +async function geocodeLocation(location: string) { try { const response = await fetch( `https://nominatim.openstreetmap.org/search?format=json&q=${encodeURIComponent(location)}`, { headers: { - 'User-Agent': 'Abide-Connect/1.0' - } - } - ) - + "User-Agent": "Abide-Connect/1.0", + }, + }, + ); + if (!response.ok) { - console.warn(`Nominatim API error: ${response.status}`) - return null + console.warn(`Nominatim API error: ${response.status}`); + return null; } - const results = await response.json() - + const results = await response.json(); + if (results.length === 0) { - console.warn(`No geocoding results found for: ${location}`) - return null + console.warn(`No geocoding results found for: ${location}`); + return null; } - const topResult = results[0] - - return{ + const topResult = results[0]; + + return { latitude: parseFloat(topResult.lat), longitude: parseFloat(topResult.lon), - address: location - } - + address: location, + }; } catch (error) { - console.error('❌ Geocoding error:', error) - return null + console.error("❌ Geocoding error:", error); + return null; } } - - export default defineEventHandler(async (event) => { - const id = getRouterParam(event, 'id') - const body = await readBody(event) + const id = getRouterParam(event, "id"); + const body = await readBody(event); if (!id) { - throw createError({ statusCode: 400, message: 'Missing event ID' }) + throw createError({ statusCode: 400, message: "Missing event ID" }); } - //check if location has already been fetched - const locationData = await prisma.location.findUnique({ - where: { - address: body.location || null - }, - select: { - latitude: true, - longitude: true, - address: true - } - }) || await geocodeLocation(body.location) + //check if location has already been fetched + const locationData = + (await prisma.location.findUnique({ + where: { + address: body.location.address || null, + }, + select: { + latitude: true, + longitude: true, + address: true, + }, + })) || (await geocodeLocation(body.location.address)); - console.log('📍 Location data from DB:', locationData) + if (!locationData) { + throw createError({ + statusCode: 500, + message: "Failed to retrieve or create location data", + }); + } + console.log("📍 Location data from DB:", locationData); if (!locationData) { - throw createError({ - statusCode: 400, - message: 'Invalid location provided and geocoding failed' - }) + throw createError({ + statusCode: 400, + message: "Invalid location provided and geocoding failed", + }); } try { // Check if event exists const foundEvent = await prisma.event.findUnique({ - where: { id } - }) + where: { id }, + }); if (!foundEvent) { - throw createError({ statusCode: 404, message: 'Event not found' }) + throw createError({ statusCode: 404, message: "Event not found" }); } // Update the event const updatedEvent = await prisma.event.update({ where: { id }, data: { - title: body.title, - shortDesc: body.shortDesc, - description: body.description, + ...body, location: { connectOrCreate: { where: { - address: body.location || null + address: body.location.address, }, - create: locationData! - } + create: locationData!, + }, }, - startTime: new Date(body.startTime), - endTime: new Date(body.endTime), - allowVolunteers: body.allowVolunteers, - allowAttendees: body.allowAttendees, }, include: { - eventAssets: true - } - }) + eventAssets: true, + }, + }); - console.log('✅ Event updated:', updatedEvent) - return updatedEvent - + console.log("✅ Event updated:", updatedEvent); + return updatedEvent; } catch (error) { - console.error('❌ Error updating event:', error) - throw createError({ - statusCode: 500, - message: 'Failed to update event' - }) + console.error("❌ Error updating event:", error); + throw createError({ + statusCode: 500, + message: "Failed to update event", + }); } -}) +}); diff --git a/server/api/events/index.get.ts b/server/api/events/index.get.ts index 5c03980..444c4ae 100644 --- a/server/api/events/index.get.ts +++ b/server/api/events/index.get.ts @@ -1,4 +1,4 @@ -import prisma from '~~/server/utils/prisma' +import prisma from "~~/server/utils/prisma"; export default defineEventHandler(async (_event) => { try { @@ -6,20 +6,20 @@ export default defineEventHandler(async (_event) => { include: { eventAssets: true, volunteerHours: true, - participants: true + participants: true, }, orderBy: { - startTime: 'asc' - } - }) + startTime: "asc", + }, + }); - console.log(`✅ Fetched ${allEvents.length} events`) - return allEvents - - } catch { - throw createError({ - statusCode: 500, - message: 'Failed to fetch events' - }) + console.log(`Fetched ${allEvents.length} events`); + return allEvents; + } catch (e) { + console.error(e); + throw createError({ + statusCode: 500, + message: "Failed to fetch events", + }); } -}) \ No newline at end of file +}); diff --git a/server/api/events/index.post.ts b/server/api/events/index.post.ts index d2d5f72..d0d3d15 100644 --- a/server/api/events/index.post.ts +++ b/server/api/events/index.post.ts @@ -72,7 +72,6 @@ export default defineEventHandler(async (event) => { }) } - try { // Create the event in the database const newEvent = await prisma.event.create({