import { Injectable, NotFoundException } from '@nestjs/common'; import { DashboardRepository } from '../repository/dashboard.repository'; import { ModuleAccessService } from 'src/module/module/service/module-access.service'; @Injectable() export class DashboardService { constructor( private readonly dashboardRepo: DashboardRepository, private readonly moduleAccessService: ModuleAccessService, ) {} async getPages(mapped_entity_type: string, loggedInUser: any) { const pages = await this.dashboardRepo.getPages( mapped_entity_type, loggedInUser, ); const access = await this.moduleAccessService.getUserPermissions({ userId: loggedInUser.id, appcode: loggedInUser.appcode, level_type: loggedInUser.level_type, level_id: loggedInUser.level_id, enterprise_id: loggedInUser.enterprise_id, }); // Build a set of accessible codes const allowedCodes = new Set( access .filter((a) => a.action === 'VIEW' && a.access === 1) .map((a) => a.code), ); // Filter pages based on access const filteredPages = pages.filter((page) => allowedCodes.has(page.code)); return filteredPages; } async getDashboardPage(pageId: number, loggedInUser: any) { const enterpriseId = loggedInUser.enterprise_id; // 1. Get page data const page = await this.dashboardRepo.getDashboardPageData( pageId ); if (!page) throw new NotFoundException(`Dashboard Page ${pageId} not found`); const layout = page.layout_json || []; const widgetIds = layout.map((b: any) => Number(b.w_id)); // 2. Fetch widget details const widgets = await this.dashboardRepo.getWidgetsByIds( widgetIds, enterpriseId, ); // Convert to map const widgetMap = {}; widgets.forEach((w) => { widgetMap[w.id] = w; }); const { layout_json, ...pageData } = page; // 3. Return return { page_data: pageData, layout_json: layout, widget_json: widgetMap, }; } }