338 lines
10 KiB
TypeScript
338 lines
10 KiB
TypeScript
import templateModel from "../../V1Models/Vizualization/templatemodel.ts";
|
|
import panelModel from "../../V1Models/Vizualization/panelmodel.ts";
|
|
import zoneModel from "../../V1Models/Builder/zoneModel.ts";
|
|
import widgetModel from "../../V1Models/Vizualization/widgemodel.ts";
|
|
import floatWidgetModel from "../../V1Models/Vizualization/floatWidget.ts";
|
|
import { existingUser } from "../helpers/v1projecthelperFns.ts";
|
|
interface IResult {
|
|
status: string;
|
|
data?: object;
|
|
}
|
|
interface IAddTemplate {
|
|
organization: string;
|
|
template: {
|
|
id: string;
|
|
name: string;
|
|
snapshot: string;
|
|
panelOrder: [];
|
|
widgets: [];
|
|
floatingWidget: [];
|
|
Widgets3D: [];
|
|
};
|
|
projectId: string;
|
|
userId: string;
|
|
}
|
|
interface ITemplateToZone {
|
|
organization: string;
|
|
templateID: string;
|
|
projectId: string;
|
|
zoneId: string;
|
|
userId: string;
|
|
}
|
|
interface ITemplate {
|
|
organization: string;
|
|
templateID: string;
|
|
projectId: string;
|
|
userId: string;
|
|
}
|
|
interface IGetTemplate {
|
|
organization: string;
|
|
projectId: string;
|
|
userId: string;
|
|
}
|
|
export const AddTemplate = async (data: IAddTemplate): Promise<IResult> => {
|
|
try {
|
|
const { organization, template, projectId, userId } = data;
|
|
const UserExists = await existingUser(userId, organization);
|
|
if (!UserExists) return { status: "User not found" };
|
|
const existingTemplate = await templateModel(organization).findOne({
|
|
templateID: template.id,
|
|
isArchive: false,
|
|
projectId: projectId,
|
|
});
|
|
if (existingTemplate) return { status: "TemplateID alreay exists" };
|
|
// return res.json({ message: "TemplateID alreay exists" });
|
|
const newTemplate = await templateModel(organization).create({
|
|
templateID: template.id,
|
|
templateName: template.name,
|
|
panelOrder: template.panelOrder,
|
|
widgets: template.widgets,
|
|
snapshot: template.snapshot,
|
|
floatWidgets: template.floatingWidget,
|
|
Widgets3D: template.Widgets3D,
|
|
});
|
|
if (newTemplate) {
|
|
// return res.status(200).json({ message: "Template saved successfully" });
|
|
const allTemplateDatas = await templateModel(organization)
|
|
.find({ isArchive: false })
|
|
.select("-_id -__v -isArchive -createdAt -updatedAt");
|
|
|
|
const formattedTemplates = allTemplateDatas.map(async (data) => ({
|
|
id: data.templateID,
|
|
name: data.templateName,
|
|
panelOrder: data.panelOrder,
|
|
widgets: data.widgets,
|
|
floatingWidget: data.floatWidgets,
|
|
widgets3D: data.Widgets3D,
|
|
snapshot: data.snapshot,
|
|
}));
|
|
return { status: "Success", data: formattedTemplates };
|
|
// return {
|
|
// success: true,
|
|
// message: "Template saved successfully",
|
|
// data: formattedTemplates,
|
|
// organization: organization,
|
|
// };
|
|
}
|
|
return { status: "Template not saved" };
|
|
} catch (error: unknown) {
|
|
if (error instanceof Error) {
|
|
return {
|
|
status: error.message,
|
|
};
|
|
} else {
|
|
return {
|
|
status: "An unexpected error occurred",
|
|
};
|
|
}
|
|
}
|
|
};
|
|
export const AddTemplateToZone = async (
|
|
data: ITemplateToZone
|
|
): Promise<IResult> => {
|
|
try {
|
|
const { organization, templateID, projectId, zoneId, userId } = data;
|
|
const UserExists = await existingUser(userId, organization);
|
|
if (!UserExists) return { status: "User not found" };
|
|
const existingZone = await zoneModel(organization).findOne({
|
|
zoneId: zoneId,
|
|
isArchive: false,
|
|
projectId: projectId,
|
|
});
|
|
if (!existingZone)
|
|
return {
|
|
status: "Zone not found ",
|
|
};
|
|
// return {
|
|
// success: false,
|
|
// message: "Zone not found ",
|
|
// organization: organization,
|
|
// };
|
|
|
|
const existingTemplate = await templateModel(organization).findOne({
|
|
templateID: templateID,
|
|
isArchive: false,
|
|
projectId: projectId,
|
|
});
|
|
if (!existingTemplate)
|
|
return {
|
|
status: "TemplateID not found",
|
|
};
|
|
// return {
|
|
// success: false,
|
|
// message: "TemplateID not found",
|
|
// organization: organization,
|
|
// };
|
|
|
|
if (existingZone.panelOrder.length > 0) {
|
|
existingZone.panelOrder = existingTemplate.panelOrder;
|
|
await existingZone.save();
|
|
// Clear existing data before adding new data
|
|
const archivePanelDatas = await panelModel(organization).find({
|
|
zoneId,
|
|
isArchive: false,
|
|
});
|
|
for (const panelData of archivePanelDatas) {
|
|
await widgetModel(organization).deleteMany({
|
|
panelID: panelData._id,
|
|
isArchive: false,
|
|
});
|
|
}
|
|
await panelModel(organization).deleteMany({
|
|
zoneId,
|
|
isArchive: false,
|
|
});
|
|
await floatWidgetModel(organization).deleteMany({
|
|
zoneId,
|
|
isArchive: false,
|
|
});
|
|
}
|
|
existingZone.panelOrder = existingTemplate.panelOrder;
|
|
await existingZone.save();
|
|
const existingPanels = await panelModel(organization).find({
|
|
zoneId,
|
|
isArchive: false,
|
|
});
|
|
const existingPanelNames = existingPanels.map(
|
|
(panel: any) => panel.panelName
|
|
);
|
|
|
|
const missingPanels = existingTemplate.panelOrder.filter(
|
|
(panelName: string) => !existingPanelNames.includes(panelName)
|
|
);
|
|
const createdPanels = await Promise.all(
|
|
missingPanels.map((panelName: any) =>
|
|
panelModel(organization).create({
|
|
zoneId,
|
|
panelName,
|
|
widgets: [],
|
|
isArchive: false,
|
|
})
|
|
)
|
|
);
|
|
|
|
for (const widgetData of existingTemplate.widgets) {
|
|
const addedExistingPanel = await panelModel(organization).findOne({
|
|
panelName: widgetData.panel,
|
|
zoneId,
|
|
isArchive: false,
|
|
});
|
|
if (!addedExistingPanel) continue;
|
|
|
|
const existingWidget = await widgetModel(organization).findOne({
|
|
panelID: addedExistingPanel._id,
|
|
widgetID: widgetData.id,
|
|
isArchive: false,
|
|
});
|
|
if (existingWidget) continue;
|
|
|
|
const newWidget = await widgetModel(organization).create({
|
|
widgetID: widgetData.id,
|
|
elementType: widgetData.type,
|
|
zoneId: zoneId,
|
|
widgetName: widgetData.widgetName || "Widget",
|
|
panelID: addedExistingPanel._id,
|
|
widgetside: widgetData.panel,
|
|
});
|
|
addedExistingPanel.widgets.push(newWidget._id);
|
|
await addedExistingPanel.save();
|
|
}
|
|
|
|
for (const floatData of existingTemplate.floatWidgets) {
|
|
const existingFloatWidget = await floatWidgetModel(organization).findOne({
|
|
floatWidgetID: floatData.id,
|
|
isArchive: false,
|
|
zoneId,
|
|
});
|
|
if (existingFloatWidget) continue;
|
|
|
|
await floatWidgetModel(organization).create({
|
|
className: floatData.className,
|
|
header: floatData.header,
|
|
floatWidgetID: floatData.id,
|
|
position: floatData.position,
|
|
per: floatData.per,
|
|
value: floatData.value,
|
|
zoneId,
|
|
});
|
|
}
|
|
const templateZoneDatas = {
|
|
template: {
|
|
id: existingTemplate.templateID,
|
|
name: existingTemplate.templateName,
|
|
panelOrder: existingTemplate.panelOrder,
|
|
widgets: existingTemplate.widgets,
|
|
snapshot: existingTemplate.snapshot,
|
|
floatingWidget: existingTemplate.floatWidgets,
|
|
},
|
|
zoneId: existingZone.zoneId,
|
|
zoneName: existingZone.zoneName,
|
|
};
|
|
|
|
return { status: "Success", data: templateZoneDatas };
|
|
// return {
|
|
// success: true,
|
|
// message: "Template placed in Zone",
|
|
// data: templateZoneDatas,
|
|
// organization: organization,
|
|
// };
|
|
} catch (error: unknown) {
|
|
if (error instanceof Error) {
|
|
return {
|
|
status: error.message,
|
|
};
|
|
} else {
|
|
return {
|
|
status: "An unexpected error occurred",
|
|
};
|
|
}
|
|
}
|
|
};
|
|
export const TemplateDelete = async (data: ITemplate): Promise<IResult> => {
|
|
try {
|
|
const { templateID, projectId, userId, organization } = data;
|
|
const UserExists = await existingUser(userId, organization);
|
|
if (!UserExists) return { status: "User not found" };
|
|
const existingTemplate = await templateModel(organization).findOne({
|
|
templateID: templateID,
|
|
isArchive: false,
|
|
projectId: projectId,
|
|
});
|
|
if (existingTemplate) {
|
|
const newTemplate = await templateModel(organization).updateOne(
|
|
{ templateID: templateID, isArchive: false, projectId: projectId },
|
|
{ $set: { isArchive: true } }
|
|
);
|
|
if (newTemplate) {
|
|
const TemplateDeleteData = existingTemplate.templateID;
|
|
return {
|
|
status: "Success",
|
|
data: TemplateDeleteData,
|
|
};
|
|
// return {
|
|
// success: true,
|
|
// message: "Template deleted successfully",
|
|
// data: TemplateDeleteData,
|
|
// organization: organization,
|
|
// };
|
|
}
|
|
return { status: "Template not Deleted" };
|
|
}
|
|
return { status: "Template not found" };
|
|
} catch (error: unknown) {
|
|
if (error instanceof Error) {
|
|
return {
|
|
status: error.message,
|
|
};
|
|
} else {
|
|
return {
|
|
status: "An unexpected error occurred",
|
|
};
|
|
}
|
|
}
|
|
};
|
|
export const GetAllTemplates = async (data: IGetTemplate): Promise<IResult> => {
|
|
try {
|
|
const { organization, userId, projectId } = data;
|
|
const UserExists = await existingUser(userId, organization);
|
|
if (!UserExists) return { status: "User not found" };
|
|
const templateDatas = await templateModel(organization)
|
|
.find({ projectId: projectId, isArchive: false })
|
|
.select("-_id -__v -isArchive -createdAt -updatedAt");
|
|
if (!templateDatas) return { status: "All Datas" };
|
|
// return res.status(200).send([]);
|
|
|
|
const formattedTemplates = templateDatas.map((data) => ({
|
|
id: data.templateID,
|
|
name: data.templateName,
|
|
panelOrder: data.panelOrder,
|
|
widgets: data.widgets,
|
|
floatingWidget: data.floatWidgets,
|
|
widgets3D: data.Widgets3D,
|
|
snapshot: data.snapshot,
|
|
}));
|
|
return { status: "Success", data: formattedTemplates };
|
|
// return res.status(200).json(formattedTemplates);
|
|
} catch (error: unknown) {
|
|
if (error instanceof Error) {
|
|
return {
|
|
status: error.message,
|
|
};
|
|
} else {
|
|
return {
|
|
status: "An unexpected error occurred",
|
|
};
|
|
}
|
|
}
|
|
};
|