From 2651b40e45daeddc038c51e9de3be685b99de71c Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Mon, 6 Nov 2023 13:58:28 -0500 Subject: [PATCH] vault backup: 2023-11-06 13:58:27 --- api.ts | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) 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