diff --git a/api.ts b/api.ts index 2e3cfe4..98c77df 100644 --- a/api.ts +++ b/api.ts @@ -32,19 +32,26 @@ await client.connect(); const query = await Deno.readTextFile("sql/get.pg.sql"); function apply_guidance(doc: any) { - - if (doc["hist"]["chan.mold.v0ds.vers"] && Array.isArray(doc["hist"]["chan.mold.v0ds.vers"])) { - // Loop through each element in the 'chan.mold.v0ds.vers' array - for (const element of doc["hist"]["chan.mold.v0ds.vers"]) { - // Process each element - 'element' is of type SeasonData - console.log(element); // Replace with actual processing logic - } - } else { - // Handle the case where 'chan.mold.v0ds.vers' is not an array or doesn't exist - console.error("'chan.mold.v0ds.vers' is not an array or does not exist in the document."); - } - return doc; + // Iterate over each key in the "hist" object + for (let key of Object.keys(doc.hist)) { + // Update each item in the current key with the most recent price + updateWithMostRecentPrice(doc.hist[key]); + } + return doc; +} + +// Function to update each item with the most recent price +function updateWithMostRecentPrice(items) { + for (let item of items) { + // Find the most recent price + //const lastPrice = findMostRecentPrice(item.season); + const years = Object.keys(item.season); + const recentYear = Math.max(...years); + const lastPrice = item.season[recentYear].price_usd; + // Append the last_price to the item + item.last_price = lastPrice; + } } // Define a route to retrieve values from the database using parameters