Skip to content

Commit 605675d

Browse files
committed
elements can be specified while dashboard is created
1 parent 93d3438 commit 605675d

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

src/components/dashboard/dashboard.controller.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ const createDashboard = async (req: Request, res: Response) => {
5454
res.status(httpStatus.INTERNAL_SERVER_ERROR).send({ message: err.message });
5555
}
5656
};
57-
//
5857

5958
const getAllDashboards = async (req: Request, res: Response) => {
6059
try {

src/components/dashboard/dashboard.interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ interface IWriteDashboard extends Document {
3838
title?: string;
3939
theme?: ITheme;
4040
layout?: ILayoutItem[];
41+
elements?: IDashboardElement[];
4142
}
4243

4344
export { IDashboard, ILayoutItem, ITheme, IWriteDashboard };

src/components/dashboard/dashboard.service.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,28 @@ import { IDashboardElement } from '@components/dashboard/dashboardElement/dashbo
1313
import { IDashboardFilter } from './dashboardFilter/dashboardFilter.interface';
1414

1515
const create = async (dashboardData: IWriteDashboard): Promise<IDashboard> => {
16-
logger.info(`creating dashboard with data ${JSON.stringify(dashboardData)}`);
17-
const newDashboard = await DashboardModel.create({
18-
elements: [],
19-
layout: [],
20-
filters: [],
21-
...dashboardData,
22-
});
23-
logger.info(`Dashboard created: %O`, newDashboard);
24-
return newDashboard;
16+
logger.info(`Creating dashboard with data: ${JSON.stringify(dashboardData)}`);
17+
18+
try {
19+
const elementIds = await Promise.all(
20+
dashboardData.elements.map(async (element) => {
21+
const createdElement =
22+
await dashboardElementService.createDashboardElement(element);
23+
return createdElement._id;
24+
}),
25+
);
26+
27+
const newDashboard = await DashboardModel.create({
28+
...dashboardData,
29+
elements: elementIds,
30+
});
31+
32+
logger.info(`Dashboard created successfully: %O`, newDashboard);
33+
return newDashboard;
34+
} catch (error) {
35+
logger.error(`Error creating dashboard: %O`, error);
36+
throw error;
37+
}
2538
};
2639

2740
const read = async (

0 commit comments

Comments
 (0)