Skip to content

Commit

Permalink
Merge pull request #9 from tokenguardio/fix/dashboard-elements-are-ad…
Browse files Browse the repository at this point in the history
…ded-properly

fix/elements can be specified while dashboard is created
  • Loading branch information
rrozek authored Mar 6, 2024
2 parents 93d3438 + 1a236f9 commit 30f5c0d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 18 deletions.
1 change: 0 additions & 1 deletion src/components/dashboard/dashboard.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/components/dashboard/dashboard.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ interface IWriteDashboard extends Document {
title?: string;
theme?: ITheme;
layout?: ILayoutItem[];
elements?: IDashboardElement[];
}

export { IDashboard, ILayoutItem, ITheme, IWriteDashboard };
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
95 changes: 79 additions & 16 deletions src/components/dashboard/dashboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,28 @@ import { IDashboardElement } from '@components/dashboard/dashboardElement/dashbo
import { IDashboardFilter } from './dashboardFilter/dashboardFilter.interface';

const create = async (dashboardData: IWriteDashboard): Promise<IDashboard> => {
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 (
Expand Down Expand Up @@ -71,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 30f5c0d

Please sign in to comment.