@@ -8,7 +8,10 @@ import Profile from "@models/Profile";
88import Link from "@models/Link" ;
99
1010export default async function handler ( req , res ) {
11- if ( req . method !== "GET" || req . query . secret !== serverEnv . LINKFREE_API_SECRET ) {
11+ if (
12+ req . method !== "GET" ||
13+ req . query . secret !== serverEnv . LINKFREE_API_SECRET
14+ ) {
1215 logger . error (
1316 `attempt to load profile json but security check failed: "${ req . query . secret } "`
1417 ) ;
@@ -195,70 +198,63 @@ export default async function handler(req, res) {
195198 }
196199
197200 // - events
198- async function getCoordinates ( city , state , country ) {
199- const location = await fetch (
200- `https://nominatim.openstreetmap.org/?addressdetails=1&city=${ city } &state=${ state } &country=${ country } &format=json&limit=1`
201- ) ;
202- const coordinates = await location . json ( ) ;
203- const final = {
204- lat : await coordinates [ 0 ] . lat ,
205- lon : await coordinates [ 0 ] . lon ,
206- } ;
207- return final ;
208- }
209- try {
210- if ( profile . events ) {
211- await Profile . findOneAndUpdate (
212- { username : profile . username } ,
213- {
214- events : await Promise . all (
215- profile . events . map ( async ( event , position ) => {
216- let coordinates =
217- event . location &&
218- event . location . city &&
219- event . location . state &&
220- event . location . country
221- ? await getCoordinates (
222- event . location . city ,
223- event . location . state ,
224- event . location . country
225- )
226- : "" ;
227- return {
228- isVirtual : event . isVirtual ,
229- color : event . color ,
230- name : event . name ,
231- description : event . description ,
232- date : {
233- start : event . date . start ,
234- end : event . date . end ,
235- } ,
236- url : event . url ,
237- location : event . location
238- ? {
239- road : event . location . road ? event . location . road : "" ,
240- city : event . location . city ? event . location . city : "" ,
241- state : event . location . state
242- ? event . location . state
243- : "" ,
244- country : event . location . country
245- ? event . location . country
246- : "" ,
247- lat : event . location . city ? coordinates . lat : "" ,
248- lon : event . location . city ? coordinates . lon : "" ,
249- }
250- : undefined ,
251- order : position ,
252- price : event . price ,
253- } ;
254- } )
255- ) ,
256- }
201+ async function getCoordinates ( city , state , country ) {
202+ let locationDb = { } ;
203+ const provided = [ city , state , country ] . filter ( ( x ) => x ) . join ( "," ) ;
204+ if ( locationDb [ provided ] ) {
205+ return locationDb [ provided ] ;
206+ }
207+ try {
208+ const location = await fetch (
209+ `https://nominatim.openstreetmap.org/?addressdetails=1&q=
210+ ${ encodeURIComponent ( provided ) } &format=json&limit=1`
257211 ) ;
212+ const coordinates = await location . json ( ) ;
213+ if ( coordinates ) {
214+ const point = {
215+ lat : coordinates [ 0 ] . lat ,
216+ lon : coordinates [ 0 ] . lon ,
217+ } ;
218+ locationDb [ provided ] = point ;
219+ return point ;
220+ }
221+ } catch ( e ) {
222+ return null ;
258223 }
259- } catch ( e ) {
260- logger . error ( e , `failed to update events for ${ profile . username } ` ) ;
224+ return null ;
261225 }
226+
227+ const events = profile . events ? await Promise . all (
228+ profile . events . map ( async ( event , position ) => {
229+ let location = { } ;
230+ if ( event . location ) {
231+ location = {
232+ location : { ...event . location } ,
233+ } ;
234+ if ( new Date ( event . date . start ) > Date . now ( ) || new Date ( event . date . end ) > Date . now ( ) ) {
235+ const coordinates = await getCoordinates (
236+ event . location . city ,
237+ event . location . state ,
238+ event . location . country
239+ ) ;
240+ if ( coordinates ) {
241+ location . location . lat = coordinates . lat ;
242+ location . location . lon = coordinates . lon ;
243+ }
244+ }
245+ }
246+ return {
247+ order : position ,
248+ ...event ,
249+ ...location ,
250+ } ;
251+ } )
252+ ) : null ;
253+
254+ await Profile . findOneAndUpdate (
255+ { username : profile . username } ,
256+ { events }
257+ ) ;
262258 } )
263259 ) ;
264260
0 commit comments