99 lines
3.5 KiB
TypeScript
99 lines
3.5 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";
|
|
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
|
|
});
|
|
|
|
await client.connect();
|
|
|
|
// Load SQL from file
|
|
const query = await Deno.readTextFile("sql/write_note.sql");
|
|
|
|
// Define a route to retrieve values from the database using parameters
|
|
router.get('/sales_walk/write_note/:bill_cust/:ship_cust/:bucket/:attainment/:notes', async (ctx) => {
|
|
|
|
const bucket = ctx.params.bucket; // Extract the bucket parameter from the route
|
|
const attainment= ctx.params.attainment; // Extract the bucket parameter from the route
|
|
const notes = ctx.params.notes; // Extract the bucket parameter from the route
|
|
const ship_cust = ctx.params.ship_cust; // Extract the ship_cust parameter from the route
|
|
const bill_cust = ctx.params.bill_cust; // Extract the ship_cust parameter from the route
|
|
|
|
//console.log(bucket)
|
|
//console.log(ship_cust)
|
|
|
|
const result = await client.queryObject({args: [ship_cust, bucket, notes], text: query} );
|
|
|
|
|
|
const currentTime = Date.now();
|
|
const dateObject = new Date(currentTime);
|
|
const currentDateTime = dateObject.toLocaleString(); //Output: 2/20/2023, 7:41:42 AM
|
|
console.log("posted: ", currentDateTime);
|
|
|
|
//ctx.response.body = result.rows;
|
|
ctx.response.body = currentDateTime;
|
|
|
|
});
|
|
|
|
router.post('/sales_walk/flag_cust', 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, bucket, attainment, notes } = body; // Destructure the needed values from the JSON
|
|
const result = await client.queryObject({args: [bill_cust, ship_cust, bucket, attainment, notes], 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:8086');
|
|
await app.listen({ port: 8086 });
|
|
|