48 lines
1.1 KiB
JavaScript
48 lines
1.1 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const DATA_FILE = path.join(__dirname, '..', 'data', 'orders.json');
|
|
|
|
function readOrders() {
|
|
try {
|
|
const raw = fs.readFileSync(DATA_FILE, 'utf-8');
|
|
return JSON.parse(raw);
|
|
} catch {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
function writeOrders(orders) {
|
|
fs.writeFileSync(DATA_FILE, JSON.stringify(orders, null, 2), 'utf-8');
|
|
}
|
|
|
|
function findOrder(orderId) {
|
|
const orders = readOrders();
|
|
return orders.find(o => o.orderId === orderId) || null;
|
|
}
|
|
|
|
function saveOrder(order) {
|
|
const orders = readOrders();
|
|
const idx = orders.findIndex(o => o.orderId === order.orderId);
|
|
if (idx !== -1) {
|
|
orders[idx] = order;
|
|
} else {
|
|
orders.push(order);
|
|
}
|
|
writeOrders(orders);
|
|
}
|
|
|
|
function updateOrderStatus(orderId, status, transactionId) {
|
|
const orders = readOrders();
|
|
const order = orders.find(o => o.orderId === orderId);
|
|
if (order) {
|
|
order.status = status;
|
|
if (transactionId) order.transactionId = transactionId;
|
|
order.updatedAt = new Date().toISOString();
|
|
writeOrders(orders);
|
|
}
|
|
return order;
|
|
}
|
|
|
|
module.exports = { readOrders, findOrder, saveOrder, updateOrderStatus };
|