194 lines
5.1 KiB
JavaScript
194 lines
5.1 KiB
JavaScript
import express from "express";
|
|
import dotenv from "dotenv"
|
|
import { randomUUID } from "crypto";
|
|
import DBAdapter, { getCurrentDate, setCurrentDate } from "./db/database.js";
|
|
import { DB_INTERNAL_ERROR, DB_USER_ERROR } from "./db/database.js";
|
|
|
|
dotenv.config({
|
|
path: './server/.env'
|
|
});
|
|
|
|
const {
|
|
HOST,
|
|
PORT,
|
|
DATABASE,
|
|
USER,
|
|
PASSWORD
|
|
} = process.env;
|
|
|
|
const app = express();
|
|
app.use(express.json());
|
|
|
|
const adapter = new DBAdapter({
|
|
dbHost: HOST,
|
|
dbPort: PORT,
|
|
dbName: DATABASE,
|
|
dbUserLogin: USER,
|
|
dbUserPassword: PASSWORD
|
|
});
|
|
|
|
function AddDays(dateString, days) {
|
|
let date = new Date(dateString);
|
|
date.setDate(date.getDate() + days);
|
|
return date.toISOString().slice(0, 10);
|
|
}
|
|
|
|
function Random_Increase() {
|
|
return Math.floor(Math.random() * 5) + 1;
|
|
}
|
|
|
|
app.get('/api/current-date', async (req, res) => {
|
|
let currentDate = await getCurrentDate();
|
|
res.json({ currentDate });
|
|
});
|
|
|
|
app.get('/api/products', async(req, res) => {
|
|
try {
|
|
const db_products = await adapter.getProducts();
|
|
|
|
const products = db_products.map(
|
|
( {id, name, quantity} ) => ({
|
|
ID: id,
|
|
pr_name: name,
|
|
count: quantity
|
|
})
|
|
);
|
|
res.statusCode = 200;
|
|
res.message = "OK";
|
|
res.json(products);
|
|
} catch (err){
|
|
res.statusCode = 500;
|
|
res.message = "WHOOPS";
|
|
res.json({
|
|
timeStamp: new Date().toISOString(),
|
|
statusCode: 500,
|
|
error : `${err}`
|
|
})
|
|
}
|
|
});
|
|
|
|
app.post('/api/current-date/advance', async (req, res) => {
|
|
let currentDate = await getCurrentDate();
|
|
try {
|
|
const raw_ids = await adapter.getOrdersByDate(currentDate);
|
|
const ids = raw_ids.map(item => item.id);
|
|
await adapter.deleteOrdersByIds(ids);
|
|
|
|
let nextDate = AddDays(currentDate, 1);
|
|
await setCurrentDate(nextDate);
|
|
res.json({ currentDate: nextDate });
|
|
} catch (err){
|
|
res.statusCode = 500;
|
|
res.message = "WHOOPS";
|
|
res.json({
|
|
timeStamp: new Date().toISOString(),
|
|
statusCode: 500,
|
|
error : `${err}`
|
|
})
|
|
}
|
|
});
|
|
|
|
app.get('/api/orders', async(req, res) => {
|
|
try {
|
|
const db_orders = await adapter.getOrders();
|
|
|
|
const orders = db_orders.map(
|
|
( {id, customer_name, order_date, status, total_amount, items} ) => ({
|
|
ID: id,
|
|
customer: customer_name,
|
|
date: order_date,
|
|
status: status,
|
|
total: total_amount,
|
|
items: items ? items.map(item => ({
|
|
product_id: item.product_id,
|
|
product_name: item.product_name || item.name,
|
|
quantity: item.quantity,
|
|
price: item.price
|
|
})) : []
|
|
})
|
|
);
|
|
|
|
res.statusCode = 200;
|
|
res.message = "OK";
|
|
res.json(orders);
|
|
|
|
} catch (err) {
|
|
res.statusCode = 500;
|
|
res.message = "WHOOPS";
|
|
res.json({
|
|
timeStamp: new Date().toISOString(),
|
|
statusCode: 500,
|
|
error: `${err}`
|
|
});
|
|
}
|
|
});
|
|
|
|
app.post('/api/orders', async (req, res) => {
|
|
let { customerName, orderDate } = req.body;
|
|
let id = randomUUID();
|
|
|
|
try {
|
|
await adapter.addOrder({
|
|
id,
|
|
customer_name: customerName,
|
|
orderDate
|
|
});
|
|
|
|
res.status(201).json({
|
|
id,
|
|
customerName,
|
|
orderDate
|
|
});
|
|
|
|
} catch (err) {
|
|
console.log('Error in POST /api/orders:', err);
|
|
switch(err.type) {
|
|
case DB_INTERNAL_ERROR:
|
|
res.status(500).json({
|
|
timeStamp: new Date().toISOString(),
|
|
statusCode: 500,
|
|
error: err.error?.message || err.message
|
|
});
|
|
break;
|
|
|
|
case DB_USER_ERROR:
|
|
res.status(400).json({
|
|
timeStamp: new Date().toISOString(),
|
|
statusCode: 400,
|
|
error: err.error?.message || err.message
|
|
});
|
|
break;
|
|
|
|
default:
|
|
res.status(500).json({
|
|
timeStamp: new Date().toISOString(),
|
|
statusCode: 500,
|
|
message: "Unknown error",
|
|
error: err.message
|
|
});
|
|
}
|
|
}
|
|
});
|
|
|
|
app.use((req, res) => {
|
|
res.status(404).json({ error: 'Invalid route' });
|
|
});
|
|
|
|
app.listen(3000, async() => {
|
|
try {
|
|
await adapter.connect();
|
|
console.log(`✅ Server running on port 3000`);
|
|
console.log(`📡 Local: http://localhost:3000`);
|
|
} catch(err){
|
|
console.log("Shutting down application...");
|
|
process.exit(100);
|
|
}
|
|
});
|
|
|
|
process.on('SIGTERM', () => {
|
|
console.log("CLOSE APP");
|
|
app.close( async() => {
|
|
await adapter.disconnect();
|
|
console.log("DB DISCONNECTED");
|
|
});
|
|
}); |