Skip to content

Commit

Permalink
refactor(config): streamline code formatting and remove extra commas
Browse files Browse the repository at this point in the history
  • Loading branch information
csulit committed Oct 25, 2024
1 parent d820746 commit a2210c8
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions config/deno-kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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"
);
}

Expand All @@ -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"
);
}

Expand All @@ -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 =
Expand All @@ -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],
Expand Down Expand Up @@ -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, {
Expand Down Expand Up @@ -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 || {}),
Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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");
Expand All @@ -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) {
Expand All @@ -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`,
});
}
};
Expand Down

0 comments on commit a2210c8

Please sign in to comment.