@@ -195,70 +195,63 @@ export default async function handler(req, res) {
195195 }
196196
197197 // - 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- }
198+ async function getCoordinates ( city , state , country ) {
199+ let locationDb = { } ;
200+ const provided = [ city , state , country ] . filter ( ( x ) => x ) . join ( "," ) ;
201+ if ( locationDb [ provided ] ) {
202+ return locationDb [ provided ] ;
203+ }
204+ try {
205+ const location = await fetch (
206+ `https://nominatim.openstreetmap.org/?addressdetails=1&q=
207+ ${ encodeURIComponent ( provided ) } &format=json&limit=1`
257208 ) ;
209+ const coordinates = await location . json ( ) ;
210+ if ( coordinates ) {
211+ const point = {
212+ lat : coordinates [ 0 ] . lat ,
213+ lon : coordinates [ 0 ] . lon ,
214+ } ;
215+ locationDb [ provided ] = point ;
216+ return point ;
217+ }
218+ } catch ( e ) {
219+ return null ;
258220 }
259- } catch ( e ) {
260- logger . error ( e , `failed to update events for ${ profile . username } ` ) ;
221+ return null ;
261222 }
223+
224+ const events = profile . events ? await Promise . all (
225+ profile . events . map ( async ( event , position ) => {
226+ let location = { } ;
227+ if ( event . location ) {
228+ location = {
229+ location : { ...event . location } ,
230+ } ;
231+ if ( new Date ( event . date . start ) > Date . now ( ) || new Date ( event . date . end ) > Date . now ( ) ) {
232+ const coordinates = await getCoordinates (
233+ event . location . city ,
234+ event . location . state ,
235+ event . location . country
236+ ) ;
237+ if ( coordinates ) {
238+ location . location . lat = coordinates . lat ;
239+ location . location . lon = coordinates . lon ;
240+ }
241+ }
242+ }
243+ return {
244+ order : position ,
245+ ...event ,
246+ ...location ,
247+ } ;
248+ } )
249+ ) : null ;
250+
251+ await Profile . findOneAndUpdate (
252+ { username : profile . username } ,
253+ { events }
254+ ) ;
262255 } )
263256 ) ;
264257
0 commit comments