Add dashboard functionality: Integrate dashboardRouter and UserDashboard model, enabling user-specific dashboard configurations. Update LoggedInView to support dynamic widget management, including adding, removing, and saving widget configurations, enhancing user experience and interactivity.
This commit is contained in:
37
backend/services/dashboardService.js
Normal file
37
backend/services/dashboardService.js
Normal file
@@ -0,0 +1,37 @@
|
||||
import BaseService from './BaseService.js';
|
||||
import UserDashboard from '../models/community/user_dashboard.js';
|
||||
|
||||
class DashboardService extends BaseService {
|
||||
/**
|
||||
* @param {string} hashedUserId
|
||||
* @returns {Promise<{ widgets: Array<{ id: string, title: string, endpoint: string }> }>}
|
||||
*/
|
||||
async getConfig(hashedUserId) {
|
||||
const user = await this.getUserByHashedId(hashedUserId);
|
||||
const row = await UserDashboard.findOne({ where: { userId: user.id } });
|
||||
const config = row?.config ?? { widgets: [] };
|
||||
if (!Array.isArray(config.widgets)) config.widgets = [];
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} hashedUserId
|
||||
* @param {{ widgets: Array<{ id: string, title: string, endpoint: string }> }} config
|
||||
*/
|
||||
async setConfig(hashedUserId, config) {
|
||||
const user = await this.getUserByHashedId(hashedUserId);
|
||||
const widgets = Array.isArray(config?.widgets) ? config.widgets : [];
|
||||
const sanitized = widgets.map(w => ({
|
||||
id: String(w?.id ?? ''),
|
||||
title: String(w?.title ?? ''),
|
||||
endpoint: String(w?.endpoint ?? '')
|
||||
})).filter(w => w.id && (w.title || w.endpoint));
|
||||
await UserDashboard.upsert({
|
||||
userId: user.id,
|
||||
config: { widgets: sanitized }
|
||||
}, { conflictFields: ['userId'] });
|
||||
return { widgets: sanitized };
|
||||
}
|
||||
}
|
||||
|
||||
export default new DashboardService();
|
||||
Reference in New Issue
Block a user