diff --git a/src/entities/Hazard.ts b/src/entities/Hazard.ts index c13c8f1..bd2b3d4 100644 --- a/src/entities/Hazard.ts +++ b/src/entities/Hazard.ts @@ -1,4 +1,10 @@ -import { BaseEntity, Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm"; +import { + BaseEntity, + Column, + Entity, + ManyToOne, + PrimaryGeneratedColumn, +} from "typeorm"; import type { User } from "./User"; @Entity() @@ -7,12 +13,12 @@ export class Hazard extends BaseEntity { id: string; @Column({ - type: "float" + type: "float", }) latitude: number; @Column({ - type: "float" + type: "float", }) longitude: number; @@ -29,4 +35,4 @@ export class Hazard extends BaseEntity { @ManyToOne("User", (u: User) => u.hazards) user: User; -} \ No newline at end of file +} diff --git a/src/main.ts b/src/main.ts index 3035a12..a4a1a7e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -166,30 +166,39 @@ if (process.env.REVIEWS_ENABLED) { }); } -const VALID_HAZARD_TYPES = [ - "bumpy-road" -]; +const VALID_HAZARD_TYPES = ["bumpy-road"]; if (process.env.HAZARDS_ENABLED) { app.get("/api/hazards", async (c) => { const { lat, lon, radius } = c.req.query(); if (!lat || !lon || !radius) { - return c.json({ error: "Latitude, longitude, and radius are required" }, 400); + return c.json( + { error: "Latitude, longitude, and radius are required" }, + 400, + ); } // Remove unnecessary precision from lat/lon const nlat = Number(parseFloat(lat).toFixed(4)); const nlon = Number(parseFloat(lon).toFixed(4)); const nradius = Number(radius); - if(isNaN(nradius) || nradius <= 0) { + if (isNaN(nradius) || nradius <= 0) { return c.json({ error: "Invalid radius" }, 400); } - if(nradius > 1000) { + if (nradius > 1000) { return c.json({ error: "Radius too large, max is 1000 km" }, 400); } - console.log(`Fetching hazards for lat: ${lat}, lon: ${lon}, radius: ${radius} km`); + console.log( + `Fetching hazards for lat: ${lat}, lon: ${lon}, radius: ${radius} km`, + ); const hazards = await Hazard.createQueryBuilder("hazard") // Crazy math to get a rough bounding box for the radius in km - .where("hazard.latitude BETWEEN :minLat AND :maxLat", { minLat: nlat - nradius / 110.574, maxLat: nlat + nradius / 110.574 }) - .andWhere("hazard.longitude BETWEEN :minLon AND :maxLon", { minLon: nlon - nradius / (111.320 * Math.cos(nlat * (Math.PI / 180))), maxLon: nlon + nradius / (111.320 * Math.cos(nlat * (Math.PI / 180))) }) + .where("hazard.latitude BETWEEN :minLat AND :maxLat", { + minLat: nlat - nradius / 110.574, + maxLat: nlat + nradius / 110.574, + }) + .andWhere("hazard.longitude BETWEEN :minLon AND :maxLon", { + minLon: nlon - nradius / (111.32 * Math.cos(nlat * (Math.PI / 180))), + maxLon: nlon + nradius / (111.32 * Math.cos(nlat * (Math.PI / 180))), + }) .getMany(); return c.json(hazards); }); @@ -202,11 +211,8 @@ if (process.env.HAZARDS_ENABLED) { 400, ); } - if(!VALID_HAZARD_TYPES.includes(type)) { - return c.json( - { error: "Invalid hazard type" }, - 400, - ); + if (!VALID_HAZARD_TYPES.includes(type)) { + return c.json({ error: "Invalid hazard type" }, 400); } const authHeader = c.req.header("Authorization");