441 lines
12 KiB
TypeScript
441 lines
12 KiB
TypeScript
import panelModel from "../../V1Models/Vizualization/panelmodel.ts";
|
|
import zoneModel from "../../V1Models/Builder/zoneModel.ts";
|
|
import widgetModel from "../../V1Models/Vizualization/widgemodel.ts";
|
|
import {
|
|
existingProjectById,
|
|
existingUser,
|
|
LivingCurrentVersion,
|
|
} from "../helpers/v1projecthelperFns.ts";
|
|
interface IResult {
|
|
status: string;
|
|
data?: object;
|
|
}
|
|
interface IAddPanel {
|
|
organization: string;
|
|
zoneUuid: string;
|
|
panelOrder: string[];
|
|
userId: string;
|
|
projectId: string;
|
|
versionId: string;
|
|
}
|
|
|
|
interface IPanel {
|
|
organization: string;
|
|
zoneUuid: string;
|
|
panelName: string;
|
|
userId: string;
|
|
projectId: string;
|
|
versionId: string;
|
|
}
|
|
interface ILockedPanel {
|
|
organization: string;
|
|
zoneUuid: string;
|
|
lockedPanel: string[];
|
|
userId: string;
|
|
projectId: string;
|
|
versionId: string;
|
|
}
|
|
export const AddPanel = async (data: IAddPanel): Promise<IResult> => {
|
|
try {
|
|
const { organization, zoneUuid, panelOrder, userId, versionId, projectId } =
|
|
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,
|
|
projectId: projectId,
|
|
versionId: versionId,
|
|
});
|
|
if (!existingZone) return { status: "Zone not found" };
|
|
await zoneModel(organization).findOneAndUpdate(
|
|
{
|
|
zoneUuid: zoneUuid,
|
|
isArchive: false,
|
|
projectId: projectId,
|
|
versionId: versionId,
|
|
},
|
|
{ panelOrder: panelOrder },
|
|
{ new: true }
|
|
);
|
|
const existingPanels = await panelModel(organization).find({
|
|
zoneUuid: zoneUuid,
|
|
projectId: projectId,
|
|
versionId: versionId,
|
|
isArchive: false,
|
|
});
|
|
|
|
const existingPanelNames = existingPanels.map(
|
|
(panel) => panel.panelName as string
|
|
);
|
|
|
|
const missingPanels = panelOrder.filter(
|
|
(panelName: string) => !existingPanelNames.includes(panelName)
|
|
);
|
|
|
|
const createdPanels = [];
|
|
for (const panelName of missingPanels) {
|
|
const newPanel = await panelModel(organization).create({
|
|
zoneUuid: zoneUuid,
|
|
panelName: panelName,
|
|
projectId: projectId,
|
|
versionId: versionId,
|
|
widgets: [],
|
|
isArchive: false,
|
|
});
|
|
createdPanels.push(newPanel);
|
|
}
|
|
|
|
if (createdPanels.length === 0) {
|
|
return { status: "No new panels were created. All panels already exist" };
|
|
}
|
|
|
|
const zoneAndPanelData = await getZoneAndPanelData(
|
|
organization,
|
|
zoneUuid,
|
|
projectId,
|
|
versionId
|
|
);
|
|
if (!zoneAndPanelData) {
|
|
return zoneAndPanelData;
|
|
}
|
|
return { status: "Success", data: createdPanels };
|
|
} catch (error: unknown) {
|
|
if (error instanceof Error) {
|
|
return {
|
|
status: error.message,
|
|
};
|
|
} else {
|
|
return {
|
|
status: "An unexpected error occurred",
|
|
};
|
|
}
|
|
}
|
|
};
|
|
export const DelPanel = async (data: IPanel): Promise<IResult> => {
|
|
try {
|
|
const { organization, zoneUuid, versionId, panelName, userId, projectId } =
|
|
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,
|
|
versionId: versionId,
|
|
isArchive: false,
|
|
projectId: projectId,
|
|
});
|
|
if (!existingZone) return { status: "Zone not found" };
|
|
const existingPanel = await panelModel(organization).findOne({
|
|
zoneUuid: zoneUuid,
|
|
projectId: projectId,
|
|
versionId: versionId,
|
|
panelName: panelName,
|
|
isArchive: false,
|
|
});
|
|
if (!existingPanel) return { status: "Panel Already Deleted" };
|
|
|
|
await panelModel(organization).updateOne(
|
|
{
|
|
_id: existingPanel._id,
|
|
projectId: projectId,
|
|
versionId: versionId,
|
|
isArchive: false,
|
|
},
|
|
{ $set: { isArchive: true } }
|
|
);
|
|
const existingWidgets = await widgetModel(organization).find({
|
|
panelID: existingPanel._id,
|
|
projectId: projectId,
|
|
versionId: versionId,
|
|
isArchive: false,
|
|
});
|
|
|
|
for (const widgetData of existingWidgets) {
|
|
widgetData.isArchive = true;
|
|
await widgetData.save();
|
|
}
|
|
|
|
if (existingZone.panelOrder.includes(existingPanel.panelName)) {
|
|
await zoneModel(organization).updateOne(
|
|
{
|
|
_id: existingZone._id,
|
|
projectId: projectId,
|
|
versionId: versionId,
|
|
},
|
|
{ $pull: { panelOrder: existingPanel.panelName } }
|
|
);
|
|
}
|
|
const zoneAndPanelData = await getZoneAndPanelData(
|
|
organization,
|
|
zoneUuid,
|
|
projectId,
|
|
versionId
|
|
);
|
|
if (!zoneAndPanelData) {
|
|
return zoneAndPanelData;
|
|
}
|
|
return { status: "Success", data: zoneAndPanelData };
|
|
} catch (error: unknown) {
|
|
if (error instanceof Error) {
|
|
return {
|
|
status: error.message,
|
|
};
|
|
} else {
|
|
return {
|
|
status: "An unexpected error occurred",
|
|
};
|
|
}
|
|
}
|
|
};
|
|
export const ClearPanel = async (data: IPanel): Promise<IResult> => {
|
|
try {
|
|
const { organization, zoneUuid, panelName, versionId, userId, projectId } =
|
|
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,
|
|
versionId: versionId,
|
|
isArchive: false,
|
|
projectId: projectId,
|
|
});
|
|
if (!existingZone) return { status: "Zone not found" };
|
|
const existingPanel = await panelModel(organization).findOne({
|
|
zoneUuid: zoneUuid,
|
|
panelName: panelName,
|
|
versionId: versionId,
|
|
projectId: projectId,
|
|
isArchive: false,
|
|
});
|
|
if (!existingPanel) return { status: "Requested Panel not found" };
|
|
|
|
const existingWidgets = await widgetModel(organization).find({
|
|
panelID: existingPanel._id,
|
|
versionId: versionId,
|
|
projectId: projectId,
|
|
isArchive: false,
|
|
});
|
|
if (existingWidgets.length === 0) return { status: "No widgets to clear" };
|
|
|
|
const clearWidgetsofPanel = await widgetModel(organization).updateMany(
|
|
{
|
|
panelID: existingPanel._id,
|
|
isArchive: false,
|
|
versionId: versionId,
|
|
projectId: projectId,
|
|
},
|
|
{ isArchive: true }
|
|
);
|
|
const removeWidgetsInPanel = await panelModel(
|
|
organization
|
|
).findOneAndUpdate(
|
|
{ _id: existingPanel._id, isArchive: false },
|
|
{ $set: { widgets: [] } },
|
|
{ new: true }
|
|
);
|
|
if (!clearWidgetsofPanel && !removeWidgetsInPanel)
|
|
return { status: "Failed to clear widgets in panel" };
|
|
|
|
const zoneAndPanelData = await getZoneAndPanelData(
|
|
organization,
|
|
zoneUuid,
|
|
projectId,
|
|
versionId
|
|
);
|
|
if (!zoneAndPanelData) {
|
|
return zoneAndPanelData;
|
|
}
|
|
|
|
return {
|
|
status: "Success",
|
|
data: zoneAndPanelData,
|
|
};
|
|
} catch (error: unknown) {
|
|
if (error instanceof Error) {
|
|
return {
|
|
status: error.message,
|
|
};
|
|
} else {
|
|
return {
|
|
status: "An unexpected error occurred",
|
|
};
|
|
}
|
|
}
|
|
};
|
|
export const LockedPanel = async (data: ILockedPanel): Promise<IResult> => {
|
|
try {
|
|
const {
|
|
organization,
|
|
zoneUuid,
|
|
lockedPanel,
|
|
versionId,
|
|
userId,
|
|
projectId,
|
|
} = 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" };
|
|
else {
|
|
const updateLockedPanel = await zoneModel(organization).findOneAndUpdate(
|
|
{
|
|
zoneUuid: zoneUuid,
|
|
versionId: versionId,
|
|
projectId: projectId,
|
|
isArchive: false,
|
|
},
|
|
{
|
|
lockedPanel: lockedPanel,
|
|
},
|
|
{ new: true }
|
|
);
|
|
const zoneAndPanelData = await getZoneAndPanelData(
|
|
organization,
|
|
zoneUuid,
|
|
projectId,
|
|
versionId
|
|
);
|
|
if (!zoneAndPanelData) {
|
|
return zoneAndPanelData;
|
|
}
|
|
if (updateLockedPanel) {
|
|
return {
|
|
status: "Success",
|
|
data: zoneAndPanelData,
|
|
};
|
|
}
|
|
return { status: "locked panel not updated" };
|
|
}
|
|
} catch (error: unknown) {
|
|
if (error instanceof Error) {
|
|
return {
|
|
status: error.message,
|
|
};
|
|
} else {
|
|
return {
|
|
status: "An unexpected error occurred",
|
|
};
|
|
}
|
|
}
|
|
};
|
|
const getZoneAndPanelData = async (
|
|
organization: string,
|
|
zoneUuid: string,
|
|
projectId: string,
|
|
versionId: string
|
|
) => {
|
|
try {
|
|
const existingZone = await zoneModel(organization)
|
|
.findOne({
|
|
zoneUuid: zoneUuid,
|
|
isArchive: false,
|
|
projectId: projectId,
|
|
versionId: versionId,
|
|
})
|
|
.select(
|
|
"panelOrder zoneName zonePoints lockedPanel zoneUuid viewPortCenter viewPortposition"
|
|
);
|
|
if (!existingZone) {
|
|
return { status: "Zone not found" };
|
|
} else {
|
|
const panelData = await panelModel(organization).find({
|
|
zoneUuid: zoneUuid,
|
|
isArchive: false,
|
|
projectId: projectId,
|
|
versionId: versionId,
|
|
});
|
|
const zoneName = existingZone.zoneName as string;
|
|
|
|
const widgets = await Promise.all(
|
|
panelData.map(async (data) => {
|
|
const widgetDataArray = await widgetModel(organization).find({
|
|
panelID: data._id,
|
|
projectId: projectId,
|
|
versionId: versionId,
|
|
isArchive: false,
|
|
});
|
|
|
|
return widgetDataArray.map((widgetData) => ({
|
|
id: widgetData.widgetID,
|
|
type: widgetData.elementType,
|
|
title: widgetData.widgetName,
|
|
panel: widgetData.widgetside,
|
|
data: widgetData.Data || [],
|
|
}));
|
|
})
|
|
);
|
|
|
|
const flattenedWidgets = widgets.flat();
|
|
|
|
const objectData = {
|
|
zoneName,
|
|
viewPortposition: existingZone.viewPortposition,
|
|
zoneUuid: existingZone.zoneUuid,
|
|
viewPortCenter: existingZone.viewPortCenter,
|
|
activeSides: existingZone.panelOrder || [],
|
|
panelOrder: existingZone.panelOrder || [],
|
|
lockedPanels: existingZone.lockedPanel || [],
|
|
points: existingZone.zonePoints || [],
|
|
widgets: flattenedWidgets,
|
|
};
|
|
|
|
return { data: objectData };
|
|
}
|
|
} catch (error: unknown) {
|
|
return { status: "Panel not found" };
|
|
}
|
|
};
|