style: run prettier
All checks were successful
TrafficCue Server CI / check (push) Successful in 37s
TrafficCue Server CD / build (push) Successful in 1m34s

This commit is contained in:
2025-09-19 21:54:48 +02:00
parent 03c2e8995b
commit a2f30118ef
2 changed files with 30 additions and 18 deletions

View File

@ -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;

View File

@ -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");