import { FastifyInstance } from 'fastify'; // import { AMENITIES, OverpassService } from '../services/overpass'; import { OverpassQuerySchema, OverpassQueryType } from '../schemas/overpass'; import db from '../../db'; // const overpassService = new OverpassService(); export default async function (fastify: FastifyInstance) { fastify.get<{ Querystring: OverpassQueryType }>('/blakus', { schema: { querystring: OverpassQuerySchema, response: { 200: { type: 'array', items: { type: 'object', properties: { id: { type: 'string' }, title: { type: 'string' }, amenity: { type: 'string' }, coords: { type: 'object', properties: { lat: { type: 'number' }, lon: { type: 'number' } } }, affinity: { type: 'number' }, // tags: { type: 'object', additionalProperties: true } } } } } }, config: { // serverCache: { ttl: 1 * 60 * 1000 } } }, async function (request, reply) { const {lat, lon} = request.query; const knownPlaces = await db.select( 'id', 'name', 'externalId', db.raw('ST_Y(coordinates::geometry) AS lat'), db.raw('ST_X(coordinates::geometry) AS lon') ).whereRaw( 'ST_DWithin(coordinates, ST_SetSRID(ST_MakePoint(?, ?), 4326), ?)', [lon, lat, 2000] ).from('places'); // const externalIds = await db.select('externalId').from('places').where('externalId', 'like', 'osm:%'); // console.log(externalIds); // overpassService.getAmenitiesAroundLocation({ // radius: 1000, // coords: { lat, lon }, // amenities: AMENITIES.Sustanance // }).then(result => { // const insertItems = result // .filter(node => { // if (!node.title || !node.coords) return false; // return !externalIds.some(({ externalId }) => externalId === `osm:${node.id}`); // }) // .map(node => ({ // name: node.title, // externalId: `osm:${node.id}`, // coordinates: db.raw('ST_SetSRID(ST_MakePoint(?, ?), 4326)', [node.coords.lon, node.coords.lat]) // })); // if (insertItems.length) db('places').insert(insertItems).then((success) => console.log({success}), (error) => console.log({error})); // }); const blakus = knownPlaces.map(place => ({ id: place.id, title: place.name, amenity: 'pub', coords: { lat: place.lat, lon: place.lon, }, affinity: Math.floor(Math.random() * 101) // TODO: this is just a placeholder for "affinity" calculation })); reply.send(blakus); }); }