2023-08-25 13:51:57 -04:00
|
|
|
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();
|
|
|
|
|
2023-10-04 08:37:51 -04:00
|
|
|
//---------dotenv info-------------
|
|
|
|
import { load } from "https://deno.land/std/dotenv/mod.ts";
|
|
|
|
|
2023-08-25 13:51:57 -04:00
|
|
|
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"];
|
|
|
|
|
2023-10-04 08:37:51 -04:00
|
|
|
|
2023-08-25 13:51:57 -04:00
|
|
|
// Configure database connection
|
|
|
|
const client = new Client({
|
|
|
|
hostname:hostname
|
|
|
|
,port: port
|
|
|
|
,user: user
|
|
|
|
,password:password
|
|
|
|
,database:database
|
2023-10-04 08:37:51 -04:00
|
|
|
,applicationName: "pricing guidance"
|
2023-08-25 13:51:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
await client.connect();
|
|
|
|
|
2023-08-30 08:30:04 -04:00
|
|
|
// Load SQL from file
|
2023-11-03 14:34:27 -04:00
|
|
|
const query = await Deno.readTextFile("sql/get.pg.sql");
|
2023-08-30 08:30:04 -04:00
|
|
|
|
2023-11-03 21:16:26 -04:00
|
|
|
function apply_guidance(doc: any) {
|
2023-11-06 15:03:33 -05:00
|
|
|
let mostRelevantMarketPrice = null;
|
|
|
|
let mostRelevantMarketKey = null;
|
|
|
|
let highestMarketRelevanceLevel = -1;
|
|
|
|
|
|
|
|
let mostRelevantCustomerPrice = null;
|
|
|
|
let mostRelevantCustomerKey = null;
|
|
|
|
let highestCustomerRelevanceLevel = -1;
|
2023-11-06 13:58:28 -05:00
|
|
|
|
2023-11-06 14:45:54 -05:00
|
|
|
// Function to update price and assign relevance indicator
|
|
|
|
function updatePriceAndAssignRelevance(items, channelFirstChar, v1ds, v0ds, histKey) {
|
|
|
|
for (let item of items) {
|
|
|
|
// Update the last_price with the most recent price
|
|
|
|
const years = Object.keys(item.season);
|
|
|
|
if (years.length > 0) {
|
|
|
|
const recentYear = Math.max(...years.map(Number));
|
|
|
|
const lastPrice = item.season[recentYear].price_usd;
|
|
|
|
item.last_price = lastPrice;
|
|
|
|
} else {
|
|
|
|
item.last_price = null; // or some default value as appropriate
|
|
|
|
}
|
2023-11-06 13:58:28 -05:00
|
|
|
|
2023-11-06 14:45:54 -05:00
|
|
|
// Initialize relevance as numeric value
|
2023-11-06 15:03:33 -05:00
|
|
|
let marketRelevance = 0; // Assume 0 is 'not relevant'
|
|
|
|
let customerRelevance = 0; // Assume 0 is 'not relevant'
|
2023-11-06 14:45:54 -05:00
|
|
|
|
|
|
|
// Check if the first character of the item's channel matches the first character of the document's channel
|
|
|
|
if (item.chan.charAt(0) === channelFirstChar) {
|
2023-11-06 15:03:33 -05:00
|
|
|
marketRelevance = 1; // 'relevant'
|
2023-11-06 14:45:54 -05:00
|
|
|
|
|
|
|
// Further refine relevance based on v1ds and v0ds
|
|
|
|
if (item.v1ds === v1ds) {
|
2023-11-06 15:03:33 -05:00
|
|
|
marketRelevance = 2; // 'most relevant' because v1ds matches
|
|
|
|
// Check for customer relevance if 'cust' key exists
|
|
|
|
customerRelevance = item.cust ? 2 : 1
|
2023-11-06 14:45:54 -05:00
|
|
|
} else if (item.v0ds === v0ds) {
|
2023-11-06 15:03:33 -05:00
|
|
|
marketRelevance = marketRelevance === 2 ? 2 : 1; // Keep relevance as is if v1ds was matched, otherwise it's just 'relevant'
|
2023-11-06 14:45:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-06 15:03:33 -05:00
|
|
|
|
2023-11-06 14:45:54 -05:00
|
|
|
// Assign the calculated relevance to the item
|
2023-11-06 15:03:33 -05:00
|
|
|
item.marketRelevance = marketRelevance;
|
|
|
|
item.customerRelevance = customerRelevance;
|
|
|
|
|
|
|
|
// Update the most relevant market price if this item's relevance is higher and it doesn't have a 'cust' key
|
|
|
|
if (marketRelevance > highestMarketRelevanceLevel && !('cust' in item)) {
|
|
|
|
highestMarketRelevanceLevel = marketRelevance;
|
|
|
|
mostRelevantMarketPrice = item.last_price;
|
|
|
|
mostRelevantMarketKey = histKey;
|
|
|
|
}
|
2023-11-06 14:45:54 -05:00
|
|
|
|
2023-11-06 15:03:33 -05:00
|
|
|
// Update the most relevant customer price if this item's relevance is higher and it has a 'cust' key
|
|
|
|
if (customerRelevance > highestCustomerRelevanceLevel && 'cust' in item) {
|
|
|
|
highestCustomerRelevanceLevel = customerRelevance;
|
|
|
|
mostRelevantCustomerPrice = item.last_price;
|
|
|
|
mostRelevantCustomerKey = histKey;
|
2023-11-06 14:33:06 -05:00
|
|
|
}
|
2023-11-06 14:26:45 -05:00
|
|
|
}
|
2023-11-06 14:45:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
updatePriceAndAssignRelevance(doc.hist[key], doc.chan[0], doc.v1ds, doc.v0ds, key);
|
|
|
|
}
|
2023-11-06 14:26:45 -05:00
|
|
|
|
2023-11-06 15:03:33 -05:00
|
|
|
// Assign the most relevant market price and key to the top level of the document
|
|
|
|
if (mostRelevantMarketPrice !== null) {
|
|
|
|
doc.mostRelevantMarketPriceInfo = {
|
|
|
|
price: mostRelevantMarketPrice,
|
|
|
|
histKey: mostRelevantMarketKey
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign the most relevant customer price and key to the top level of the document
|
|
|
|
if (mostRelevantCustomerPrice !== null) {
|
|
|
|
doc.mostRelevantCustomerPriceInfo = {
|
|
|
|
price: mostRelevantCustomerPrice,
|
|
|
|
histKey: mostRelevantCustomerKey
|
2023-11-06 14:45:54 -05:00
|
|
|
};
|
2023-11-06 14:26:45 -05:00
|
|
|
}
|
2023-11-06 14:45:54 -05:00
|
|
|
|
|
|
|
return doc;
|
2023-11-06 14:26:45 -05:00
|
|
|
}
|
|
|
|
|
2023-11-06 14:45:54 -05:00
|
|
|
|
2023-11-06 13:58:28 -05:00
|
|
|
// 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;
|
|
|
|
}
|
2023-11-03 21:16:26 -04:00
|
|
|
}
|
|
|
|
|
2023-08-30 08:30:04 -04:00
|
|
|
// Define a route to retrieve values from the database using parameters
|
2023-11-03 16:11:32 -04:00
|
|
|
router.get('/code_price/:billcode/:shipcode/:partcode/:qty', async (ctx) => {
|
2023-08-30 08:30:04 -04:00
|
|
|
|
2023-11-03 16:11:32 -04:00
|
|
|
const partcode = ctx.params.partcode;
|
|
|
|
const billcode = ctx.params.billcode;
|
|
|
|
const shipcode = ctx.params.shipcode;
|
|
|
|
const qty = ctx.params.qty;
|
2023-08-30 08:30:04 -04:00
|
|
|
|
|
|
|
//console.log(partcode)
|
|
|
|
//console.log(customer)
|
2023-11-03 16:11:32 -04:00
|
|
|
const result = await client.queryObject({args: [billcode, shipcode, partcode, qty], text: query} );
|
2023-11-03 21:16:26 -04:00
|
|
|
const procd = apply_guidance(result.rows[0]["doc"])
|
|
|
|
ctx.response.body = procd
|
2023-08-25 13:51:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
app.use(router.routes());
|
|
|
|
app.use(router.allowedMethods());
|
|
|
|
|
|
|
|
// Start the server
|
2023-10-04 08:37:51 -04:00
|
|
|
console.log('Server is running on http://usmidsap02:8090');
|
|
|
|
await app.listen({ port: 8090 });
|
2023-08-25 13:51:57 -04:00
|
|
|
|