433 lines
11 KiB
JavaScript
433 lines
11 KiB
JavaScript
const express = require('express');
|
|
const sqlite3 = require('sqlite3').verbose();
|
|
const path = require('path');
|
|
const app = express();
|
|
|
|
// Middleware
|
|
app.use(express.json());
|
|
app.use(express.static('public'));
|
|
|
|
// CORS middleware
|
|
app.use((req, res, next) => {
|
|
res.header('Access-Control-Allow-Origin', '*');
|
|
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
|
|
res.header('Access-Control-Allow-Headers', 'Content-Type');
|
|
if (req.method === 'OPTIONS') {
|
|
return res.sendStatus(200);
|
|
}
|
|
next();
|
|
});
|
|
|
|
// Initialize SQLite database
|
|
const db = new sqlite3.Database('./products.db', (err) => {
|
|
if (err) console.error(err.message);
|
|
console.log('Connected to products database.');
|
|
});
|
|
|
|
// Create tables
|
|
db.serialize(() => {
|
|
// Batches table
|
|
db.run(`CREATE TABLE IF NOT EXISTS batches (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
batch_name TEXT NOT NULL UNIQUE,
|
|
createdAt DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
)`);
|
|
|
|
// Items table (for valid items)
|
|
db.run(`CREATE TABLE IF NOT EXISTS items (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
batch_id INTEGER NOT NULL,
|
|
brand TEXT,
|
|
mpn TEXT NOT NULL,
|
|
mpn_custom TEXT,
|
|
sn TEXT NOT NULL,
|
|
createdAt DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (batch_id) REFERENCES batches(id) ON DELETE CASCADE
|
|
)`);
|
|
|
|
// Items_mix table (for mixed MPN items)
|
|
db.run(`CREATE TABLE IF NOT EXISTS items_mix (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
batch_id INTEGER NOT NULL,
|
|
brand TEXT,
|
|
mpn TEXT NOT NULL,
|
|
mpn_custom TEXT,
|
|
sn TEXT NOT NULL,
|
|
createdAt DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (batch_id) REFERENCES batches(id) ON DELETE CASCADE
|
|
)`);
|
|
|
|
// Create indexes for better performance
|
|
db.run(`CREATE INDEX IF NOT EXISTS idx_items_batch_id ON items(batch_id)`);
|
|
db.run(`CREATE INDEX IF NOT EXISTS idx_items_mix_batch_id ON items_mix(batch_id)`);
|
|
db.run(`CREATE INDEX IF NOT EXISTS idx_items_mpn ON items(mpn)`);
|
|
db.run(`CREATE INDEX IF NOT EXISTS idx_items_sn ON items(sn)`);
|
|
db.run(`CREATE INDEX IF NOT EXISTS idx_items_brand ON items(brand)`);
|
|
db.run(`CREATE INDEX IF NOT EXISTS idx_items_mix_brand ON items_mix(brand)`);
|
|
});
|
|
|
|
// ==================== BATCH API ROUTES ====================
|
|
|
|
function runAsync(db, sql, params = []) {
|
|
return new Promise((resolve, reject) => {
|
|
db.run(sql, params, function (err) {
|
|
if (err) reject(err);
|
|
else resolve(this); // this.lastID, this.changes
|
|
});
|
|
});
|
|
}
|
|
|
|
function prepareRunAsync(stmt, params = []) {
|
|
return new Promise((resolve, reject) => {
|
|
stmt.run(params, function (err) {
|
|
if (err) reject(err);
|
|
else resolve();
|
|
});
|
|
});
|
|
}
|
|
|
|
function finalizeAsync(stmt) {
|
|
return new Promise((resolve, reject) => {
|
|
stmt.finalize((err) => (err ? reject(err) : resolve()));
|
|
});
|
|
}
|
|
|
|
// Save batch with items and items_mix
|
|
app.post('/api/batch/save', async (req, res) => {
|
|
const { batch_name, items, items_mix } = req.body;
|
|
|
|
if (!batch_name) {
|
|
return res.status(400).json({ error: 'batch_name is required' });
|
|
}
|
|
|
|
if (!Array.isArray(items) || items.length === 0) {
|
|
return res.status(400).json({ error: 'items array is required and must not be empty' });
|
|
}
|
|
|
|
let insertedItems = 0;
|
|
let insertedMixItems = 0;
|
|
let errors = [];
|
|
|
|
try {
|
|
// BEGIN
|
|
await runAsync(db, 'BEGIN TRANSACTION');
|
|
|
|
// Insert batch
|
|
const batchResult = await runAsync(
|
|
db,
|
|
'INSERT INTO batches (batch_name) VALUES (?)',
|
|
[batch_name]
|
|
);
|
|
|
|
const batchId = batchResult.lastID;
|
|
|
|
// ===== Insert items =====
|
|
const itemStmt = db.prepare(
|
|
'INSERT INTO items (batch_id, brand, mpn, mpn_custom, sn) VALUES (?, ?, ?, ?, ?)'
|
|
);
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
const item = items[i];
|
|
|
|
if (!item.mpn || !item.sn) {
|
|
errors.push(`Item at index ${i} is missing mpn or sn`);
|
|
continue;
|
|
}
|
|
|
|
try {
|
|
await prepareRunAsync(itemStmt, [
|
|
batchId,
|
|
item.brand || null,
|
|
item.mpn,
|
|
item.mpn_custom || null,
|
|
item.sn
|
|
]);
|
|
insertedItems++;
|
|
} catch (err) {
|
|
errors.push(`Error inserting item at index ${i}: ${err.message}`);
|
|
}
|
|
}
|
|
|
|
await finalizeAsync(itemStmt);
|
|
|
|
// ===== Insert items_mix (optional) =====
|
|
if (Array.isArray(items_mix) && items_mix.length > 0) {
|
|
const mixStmt = db.prepare(
|
|
'INSERT INTO items_mix (batch_id, brand, mpn, mpn_custom, sn) VALUES (?, ?, ?, ?, ?)'
|
|
);
|
|
|
|
for (let i = 0; i < items_mix.length; i++) {
|
|
const item = items_mix[i];
|
|
|
|
if (!item.mpn || !item.sn) {
|
|
errors.push(`Mixed item at index ${i} is missing mpn or sn`);
|
|
continue;
|
|
}
|
|
|
|
try {
|
|
await prepareRunAsync(mixStmt, [
|
|
batchId,
|
|
item.brand || null,
|
|
item.mpn,
|
|
item.mpn_custom || null,
|
|
item.sn
|
|
]);
|
|
insertedMixItems++;
|
|
} catch (err) {
|
|
errors.push(`Error inserting mixed item at index ${i}: ${err.message}`);
|
|
}
|
|
}
|
|
|
|
await finalizeAsync(mixStmt);
|
|
}
|
|
|
|
// COMMIT
|
|
await runAsync(db, 'COMMIT');
|
|
|
|
return res.json({
|
|
success: true,
|
|
batch_id: batchId,
|
|
batch_name,
|
|
inserted_items: insertedItems,
|
|
inserted_mix_items: insertedMixItems,
|
|
errors: errors.length ? errors : undefined
|
|
});
|
|
|
|
} catch (err) {
|
|
// ROLLBACK nếu có lỗi nghiêm trọng
|
|
try {
|
|
await runAsync(db, 'ROLLBACK');
|
|
} catch (_) { }
|
|
|
|
return res.status(500).json({
|
|
error: err.message
|
|
});
|
|
}
|
|
});
|
|
|
|
|
|
// Get all batches with their items and items_mix
|
|
app.get('/api/batch/get-all', (req, res) => {
|
|
const page = parseInt(req.query.page) || 1;
|
|
const limit = parseInt(req.query.limit) || 50;
|
|
const search = req.query.search || '';
|
|
const sortBy = req.query.sortBy || 'id';
|
|
const sortOrder = req.query.sortOrder || 'DESC';
|
|
const offset = (page - 1) * limit;
|
|
|
|
// Validate sortBy
|
|
const validColumns = ['id', 'batch_name', 'createdAt'];
|
|
const column = validColumns.includes(sortBy) ? sortBy : 'id';
|
|
const order = sortOrder.toUpperCase() === 'ASC' ? 'ASC' : 'DESC';
|
|
|
|
let query = 'SELECT * FROM batches';
|
|
let countQuery = 'SELECT COUNT(*) as total FROM batches';
|
|
let params = [];
|
|
|
|
if (search) {
|
|
const searchCondition = `
|
|
WHERE
|
|
batch_name LIKE ?
|
|
OR id LIKE ?
|
|
OR EXISTS (
|
|
SELECT 1 FROM items
|
|
WHERE items.batch_id = batches.id
|
|
AND sn LIKE ?
|
|
)
|
|
OR EXISTS (
|
|
SELECT 1 FROM items_mix
|
|
WHERE items_mix.batch_id = batches.id
|
|
AND sn LIKE ?
|
|
)
|
|
`;
|
|
|
|
query += searchCondition;
|
|
countQuery += searchCondition;
|
|
|
|
const searchParam = `%${search}%`;
|
|
params = [searchParam, searchParam, searchParam, searchParam];
|
|
}
|
|
|
|
query += ` ORDER BY ${column} ${order} LIMIT ? OFFSET ?`;
|
|
params.push(limit, offset);
|
|
|
|
db.get(countQuery, search ? params.slice(0, 2) : [], (err, countRow) => {
|
|
if (err) {
|
|
return res.status(500).json({ error: err.message });
|
|
}
|
|
|
|
db.all(query, params, (err, batches) => {
|
|
if (err) {
|
|
return res.status(500).json({ error: err.message });
|
|
}
|
|
|
|
if (batches.length === 0) {
|
|
return res.json({
|
|
batches: [],
|
|
total: 0,
|
|
page,
|
|
limit,
|
|
totalPages: 0
|
|
});
|
|
}
|
|
|
|
// Get items for each batch
|
|
let processed = 0;
|
|
const batchesWithItems = [];
|
|
|
|
batches.forEach(batch => {
|
|
// Get items
|
|
db.all('SELECT brand, mpn, mpn_custom, sn, createdAt FROM items WHERE batch_id = ?', [batch.id], (err, items) => {
|
|
if (err) {
|
|
console.error('Error fetching items:', err);
|
|
items = [];
|
|
}
|
|
|
|
// Get items_mix
|
|
db.all('SELECT brand, mpn, mpn_custom, sn, createdAt FROM items_mix WHERE batch_id = ?', [batch.id], (err, items_mix) => {
|
|
if (err) {
|
|
console.error('Error fetching items_mix:', err);
|
|
items_mix = [];
|
|
}
|
|
|
|
batchesWithItems.push({
|
|
id: batch.id,
|
|
batch_name: batch.batch_name,
|
|
createdAt: batch.createdAt,
|
|
items: items || [],
|
|
items_mix: items_mix || []
|
|
});
|
|
|
|
processed++;
|
|
|
|
if (processed === batches.length) {
|
|
res.json({
|
|
batches: batchesWithItems,
|
|
total: countRow.total,
|
|
page,
|
|
limit,
|
|
totalPages: Math.ceil(countRow.total / limit)
|
|
});
|
|
}
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
// Get single batch by ID
|
|
app.get('/api/batch/get/:id', (req, res) => {
|
|
const id = req.params.id;
|
|
|
|
db.get('SELECT * FROM batches WHERE id = ?', [id], (err, batch) => {
|
|
if (err) {
|
|
return res.status(500).json({ error: err.message });
|
|
}
|
|
|
|
if (!batch) {
|
|
return res.status(404).json({ error: 'Batch not found' });
|
|
}
|
|
|
|
// Get items
|
|
db.all('SELECT brand, mpn, mpn_custom, sn, createdAt FROM items WHERE batch_id = ?', [id], (err, items) => {
|
|
if (err) {
|
|
return res.status(500).json({ error: err.message });
|
|
}
|
|
|
|
// Get items_mix
|
|
db.all('SELECT brand, mpn, mpn_custom, sn, createdAt FROM items_mix WHERE batch_id = ?', [id], (err, items_mix) => {
|
|
if (err) {
|
|
return res.status(500).json({ error: err.message });
|
|
}
|
|
|
|
res.json({
|
|
id: batch.id,
|
|
batch_name: batch.batch_name,
|
|
createdAt: batch.createdAt,
|
|
items: items || [],
|
|
items_mix: items_mix || []
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
// Delete batch (cascade delete items and items_mix)
|
|
app.delete('/api/batch/delete/:id', (req, res) => {
|
|
const id = req.params.id;
|
|
|
|
db.run('DELETE FROM batches WHERE id = ?', id, function (err) {
|
|
if (err) {
|
|
return res.status(500).json({ error: err.message });
|
|
}
|
|
|
|
if (this.changes === 0) {
|
|
return res.status(404).json({ error: 'Batch not found' });
|
|
}
|
|
|
|
res.json({ success: true, deleted: this.changes });
|
|
});
|
|
});
|
|
|
|
// Search items across all batches
|
|
app.get('/api/items/search', (req, res) => {
|
|
const search = req.query.q || '';
|
|
|
|
if (!search) {
|
|
return res.status(400).json({ error: 'Search query is required' });
|
|
}
|
|
|
|
const searchParam = `%${search}%`;
|
|
|
|
const query = `
|
|
SELECT
|
|
b.id as batch_id,
|
|
b.batch_name,
|
|
i.brand,
|
|
i.mpn,
|
|
i.mpn_custom,
|
|
i.sn,
|
|
i.createdAt,
|
|
'items' as type
|
|
FROM items i
|
|
JOIN batches b ON i.batch_id = b.id
|
|
WHERE i.mpn LIKE ? OR i.sn LIKE ? OR i.brand LIKE ? OR i.mpn_custom LIKE ?
|
|
UNION ALL
|
|
SELECT
|
|
b.id as batch_id,
|
|
b.batch_name,
|
|
im.brand,
|
|
im.mpn,
|
|
im.mpn_custom,
|
|
im.sn,
|
|
im.createdAt,
|
|
'items_mix' as type
|
|
FROM items_mix im
|
|
JOIN batches b ON im.batch_id = b.id
|
|
WHERE im.mpn LIKE ? OR im.sn LIKE ? OR im.brand LIKE ? OR im.mpn_custom LIKE ?
|
|
ORDER BY createdAt DESC
|
|
LIMIT 100
|
|
`;
|
|
|
|
db.all(query, [
|
|
searchParam, searchParam, searchParam, searchParam,
|
|
searchParam, searchParam, searchParam, searchParam
|
|
], (err, results) => {
|
|
if (err) {
|
|
return res.status(500).json({ error: err.message });
|
|
}
|
|
|
|
res.json({ results });
|
|
});
|
|
});
|
|
|
|
// Serve HTML UI
|
|
app.get('/', (req, res) => {
|
|
res.sendFile(path.join(__dirname, 'public', 'index.html'));
|
|
});
|
|
|
|
const PORT = process.env.PORT || 4444;
|
|
app.listen(PORT, () => {
|
|
console.log(`Server is running on http://localhost:${PORT}`);
|
|
}); |