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:
@@ -238,9 +238,12 @@ export default class DBAdapter {
|
||||
'SELECT quantity FROM products WHERE id = ?', [productId]
|
||||
);
|
||||
|
||||
// TODO
|
||||
if (!product || product.quantity < quantity){
|
||||
return Promise.reject();
|
||||
const productRow = product?.[0];
|
||||
if (!productRow || productRow.quantity < quantity){
|
||||
return Promise.reject({
|
||||
type: DB_USER_ERROR,
|
||||
error: new Error("Insufficient product quantity")
|
||||
});
|
||||
}
|
||||
|
||||
const result = await this.#dbClient.query(
|
||||
@@ -249,7 +252,7 @@ export default class DBAdapter {
|
||||
);
|
||||
|
||||
await this.#dbClient.query(
|
||||
'UPDATE products SET quantity = quantity - ? WHERE id = >',
|
||||
'UPDATE products SET quantity = quantity - ? WHERE id = ?',
|
||||
[quantity, productId]
|
||||
);
|
||||
|
||||
@@ -267,37 +270,44 @@ export default class DBAdapter {
|
||||
'SELECT product_id, quantity FROM order_items WHERE id = ?', [itemId]
|
||||
);
|
||||
|
||||
// TODO
|
||||
if (!item){
|
||||
return Promise.reject();
|
||||
const itemRow = item?.[0];
|
||||
if (!itemRow){
|
||||
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){
|
||||
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){
|
||||
return Promise.reject();
|
||||
const productRow = product?.[0];
|
||||
if (!productRow || productRow.quantity < diff){
|
||||
return Promise.reject({
|
||||
type: DB_USER_ERROR,
|
||||
error: new Error("Insufficient product quantity")
|
||||
});
|
||||
}
|
||||
|
||||
await this.#dbClient.query(
|
||||
'UPDATE products SET quantity = quantity - ? WHERE id = >',
|
||||
[diff, item.productId]
|
||||
'UPDATE products SET quantity = quantity - ? WHERE id = ?',
|
||||
[diff, itemRow.product_id]
|
||||
);
|
||||
}
|
||||
|
||||
if (diff < 0) {
|
||||
await this.#dbClient.query(
|
||||
'UPDATE products SET quantity = quantity - ? WHERE id = >',
|
||||
[-diff, item.productId]
|
||||
'UPDATE products SET quantity = quantity + ? WHERE id = ?',
|
||||
[-diff, itemRow.product_id]
|
||||
);
|
||||
}
|
||||
|
||||
await this.#dbClient.query(
|
||||
'UPDATE order_items SET quantity = ? WHERE id = >',
|
||||
'UPDATE order_items SET quantity = ? WHERE id = ?',
|
||||
[quantity, itemId]
|
||||
);
|
||||
|
||||
@@ -315,14 +325,17 @@ export default class DBAdapter {
|
||||
'SELECT product_id, quantity FROM order_items WHERE id = ?', [itemId]
|
||||
);
|
||||
|
||||
// TODO
|
||||
if (!item){
|
||||
return Promise.reject();
|
||||
const itemRow = item?.[0];
|
||||
if (!itemRow){
|
||||
return Promise.reject({
|
||||
type: DB_USER_ERROR,
|
||||
error: new Error("Order item not found")
|
||||
});
|
||||
}
|
||||
|
||||
await this.#dbClient.query(
|
||||
'UPDATE products SET quantity = quantity + ? WHERE id = >',
|
||||
[item.quantity, item.productId]
|
||||
'UPDATE products SET quantity = quantity + ? WHERE id = ?',
|
||||
[itemRow.quantity, itemRow.product_id]
|
||||
);
|
||||
|
||||
await this.#dbClient.query(
|
||||
@@ -344,13 +357,16 @@ export default class DBAdapter {
|
||||
'SELECT id, order_id FROM order_items WHERE id = ?', [itemId]
|
||||
);
|
||||
|
||||
// TODO
|
||||
if (!item){
|
||||
return Promise.reject();
|
||||
const itemRow = item?.[0];
|
||||
if (!itemRow){
|
||||
return Promise.reject({
|
||||
type: DB_USER_ERROR,
|
||||
error: new Error("Order item not found")
|
||||
});
|
||||
}
|
||||
|
||||
await this.#dbClient.query(
|
||||
'UPDATE order_items SET order_id = ? WHERE id = >',
|
||||
'UPDATE order_items SET order_id = ? WHERE id = ?',
|
||||
[targetOrderId, itemId]
|
||||
);
|
||||
|
||||
|
||||
@@ -16,6 +16,8 @@ const {
|
||||
PASSWORD
|
||||
} = process.env;
|
||||
|
||||
const appPort = Number(PORT) || 3000;
|
||||
|
||||
const app = express();
|
||||
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) => {
|
||||
try {
|
||||
await adapter.deleteOrderItem(req.params.itemId);
|
||||
res.statusCode = 204;
|
||||
res.message = "OK";
|
||||
res.json({message: "success"});
|
||||
res.status(204).end();
|
||||
} catch (err){
|
||||
res.statusCode = 500;
|
||||
res.message = "WHOOPS";
|
||||
@@ -283,11 +283,11 @@ app.use((req, res) => {
|
||||
res.status(404).json({ error: 'Invalid route' });
|
||||
});
|
||||
|
||||
app.listen(3000, async() => {
|
||||
const server = app.listen(appPort, async() => {
|
||||
try {
|
||||
await adapter.connect();
|
||||
console.log(`✅ Server running on port 3000`);
|
||||
console.log(`📡 Local: http://localhost:3000`);
|
||||
console.log(`✅ Server running on port ${appPort}`);
|
||||
console.log(`📡 Local: http://localhost:${appPort}`);
|
||||
} catch(err){
|
||||
console.log("Shutting down application...");
|
||||
process.exit(100);
|
||||
@@ -296,7 +296,7 @@ app.listen(3000, async() => {
|
||||
|
||||
process.on('SIGTERM', () => {
|
||||
console.log("CLOSE APP");
|
||||
app.close( async() => {
|
||||
server.close(async () => {
|
||||
await adapter.disconnect();
|
||||
console.log("DB DISCONNECTED");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user