bizmatch-project/bizmatch-server/src/utils.ts

31 lines
1.2 KiB
TypeScript

import { sql } from 'drizzle-orm';
import { businesses, commercials, users } from './drizzle/schema.js';
export const EARTH_RADIUS_KM = 6371; // Erdradius in Kilometern
export const EARTH_RADIUS_MILES = 3959; // Erdradius in Meilen
export function convertStringToNullUndefined(value) {
// Konvertiert den Wert zu Kleinbuchstaben für eine case-insensitive Überprüfung
const lowerCaseValue = typeof value === 'boolean' ? value : value?.toLowerCase();
if (lowerCaseValue === 'null') {
return null;
} else if (lowerCaseValue === 'undefined') {
return undefined;
}
// Gibt den Originalwert zurück, wenn es sich nicht um 'null' oder 'undefined' handelt
return value;
}
export const getDistanceQuery = (schema: typeof businesses | typeof commercials | typeof users, lat: number, lon: number, unit: 'km' | 'miles' = 'miles') => {
const radius = unit === 'km' ? EARTH_RADIUS_KM : EARTH_RADIUS_MILES;
return sql`
${radius} * 2 * ASIN(SQRT(
POWER(SIN((${lat} - ${schema.latitude}) * PI() / 180 / 2), 2) +
COS(${lat} * PI() / 180) * COS(${schema.latitude} * PI() / 180) *
POWER(SIN((${lon} - ${schema.longitude}) * PI() / 180 / 2), 2)
))
`;
};