Skip to content

Commit

Permalink
handle dashboard update properly (still without filters)
Browse files Browse the repository at this point in the history
  • Loading branch information
rrozek committed Mar 6, 2024
1 parent 605675d commit 1a236f9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/components/dashboard/dashboard.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const themeSchema = new mongoose.Schema<ITheme>({
const dashboardSchema = new mongoose.Schema<IDashboard & Document>({
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,
});
Expand Down
64 changes: 57 additions & 7 deletions src/components/dashboard/dashboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,68 @@ const getAll = async (): Promise<IDashboard[]> => {

const update = async (
dashboardId: string,
dashboard: IWriteDashboard,
dashboardData: IWriteDashboard,
): Promise<IDashboard> => {
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);
Expand Down

0 comments on commit 1a236f9

Please sign in to comment.