From 2fe0b91c427bbae00778fd6e3a663c192e1832a2 Mon Sep 17 00:00:00 2001 From: Shreya Date: Sat, 28 Mar 2026 13:24:37 -0500 Subject: [PATCH 1/2] fix: geocoding and location updates for event editing through patch is fixed. error messages show for the user if geocoding fails --- app/pages/events/[id].vue | 10 ++++++++++ server/api/events/[id]/index.patch.ts | 22 ++++++++++------------ server/api/events/index.post.ts | 1 - 3 files changed, 20 insertions(+), 13 deletions(-) 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/server/api/events/[id]/index.patch.ts b/server/api/events/[id]/index.patch.ts index 0bbe926..a6d7a52 100644 --- a/server/api/events/[id]/index.patch.ts +++ b/server/api/events/[id]/index.patch.ts @@ -1,3 +1,4 @@ +import { error } from 'node:console' import prisma from '~~/server/utils/prisma' // Geocode location using Nominatim @@ -39,7 +40,6 @@ async function geocodeLocation( location: string) { } - export default defineEventHandler(async (event) => { const id = getRouterParam(event, 'id') const body = await readBody(event) @@ -51,17 +51,20 @@ export default defineEventHandler(async (event) => { //check if location has already been fetched const locationData = await prisma.location.findUnique({ where: { - address: body.location || null + address: body.location.address || null }, select: { latitude: true, longitude: true, address: true } - }) || await geocodeLocation(body.location) + }) || 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({ @@ -84,21 +87,16 @@ export default defineEventHandler(async (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! } }, - startTime: new Date(body.startTime), - endTime: new Date(body.endTime), - allowVolunteers: body.allowVolunteers, - allowAttendees: body.allowAttendees, + }, include: { eventAssets: true 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({ From 479b0b2cdbfb95680babfeada6bace784a4fd664 Mon Sep 17 00:00:00 2001 From: Vikas Thoutam Date: Mon, 11 May 2026 19:14:45 -0500 Subject: [PATCH 2/2] chore: update workspace yaml and add logging to API --- pnpm-workspace.yaml | 11 ++- server/api/events/[id]/index.patch.ts | 123 +++++++++++++------------- server/api/events/index.get.ts | 28 +++--- 3 files changed, 83 insertions(+), 79 deletions(-) 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 a6d7a52..071a259 100644 --- a/server/api/events/[id]/index.patch.ts +++ b/server/api/events/[id]/index.patch.ts @@ -1,86 +1,87 @@ -import { error } from 'node:console' -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.address || null - }, - select: { - latitude: true, - longitude: true, - address: true - } - }) || await geocodeLocation(body.location.address) + //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)); if (!locationData) { - throw createError({ statusCode: 500, message: 'Failed to retrieve or create location data' }) + throw createError({ + statusCode: 500, + message: "Failed to retrieve or create location data", + }); } - console.log('📍 Location data from DB:', locationData) + 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 @@ -91,26 +92,24 @@ export default defineEventHandler(async (event) => { location: { connectOrCreate: { where: { - address: body.location.address + address: body.location.address, }, - create: locationData! - } + create: locationData!, + }, }, - }, 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 +});