72 lines
2.2 KiB
TypeScript
72 lines
2.2 KiB
TypeScript
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) {
|
|
|
|
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;
|
|
}
|
|
|
|
// 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 });
|
|
|