Umbau zu location / companyLocation
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { sql } from 'drizzle-orm';
|
||||
import { businesses, commercials, users } from './drizzle/schema.js';
|
||||
import { User } from './models/db.model.js';
|
||||
import { BusinessListing, CommercialPropertyListing, User } from './models/db.model.js';
|
||||
export const EARTH_RADIUS_KM = 6371; // Erdradius in Kilometern
|
||||
export const EARTH_RADIUS_MILES = 3959; // Erdradius in Meilen
|
||||
export function convertStringToNullUndefined(value) {
|
||||
@@ -29,8 +29,43 @@ export const getDistanceQuery = (schema: typeof businesses | typeof commercials
|
||||
`;
|
||||
};
|
||||
|
||||
type DrizzleUser = typeof users.$inferSelect; //Partial<InferInsertModel<typeof users>>;
|
||||
|
||||
type DrizzleUser = typeof users.$inferSelect;
|
||||
type DrizzleBusinessListing = typeof businesses.$inferSelect;
|
||||
type DrizzleCommercialPropertyListing = typeof commercials.$inferSelect;
|
||||
export function convertBusinessToDrizzleBusiness(businessListing: Partial<BusinessListing>): DrizzleBusinessListing {
|
||||
return flattenObject(businessListing);
|
||||
}
|
||||
export function convertDrizzleBusinessToBusiness(drizzleBusinessListing: Partial<DrizzleBusinessListing>): BusinessListing {
|
||||
const o = {
|
||||
location_city: drizzleBusinessListing.city,
|
||||
location_state: drizzleBusinessListing.state,
|
||||
location_latitude: drizzleBusinessListing.latitude,
|
||||
location_longitude: drizzleBusinessListing.longitude,
|
||||
...drizzleBusinessListing,
|
||||
};
|
||||
delete o.city;
|
||||
delete o.state;
|
||||
delete o.latitude;
|
||||
delete o.longitude;
|
||||
return unflattenObject(o);
|
||||
}
|
||||
export function convertCommercialToDrizzleCommercial(commercialPropertyListing: Partial<CommercialPropertyListing>): DrizzleCommercialPropertyListing {
|
||||
return flattenObject(commercialPropertyListing);
|
||||
}
|
||||
export function convertDrizzleCommercialToCommercial(drizzleCommercialPropertyListing: Partial<DrizzleCommercialPropertyListing>): CommercialPropertyListing {
|
||||
const o = {
|
||||
location_city: drizzleCommercialPropertyListing.city,
|
||||
location_state: drizzleCommercialPropertyListing.state,
|
||||
location_latitude: drizzleCommercialPropertyListing.latitude,
|
||||
location_longitude: drizzleCommercialPropertyListing.longitude,
|
||||
...drizzleCommercialPropertyListing,
|
||||
};
|
||||
delete o.city;
|
||||
delete o.state;
|
||||
delete o.latitude;
|
||||
delete o.longitude;
|
||||
return unflattenObject(o);
|
||||
}
|
||||
export function convertUserToDrizzleUser(user: Partial<User>): DrizzleUser {
|
||||
const { companyLocation, ...restUser } = user;
|
||||
|
||||
@@ -96,3 +131,42 @@ export function convertDrizzleUserToUser(drizzleUser: Partial<DrizzleUser>): Use
|
||||
};
|
||||
return user;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user