price_api/api.ts

187 lines
7.1 KiB
TypeScript
Raw Normal View History

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;
2023-11-06 15:31:06 -05:00
let mostRelevantMarketSeason = null;
2023-11-06 15:03:33 -05:00
let highestMarketRelevanceLevel = -1;
2023-11-06 16:09:59 -05:00
let mostRelevantCustomerPriceEarly = null;
let mostRelevantCustomerPriceRecent = null;
2023-11-06 15:03:33 -05:00
let mostRelevantCustomerKey = null;
2023-11-06 16:09:59 -05:00
let mostRelevantCustomerSeasonEarly = null;
let mostRelevantCustomerSeasonRecent = null;
2023-11-06 15:03:33 -05:00
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
2023-11-06 15:08:01 -05:00
function setAnchors(items, channelFirstChar, v1ds, v0ds, histKey) {
2023-11-06 14:45:54 -05:00
for (let item of items) {
// Update the last_price with the most recent price
2023-11-06 20:22:25 -05:00
const years = Object.keys(item.season).map(Number).filter(year => year >= 2020);
2023-11-06 14:45:54 -05:00
if (years.length > 0) {
const recentYear = Math.max(...years.map(Number));
2023-11-06 16:09:59 -05:00
const earlyYear = Math.min(...years.map(Number));
2023-11-06 14:45:54 -05:00
const lastPrice = item.season[recentYear].price_usd;
2023-11-06 16:09:59 -05:00
const earlyPrice = item.season[earlyYear].price_usd;
2023-11-06 14:45:54 -05:00
item.last_price = lastPrice;
2023-11-06 16:09:59 -05:00
item.early_price = earlyPrice;
2023-11-06 15:31:06 -05:00
item.last_season = recentYear;
2023-11-06 16:09:59 -05:00
item.early_season = earlyYear;
2023-11-06 14:45:54 -05:00
} 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
2023-11-06 15:31:06 -05:00
if (marketRelevance > highestMarketRelevanceLevel) {
2023-11-06 15:03:33 -05:00
highestMarketRelevanceLevel = marketRelevance;
mostRelevantMarketPrice = item.last_price;
mostRelevantMarketKey = histKey;
2023-11-06 15:31:06 -05:00
mostRelevantMarketSeason = item.last_season; // Assuming 'season' is the key where the season info is stored
2023-11-06 15:03:33 -05:00
}
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
2023-11-06 15:31:06 -05:00
if (customerRelevance > highestCustomerRelevanceLevel) {
2023-11-06 15:03:33 -05:00
highestCustomerRelevanceLevel = customerRelevance;
2023-11-06 16:09:59 -05:00
mostRelevantCustomerPriceRecent = item.last_price;
mostRelevantCustomerPriceEarly = item.early_price;
2023-11-06 15:03:33 -05:00
mostRelevantCustomerKey = histKey;
2023-11-06 16:09:59 -05:00
mostRelevantCustomerSeasonRecent = item.last_season; // Assuming 'season' is the key where the season info is stored
mostRelevantCustomerSeasonEarly = item.early_season; // Assuming 'season' is the key where the season info is stored
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
2023-11-06 15:08:01 -05:00
setAnchors(doc.hist[key], doc.chan[0], doc.v1ds, doc.v0ds, key);
2023-11-06 14:45:54 -05:00
}
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,
2023-11-06 15:31:06 -05:00
histKey: mostRelevantMarketKey,
season: mostRelevantMarketSeason
2023-11-06 15:03:33 -05:00
};
}
// Assign the most relevant customer price and key to the top level of the document
2023-11-06 16:09:59 -05:00
if (mostRelevantCustomerPriceRecent !== null) {
2023-11-06 15:03:33 -05:00
doc.mostRelevantCustomerPriceInfo = {
2023-11-06 16:09:59 -05:00
price: mostRelevantCustomerPriceRecent,
price_early: mostRelevantCustomerPriceEarly,
2023-11-06 15:31:06 -05:00
histKey: mostRelevantCustomerKey,
2023-11-06 16:09:59 -05:00
season: mostRelevantCustomerSeasonRecent,
season_early: mostRelevantCustomerSeasonEarly
2023-11-06 14:45:54 -05:00
};
2023-11-06 14:26:45 -05:00
}
2023-11-06 14:45:54 -05:00
2023-11-06 15:18:25 -05:00
doc.targetPrice = doc.v1tp ?? doc.v0tp ?? null;
2023-11-06 15:31:06 -05:00
// Determine the anchor price and source
2023-11-06 16:09:59 -05:00
if (doc.targetPrice !== undefined && (mostRelevantCustomerPriceEarly === undefined || doc.targetPrice < mostRelevantCustomerPriceEarly)) {
2023-11-06 15:31:06 -05:00
doc.anchorPrice = doc.targetPrice;
2023-11-06 15:34:50 -05:00
doc.anchorSource = 'Target Price';
2023-11-06 16:09:59 -05:00
} else if (mostRelevantCustomerPriceEarly !== undefined) {
doc.anchorPrice = mostRelevantCustomerPriceEarly;
doc.anchorSource = mostRelevantCustomerSeasonEarly + ' Customer Price';
2023-11-06 15:31:06 -05:00
} else {
doc.anchorPrice = null;
doc.anchorSource = 'none'; // or any other default value you wish to indicate no anchor price was found
}
2023-11-06 15:18:25 -05:00
2023-11-07 01:12:34 -05:00
const inflation = Math.max(...Object.keys(doc.iidx).map(Number));
const inflationFactor = doc.iidx[inflation] + 1;
doc.inflationFactor = inflationFactor;
var calcPrice = doc.anchorPrice * doc.inflationFactor;
calcPrice = parseFloat(calcPrice.toFixed(5));
let finalReason = ""
if (calcPrice >= doc.list ) {
doc.calcCeiling = "Cap At List";
doc.finalPrice = doc.list;
finalReason = `${doc.anchorSource} ${doc.anchorPrice} x ${inflationFactor} = ${calcPrice} but cap at list ${doc.list}`;
} else {
doc.finalPrice = calcPrice
finalReason = `${doc.anchorSource} ${doc.anchorPrice} x ${inflationFactor} = ${calcPrice}`;
}
doc.finalReason = finalReason;
2023-11-06 14:45:54 -05:00
return doc;
2023-11-06 14:26:45 -05: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 });