Merge branch 'db_setting'
This commit is contained in:
39
server.js
39
server.js
@@ -706,16 +706,41 @@ app.delete('/api/system-users/:id', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/maps/save', (req, res) => {
|
||||
app.post('/api/maps/save', async (req, res) => {
|
||||
let connection;
|
||||
try {
|
||||
const { path, boxes } = req.body;
|
||||
if (!path) return res.status(400).json({ error: 'Path is required' });
|
||||
let config = {};
|
||||
if (fs.existsSync('map_config.json')) config = JSON.parse(fs.readFileSync('map_config.json', 'utf8') || '{}');
|
||||
config[path] = boxes;
|
||||
fs.writeFileSync('map_config.json', JSON.stringify(config, null, 2));
|
||||
res.json({ success: true });
|
||||
} catch (err) { handleError(res, err, 'SAVE MAPS'); }
|
||||
|
||||
// 1. Get old config to track movements
|
||||
let oldConfig = {};
|
||||
if (fs.existsSync('map_config.json')) {
|
||||
oldConfig = JSON.parse(fs.readFileSync('map_config.json', 'utf8') || '{}');
|
||||
}
|
||||
const oldBoxes = oldConfig[path] || [];
|
||||
|
||||
// 2. Save new config to file
|
||||
oldConfig[path] = boxes;
|
||||
fs.writeFileSync('map_config.json', JSON.stringify(oldConfig, null, 2));
|
||||
|
||||
// 3. Sync Database Assets (asset_location table)
|
||||
connection = await pool.getConnection();
|
||||
for (const box of boxes) {
|
||||
if (box.asset_id) {
|
||||
console.log(`Syncing asset ${box.asset_id} to new position: [${box.x}, ${box.y}]`);
|
||||
await connection.query(
|
||||
'UPDATE asset_location SET loc_x = ?, loc_y = ? WHERE asset_id = ? AND is_active = 1',
|
||||
[box.x, box.y, box.asset_id]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
res.json({ success: true, message: 'Map and Database synced successfully' });
|
||||
} catch (err) {
|
||||
handleError(res, err, 'SAVE MAPS SYNC');
|
||||
} finally {
|
||||
if (connection) connection.release();
|
||||
}
|
||||
});
|
||||
|
||||
// 7. File Upload API (Base64)
|
||||
|
||||
Reference in New Issue
Block a user