Updated the operations dashboard to include a rain outlook feature, providing users with a 7-day forecast based on site coordinates. Added latitude and longitude fields to the site form for better weather data integration. Enhanced the fuel log and site management functionalities to utilize dynamic fuel pricing based on historical data. Improved overall data handling and user experience across various dashboard components.
44 lines
1.4 KiB
TypeScript
44 lines
1.4 KiB
TypeScript
/**
|
|
* Coordinate parsing shared by POST /api/sites and PATCH /api/sites/[id].
|
|
*
|
|
* The site form posts strings, so "-26.2041" has to be accepted as
|
|
* readily as -26.2041. An empty string means "clear this", which is
|
|
* distinct from "not supplied" — the caller decides which by checking
|
|
* whether the key was in the body at all.
|
|
*
|
|
* Out-of-range values are rejected rather than clamped: a latitude of 91
|
|
* is a typo, and silently moving the site to the North Pole would show a
|
|
* confident forecast for the wrong place.
|
|
*/
|
|
export const COORDINATE_RANGE = {
|
|
latitude: { min: -90, max: 90 },
|
|
longitude: { min: -180, max: 180 },
|
|
} as const;
|
|
|
|
export type CoordinateKind = keyof typeof COORDINATE_RANGE;
|
|
|
|
export type CoordinateParse =
|
|
| { ok: true; value: number | null }
|
|
| { ok: false; error: string };
|
|
|
|
export function parseCoordinate(
|
|
value: unknown,
|
|
kind: CoordinateKind,
|
|
): CoordinateParse {
|
|
if (value === null || value === "") return { ok: true, value: null };
|
|
|
|
const n = typeof value === "number" ? value : Number(value);
|
|
if (typeof value !== "number" && typeof value !== "string") {
|
|
return { ok: false, error: `${kind} must be a number` };
|
|
}
|
|
if (!Number.isFinite(n)) {
|
|
return { ok: false, error: `${kind} must be a number` };
|
|
}
|
|
|
|
const { min, max } = COORDINATE_RANGE[kind];
|
|
if (n < min || n > max) {
|
|
return { ok: false, error: `${kind} must be between ${min} and ${max}` };
|
|
}
|
|
|
|
return { ok: true, value: n };
|
|
}
|