Dynamic Dashboard with Widget Library
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
const express = require('express');
|
||||
const { all, one } = require('../db');
|
||||
const { all, now, one, parseJson, run } = require('../db');
|
||||
const { bookLedger } = require('../services/books');
|
||||
const { runReport } = require('../services/reportEngine');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
@@ -27,4 +29,45 @@ router.get('/dashboard', (req, res) => {
|
||||
res.json({ stats, byCategory, byStatus, upcomingTasks, auditTrail });
|
||||
});
|
||||
|
||||
// ---- Per-user dashboard layout (the drag-and-drop widget arrangement) --------
|
||||
|
||||
router.get('/dashboard/layout', (req, res) => {
|
||||
const row = one('SELECT layout_json FROM dashboard_layouts WHERE user_id = ?', [req.user.id]);
|
||||
res.json({ layout: row ? parseJson(row.layout_json, null) : null });
|
||||
});
|
||||
|
||||
router.put('/dashboard/layout', (req, res) => {
|
||||
const layout = Array.isArray(req.body.layout) ? req.body.layout : [];
|
||||
run(
|
||||
`INSERT INTO dashboard_layouts (user_id, layout_json, updated_at) VALUES (?, ?, ?)
|
||||
ON CONFLICT(user_id) DO UPDATE SET layout_json = excluded.layout_json, updated_at = excluded.updated_at`,
|
||||
[req.user.id, JSON.stringify(layout), now()]
|
||||
);
|
||||
res.json({ layout });
|
||||
});
|
||||
|
||||
// ---- Depreciation aggregator for the dashboard charts (one round-trip) -------
|
||||
// byBook: YTD + accumulated per enabled depreciation book; byCategory: accumulated by category (primary book).
|
||||
router.get('/dashboard/depreciation', (req, res) => {
|
||||
const cid = req.companyId || null;
|
||||
const year = Number(req.query.year) || new Date().getFullYear();
|
||||
const books = all(
|
||||
"SELECT code, is_primary FROM books WHERE (? IS NULL OR entity_id = ?) AND enabled = 1 AND book_type != 'maintenance' ORDER BY is_primary DESC, sort_order, id",
|
||||
[cid, cid]
|
||||
);
|
||||
const byBook = books.map((b) => {
|
||||
const ledger = bookLedger(b.code, year, cid);
|
||||
return { book: b.code, ytd: ledger ? ledger.summary.depreciation : 0, accumulated: ledger ? ledger.summary.accumulated : 0 };
|
||||
});
|
||||
const primary = (books.find((b) => b.is_primary) || books[0] || {}).code || 'GAAP';
|
||||
let byCategory = [];
|
||||
try {
|
||||
byCategory = runReport('depreciation_summary', { book: primary, year, entity_id: cid }).rows
|
||||
.map((r) => ({ category: r.category, accumulated: r.accumulated, depreciation: r.depreciation }));
|
||||
} catch {
|
||||
byCategory = [];
|
||||
}
|
||||
res.json({ year, book: primary, byBook, byCategory });
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user