Merge pull request #1 from Tsettaro/codex/check-server-side-code

Fix DB queries and errors, use configured PORT and graceful shutdown, restore Random_Increase
This commit is contained in:
Artur
2025-12-31 15:14:32 +03:00
committed by GitHub
2 changed files with 50 additions and 34 deletions

View File

@@ -238,9 +238,12 @@ export default class DBAdapter {
'SELECT quantity FROM products WHERE id = ?', [productId] 'SELECT quantity FROM products WHERE id = ?', [productId]
); );
// TODO const productRow = product?.[0];
if (!product || product.quantity < quantity){ if (!productRow || productRow.quantity < quantity){
return Promise.reject(); return Promise.reject({
type: DB_USER_ERROR,
error: new Error("Insufficient product quantity")
});
} }
const result = await this.#dbClient.query( const result = await this.#dbClient.query(
@@ -249,7 +252,7 @@ export default class DBAdapter {
); );
await this.#dbClient.query( await this.#dbClient.query(
'UPDATE products SET quantity = quantity - ? WHERE id = >', 'UPDATE products SET quantity = quantity - ? WHERE id = ?',
[quantity, productId] [quantity, productId]
); );
@@ -267,37 +270,44 @@ export default class DBAdapter {
'SELECT product_id, quantity FROM order_items WHERE id = ?', [itemId] 'SELECT product_id, quantity FROM order_items WHERE id = ?', [itemId]
); );
// TODO const itemRow = item?.[0];
if (!item){ if (!itemRow){
return Promise.reject(); return Promise.reject({
type: DB_USER_ERROR,
error: new Error("Order item not found")
});
} }
const diff = quantity - item.quantity; const diff = quantity - itemRow.quantity;
if (diff > 0){ if (diff > 0){
const product = await this.#dbClient.query( const product = await this.#dbClient.query(
'SELECT quantity FROM products WHERE id = ?', [item.productId] 'SELECT quantity FROM products WHERE id = ?', [itemRow.product_id]
); );
if (product.quantity < diff){ const productRow = product?.[0];
return Promise.reject(); if (!productRow || productRow.quantity < diff){
return Promise.reject({
type: DB_USER_ERROR,
error: new Error("Insufficient product quantity")
});
} }
await this.#dbClient.query( await this.#dbClient.query(
'UPDATE products SET quantity = quantity - ? WHERE id = >', 'UPDATE products SET quantity = quantity - ? WHERE id = ?',
[diff, item.productId] [diff, itemRow.product_id]
); );
} }
if (diff < 0) { if (diff < 0) {
await this.#dbClient.query( await this.#dbClient.query(
'UPDATE products SET quantity = quantity - ? WHERE id = >', 'UPDATE products SET quantity = quantity + ? WHERE id = ?',
[-diff, item.productId] [-diff, itemRow.product_id]
); );
} }
await this.#dbClient.query( await this.#dbClient.query(
'UPDATE order_items SET quantity = ? WHERE id = >', 'UPDATE order_items SET quantity = ? WHERE id = ?',
[quantity, itemId] [quantity, itemId]
); );
@@ -315,14 +325,17 @@ export default class DBAdapter {
'SELECT product_id, quantity FROM order_items WHERE id = ?', [itemId] 'SELECT product_id, quantity FROM order_items WHERE id = ?', [itemId]
); );
// TODO const itemRow = item?.[0];
if (!item){ if (!itemRow){
return Promise.reject(); return Promise.reject({
type: DB_USER_ERROR,
error: new Error("Order item not found")
});
} }
await this.#dbClient.query( await this.#dbClient.query(
'UPDATE products SET quantity = quantity + ? WHERE id = >', 'UPDATE products SET quantity = quantity + ? WHERE id = ?',
[item.quantity, item.productId] [itemRow.quantity, itemRow.product_id]
); );
await this.#dbClient.query( await this.#dbClient.query(
@@ -344,13 +357,16 @@ export default class DBAdapter {
'SELECT id, order_id FROM order_items WHERE id = ?', [itemId] 'SELECT id, order_id FROM order_items WHERE id = ?', [itemId]
); );
// TODO const itemRow = item?.[0];
if (!item){ if (!itemRow){
return Promise.reject(); return Promise.reject({
type: DB_USER_ERROR,
error: new Error("Order item not found")
});
} }
await this.#dbClient.query( await this.#dbClient.query(
'UPDATE order_items SET order_id = ? WHERE id = >', 'UPDATE order_items SET order_id = ? WHERE id = ?',
[targetOrderId, itemId] [targetOrderId, itemId]
); );
@@ -360,4 +376,4 @@ export default class DBAdapter {
return Promise.reject(); return Promise.reject();
} }
} }
} }

View File

@@ -16,6 +16,8 @@ const {
PASSWORD PASSWORD
} = process.env; } = process.env;
const appPort = Number(PORT) || 3000;
const app = express(); const app = express();
app.use(express.json()); app.use(express.json());
@@ -240,9 +242,7 @@ app.put('/api/orders/:orderId/items/:itemId', async (req, res) => {
app.delete('/api/orders/:orderId/items/:itemId', async (req, res) => { app.delete('/api/orders/:orderId/items/:itemId', async (req, res) => {
try { try {
await adapter.deleteOrderItem(req.params.itemId); await adapter.deleteOrderItem(req.params.itemId);
res.statusCode = 204; res.status(204).end();
res.message = "OK";
res.json({message: "success"});
} catch (err){ } catch (err){
res.statusCode = 500; res.statusCode = 500;
res.message = "WHOOPS"; res.message = "WHOOPS";
@@ -283,11 +283,11 @@ app.use((req, res) => {
res.status(404).json({ error: 'Invalid route' }); res.status(404).json({ error: 'Invalid route' });
}); });
app.listen(3000, async() => { const server = app.listen(appPort, async() => {
try { try {
await adapter.connect(); await adapter.connect();
console.log(`✅ Server running on port 3000`); console.log(`✅ Server running on port ${appPort}`);
console.log(`📡 Local: http://localhost:3000`); console.log(`📡 Local: http://localhost:${appPort}`);
} catch(err){ } catch(err){
console.log("Shutting down application..."); console.log("Shutting down application...");
process.exit(100); process.exit(100);
@@ -296,8 +296,8 @@ app.listen(3000, async() => {
process.on('SIGTERM', () => { process.on('SIGTERM', () => {
console.log("CLOSE APP"); console.log("CLOSE APP");
app.close( async() => { server.close(async () => {
await adapter.disconnect(); await adapter.disconnect();
console.log("DB DISCONNECTED"); console.log("DB DISCONNECTED");
}); });
}); });