From a2210c8aeda4f35123265e6babf3e037b470bde4 Mon Sep 17 00:00:00 2001 From: casulit Date: Fri, 25 Oct 2024 13:31:55 +0800 Subject: [PATCH] refactor(config): streamline code formatting and remove extra commas --- config/deno-kv.ts | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/config/deno-kv.ts b/config/deno-kv.ts index c9c7b47..9e7e7f6 100644 --- a/config/deno-kv.ts +++ b/config/deno-kv.ts @@ -105,7 +105,7 @@ function cleanSpecialCharacters(input: string): string { // Remove emojis and other special characters const cleanedString = encodedString.replace( /[\u{1F600}-\u{1F64F}\u{1F300}-\u{1F5FF}\u{1F680}-\u{1F6FF}\u{1F1E0}-\u{1F1FF}\u{2600}-\u{26FF}\u{2700}-\u{27BF}]/gu, - "", + "" ); // Remove extra whitespace @@ -127,7 +127,7 @@ export async function listenQueue(kv: Deno.Kv) { try { const attributesLength = Object.keys( - msg.data.dataLayer.attributes, + msg.data.dataLayer.attributes ).length; console.log("attributesLength:", attributesLength); @@ -142,7 +142,7 @@ export async function listenQueue(kv: Deno.Kv) { if (!msg.data.dataLayer.attributes || attributesLength < 3) { throw new Error( - "Attributes are missing, undefined, or have fewer than 3 properties", + "Attributes are missing, undefined, or have fewer than 3 properties" ); } @@ -151,7 +151,7 @@ export async function listenQueue(kv: Deno.Kv) { typeof msg.data.dataLayer.location !== "object" ) { throw new Error( - "Location is missing, undefined, or not an object", + "Location is missing, undefined, or not an object" ); } @@ -161,7 +161,7 @@ export async function listenQueue(kv: Deno.Kv) { const images = msg.data.images as { src: string }[]; const isCondominium = msg.data.dataLayer.attributes.attribute_set_name === - "Condominium"; + "Condominium"; const isHouse = msg.data.dataLayer.attributes.attribute_set_name === "House"; const isWarehouse = @@ -182,8 +182,8 @@ export async function listenQueue(kv: Deno.Kv) { }; const price = msg.data.dataLayer?.attributes?.price; - const priceFormatted = msg.data.dataLayer?.attributes - ?.price_formatted; + const priceFormatted = + msg.data.dataLayer?.attributes?.price_formatted; await transaction.queryArray({ args: [price, priceFormatted, listing.id], @@ -263,9 +263,8 @@ export async function listenQueue(kv: Deno.Kv) { const productOwnerName = msg.data.dataLayer?.product_owner_name; const location: Location = msg.data.dataLayer.location; const dataLayerAttributes = msg.data.dataLayer.attributes; - const offerTypeId = dataLayerAttributes.offer_type === "Rent" - ? 2 - : 1; + const offerTypeId = + dataLayerAttributes.offer_type === "Rent" ? 2 : 1; const sellerIsTrusted = dataLayerAttributes?.seller_is_trusted; const locationData = await getLocation(client_2, { @@ -294,7 +293,7 @@ export async function listenQueue(kv: Deno.Kv) { JSON.stringify(images.map((image) => image.src)), JSON.stringify(dataLayerAttributes?.amenities || {}), JSON.stringify( - dataLayerAttributes?.property_features || {}, + dataLayerAttributes?.property_features || {} ), JSON.stringify(dataLayerAttributes?.indoor_features || {}), JSON.stringify(dataLayerAttributes?.outdoor_features || {}), @@ -378,7 +377,7 @@ export async function listenQueue(kv: Deno.Kv) { `https://www.lamudi.com.ph/${dataLayerAttributes?.urlkey_details}`, dataLayerAttributes?.project_name || null, cleanSpecialCharacters( - msg.data.dataLayer?.description?.text, + msg.data.dataLayer?.description?.text ), true, address, @@ -389,8 +388,7 @@ export async function listenQueue(kv: Deno.Kv) { offerTypeId, newProperty.id, ], - text: - `INSERT INTO Listing (title, url, project_name, description, is_scraped, address, price_formatted, price, offer_type_id, property_id) + text: `INSERT INTO Listing (title, url, project_name, description, is_scraped, address, price_formatted, price, offer_type_id, property_id) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING id`, }); } catch (error) { @@ -435,8 +433,7 @@ export async function listenQueue(kv: Deno.Kv) { msg.data.listingUrl, JSON.stringify(msg.data.images), ], - text: - `INSERT INTO Lamudi_raw_data (json_data, listingUrl, images) VALUES ($1, $2, $3)`, + text: `INSERT INTO Lamudi_raw_data (json_data, listingUrl, images) VALUES ($1, $2, $3)`, }); await transaction.commit(); console.log("Transaction successfully committed for create"); @@ -455,7 +452,7 @@ export async function listenQueue(kv: Deno.Kv) { try { const property = await client_1.queryObject( - `SELECT * FROM Property WHERE ai_generated_description IS NULL AND property_type_id IN (1, 3) ORDER BY created_at DESC LIMIT 10`, + `SELECT * FROM Property WHERE ai_generated_description IS NULL AND property_type_id IN (1, 3) ORDER BY created_at DESC LIMIT 10` ); if (property.rowCount && property.rowCount > 0) { @@ -466,17 +463,22 @@ export async function listenQueue(kv: Deno.Kv) { }; const aiGeneratedDescription = await openaiAssistant( - JSON.stringify(row), + JSON.stringify(row) ); + try { + JSON.parse(aiGeneratedDescription); + } catch { + throw Error("Invalid AI description format"); + } + if (aiGeneratedDescription) { await client_1.queryObject({ args: [ JSON.stringify(aiGeneratedDescription), propertyData.id, ], - text: - `UPDATE Property SET ai_generated_description = $1 WHERE id = $2`, + text: `UPDATE Property SET ai_generated_description = $1 WHERE id = $2`, }); } };