import { sql } from 'drizzle-orm'; import { businesses, businesses_json, commercials, commercials_json, users, users_json } from './drizzle/schema'; 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_json | typeof commercials_json | typeof users_json, 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.data}->'location'->>'latitude')::float) * PI() / 180 / 2), 2) + COS(${lat} * PI() / 180) * COS((${schema.data}->'location'->>'latitude')::float * PI() / 180) * POWER(SIN((${lon} - (${schema.data}->'location'->>'longitude')::float) * PI() / 180 / 2), 2) )) `; }; export type DrizzleUser = typeof users.$inferSelect; export type DrizzleBusinessListing = typeof businesses.$inferSelect; export type DrizzleCommercialPropertyListing = typeof commercials.$inferSelect; export function splitName(fullName: string): { firstname: string; lastname: string } { const parts = fullName.trim().split(/\s+/); // Teile den Namen am Leerzeichen auf if (parts.length === 1) { // Falls es nur ein Teil gibt, ist firstname und lastname gleich return { firstname: parts[0], lastname: parts[0] }; } else { // Ansonsten ist der letzte Teil der lastname, der Rest der firstname const lastname = parts.pop()!; const firstname = parts.join(' '); return { firstname, lastname }; } }