price_api/api.ts

64 lines
2.3 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";
import { apply_guidance } from './apply_guidance.ts';
2023-10-04 08:37:51 -04:00
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({
2023-11-07 08:38:24 -05:00
hostname:hostname
,port: port
,user: user
,password:password
,database:database
,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-11-08 13:20:53 -05:00
const query_dseg = await Deno.readTextFile("sql/get_dseg.pg.sql");
2023-08-30 08:30:04 -04:00
2023-11-08 13:20:53 -05:00
// exact scenario for existing codes
2023-11-15 13:47:00 -05:00
router.get('/code/:billcode/:shipcode/:partcode/:qty', async (ctx) => {
2023-11-10 16:08:54 -05:00
const { billcode, shipcode, partcode, qty } = ctx.params;
2023-11-03 16:11:32 -04:00
const result = await client.queryObject({args: [billcode, shipcode, partcode, qty], text: query} );
2023-11-10 16:08:54 -05:00
ctx.response.body = apply_guidance(result.rows[0]["doc"]);
2023-08-25 13:51:57 -04:00
});
2023-11-08 13:20:53 -05:00
// specific customres codes but generic style code and data segment to accomodate custom colors and branding
2023-11-15 13:47:00 -05:00
router.get('/dseg/:billcode/:shipcode/:stlc/:dseg/:qty', async (ctx) => {
2023-11-10 16:08:54 -05:00
const { billcode, shipcode, stlc, dseg, qty } = ctx.params;
2023-11-08 13:20:53 -05:00
const result = await client.queryObject({args: [billcode, shipcode, stlc, dseg, qty], text: query_dseg} );
2023-11-10 16:08:54 -05:00
ctx.response.body = apply_guidance(result.rows[0]["doc"]);
2023-11-08 13:20:53 -05:00
});
2023-11-15 13:47:00 -05:00
// specific customres codes but generic style code and data segment to accomodate custom colors and branding
router.get('/dseg_price/:billcode/:shipcode/:stlc/:dseg/:qty', async (ctx) => {
const { billcode, shipcode, stlc, dseg, qty } = ctx.params;
const result = await client.queryObject({args: [billcode, shipcode, stlc, dseg, qty], text: query_dseg} );
const ag = apply_guidance(result.rows[0]["doc"]);
ctx.response.body = ag.guidance.FinalReason;
});
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 });