From d71a5c25c3f898adb0a99ccd815653c5ecb6a815 Mon Sep 17 00:00:00 2001 From: Andreas Knuth Date: Sun, 11 Aug 2024 13:06:48 +0200 Subject: [PATCH] remove id before create Listing, set location/companyLocation to null if not set --- .../src/listings/business-listing.service.ts | 1 + .../listings/commercial-property.service.ts | 1 + bizmatch-server/src/utils.ts | 33 +++++++++++-------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/bizmatch-server/src/listings/business-listing.service.ts b/bizmatch-server/src/listings/business-listing.service.ts index d075b21..fecc507 100644 --- a/bizmatch-server/src/listings/business-listing.service.ts +++ b/bizmatch-server/src/listings/business-listing.service.ts @@ -176,6 +176,7 @@ export class BusinessListingService { data.updated = new Date(); const validatedBusinessListing = BusinessListingSchema.parse(data); const convertedBusinessListing = convertBusinessToDrizzleBusiness(data); + delete convertedBusinessListing.id; const [createdListing] = await this.conn.insert(businesses).values(convertedBusinessListing).returning(); return convertDrizzleBusinessToBusiness(createdListing); } catch (error) { diff --git a/bizmatch-server/src/listings/commercial-property.service.ts b/bizmatch-server/src/listings/commercial-property.service.ts index 7399d4b..789181e 100644 --- a/bizmatch-server/src/listings/commercial-property.service.ts +++ b/bizmatch-server/src/listings/commercial-property.service.ts @@ -127,6 +127,7 @@ export class CommercialPropertyService { data.updated = new Date(); const validatedCommercialPropertyListing = CommercialPropertyListingSchema.parse(data); const convertedCommercialPropertyListing = convertCommercialToDrizzleCommercial(data); + delete convertedCommercialPropertyListing.id; const [createdListing] = await this.conn.insert(commercials).values(convertedCommercialPropertyListing).returning(); return convertDrizzleCommercialToCommercial(createdListing); } catch (error) { diff --git a/bizmatch-server/src/utils.ts b/bizmatch-server/src/utils.ts index e9a3cce..156f11f 100644 --- a/bizmatch-server/src/utils.ts +++ b/bizmatch-server/src/utils.ts @@ -40,12 +40,14 @@ export function convertBusinessToDrizzleBusiness(businessListing: Partial): BusinessListing { const o = { - location_name: drizzleBusinessListing.city, - location_state: drizzleBusinessListing.state, - location_latitude: drizzleBusinessListing.latitude, - location_longitude: drizzleBusinessListing.longitude, + 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; @@ -60,12 +62,14 @@ export function convertCommercialToDrizzleCommercial(commercialPropertyListing: } export function convertDrizzleCommercialToCommercial(drizzleCommercialPropertyListing: Partial): CommercialPropertyListing { const o = { - location_name: drizzleCommercialPropertyListing.city, - location_state: drizzleCommercialPropertyListing.state, - location_latitude: drizzleCommercialPropertyListing.latitude, - location_longitude: drizzleCommercialPropertyListing.longitude, + location: drizzleCommercialPropertyListing.city ? undefined : null, + location_name: drizzleCommercialPropertyListing.city ? drizzleCommercialPropertyListing.city : undefined, + location_state: drizzleCommercialPropertyListing.state ? drizzleCommercialPropertyListing.state : 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.latitude; @@ -80,17 +84,20 @@ export function convertUserToDrizzleUser(user: Partial): DrizzleUser { } export function convertDrizzleUserToUser(drizzleUser: Partial): User { - const o = { - companyLocation_name: drizzleUser.city, - companyLocation_state: drizzleUser.state, - companyLocation_latitude: drizzleUser.latitude, - companyLocation_longitude: drizzleUser.longitude, + 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 {