Files
Dwinzo-Backend-V0.0/src/shared/services/visualization/templateService.ts

408 lines
12 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 {
existingProjectById,
existingUser,
LivingCurrentVersion,
} 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;
versionId: string;
userId: string;
}
interface ITemplateToZone {
organization: string;
templateID: string;
projectId: string;
versionId: string;
zoneUuid: string;
userId: string;
}
interface ITemplate {
organization: string;
templateID: string;
projectId: string;
versionId: string;
userId: string;
}
interface IGetTemplate {
organization: string;
projectId: string;
versionId: string;
userId: string;
}
export const AddTemplate = async (data: IAddTemplate): Promise<IResult> => {
try {
const { organization, template, projectId, versionId, userId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) {
return { status: "User not found" };
}
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) {
return { status: "Project not found" };
}
const ExistingVersion = await LivingCurrentVersion(
organization,
LivingProject._id,
versionId
);
if (!ExistingVersion) return { status: "Version Data not found" };
const existingTemplate = await templateModel(organization).findOne({
templateID: template.id,
isArchive: false,
versionId: versionId,
projectId: projectId,
});
if (existingTemplate) return { status: "TemplateID alreay exists" };
const newTemplate = await templateModel(organization).create({
templateID: template.id,
versionId: versionId,
projectId: projectId,
templateName: template.name,
panelOrder: template.panelOrder,
widgets: template.widgets,
snapshot: template.snapshot,
floatWidgets: template.floatingWidget,
Widgets3D: template.Widgets3D,
});
if (newTemplate) {
const allTemplateDatas = await templateModel(organization)
.find({
isArchive: false,
versionId: versionId,
projectId: projectId,
})
.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 { 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, versionId, projectId, zoneUuid, userId } =
data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) return { status: "Project not found" };
const ExistingVersion = await LivingCurrentVersion(
organization,
LivingProject._id,
versionId
);
if (!ExistingVersion) return { status: "Version Data not found" };
const existingZone = await zoneModel(organization).findOne({
zoneUuid: zoneUuid,
isArchive: false,
versionId: versionId,
projectId: projectId,
});
if (!existingZone)
return {
status: "Zone not found ",
};
const existingTemplate = await templateModel(organization).findOne({
templateID: templateID,
isArchive: false,
versionId: versionId,
projectId: projectId,
});
if (!existingTemplate)
return {
status: "TemplateID not found",
};
if (existingZone.panelOrder.length > 0) {
existingZone.panelOrder = existingTemplate.panelOrder;
await existingZone.save();
const archivePanelDatas = await panelModel(organization).find({
zoneUuid,
versionId: versionId,
projectId: projectId,
isArchive: false,
});
for (const panelData of archivePanelDatas) {
await widgetModel(organization).deleteMany({
panelID: panelData._id,
versionId: versionId,
projectId: projectId,
isArchive: false,
});
}
await panelModel(organization).deleteMany({
zoneUuid,
versionId: versionId,
projectId: projectId,
isArchive: false,
});
await floatWidgetModel(organization).deleteMany({
zoneUuid,
versionId: versionId,
projectId: projectId,
isArchive: false,
});
}
existingZone.panelOrder = existingTemplate.panelOrder;
await existingZone.save();
const existingPanels = await panelModel(organization).find({
zoneUuid,
versionId: versionId,
projectId: projectId,
isArchive: false,
});
const existingPanelNames = existingPanels.map(
(panel) => panel.panelName as string
);
const missingPanels = existingTemplate.panelOrder.filter(
(panelName: string) => !existingPanelNames.includes(panelName)
);
await Promise.all(
missingPanels.map((panelName: any) =>
panelModel(organization).create({
zoneUuid,
versionId: versionId,
projectId: projectId,
panelName,
widgets: [],
isArchive: false,
})
)
);
for (const widgetData of existingTemplate.widgets) {
const addedExistingPanel = await panelModel(organization).findOne({
panelName: widgetData.panel,
versionId: versionId,
projectId: projectId,
zoneUuid,
isArchive: false,
});
if (!addedExistingPanel) continue;
const existingWidget = await widgetModel(organization).findOne({
panelID: addedExistingPanel._id,
versionId: versionId,
projectId: projectId,
widgetID: widgetData.id,
isArchive: false,
});
if (existingWidget) continue;
const newWidget = await widgetModel(organization).create({
widgetID: widgetData.id,
versionId: versionId,
projectId: projectId,
elementType: widgetData.type,
zoneUuid: zoneUuid,
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,
versionId: versionId,
projectId: projectId,
isArchive: false,
zoneUuid,
});
if (existingFloatWidget) continue;
await floatWidgetModel(organization).create({
className: floatData.className,
versionId: versionId,
projectId: projectId,
header: floatData.header,
floatWidgetID: floatData.id,
position: floatData.position,
per: floatData.per,
value: floatData.value,
zoneUuid,
});
}
const templateZoneDatas = {
template: {
id: existingTemplate.templateID,
name: existingTemplate.templateName,
panelOrder: existingTemplate.panelOrder,
widgets: existingTemplate.widgets,
snapshot: existingTemplate.snapshot,
floatingWidget: existingTemplate.floatWidgets,
},
zoneUuid: existingZone.zoneUuid,
zoneName: existingZone.zoneName,
};
return { status: "Success", data: templateZoneDatas };
} 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, versionId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) return { status: "Project not found" };
const ExistingVersion = await LivingCurrentVersion(
organization,
LivingProject._id,
versionId
);
if (!ExistingVersion) return { status: "Version Data not found" };
const existingTemplate = await templateModel(organization).findOne({
templateID: templateID,
isArchive: false,
versionId: versionId,
projectId: projectId,
});
if (existingTemplate) {
const newTemplate = await templateModel(organization).updateOne(
{
templateID: templateID,
isArchive: false,
versionId: versionId,
projectId: projectId,
},
{ $set: { isArchive: true } }
);
if (newTemplate) {
const TemplateDeleteData = existingTemplate.templateID;
return {
status: "Success",
data: TemplateDeleteData,
};
}
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,versionId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) return { status: "Project not found" };
const VersionGetId = versionId ? versionId : LivingProject.Present_version;
const ExistingVersion = await LivingCurrentVersion(
organization,
LivingProject._id,
VersionGetId
);
if (!ExistingVersion) return { status: "Version Data not found" };
const templateDatas = await templateModel(organization)
.find({
versionId: ExistingVersion._id,
projectId: projectId,
isArchive: false,
})
.select("-_id -__v -isArchive -createdAt -updatedAt");
if (!templateDatas) return { status: "All Datas" };
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 };
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};