diff --git a/api.ts b/api.ts index a728519..4fa1a31 100644 --- a/api.ts +++ b/api.ts @@ -33,8 +33,10 @@ const query = await Deno.readTextFile("sql/get.pg.sql"); function apply_guidance(doc: any) { let mostRelevantMarketPrice = null; + let mostRelevantMarketPriceEarly = null; let mostRelevantMarketKey = null; let mostRelevantMarketSeason = null; + let mostRelevantMarketSeasonEarly = null; let highestMarketRelevanceLevel = -1; let mostRelevantCustomerPriceEarly = null; @@ -74,9 +76,10 @@ function apply_guidance(doc: any) { if (item.v1ds === v1ds) { marketRelevance = 2; // 'most relevant' because v1ds matches // Check for customer relevance if 'cust' key exists - customerRelevance = item.cust ? 2 : 1 + customerRelevance = item.cust ? 2 : 0 } else if (item.v0ds === v0ds) { marketRelevance = marketRelevance === 2 ? 2 : 1; // Keep relevance as is if v1ds was matched, otherwise it's just 'relevant' + customerRelevance = item.cust ? 1 : 0 } } @@ -89,8 +92,10 @@ function apply_guidance(doc: any) { if (marketRelevance > highestMarketRelevanceLevel) { highestMarketRelevanceLevel = marketRelevance; mostRelevantMarketPrice = item.last_price; + mostRelevantMarketPriceEarly = item.early_price; mostRelevantMarketKey = histKey; mostRelevantMarketSeason = item.last_season; // Assuming 'season' is the key where the season info is stored + mostRelevantMarketSeasonEarly = item.early_season; } // Update the most relevant customer price if this item's relevance is higher and it has a 'cust' key @@ -107,41 +112,43 @@ function apply_guidance(doc: any) { // Iterate over each key in the "hist" object for (let key of Object.keys(doc.hist)) { - // Update price and relevance for each item in the current key - setAnchors(doc.hist[key], doc.chan[0], doc.v1ds, doc.v0ds, key); + // Update price and relevance for each item in the current key + setAnchors(doc.hist[key], doc.chan[0], doc.v1ds, doc.v0ds, key); } // Assign the most relevant market price and key to the top level of the document if (mostRelevantMarketPrice !== null) { - doc.mostRelevantMarketPriceInfo = { - price: mostRelevantMarketPrice, - histKey: mostRelevantMarketKey, - season: mostRelevantMarketSeason - }; + doc.mostRelevantMarketPriceInfo = { + price: mostRelevantMarketPrice, + price_early: mostRelevantMarketPriceEarly, + histKey: mostRelevantMarketKey, + season: mostRelevantMarketSeason, + season_early: mostRelevantMarketSeasonEarly + }; } // Assign the most relevant customer price and key to the top level of the document if (mostRelevantCustomerPriceRecent !== null) { - doc.mostRelevantCustomerPriceInfo = { - price: mostRelevantCustomerPriceRecent, - price_early: mostRelevantCustomerPriceEarly, - histKey: mostRelevantCustomerKey, - season: mostRelevantCustomerSeasonRecent, - season_early: mostRelevantCustomerSeasonEarly - }; + doc.mostRelevantCustomerPriceInfo = { + price: mostRelevantCustomerPriceRecent, + price_early: mostRelevantCustomerPriceEarly, + histKey: mostRelevantCustomerKey, + season: mostRelevantCustomerSeasonRecent, + season_early: mostRelevantCustomerSeasonEarly + }; } doc.targetPrice = doc.v1tp ?? doc.v0tp ?? null; // Determine the anchor price and source if (doc.targetPrice !== undefined && (mostRelevantCustomerPriceEarly === undefined || doc.targetPrice < mostRelevantCustomerPriceEarly)) { - doc.anchorPrice = doc.targetPrice; - doc.anchorSource = 'Target Price'; + doc.anchorPrice = doc.targetPrice; + doc.anchorSource = 'Target Price'; } else if (mostRelevantCustomerPriceEarly !== undefined) { - doc.anchorPrice = mostRelevantCustomerPriceEarly; - doc.anchorSource = mostRelevantCustomerSeasonEarly + ' Customer Price'; + doc.anchorPrice = mostRelevantCustomerPriceEarly; + doc.anchorSource = mostRelevantCustomerSeasonEarly + ' Customer Price'; } else { - doc.anchorPrice = null; - doc.anchorSource = 'none'; // or any other default value you wish to indicate no anchor price was found + doc.anchorPrice = null; + doc.anchorSource = 'none'; // or any other default value you wish to indicate no anchor price was found } const inflation = Math.max(...Object.keys(doc.iidx).map(Number));