update dasboard ,noti
This commit is contained in:
@@ -18,6 +18,7 @@ const { buildExportRequestWorkbook } = require('./export-request-workbook');
|
||||
const { ensureAssetExportDocumentSchema } = require('./asset-export-documents');
|
||||
const { ensureRobotSchema, createRobotRouter } = require('./robots');
|
||||
const { ensureRobotBomSchema, createRobotBomRouter } = require('./robot-bom');
|
||||
const { createAnnouncementRouter } = require('./announcements');
|
||||
const { getDocumentFormat, isPdfBuffer, validateDocumentFile, normalizeUploadedFileName,
|
||||
sanitizeDocumentFileName, getDocumentContentDisposition, accept: documentAccept } = require('./document-files');
|
||||
|
||||
@@ -3071,6 +3072,18 @@ async function createTables() {
|
||||
)
|
||||
END`,
|
||||
|
||||
`IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'NotificationPopupDismissals')
|
||||
BEGIN
|
||||
CREATE TABLE NotificationPopupDismissals (
|
||||
NotificationId BIGINT NOT NULL,
|
||||
UserId INT NOT NULL,
|
||||
DismissedAt DATETIME NOT NULL DEFAULT (DATEADD(HOUR, 7, SYSUTCDATETIME())),
|
||||
PRIMARY KEY (NotificationId, UserId),
|
||||
FOREIGN KEY (NotificationId) REFERENCES Notifications(NotificationId) ON DELETE CASCADE,
|
||||
FOREIGN KEY (UserId) REFERENCES Users(UserId) ON DELETE CASCADE
|
||||
)
|
||||
END`,
|
||||
|
||||
// AuditLog Table
|
||||
`IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'AuditLog')
|
||||
BEGIN
|
||||
@@ -4273,6 +4286,11 @@ async function safeCreateSystemNotification(payload) {
|
||||
// API ROUTES - Notifications
|
||||
// ==========================================
|
||||
|
||||
app.use('/api/notifications', createAnnouncementRouter({
|
||||
getPool: () => pool, sql, requireManage: requireAssetOrAdmin,
|
||||
getUserId: getUserIdFromRequest, getRole: getRequesterRole, sendInternalError
|
||||
}));
|
||||
|
||||
app.get('/api/notifications', async (req, res) => {
|
||||
try {
|
||||
const userId = getUserIdFromRequest(req);
|
||||
@@ -4360,7 +4378,7 @@ app.get('/api/notifications', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/notifications', requireAdmin, async (req, res) => {
|
||||
app.post('/api/notifications', requireAssetOrAdmin, async (req, res) => {
|
||||
try {
|
||||
const title = String(req.body?.title || '').trim();
|
||||
const message = String(req.body?.message || '').trim();
|
||||
@@ -4379,7 +4397,7 @@ app.post('/api/notifications', requireAdmin, async (req, res) => {
|
||||
if (rawActionUrl && !actionUrl) {
|
||||
return res.status(400).json({ success: false, message: 'Liên kết chỉ được dùng đường dẫn nội bộ bắt đầu bằng / hoặc #' });
|
||||
}
|
||||
if (expiresAt && (Number.isNaN(expiresAt.getTime()) || expiresAt.getTime() <= Date.now())) {
|
||||
if (!expiresAt || Number.isNaN(expiresAt.getTime()) || expiresAt.getTime() <= Date.now()) {
|
||||
return res.status(400).json({ success: false, message: 'Thời gian hết hạn phải ở tương lai' });
|
||||
}
|
||||
|
||||
@@ -4490,7 +4508,7 @@ app.put('/api/notifications/:id/read', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
app.delete('/api/notifications/:id', requireAdmin, async (req, res) => {
|
||||
app.delete('/api/notifications/:id', requireAssetOrAdmin, async (req, res) => {
|
||||
try {
|
||||
const notificationId = Number(req.params.id);
|
||||
if (!Number.isInteger(notificationId) || notificationId <= 0) {
|
||||
@@ -4499,10 +4517,13 @@ app.delete('/api/notifications/:id', requireAdmin, async (req, res) => {
|
||||
|
||||
const result = await pool.request()
|
||||
.input('notificationId', sql.BigInt, notificationId)
|
||||
.input('userId', sql.Int, getUserIdFromRequest(req))
|
||||
.input('isAdmin', sql.Bit, getRequesterRole(req) === 'admin')
|
||||
.query(`
|
||||
UPDATE Notifications
|
||||
SET IsActive = 0
|
||||
WHERE NotificationId = @notificationId AND Source = 'admin' AND IsActive = 1;
|
||||
WHERE NotificationId = @notificationId AND Source = 'admin' AND IsActive = 1
|
||||
AND (@isAdmin = 1 OR CreatedBy = @userId);
|
||||
SELECT @@ROWCOUNT AS Affected;
|
||||
`);
|
||||
|
||||
@@ -9721,6 +9742,10 @@ app.get('/api/database/info', requireAdmin, async (req, res) => {
|
||||
// Error Handling
|
||||
// ==========================================
|
||||
|
||||
app.use('/api', (req, res) => {
|
||||
res.status(404).json({ success: false, code: 'API_NOT_FOUND', message: 'API không tồn tại. Vui lòng cập nhật backend rồi thử lại.' });
|
||||
});
|
||||
|
||||
app.use((err, req, res, next) => {
|
||||
console.error('Unhandled request error:', err.message);
|
||||
if (err instanceof multer.MulterError) {
|
||||
|
||||
Reference in New Issue
Block a user