customer_review/api.ts

76 lines
2.5 KiB
TypeScript
Raw Normal View History

2023-12-04 12:09:51 -05: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";
import { load } from "https://deno.land/std/dotenv/mod.ts";
const app = new Application();
const router = new Router();
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: "customer review"
});
await client.connect();
// Load SQL from file
const query = await Deno.readTextFile("sql/write_note.sql");
router.post('/cust_review', async (ctx) => {
try {
const currentTime = new Date().toLocaleString('en-US', { timeZone: 'America/New_York' });
console.log("------------------------",currentTime,"-----------------------------")
const clientIp = ctx.request.ip;
console.log('Client IP:', clientIp);
//console.log('URL:', ctx.request.url);
//console.log('Method:', ctx.request.method);
// Log headers
//console.log('Headers:', [...ctx.request.headers].map(header => `${header[0]}: ${header[1]}`).join(', '));
// Get the body object to log the type
//const body = await ctx.request.body();
//console.log('Body Type:', body.type); // Logging the body type
// Log the body as text
const bodyText = await ctx.request.body({ type: 'text' }).value;
console.log('Body Text:', bodyText);
// If you expect a JSON body, you can then parse it
if (ctx.request.hasBody) {
const body = JSON.parse(bodyText);
console.log("Body JSON:", body);
const { bill_cust, ship_cust, expected, expecteduom} = body; // Destructure the needed values from the JSON
const result = await client.queryObject({args: [bill_cust, ship_cust, expected, expecteduom], text: query} );
}
} catch (error) {
console.error('Error:', error.message);
ctx.response.status = 500;
ctx.response.body = { error: 'Internal Server Error' };
}
});
app.use(router.routes());
app.use(router.allowedMethods());
// Start the server
console.log('Server is running on http://localhost:8087');
await app.listen({ port: 8087 });