import { Application, Router } from 'https://deno.land/x/oak/mod.ts'; import { Client } from "https://deno.land/x/postgres@v0.17.0/mod.ts"; const app = new Application(); const router = new Router(); //---------dotenv info------------- import { load } from "https://deno.land/std/dotenv/mod.ts"; const env = await load(); const hostname = env["HOSTNAME"]; const port = env["PORT"]; const user = env["USER"]; const password = env["PASSWORD"]; const database = env["DATABASE"]; const app_port = env["APP_PORT"]; // Configure database connection const client = new Client({ hostname:hostname ,port: port ,user: user ,password:password ,database:database ,applicationName: "pricing guidance" }); await client.connect(); // Load SQL from file const query = await Deno.readTextFile("sql/get.pg.sql"); function apply_guidance(doc: any) { // 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]); console.log(doc.chan[0]); assignRelevanceIndicator(doc.hist[key], doc.chan[0], doc.v1ds, doc.v0ds); } return doc; } function assignRelevanceIndicator(items, channelFirstChar, v1ds, v0ds) { for (let item of items) { // Check if the first character of the item's channel matches the first character of the document's channel let relevance = (item.chan === channelFirstChar) ? 'relevant' : 'not relevant'; // Further refine relevance based on v1ds and v0ds if (v1ds === item.v1ds && relevance === 'relevant') { relevance = 'most relevant'; // v1ds is more relevant than v0ds } else if (v0ds === item.v0ds && relevance === 'relevant') { relevance = 'relevant'; // v0ds is relevant but less so than v1ds } else { relevance = 'not relevant'; // Neither v1ds nor v0ds } // Assign the calculated relevance to the item item.relevance = relevance; } } // 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 router.get('/code_price/:billcode/:shipcode/:partcode/:qty', async (ctx) => { const partcode = ctx.params.partcode; const billcode = ctx.params.billcode; const shipcode = ctx.params.shipcode; const qty = ctx.params.qty; //console.log(partcode) //console.log(customer) const result = await client.queryObject({args: [billcode, shipcode, partcode, qty], text: query} ); const procd = apply_guidance(result.rows[0]["doc"]) ctx.response.body = procd }); app.use(router.routes()); app.use(router.allowedMethods()); // Start the server console.log('Server is running on http://usmidsap02:8090'); await app.listen({ port: 8090 });