import panelModel from "../../V1Models/Vizualization/panelmodel.ts"; import zoneModel from "../../V1Models/Builder/zoneModel.ts"; import widgetModel from "../../V1Models/Vizualization/widgemodel.ts"; import { existingProjectById, existingUser, } from "../helpers/v1projecthelperFns.ts"; interface IResult { status: string; data?: object; } interface IAddPanel { organization: string; zoneUuid: string; panelOrder: string[]; userId: string; projectId: string; } interface IPanel { organization: string; zoneUuid: string; panelName: string; userId: string; projectId: string; } interface ILockedPanel { organization: string; zoneUuid: string; lockedPanel: string[]; userId: string; projectId: string; } export const AddPanel = async (data: IAddPanel): Promise => { try { const { organization, zoneUuid, panelOrder, 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 existingZone = await zoneModel(organization).findOne({ zoneUuid: zoneUuid, isArchive: false, projectId: projectId, }); if (!existingZone) return { status: "Zone not found" }; await zoneModel(organization).findOneAndUpdate( { zoneUuid: zoneUuid, isArchive: false }, { panelOrder: panelOrder }, { new: true } ); const existingPanels = await panelModel(organization).find({ zoneUuid: zoneUuid, 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, 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 ); 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 => { try { const { organization, zoneUuid, 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 existingZone = await zoneModel(organization).findOne({ zoneUuid: zoneUuid, isArchive: false, projectId: projectId, }); if (!existingZone) return { status: "Zone not found" }; const existingPanel = await panelModel(organization).findOne({ zoneUuid: zoneUuid, panelName: panelName, isArchive: false, }); if (!existingPanel) return { status: "Panel Already Deleted" }; await panelModel(organization).updateOne( { _id: existingPanel._id, isArchive: false }, { $set: { isArchive: true } } ); const existingWidgets = await widgetModel(organization).find({ panelID: existingPanel._id, 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 }, { $pull: { panelOrder: existingPanel.panelName } } ); } const zoneAndPanelData = await getZoneAndPanelData( organization, zoneUuid, projectId ); 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 => { try { const { organization, zoneUuid, 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 existingZone = await zoneModel(organization).findOne({ zoneUuid: zoneUuid, isArchive: false, projectId: projectId, }); if (!existingZone) return { status: "Zone not found" }; const existingPanel = await panelModel(organization).findOne({ zoneUuid: zoneUuid, panelName: panelName, isArchive: false, }); if (!existingPanel) return { status: "Requested Panel not found" }; const existingWidgets = await widgetModel(organization).find({ panelID: existingPanel._id, isArchive: false, }); if (existingWidgets.length === 0) return { status: "No widgets to clear" }; const clearWidgetsofPanel = await widgetModel(organization).updateMany( { panelID: existingPanel._id, isArchive: false }, { 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 ); 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 => { try { const { organization, zoneUuid, lockedPanel, 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 existingZone = await zoneModel(organization).findOne({ zoneUuid: zoneUuid, isArchive: false, projectId: projectId, }); if (!existingZone) return { status: "Zone not found" }; else { const updateLockedPanel = await zoneModel(organization).findOneAndUpdate( { zoneUuid: zoneUuid, isArchive: false }, { lockedPanel: lockedPanel, }, { new: true } ); const zoneAndPanelData = await getZoneAndPanelData( organization, zoneUuid, projectId ); 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 ) => { try { const existingZone = await zoneModel(organization) .findOne({ zoneUuid: zoneUuid, isArchive: false, projectId: projectId, }) .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, }); const zoneName = existingZone.zoneName as string; const widgets = await Promise.all( panelData.map(async (data) => { const widgetDataArray = await widgetModel(organization).find({ panelID: data._id, 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" }; } };