import { sql } from 'drizzle-orm'; import { businesses, commercials, users } 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 | 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) // )) // `; return sql` ${radius} * 2 * ASIN(SQRT( POWER(SIN((${lat} - (${schema.location}->>'latitude')::float) * PI() / 180 / 2), 2) + COS(${lat} * PI() / 180) * COS((${schema.location}->>'latitude')::float * PI() / 180) * POWER(SIN((${lon} - (${schema.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 convertBusinessToDrizzleBusiness(businessListing: Partial): DrizzleBusinessListing { // const drizzleBusinessListing = flattenObject(businessListing); // drizzleBusinessListing.city = drizzleBusinessListing.name; // delete drizzleBusinessListing.name; // return drizzleBusinessListing; // } // export function convertDrizzleBusinessToBusiness(drizzleBusinessListing: Partial): BusinessListing { // const o = { // location: drizzleBusinessListing.city ? undefined : null, // location_name: drizzleBusinessListing.city ? drizzleBusinessListing.city : undefined, // location_state: drizzleBusinessListing.state ? drizzleBusinessListing.state : undefined, // location_latitude: drizzleBusinessListing.latitude ? drizzleBusinessListing.latitude : undefined, // location_longitude: drizzleBusinessListing.longitude ? drizzleBusinessListing.longitude : undefined, // ...drizzleBusinessListing, // }; // Object.keys(o).forEach(key => (o[key] === undefined ? delete o[key] : {})); // delete o.city; // delete o.state; // delete o.latitude; // delete o.longitude; // return unflattenObject(o); // } // export function convertCommercialToDrizzleCommercial(commercialPropertyListing: Partial): DrizzleCommercialPropertyListing { // const drizzleCommercialPropertyListing = flattenObject(commercialPropertyListing); // drizzleCommercialPropertyListing.city = drizzleCommercialPropertyListing.name; // delete drizzleCommercialPropertyListing.name; // return drizzleCommercialPropertyListing; // } // export function convertDrizzleCommercialToCommercial(drizzleCommercialPropertyListing: Partial): CommercialPropertyListing { // const o = { // location: drizzleCommercialPropertyListing.city ? undefined : null, // location_name: drizzleCommercialPropertyListing.city ? drizzleCommercialPropertyListing.city : undefined, // location_state: drizzleCommercialPropertyListing.state ? drizzleCommercialPropertyListing.state : undefined, // location_street: drizzleCommercialPropertyListing.street ? drizzleCommercialPropertyListing.street : undefined, // location_housenumber: drizzleCommercialPropertyListing.housenumber ? drizzleCommercialPropertyListing.housenumber : undefined, // location_county: drizzleCommercialPropertyListing.county ? drizzleCommercialPropertyListing.county : undefined, // location_zipCode: drizzleCommercialPropertyListing.zipCode ? drizzleCommercialPropertyListing.zipCode : undefined, // location_latitude: drizzleCommercialPropertyListing.latitude ? drizzleCommercialPropertyListing.latitude : undefined, // location_longitude: drizzleCommercialPropertyListing.longitude ? drizzleCommercialPropertyListing.longitude : undefined, // ...drizzleCommercialPropertyListing, // }; // Object.keys(o).forEach(key => (o[key] === undefined ? delete o[key] : {})); // delete o.city; // delete o.state; // delete o.street; // delete o.housenumber; // delete o.county; // delete o.zipCode; // delete o.latitude; // delete o.longitude; // return unflattenObject(o); // } // export function convertUserToDrizzleUser(user: Partial): DrizzleUser { // const drizzleUser = flattenObject(user); // drizzleUser.city = drizzleUser.name; // delete drizzleUser.name; // return drizzleUser; // } // export function convertDrizzleUserToUser(drizzleUser: Partial): User { // const o: any = { // companyLocation: drizzleUser.city ? undefined : null, // companyLocation_name: drizzleUser.city ? drizzleUser.city : undefined, // companyLocation_state: drizzleUser.state ? drizzleUser.state : undefined, // companyLocation_latitude: drizzleUser.latitude ? drizzleUser.latitude : undefined, // companyLocation_longitude: drizzleUser.longitude ? drizzleUser.longitude : undefined, // ...drizzleUser, // }; // Object.keys(o).forEach(key => (o[key] === undefined ? delete o[key] : {})); // delete o.city; // delete o.state; // delete o.latitude; // delete o.longitude; // return unflattenObject(o); // } // function flattenObject(obj: any, res: any = {}): any { // for (const key in obj) { // if (obj.hasOwnProperty(key)) { // const value = obj[key]; // if (typeof value === 'object' && value !== null && !Array.isArray(value)) { // if (value instanceof Date) { // res[key] = value; // } else { // flattenObject(value, res); // } // } else { // res[key] = value; // } // } // } // return res; // } // function unflattenObject(obj: any, separator: string = '_'): any { // const result: any = {}; // for (const key in obj) { // if (obj.hasOwnProperty(key)) { // const keys = key.split(separator); // keys.reduce((acc, curr, idx) => { // if (idx === keys.length - 1) { // acc[curr] = obj[key]; // } else { // if (!acc[curr]) { // acc[curr] = {}; // } // } // return acc[curr]; // }, result); // } // } // return result; // } 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 }; } }