diff --git a/src/components/dashboard/dashboard.controller.ts b/src/components/dashboard/dashboard.controller.ts index 0e9cef0..2ccd044 100644 --- a/src/components/dashboard/dashboard.controller.ts +++ b/src/components/dashboard/dashboard.controller.ts @@ -54,7 +54,6 @@ const createDashboard = async (req: Request, res: Response) => { res.status(httpStatus.INTERNAL_SERVER_ERROR).send({ message: err.message }); } }; -// const getAllDashboards = async (req: Request, res: Response) => { try { diff --git a/src/components/dashboard/dashboard.interface.ts b/src/components/dashboard/dashboard.interface.ts index 86a9e71..b1118c2 100644 --- a/src/components/dashboard/dashboard.interface.ts +++ b/src/components/dashboard/dashboard.interface.ts @@ -38,6 +38,7 @@ interface IWriteDashboard extends Document { title?: string; theme?: ITheme; layout?: ILayoutItem[]; + elements?: IDashboardElement[]; } export { IDashboard, ILayoutItem, ITheme, IWriteDashboard }; diff --git a/src/components/dashboard/dashboard.model.ts b/src/components/dashboard/dashboard.model.ts index 925c43d..0742de5 100644 --- a/src/components/dashboard/dashboard.model.ts +++ b/src/components/dashboard/dashboard.model.ts @@ -32,7 +32,7 @@ const themeSchema = new mongoose.Schema({ const dashboardSchema = new mongoose.Schema({ title: { type: String, required: true }, elements: [{ type: mongoose.Schema.Types.ObjectId, ref: 'DashboardElement' }], - filters: [{type: mongoose.Schema.Types.ObjectId, ref: 'DashboardFilter'}], + filters: [{ type: mongoose.Schema.Types.ObjectId, ref: 'DashboardFilter' }], layout: [layoutSchema], theme: themeSchema, }); diff --git a/src/components/dashboard/dashboard.service.ts b/src/components/dashboard/dashboard.service.ts index 9608a04..51ee8fa 100644 --- a/src/components/dashboard/dashboard.service.ts +++ b/src/components/dashboard/dashboard.service.ts @@ -13,15 +13,28 @@ import { IDashboardElement } from '@components/dashboard/dashboardElement/dashbo import { IDashboardFilter } from './dashboardFilter/dashboardFilter.interface'; const create = async (dashboardData: IWriteDashboard): Promise => { - logger.info(`creating dashboard with data ${JSON.stringify(dashboardData)}`); - const newDashboard = await DashboardModel.create({ - elements: [], - layout: [], - filters: [], - ...dashboardData, - }); - logger.info(`Dashboard created: %O`, newDashboard); - return newDashboard; + logger.info(`Creating dashboard with data: ${JSON.stringify(dashboardData)}`); + + try { + const elementIds = await Promise.all( + dashboardData.elements.map(async (element) => { + const createdElement = + await dashboardElementService.createDashboardElement(element); + return createdElement._id; + }), + ); + + const newDashboard = await DashboardModel.create({ + ...dashboardData, + elements: elementIds, + }); + + logger.info(`Dashboard created successfully: %O`, newDashboard); + return newDashboard; + } catch (error) { + logger.error(`Error creating dashboard: %O`, error); + throw error; + } }; const read = async ( @@ -71,18 +84,68 @@ const getAll = async (): Promise => { const update = async ( dashboardId: string, - dashboard: IWriteDashboard, + dashboardData: IWriteDashboard, ): Promise => { - logger.debug('log dashboard', dashboard); - const updatedDashboard = await DashboardModel.findOneAndUpdate( - { _id: dashboardId }, - dashboard, + // Read existing dashboard data from the database + const existingDashboard = await read(dashboardId); + + if (!existingDashboard) { + throw new Error('Dashboard not found'); + } + + const updatedElementsIds = await Promise.all( + dashboardData.elements.map(async (element) => { + logger.info(`${element.id}`); + const existingElement = existingDashboard.elements.find( + (el) => el.id === element.id, + ); + + if (existingElement) { + await dashboardElementService.updateDashboardElement( + existingElement._id, + element, + ); + return existingElement._id; // Return the ID of existing element + } else { + const newElement = await dashboardElementService.createDashboardElement( + element, + ); + return newElement._id; // Return the ID of newly created element + } + }), + ); + + // Remove missing elements + const removedElementsIds = existingDashboard.elements + .filter( + (element) => + !dashboardData.elements.some( + (reqElement) => reqElement.id === element.id, + ), + ) + .map((element) => element._id); + + await Promise.all( + removedElementsIds.map(async (elementId) => { + await dashboardElementService.deleteDashboardElement(elementId); + return elementId; // Return the ID of removed element + }), + ); + + logger.info(`removedElementsIds ${removedElementsIds}`); + logger.info(`updatedElementsIds ${updatedElementsIds}`); + // Update the dashboard + const updatedDashboard = await DashboardModel.findByIdAndUpdate( + dashboardId, + { + ...dashboardData, + elements: updatedElementsIds, + }, { new: true }, ); - logger.debug(`log updatedDashboard', ${updatedDashboard}`); if (!updatedDashboard) { - throw new Error('Dashboard not found'); + throw new Error('Failed to update dashboard'); } logger.debug(`Dashboard updated: %O`, updatedDashboard);