diff --git a/server/src/controllers/weather/getWeather.ts b/server/src/controllers/weather/getWeather.ts index 5e26aeeef..d4dd29bce 100644 --- a/server/src/controllers/weather/getWeather.ts +++ b/server/src/controllers/weather/getWeather.ts @@ -26,7 +26,7 @@ export function getWeatherRoute() { .input(z.object({ lat: z.number(), lon: z.number() })) .query(async (opts) => { const { lat, lon } = opts.input; - const { env }: any = opts.ctx; + const { env } = opts.ctx; return await getWeatherService( env.WEATHER_URL, env.OPENWEATHER_KEY, diff --git a/server/src/drizzle/methods/Node.ts b/server/src/drizzle/methods/Node.ts index 282e31433..daf660d08 100644 --- a/server/src/drizzle/methods/Node.ts +++ b/server/src/drizzle/methods/Node.ts @@ -1,21 +1,15 @@ import { eq } from 'drizzle-orm'; -import { createDb } from '../../db/client'; +import { DbClient } from '../../db/client'; import { node } from '../../db/schema'; -import { getDB } from '../../trpc/context'; export class Node { - async createInstance() { - const dbInstance = await createDb(getDB()); - return dbInstance; - } - async update( data: any, id: string, filter = eq(node.id, id), returning = null, ) { - return (await this.createInstance()) + return DbClient.instance .update(node) .set(data) .where(filter) @@ -24,39 +18,22 @@ export class Node { } async delete(id: string, filter = eq(node.id, id)) { - return (await this.createInstance()) - .delete(node) - .where(filter) - .returning() - .get(); + return DbClient.instance.delete(node).where(filter).returning().get(); } async findById(id: string, filter = eq(node.id, id)) { - return (await this.createInstance()) - .select() - .from(node) - .where(filter) - .limit(1) - .get(); + return DbClient.instance.select().from(node).where(filter).limit(1).get(); } async findMany(filter = null) { - return (await this.createInstance()) - .select() - .from(node) - .where(filter) - .get(); + return DbClient.instance.select().from(node).where(filter).get(); } async findUniqueNode(query) { - return (await this.createInstance()).query.node.findFirst(query); + return DbClient.instance.query.node.findFirst(query); } async create(data: any) { - return (await this.createInstance()) - .insert(node) - .values(data) - .returning() - .get(); + return DbClient.instance.insert(node).values(data).returning().get(); } } diff --git a/server/src/drizzle/methods/Way.ts b/server/src/drizzle/methods/Way.ts index 724524fc2..0e5c86256 100644 --- a/server/src/drizzle/methods/Way.ts +++ b/server/src/drizzle/methods/Way.ts @@ -1,21 +1,15 @@ import { eq } from 'drizzle-orm'; -import { createDb } from '../../db/client'; +import { DbClient } from '../../db/client'; import { way } from '../../db/schema'; -import { getDB } from '../../trpc/context'; export class Way { - async createInstance() { - const dbInstance = await createDb(getDB()); - return dbInstance; - } - async update( data: any, id: string, filter = eq(way.id, id), returning = null, ) { - return (await this.createInstance()) + return DbClient.instance .update(way) .set(data) .where(filter) @@ -24,35 +18,22 @@ export class Way { } async delete(id: string, filter = eq(way.id, id)) { - return (await this.createInstance()) - .delete(way) - .where(filter) - .returning() - .get(); + return DbClient.instance.delete(way).where(filter).returning().get(); } async findById(id: string, filter = eq(way.id, id)) { - return (await this.createInstance()) - .select() - .from(way) - .where(filter) - .limit(1) - .get(); + return DbClient.instance.select().from(way).where(filter).limit(1).get(); } async findMany(filter = null) { - return (await this.createInstance()).select().from(way).where(filter).get(); + return DbClient.instance.select().from(way).where(filter).get(); } async findUniqueWay(query) { - return (await this.createInstance()).query.way.findFirst(query); + return DbClient.instance.query.way.findFirst(query); } async create(data: any) { - return (await this.createInstance()) - .insert(way) - .values(data) - .returning() - .get(); + return DbClient.instance.insert(way).values(data).returning().get(); } } diff --git a/server/src/services/osm/getParksOSMService.ts b/server/src/services/osm/getParksOSMService.ts index ace82b0c0..f5a7575d5 100644 --- a/server/src/services/osm/getParksOSMService.ts +++ b/server/src/services/osm/getParksOSMService.ts @@ -24,7 +24,7 @@ export async function getParksOSMService(lat, lon, radius, osmUri) { const data = await response.json(); const geojsonData = osmtogeojson(data); - console.log('geojsonData==============', geojsonData); + // console.log('geojsonData==============', geojsonData); updateDatabaseWithGeoJSONDataFromOverpass(geojsonData); diff --git a/server/src/tests/routes/osm.spec.ts b/server/src/tests/routes/osm.spec.ts new file mode 100644 index 000000000..2abf75bdd --- /dev/null +++ b/server/src/tests/routes/osm.spec.ts @@ -0,0 +1,160 @@ +import { describe, it, expect, beforeAll, beforeEach } from 'vitest'; +import { setupTest } from '../testHelpers'; +import type { trpcCaller } from '../testHelpers'; +import { env } from 'cloudflare:test'; +import { Node as NodeClass } from '../../drizzle/methods/Node'; + +interface GeojsonType { + type: string; + properties: Object; + geometry: { + type: 'Point'; + coordinates: any; // from mongoose.Schema.Types.Mixed + }; +} + +describe('OSM routes', () => { + let caller: trpcCaller; + const nodeClass = new NodeClass(); + + beforeAll(async () => { + caller = await setupTest(env); + }); + + describe('getPhotonResults', () => { + it( + 'should get photon results based on search string', + { + timeout: 10000, + }, + async () => { + const results = await caller.getPhotonResults({ + searchString: 'test', + }); + expect(results).toBeDefined(); + }, + ); + }); + + describe('getTrailsOSM', () => { + it( + 'should get trails', + { + timeout: 10000, + }, + async () => { + const results = await caller.getTrailsOSM({ + lat: 20.5937, + lon: 78.9629, + }); + expect(results).toBeDefined(); + }, + ); + }); + + describe('getParksOSM', () => { + it( + 'should get parks', + { + timeout: 10000, + }, + async () => { + const results = await caller.getParksOSM({ + lat: 20.5937, + lon: 78.9629, + }); + expect(results).toBeDefined(); + }, + ); + }); + + describe('getOsm', () => { + it( + 'should get Osm routes based on start and end points', + { + timeout: 10000, + }, + async () => { + const results = await caller.getOsm({ + activityType: 'hiking', + startPoint: { + latitude: 20.5937, + longitude: 78.9629, + }, + endPoint: { + latitude: 20.5937, + longitude: 78.9629, + }, + }); + expect(results).toBeDefined(); + }, + ); + }); + + describe('postSingleGeoJSON', () => { + it( + 'should post geojson', + { + timeout: 10000, + }, + async () => { + const geojson: GeojsonType = { + properties: { + osm_id: 1, + osm_type: 'node', + }, + type: 'Feature', + geometry: { + coordinates: [], + type: 'Point', + }, + }; + const results = await caller.postSingleGeoJSON({ + geojson, + }); + expect(results).toBeDefined(); + }, + ); + }); + + describe('getDestination', () => { + it( + 'should get destination route', + { + timeout: 10000, + }, + async () => { + const node = await nodeClass.create({ + osm_id: 2311693, + lat: 22.231897, + lon: 60.435018, + tags: '{}', + }); + const results = await caller.getDestination({ id: node.id }); + expect(results).toBeDefined(); + }, + ); + }); + + describe('getPhotonDetails', () => { + it( + 'should get photon details', + { + timeout: 10000, + }, + async () => { + const node = await nodeClass.create({ + osm_id: 2311693, + lat: 22.231897, + lon: 60.435018, + tags: '{}', + }); + const results = await caller.getPhotonDetails({ + id: node.id, + type: 'node', + }); + expect(results).toBeDefined(); + }, + ); + }); +}); diff --git a/server/src/tests/routes/weather.test.ts b/server/src/tests/routes/weather.spec.ts similarity index 79% rename from server/src/tests/routes/weather.test.ts rename to server/src/tests/routes/weather.spec.ts index 32684a405..2e671aaef 100644 --- a/server/src/tests/routes/weather.test.ts +++ b/server/src/tests/routes/weather.spec.ts @@ -1,4 +1,4 @@ -import { describe, it, expect, beforeEach } from 'vitest'; +import { describe, it, expect, beforeAll } from 'vitest'; import { setupTest } from '../utils/testHelpers'; import type { trpcCaller } from '../utils/testHelpers'; import { env } from 'cloudflare:test'; @@ -6,7 +6,7 @@ import { env } from 'cloudflare:test'; describe('Get weather information', () => { let caller: trpcCaller; - beforeEach(async () => { + beforeAll(async () => { caller = await setupTest(env); }); @@ -15,6 +15,7 @@ describe('Get weather information', () => { lat: 20.5937, lon: 78.9629, }; + const weather = await caller.getWeather(coordinates); expect(weather).toBeDefined(); expect(weather?.coord?.lon).toEqual(coordinates?.lon); @@ -28,7 +29,7 @@ describe('Get weather information', () => { }; const weather = await caller.getWeatherWeek(coordinates); expect(weather).toBeDefined(); - expect(weather?.city?.coord?.lon).toEqual(coordinates?.lon); - expect(weather?.city?.coord?.lat).toEqual(coordinates?.lat); + expect(weather?.coord?.lon).toEqual(coordinates?.lon); + expect(weather?.coord?.lat).toEqual(coordinates?.lat); }); -}); \ No newline at end of file +}); diff --git a/server/src/utils/osmFunctions/modelHandlers.ts b/server/src/utils/osmFunctions/modelHandlers.ts index a99373d53..d8fe9aac7 100644 --- a/server/src/utils/osmFunctions/modelHandlers.ts +++ b/server/src/utils/osmFunctions/modelHandlers.ts @@ -1,4 +1,6 @@ import { Node } from '../../drizzle/methods/Node'; +import { Way } from '../../drizzle/methods/Way'; + import { createInstanceFromCoordinates, coordinatesToInstances, @@ -17,15 +19,15 @@ export const modelMappingFunc = (type: string) => { case 'node': case 'n': // In case 'n' is sent case 'N': // In case 'N' is sent - return 'Node'; + return new Node(); case 'way': case 'w': // Map 'W' to Way case 'W': // Map 'W' to Way - return 'Way'; - case 'relation': - case 'r': // In case 'r' is sent - case 'R': // In case 'R' is sent - return 'Relation'; + return new Way(); + // case 'relation': + // case 'r': // In case 'r' is sent + // case 'R': // In case 'R' is sent + // return 'Relation'; default: return null; } @@ -50,10 +52,12 @@ export async function fromOSM(Model: any, data: any) { const nodeClass = new Node(); // Find or create nodes const ids = data.nodes.map((node: any) => node.id); - const instances = await nodeClass.create(data.nodes); + const instances = await Promise.all( + data.nodes.map(async (node) => nodeClass.create(node)), + ); // Add nodes to instance - instanceData.nodes = instances.map((instance: any) => instance._id); + instanceData.nodes = instances.map((instance: any) => instance.id); // Create instance const newInstance = await Model.create({ data: instanceData }); @@ -162,8 +166,8 @@ export async function toGeoJSON(Model: any, instance: any) { * @param {string} type - The type of the item to search for. * @return {Promise} A promise that resolves to the found item, or null if not found. */ -export function findExisting(Model: any, id: any, type: string) { - return Model.findFirst({ where: { osm_id: id, osm_type: type } }); +export function findExisting(Model: any, id: any) { + return Model.findById(id); } /** @@ -276,7 +280,7 @@ export async function processElement(element: any) { return; } - let instance = await findExisting(ModelForElement, id, type); + let instance = await findExisting(ModelForElement, id); if (instance) { if (isGeoJSONFormat(element)) { instance = await updateInstanceFromGeoJSON(instance, element);