sales_walk/api.ts

100 lines
3.6 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";
2023-08-30 09:10:39 -04:00
import { load } from "https://deno.land/std/dotenv/mod.ts";
2023-08-25 13:51:57 -04:00
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
2024-01-31 16:37:41 -05:00
,applicationName: "account status"
2023-08-25 13:51:57 -04:00
});
await client.connect();
2023-08-30 08:30:04 -04:00
// Load SQL from file
2023-09-01 08:29:11 -04:00
const query = await Deno.readTextFile("sql/write_note.sql");
2023-08-30 08:30:04 -04:00
// Define a route to retrieve values from the database using parameters
2023-10-18 15:00:33 -04:00
router.get('/sales_walk/write_note/:bill_cust/:ship_cust/:bucket/:attainment/:notes', async (ctx) => {
2023-08-30 08:30:04 -04:00
2023-09-01 08:29:11 -04:00
const bucket = ctx.params.bucket; // Extract the bucket parameter from the route
2023-10-18 15:00:33 -04:00
const attainment= ctx.params.attainment; // Extract the bucket parameter from the route
2023-09-01 08:29:11 -04:00
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
2023-10-18 15:00:33 -04:00
const bill_cust = ctx.params.bill_cust; // Extract the ship_cust parameter from the route
2023-08-30 08:30:04 -04:00
2023-09-01 08:29:11 -04:00
//console.log(bucket)
//console.log(ship_cust)
2023-08-30 08:30:04 -04:00
2023-09-01 08:29:11 -04:00
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;
2023-08-30 08:30:04 -04:00
2023-08-25 13:51:57 -04:00
});
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);
2023-10-24 16:22:00 -04:00
const { bill_cust, ship_cust, bucket, attainment, notes , fcnotes} = body; // Destructure the needed values from the JSON
const result = await client.queryObject({args: [bill_cust, ship_cust, bucket, attainment, notes, fcnotes], text: query} );
}
} catch (error) {
console.error('Error:', error.message);
ctx.response.status = 500;
ctx.response.body = { error: 'Internal Server Error' };
}
});
2023-08-25 13:51:57 -04:00
app.use(router.routes());
app.use(router.allowedMethods());
// Start the server
2023-10-18 15:00:33 -04:00
console.log('Server is running on http://localhost:8086');
await app.listen({ port: 8086 });
2023-08-25 13:51:57 -04:00