Skip to content

Commit

Permalink
refactor(config): standardize trailing commas and improve formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
csulit committed Oct 25, 2024
1 parent 17b265d commit f346d00
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 40 deletions.
36 changes: 20 additions & 16 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,8 +263,9 @@ 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 @@ -293,7 +294,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 @@ -377,7 +378,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 @@ -388,7 +389,8 @@ 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 @@ -433,7 +435,8 @@ 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 @@ -452,7 +455,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 @@ -463,7 +466,7 @@ export async function listenQueue(kv: Deno.Kv) {
};

const aiGeneratedDescription = await openaiAssistant(
JSON.stringify(row)
JSON.stringify(row),
);

if (aiGeneratedDescription) {
Expand All @@ -472,7 +475,8 @@ export async function listenQueue(kv: Deno.Kv) {
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
76 changes: 53 additions & 23 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import { cors } from "npm:hono/cors";

import { dbPool } from "./config/postgres.ts";
import { getKvInstance, listenQueue, sendMessage } from "./config/deno-kv.ts";
import { openaiAssistant } from "./services/openai-assistant.ts";

const app = new Hono();
const kv = await getKvInstance();

app.use("*", cors({
origin: "*",
}));
app.use(
"*",
cors({
origin: "*",
}),
);

app.get("/api/properties", async (c: Context) => {
using client = await dbPool.connect();
Expand Down Expand Up @@ -131,7 +135,7 @@ app.get("/api/properties", async (c: Context) => {
addWhereCondition(
`p.building_size BETWEEN $${paramCounter} AND $${paramCounter + 1}`,
parseFloat(query.building_size_min),
parseFloat(query.building_size_max)
parseFloat(query.building_size_max),
);
}

Expand All @@ -140,7 +144,7 @@ app.get("/api/properties", async (c: Context) => {
addWhereCondition(
`p.floor_size BETWEEN $${paramCounter} AND $${paramCounter + 1}`,
parseFloat(query.floor_size_min),
parseFloat(query.floor_size_max)
parseFloat(query.floor_size_max),
);
}

Expand All @@ -149,7 +153,7 @@ app.get("/api/properties", async (c: Context) => {
addWhereCondition(
`p.lot_size BETWEEN $${paramCounter} AND $${paramCounter + 1}`,
parseFloat(query.lot_size_min),
parseFloat(query.lot_size_max)
parseFloat(query.lot_size_max),
);
}

Expand All @@ -158,7 +162,7 @@ app.get("/api/properties", async (c: Context) => {
addWhereCondition(
`p.ceiling_height BETWEEN $${paramCounter} AND $${paramCounter + 1}`,
parseFloat(query.ceiling_height_min),
parseFloat(query.ceiling_height_max)
parseFloat(query.ceiling_height_max),
);
}

Expand All @@ -167,7 +171,7 @@ app.get("/api/properties", async (c: Context) => {
addWhereCondition(
`p.no_of_bedrooms BETWEEN $${paramCounter} AND $${paramCounter + 1}`,
parseInt(query.no_of_bedrooms_min),
parseInt(query.no_of_bedrooms_max)
parseInt(query.no_of_bedrooms_max),
);
}

Expand All @@ -176,16 +180,18 @@ app.get("/api/properties", async (c: Context) => {
addWhereCondition(
`p.no_of_bathrooms BETWEEN $${paramCounter} AND $${paramCounter + 1}`,
parseInt(query.no_of_bathrooms_min),
parseInt(query.no_of_bathrooms_max)
parseInt(query.no_of_bathrooms_max),
);
}

// Add number of parking spaces range condition if both min and max are provided
if (query.no_of_parking_spaces_min && query.no_of_parking_spaces_max) {
addWhereCondition(
`p.no_of_parking_spaces BETWEEN $${paramCounter} AND $${paramCounter + 1}`,
`p.no_of_parking_spaces BETWEEN $${paramCounter} AND $${
paramCounter + 1
}`,
parseInt(query.no_of_parking_spaces_min),
parseInt(query.no_of_parking_spaces_max)
parseInt(query.no_of_parking_spaces_max),
);
}

Expand All @@ -198,10 +204,13 @@ app.get("/api/properties", async (c: Context) => {
let orderByClause = "";
const validSortFields = ["id", "created_at", "price"];
const validSortOrders = ["ASC", "DESC"];

if (validSortFields.includes(query.sort_by) &&
validSortOrders.includes(query.sort_order.toUpperCase())) {
orderByClause = `ORDER BY l.${query.sort_by} ${query.sort_order.toUpperCase()}`;

if (
validSortFields.includes(query.sort_by) &&
validSortOrders.includes(query.sort_order.toUpperCase())
) {
orderByClause =
`ORDER BY l.${query.sort_by} ${query.sort_order.toUpperCase()}`;
} else {
orderByClause = "ORDER BY l.created_at DESC";
}
Expand Down Expand Up @@ -322,13 +331,20 @@ app.get("/api/properties", async (c: Context) => {
app.get("/api/properties/valuation", async (c: Context) => {
const data = c.req.query();

if (!data.property_type_id || !data.size_in_sqm) {
return c.json({ error: "Property type and size are required" }, 400);
if (!data.property_type_id || !data.size_in_sqm || !data.city_id) {
return c.json({ error: "Property type, size and city are required" }, 400);
}

const propertyTypeId = parseInt(data.property_type_id);
if (propertyTypeId < 1 || propertyTypeId > 4) {
return c.json({ error: "Invalid property type ID. Must be between 1 and 4" }, 400);
return c.json({
error: "Invalid property type ID. Must be between 1 and 4",
}, 400);
}

const cityId = parseInt(data.city_id);
if (isNaN(cityId) || cityId < 1) {
return c.json({ error: "Invalid city ID" }, 400);
}

return c.json({ data: null });
Expand All @@ -337,7 +353,7 @@ app.get("/api/properties/valuation", async (c: Context) => {
app.get("/api/properties/cities", async (c: Context) => {
using client = await dbPool.connect();
const query = c.req.query();
const search = query.search || '';
const search = query.search || "";

const cities = await client.queryObject({
args: [`%${search}%`],
Expand All @@ -353,17 +369,18 @@ app.get("/api/properties/cities", async (c: Context) => {
WHERE LOWER(ct.city) LIKE LOWER($1)
ORDER BY ct.city ASC
LIMIT 10
`
`,
});

return c.json({
data: cities.rows
return c.json({
data: cities.rows,
});
});

app.get("/api/properties/:id", async (c: Context) => {
using client = await dbPool.connect();
const id = c.req.param("id");
const query = c.req.query();

if (!id) {
return c.json({ error: "Property ID is required" }, 400);
Expand Down Expand Up @@ -439,7 +456,20 @@ app.get("/api/properties/:id", async (c: Context) => {
`,
});

return c.json({ data: property.rows[0] });
const propertyData = property.rows[0] as any;

if (query.regenerate_ai_description === "true") {
const aiDescription = await openaiAssistant(JSON.stringify(propertyData));

propertyData.ai_generated_description = aiDescription;

await client.queryObject({
args: [propertyData.id, aiDescription],
text: `UPDATE Property SET ai_generated_description = $2 WHERE id = $1`,
});
}

return c.json({ data: propertyData });
});

app.post("/", async (c: Context) => {
Expand Down
2 changes: 1 addition & 1 deletion services/openai-assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const openaiAssistant = async (question: string) => {

const lastMessageForRun = messages.data
.filter(
(message) => message.run_id === run.id && message.role === "assistant"
(message) => message.run_id === run.id && message.role === "assistant",
)
.pop();

Expand Down

0 comments on commit f346d00

Please sign in to comment.