Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/elements can be specified while dashboard is created #9

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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