diff --git a/src/api-server/Routes/floadWidgetRoute.ts b/src/api-server/Routes/floadWidgetRoute.ts index fe7f307..e226ee6 100644 --- a/src/api-server/Routes/floadWidgetRoute.ts +++ b/src/api-server/Routes/floadWidgetRoute.ts @@ -1,5 +1,5 @@ import * as express from "express"; -import { FloatWidgetService } from "../controller/visualization/FloatWidgetService.ts"; +import { FloatWidgetService } from "../controller/visualization/floatWidgetService.ts"; const router = express.Router(); router.post("/floatwidget/save", FloatWidgetService.addfloatWidget); router.patch("/floatwidget/delete", FloatWidgetService.deletefloatWidget); diff --git a/src/api-server/V1/v1Controllers/vizualizationController/v1floatWidgetController.ts b/src/api-server/V1/v1Controllers/vizualizationController/v1floatWidgetController.ts new file mode 100644 index 0000000..dba9ba9 --- /dev/null +++ b/src/api-server/V1/v1Controllers/vizualizationController/v1floatWidgetController.ts @@ -0,0 +1,306 @@ +import { Response } from "express"; +import { AuthenticatedRequest } from "../../../../shared/utils/token.ts"; +import { + AddFloat, + DelFloat, + DuplicateFloat, + GetFloatWidget, + SingleFloatWidget, +} from "../../../../shared/services/visualization/floatWidgetService.ts"; + +export const FloatAddController = async ( + req: AuthenticatedRequest, + res: Response +): Promise => { + try { + const { userId, organization } = req.user || {}; + const { widget, zoneId, index, projectId } = req.body; + if ( + !userId || + !organization || + !widget || + !zoneId || + !index || + !projectId + ) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const result = await AddFloat({ + organization, + widget, + zoneId, + index, + projectId, + userId, + }); + + switch (result.status) { + case "User not found": + res.status(404).json({ + message: "User not found", + }); + break; + case "Zone not found for the zoneId": + res.status(404).json({ + message: "Zone not found for the zoneId", + }); + break; + case "Widget updated successfully": + res.status(200).json({ + message: "Widget updated successfully", + }); + break; + case "Failed to create FloatWidget": + res.status(400).json({ + message: "Failed to create FloatWidget", + }); + break; + case "Success": + res.status(200).json({ + message: "FloatWidget created successfully", + }); + break; + default: + res.status(500).json({ + message: "Internal server error", + }); + break; + } + } catch (error) { + res.status(500).json({ + message: "Unknown error", + }); + return; + } +}; +export const DeleteFloatController = async ( + req: AuthenticatedRequest, + res: Response +): Promise => { + try { + const { userId, organization } = req.user || {}; + const { floatWidgetID, projectId, zoneId } = req.body; + if (!userId || !organization || !floatWidgetID || !projectId || !zoneId) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const result = await DelFloat({ + organization, + floatWidgetID, + zoneId, + projectId, + userId, + }); + switch (result.status) { + case "User not found": + res.status(404).json({ + message: "User not found", + }); + break; + case "Zone not found for the zoneId": + res.status(404).json({ + message: "Zone not found for the zoneId", + }); + break; + case "FloatWidget not found for the Id": + res.status(404).json({ + message: "FloatWidget not found for the Id", + }); + break; + case "FloatWidget not deleted": + res.status(200).json({ + message: "FloatWidget not deleted", + }); + break; + case "Success": + res.status(200).json({ + message: "FloatingWidget deleted successfully", + }); + break; + default: + res.status(500).json({ + message: "Internal server error", + }); + break; + } + } catch (error) { + res.status(500).json({ + message: "Unknown error", + }); + return; + } +}; +export const DuplicateFloatController = async ( + req: AuthenticatedRequest, + res: Response +): Promise => { + try { + const { userId, organization } = req.user || {}; + const { widget, projectId, zoneId, index } = req.body; + if ( + !userId || + !organization || + !widget || + !projectId || + !zoneId || + !index + ) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const result = await DuplicateFloat({ + organization, + widget, + zoneId, + index, + projectId, + userId, + }); + switch (result.status) { + case "User not found": + res.status(404).json({ + message: "User not found", + }); + break; + case "Zone not found for the zoneId": + res.status(404).json({ + message: "Zone not found for the zoneId", + }); + break; + case "FloatWidget update unsuccessfull": + res.status(200).json({ + message: "FloatWidget update unsuccessfull", + }); + break; + case "Widget updated successfully": + res.status(200).json({ + message: "Widget updated successfully", + }); + break; + case "Failed to duplicate FloatWidget": + res.status(200).json({ + message: "Failed to duplicate FloatWidget", + }); + break; + case "Success": + res.status(200).json({ + message: "Duplicate FloatWidget created successfully", + }); + break; + default: + res.status(500).json({ + message: "Internal server error", + }); + break; + } + } catch (error) { + res.status(500).json({ + message: "Unknown error", + }); + return; + } +}; +export const GetFloatController = async ( + req: AuthenticatedRequest, + res: Response +): Promise => { + try { + const { userId, organization } = req.user || {}; + const { projectId, zoneId } = req.params; + if (!userId || !organization || !projectId || !zoneId) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const result = await GetFloatWidget({ + organization, + zoneId, + projectId, + userId, + }); + switch (result.status) { + case "User not found": + res.status(404).json({ + message: "User not found", + }); + break; + case "Zone not found": + res.status(404).json({ + message: "Zone not found", + }); + break; + case "All Datas": + res.status(200).json([]); + break; + case "Success": + res.status(200).json(result.data); + break; + default: + res.status(500).json({ + message: "Internal server error", + }); + break; + } + } catch (error) { + res.status(500).json({ + message: "Unknown error", + }); + return; + } +}; +export const SingleFloatController = async ( + req: AuthenticatedRequest, + res: Response +): Promise => { + try { + const { userId, organization } = req.user || {}; + const { floatWidgetID } = req.params; + if (!userId || !organization || !floatWidgetID) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const result = await SingleFloatWidget({ + organization, + floatWidgetID, + userId, + }); + switch (result.status) { + case "User not found": + res.status(404).json({ + message: "User not found", + }); + break; + case "Widget not found": + res.status(404).json({ + message: "Widget not found", + }); + break; + + case "Success": + res.status(200).json({ + Data: result.data, + }); + break; + default: + res.status(500).json({ + message: "Internal server error", + }); + break; + } + } catch (error) { + res.status(500).json({ + message: "Unknown error", + }); + return; + } +}; diff --git a/src/api-server/V1/v1Controllers/vizualizationController/v1panelController.ts b/src/api-server/V1/v1Controllers/vizualizationController/v1panelController.ts new file mode 100644 index 0000000..12adb8b --- /dev/null +++ b/src/api-server/V1/v1Controllers/vizualizationController/v1panelController.ts @@ -0,0 +1,232 @@ +import { Response } from "express"; +import { AuthenticatedRequest } from "../../../../shared/utils/token.ts"; +import { + AddPanel, + ClearPanel, + DelPanel, + LockedPanel, +} from "../../../../shared/services/visualization/panelService.ts"; + +export const AddPanelController = async ( + req: AuthenticatedRequest, + res: Response +): Promise => { + try { + const { userId, organization } = req.user || {}; + const { panelOrder, zoneId, projectId } = req.body; + if (!userId || !organization || !panelOrder || !zoneId || !projectId) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const result = await AddPanel({ + organization, + panelOrder, + zoneId, + projectId, + userId, + }); + + switch (result.status) { + case "User not found": + res.status(404).json({ + message: "User not found", + }); + break; + case "Zone not found": + res.status(404).json({ + message: "Zone not found", + }); + break; + case "No new panels were created. All panels already exist": + res.status(200).json({ + message: "No new panels were created. All panels already exist", + }); + break; + case "Success": + res.status(200).json({ + message: "Panels created successfully", + panelID: result.data, + }); + break; + default: + res.status(500).json({ + message: "Internal server error", + }); + break; + } + } catch (error) { + res.status(500).json({ + message: "Unknown error", + }); + return; + } +}; +export const DeletePanelController = async ( + req: AuthenticatedRequest, + res: Response +): Promise => { + try { + const { userId, organization } = req.user || {}; + const { panelName, projectId, zoneId } = req.body; + if (!userId || !organization || !panelName || !projectId || !zoneId) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const result = await DelPanel({ + organization, + panelName, + zoneId, + projectId, + userId, + }); + switch (result.status) { + case "User not found": + res.status(404).json({ + message: "User not found", + }); + break; + case "Zone not found": + res.status(404).json({ + message: "Zone not found", + }); + break; + case "Panel Already Deleted": + res.status(409).json({ + message: "Panel Already Deleted", + }); + break; + case "Success": + res.status(200).json({ + message: "Panel deleted successfully", + }); + break; + default: + res.status(500).json({ + message: "Internal server error", + }); + break; + } + } catch (error) { + res.status(500).json({ + message: "Unknown error", + }); + return; + } +}; +export const ClearPanelController = async ( + req: AuthenticatedRequest, + res: Response +): Promise => { + try { + const { userId, organization } = req.user || {}; + const { panelName, projectId, zoneId } = req.body; + if (!userId || !organization || !panelName || !projectId || !zoneId) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const result = await ClearPanel({ + organization, + panelName, + zoneId, + projectId, + userId, + }); + switch (result.status) { + case "User not found": + res.status(404).json({ + message: "User not found", + }); + break; + case "Zone not found": + res.status(404).json({ + message: "Zone not found", + }); + break; + case "Requested Panel not found": + res.status(200).json({ + message: "Requested Panel not found", + }); + break; + case "No widgets to clear": + res.status(200).json({ + message: "No widgets to clear", + }); + break; + case "Success": + res.status(200).json({ + message: "PanelWidgets cleared successfully", + }); + break; + default: + res.status(500).json({ + message: "Internal server error", + }); + break; + } + } catch (error) { + res.status(500).json({ + message: "Unknown error", + }); + return; + } +}; +export const LockedPanelController = async ( + req: AuthenticatedRequest, + res: Response +): Promise => { + try { + const { userId, organization } = req.user || {}; + const { projectId, zoneId, lockedPanel } = req.body; + if (!userId || !organization || !projectId || !zoneId || !lockedPanel) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const result = await LockedPanel({ + organization, + zoneId, + lockedPanel, + userId, + projectId, + }); + switch (result.status) { + case "User not found": + res.status(404).json({ + message: "User not found", + }); + break; + case "Zone not found": + res.status(404).json({ + message: "Zone not found", + }); + break; + case "locked panel not updated": + res.status(200).json({ + message: "locked panel not updated", + }); + break; + case "Success": + res.status(200).json({ + message: "locked panel updated successfully", + }); + break; + default: + res.status(500).json({ + message: "Internal server error", + }); + break; + } + } catch (error) { + res.status(500).json({ + message: "Unknown error", + }); + return; + } +}; diff --git a/src/api-server/V1/v1Controllers/vizualizationController/v1templateController.ts b/src/api-server/V1/v1Controllers/vizualizationController/v1templateController.ts new file mode 100644 index 0000000..b044051 --- /dev/null +++ b/src/api-server/V1/v1Controllers/vizualizationController/v1templateController.ts @@ -0,0 +1,215 @@ +import { Response } from "express"; +import { AuthenticatedRequest } from "../../../../shared/utils/token.ts"; +import { + AddTemplate, + AddTemplateToZone, + GetAllTemplates, + TemplateDelete, +} from "../../../../shared/services/visualization/templateService.ts"; + +export const AddTemplateController = async ( + req: AuthenticatedRequest, + res: Response +): Promise => { + try { + const { userId, organization } = req.user || {}; + const { template, projectId } = req.body; + if (!userId || !organization || !template || !projectId) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const result = await AddTemplate({ + organization, + template, + projectId, + userId, + }); + + switch (result.status) { + case "User not found": + res.status(404).json({ + message: "User not found", + }); + break; + case "TemplateID alreay exists": + res.status(200).json({ + message: "TemplateID alreay exists", + }); + break; + case "Template not saved": + res.status(200).json({ + message: "Template not saved", + }); + break; + case "Success": + res.status(200).json({ + message: "Template saved successfully", + }); + break; + default: + res.status(500).json({ + message: "Internal server error", + }); + break; + } + } catch (error) { + res.status(500).json({ + message: "Unknown error", + }); + return; + } +}; +export const AddTemToZoneController = async ( + req: AuthenticatedRequest, + res: Response +): Promise => { + try { + const { userId, organization } = req.user || {}; + const { templateID, projectId, zoneId } = req.body; + if (!userId || !organization || !templateID || !projectId || !zoneId) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const result = await AddTemplateToZone({ + organization, + templateID, + zoneId, + projectId, + userId, + }); + switch (result.status) { + case "User not found": + res.status(404).json({ + message: "User not found", + }); + break; + case "Zone not found": + res.status(404).json({ + message: "Zone not found", + }); + break; + case "TemplateID not found": + res.status(409).json({ + message: "TemplateID not found", + }); + break; + case "Success": + res.status(201).json({ + message: "Template placed in Zone", + data: result.data, + }); + break; + default: + res.status(500).json({ + message: "Internal server error", + }); + break; + } + } catch (error) { + res.status(500).json({ + message: "Unknown error", + }); + return; + } +}; +export const TemplateDeleteController = async ( + req: AuthenticatedRequest, + res: Response +): Promise => { + try { + const { userId, organization } = req.user || {}; + const { templateID, projectId } = req.body; + if (!userId || !organization || !templateID || !projectId) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const result = await TemplateDelete({ + userId, + organization, + templateID, + projectId, + }); + switch (result.status) { + case "User not found": + res.status(404).json({ + message: "User not found", + }); + break; + case "Template not found": + res.status(404).json({ + message: "Template not found", + }); + break; + case "Template not Deleted": + res.status(200).json({ + message: "Template not Deleted", + }); + break; + case "Success": + res.status(200).json({ + message: "Template deleted successfully", + }); + break; + default: + res.status(500).json({ + message: "Internal server error", + }); + break; + } + } catch (error) { + res.status(500).json({ + message: "Unknown error", + }); + return; + } +}; +export const GetTemplateController = async ( + req: AuthenticatedRequest, + res: Response +): Promise => { + try { + const { userId, organization } = req.user || {}; + const { projectId } = req.params; + if (!userId || !organization || !projectId) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const result = await GetAllTemplates({ + organization, + userId, + projectId, + }); + switch (result.status) { + case "User not found": + res.status(404).json({ + message: "User not found", + }); + break; + case "All Datas": + res.status(404).json([]); + break; + + case "Success": + res.status(200).json(result.data); + break; + default: + res.status(500).json({ + message: "Internal server error", + }); + break; + } + } catch (error) { + res.status(500).json({ + message: "Unknown error", + }); + return; + } +}; diff --git a/src/api-server/V1/v1Controllers/vizualizationController/v1widgetController.ts b/src/api-server/V1/v1Controllers/vizualizationController/v1widgetController.ts new file mode 100644 index 0000000..7112567 --- /dev/null +++ b/src/api-server/V1/v1Controllers/vizualizationController/v1widgetController.ts @@ -0,0 +1,130 @@ +import { Response } from "express"; +import { AuthenticatedRequest } from "../../../../shared/utils/token.ts"; +import { + AddWidget, + WidgetDelete, +} from "../../../../shared/services/visualization/widgetService.ts"; + +export const AddWidgetController = async ( + req: AuthenticatedRequest, + res: Response +): Promise => { + try { + const { userId, organization } = req.user || {}; + const { widget, projectId, zoneId } = req.body; + if (!userId || !organization || !widget || !projectId || !zoneId) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const result = await AddWidget({ + organization, + widget, + projectId, + userId, + zoneId, + }); + + switch (result.status) { + case "User not found": + res.status(404).json({ + message: "User not found", + }); + break; + case "Zone not found for the zoneId": + res.status(404).json({ + message: "Zone not found", + }); + case "panelName not found": + res.status(404).json({ + message: "panelName not found", + }); + break; + case "Widget update unsuccessfull": + res.status(200).json({ + message: "Widget update unsuccessfull", + }); + break; + case "Widget update successfully": + res.status(200).json({ + message: "Widget updated successfully", + }); + break; + case "Success": + res.status(200).json({ + message: "Widget created successfully", + }); + break; + case "Type mismatch": + res.status(200).json({ + message: "Type mismatch", + }); + break; + default: + res.status(500).json({ + message: "Internal server error", + }); + break; + } + } catch (error) { + res.status(500).json({ + message: "Unknown error", + }); + return; + } +}; +export const WidgetDeleteController = async ( + req: AuthenticatedRequest, + res: Response +): Promise => { + try { + const { userId, organization } = req.user || {}; + const { widgetID, projectId, zoneId } = req.body; + if (!userId || !organization || !widgetID || !projectId || !zoneId) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const result = await WidgetDelete({ + organization, + widgetID, + zoneId, + projectId, + userId, + }); + switch (result.status) { + case "User not found": + res.status(404).json({ + message: "User not found", + }); + break; + case "Zone not found for the zoneId": + res.status(404).json({ + message: "Zone not found for the zoneId", + }); + break; + case "Widget not found": + res.status(409).json({ + message: "Widget not found", + }); + break; + case "Success": + res.status(200).json({ + message: "Widget deleted successfully", + }); + break; + default: + res.status(500).json({ + message: "Internal server error", + }); + break; + } + } catch (error) { + res.status(500).json({ + message: "Unknown error", + }); + return; + } +}; diff --git a/src/api-server/V1/v1Controllers/vizualizationController/widget3Dcontroller.ts b/src/api-server/V1/v1Controllers/vizualizationController/widget3Dcontroller.ts new file mode 100644 index 0000000..41defc8 --- /dev/null +++ b/src/api-server/V1/v1Controllers/vizualizationController/widget3Dcontroller.ts @@ -0,0 +1,252 @@ +import { Response } from "express"; +import { AuthenticatedRequest } from "../../../../shared/utils/token.ts"; +import { + Add3DWidget, + Delete3Dwidget, + Get3Dwidget, + Update3Dwidget, +} from "../../../../shared/services/visualization/widget3dService.ts"; + +export const Add3dWidgetController = async ( + req: AuthenticatedRequest, + res: Response +): Promise => { + try { + const { userId, organization } = req.user || {}; + const { widget, projectId, zoneId } = req.body; + if (!userId || !organization || !widget || !projectId || !zoneId) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const result = await Add3DWidget({ + organization, + widget, + userId, + zoneId, + projectId, + }); + + switch (result.status) { + case "User not found": + res.status(404).json({ + message: "User not found", + }); + break; + case "Zone not found for the zoneId": + res.status(200).json({ + message: "Zone not found", + }); + break; + case "Widget not updated": + res.status(200).json({ + message: "Widget not updated", + }); + break; + case "widget update successfully": + res.status(200).json({ + message: "widget update successfully", + }); + break; + case "Widget 3d not created": + res.status(200).json({ + message: "Widget 3d not created", + }); + break; + case "Success": + res.status(200).json({ + message: "Widget created successfully", + }); + break; + default: + res.status(500).json({ + message: "Internal server error", + }); + break; + } + } catch (error) { + res.status(500).json({ + message: "Unknown error", + }); + return; + } +}; +export const Update3DwidgetController = async ( + req: AuthenticatedRequest, + res: Response +): Promise => { + try { + const { userId, organization } = req.user || {}; + const { id, position, rotation, projectId, zoneId } = req.body; + if ( + !userId || + !organization || + !id || + !position || + !rotation || + !projectId || + !zoneId + ) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const result = await Update3Dwidget({ + organization, + id, + position, + rotation, + userId, + zoneId, + projectId, + }); + switch (result.status) { + case "User not found": + res.status(404).json({ + message: "User not found", + }); + break; + case "Zone not found": + res.status(404).json({ + message: "Zone not found", + }); + break; + case "Success": + res.status(200).json({ + message: "widget3D update successfully", + }); + break; + case "Widget not updated": + res.status(200).json({ + message: "Widget not updated", + }); + break; + case "widget not found": + res.status(200).json({ + message: "widget not found", + }); + break; + default: + res.status(500).json({ + message: "Internal server error", + }); + break; + } + } catch (error) { + res.status(500).json({ + message: "Unknown error", + }); + return; + } +}; +export const Delete3DwidgetController = async ( + req: AuthenticatedRequest, + res: Response +): Promise => { + try { + const { userId, organization } = req.user || {}; + const { id, projectId, zoneId } = req.body; + if (!userId || !organization || !id || !projectId || !zoneId) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const result = await Delete3Dwidget({ + organization, + id, + userId, + zoneId, + projectId, + }); + switch (result.status) { + case "User not found": + res.status(404).json({ + message: "User not found", + }); + break; + case "Zone not found": + res.status(404).json({ + message: "Zone not found", + }); + break; + case "3D widget not found for the ID": + res.status(404).json({ + message: "3D widget not found for the ID", + }); + break; + case "3DWidget delete unsuccessfull": + res.status(200).json({ + message: "3DWidget delete unsuccessfull", + }); + break; + case "Success": + res.status(200).json({ + message: "3DWidget deleted successfully", + }); + break; + default: + res.status(500).json({ + message: "Internal server error", + }); + break; + } + } catch (error) { + res.status(500).json({ + message: "Unknown error", + }); + return; + } +}; +export const Get3DWidgetController = async ( + req: AuthenticatedRequest, + res: Response +): Promise => { + try { + const { userId, organization } = req.user || {}; + const { projectId, zoneId } = req.params; + if (!userId || !organization || !projectId) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const result = await Get3Dwidget({ + organization, + userId, + zoneId, + projectId, + }); + switch (result.status) { + case "User not found": + res.status(404).json({ + message: "User not found", + }); + break; + case "Zone not found": + res.status(404).json({ + message: "Zone not found", + }); + break; + case "All 3Dwidgets": + res.status(200).json([]); + break; + + case "Success": + res.status(200).json(result.data); + break; + default: + res.status(500).json({ + message: "Internal server error", + }); + break; + } + } catch (error) { + res.status(500).json({ + message: "Unknown error", + }); + return; + } +}; diff --git a/src/api-server/controller/visualization/3dWidgetService.ts b/src/api-server/controller/visualization/3dWidgetService.ts index ae721e9..5b22999 100644 --- a/src/api-server/controller/visualization/3dWidgetService.ts +++ b/src/api-server/controller/visualization/3dWidgetService.ts @@ -129,7 +129,8 @@ export class Widget3dService { widgetID: id, isArchive: false, }); - if (!widgetData) return res.status(204).json({ message: "Widget not found" }); + if (!widgetData) + return res.status(204).json({ message: "Widget not found" }); const structureData = { measurements: widgetData?.Data?.measurements, duration: widgetData?.Data?.duration, diff --git a/src/api-server/controller/visualization/panelService.ts b/src/api-server/controller/visualization/panelService.ts index 3ee7689..db3511d 100644 --- a/src/api-server/controller/visualization/panelService.ts +++ b/src/api-server/controller/visualization/panelService.ts @@ -92,7 +92,7 @@ export class PanelService { if (existingZone.panelOrder.includes(existingPanel.panelName)) { existingZone.panelOrder.indexOf(existingPanel.panelName); - const zonepanelname = await zoneSchema(organization).updateOne( + await zoneSchema(organization).updateOne( { _id: existingZone._id }, { $pull: { panelOrder: existingPanel.panelName } } ); diff --git a/src/api-server/controller/visualization/widgetService.ts b/src/api-server/controller/visualization/widgetService.ts index 0abde31..9c7562a 100644 --- a/src/api-server/controller/visualization/widgetService.ts +++ b/src/api-server/controller/visualization/widgetService.ts @@ -61,7 +61,6 @@ export class WidgetService { return res .status(200) .json({ message: "Widget updated successfully" }); - } const newWidget = await widgetSchema(organization).create({ widgetID: widget.id, diff --git a/src/shared/services/helpers/ProjecthelperFn.ts b/src/shared/services/helpers/ProjecthelperFn.ts index 62ff0d2..c291fde 100644 --- a/src/shared/services/helpers/ProjecthelperFn.ts +++ b/src/shared/services/helpers/ProjecthelperFn.ts @@ -20,9 +20,7 @@ export const existingUser = async (userId: string, organization: string) => { console.log("Invalid ObjectId format"); return null; } - const userData = await userModel(organization).findOne({ - _id: userId, - }); + const userData = await userModel(organization).findOne({ _id: userId }); return userData; }; diff --git a/src/shared/services/project/project-Services.ts b/src/shared/services/project/project-Services.ts index 24366b3..c71b982 100644 --- a/src/shared/services/project/project-Services.ts +++ b/src/shared/services/project/project-Services.ts @@ -98,10 +98,12 @@ export const GetAllProjects = async (data: GetProjectsInterface) => { if (!existingUser) return { status: "User not found" }; const projectDatas = await projectModel(organization) .find({ - createdBy:userId, + createdBy: userId, isArchive: false, }) - .select("_id projectName createdBy thumbnail createdAt projectUuid createdAt"); + .select( + "_id projectName createdBy thumbnail createdAt projectUuid createdAt" + ); if (projectDatas) return { status: "Success", Datas: projectDatas }; } catch (error: unknown) { return { status: error }; diff --git a/src/shared/services/v1home/v1homeservice.ts b/src/shared/services/v1home/v1homeservice.ts index fd66908..4dab787 100644 --- a/src/shared/services/v1home/v1homeservice.ts +++ b/src/shared/services/v1home/v1homeservice.ts @@ -29,6 +29,7 @@ export const RecentlyAdded = async (data: IRecentData) => { try { const { userId, organization, role } = data; const userExisting = await existingUser(userId, organization); + console.log('userExisting: ', userExisting); if (!userExisting) return { status: "User not found" }; const userRecentData = await UsersDataModel(organization) .findOne({ userId: userId, isArchive: false }) diff --git a/src/shared/services/visualization/floatWidgetService.ts b/src/shared/services/visualization/floatWidgetService.ts index e5b3efc..ca708e0 100644 --- a/src/shared/services/visualization/floatWidgetService.ts +++ b/src/shared/services/visualization/floatWidgetService.ts @@ -1,3 +1,451 @@ import floatWidgetModel from "../../V1Models/Vizualization/floatWidget.ts"; import zoneModel from "../../V1Models/Builder/zoneModel.ts"; -// export const \ No newline at end of file +import { existingUser } from "../helpers/v1projecthelperFns.ts"; + +interface IResult { + status: string; + data?: object; +} +interface IAddFloatData { + userId: string; + organization: string; + widget: { + className: string; + id: string; + iconName: string; + header: string; + floatWidgetID: string; + position: {}; + per: string; + value: string; + isArchive: boolean; + zoneId: string; + Data: { + measurements: {}; + duration: string; + }; + }; + zoneId: string; + index: number; + projectId: string; +} +interface IDelFloat { + userId: string; + organization: string; + zoneId: string; + floatWidgetID: string; + projectId: string; +} +interface ISingleFloat { + userId: string; + organization: string; + floatWidgetID: string; +} +interface IGetZoneFloat { + userId: string; + organization: string; + zoneId: string; + projectId: string; +} +interface IDuplicateFloatData { + userId: string; + organization: string; + widget: { + className: string; + id: string; + iconName: string; + header: string; + floatWidgetID: string; + position: {}; + per: string; + value: string; + isArchive: boolean; + zoneId: string; + Data: { + measurements: {}; + duration: string; + }; + }; + zoneId: string; + index: number; + projectId: string; +} +export const AddFloat = async (data: IAddFloatData): Promise => { + try { + const { organization, widget, zoneId, index, projectId, 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 for the zoneId" }; + // return { + // success: false, + // message: "Zone not found for the zoneId", + // organization: organization, + // }; + + const existingFloatWidget = await floatWidgetModel(organization).findOne({ + floatWidgetID: widget.id, + isArchive: false, + zoneId: zoneId, + }); + if (existingFloatWidget) { + const updateFloatWidget = await floatWidgetModel( + organization + ).findOneAndUpdate( + { + floatWidgetID: widget.id, + isArchive: false, + }, + { + $set: { + Data: { + measurements: widget?.Data?.measurements, + duration: widget?.Data?.duration, + }, + header: widget?.header, + position: widget?.position, + }, + }, + { + upsert: true, + new: true, + } + ); + const floatUpdateDatas = { + position: updateFloatWidget.position, + index: index, + zoneId: zoneId, + zoneName: existingZone.zoneName, + }; + return { status: "Widget updated successfully", data: floatUpdateDatas }; + // return { + // success: true, + // message: "Widget updated successfully", + // data: floatUpdateDatas, + // organization: organization, + // }; + } else { + const newFloadWidget = await floatWidgetModel(organization).create({ + className: widget.className, + iconName: widget.iconName, + header: widget.header, + floatWidgetID: widget.id, + position: widget.position, + per: widget.per, + value: widget.value, + zoneId: zoneId, + }); + if (newFloadWidget) { + const floatDatas = { + widget: { + position: newFloadWidget.position, + header: newFloadWidget.header, + value: newFloadWidget.value, + per: newFloadWidget.per, + className: newFloadWidget.className, + id: newFloadWidget.floatWidgetID, + }, + zoneId: zoneId, + zoneName: existingZone.zoneName, + }; + return { status: "Success", data: floatDatas }; + // return { + // success: true, + // message: "FloatWidget created successfully", + // data: floatDatas, + // organization: organization, + // }; + } + return { status: "Failed to create FloatWidget" }; + } + } catch (error: unknown) { + if (error instanceof Error) { + return { + status: error.message, + }; + } else { + return { + status: "An unexpected error occurred", + }; + } + } +}; + +export const DelFloat = async (data: IDelFloat): Promise => { + try { + const { organization, floatWidgetID, zoneId, projectId, 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 for the zoneId" }; + // return { + // success: false, + // message: "Zone not found for the zoneId", + // organization: organization, + // }; + + const findfloatWidget = await floatWidgetModel(organization).findOne({ + floatWidgetID: floatWidgetID, + isArchive: false, + }); + if (!findfloatWidget) return { status: "FloatWidget not found for the Id" }; + // return { + // success: false, + // message: "Zone not found for the zoneId", + // organization: organization, + // }; + const widgetData = await floatWidgetModel(organization).findByIdAndUpdate( + { _id: findfloatWidget._id, isArchive: false }, + { isArchive: true }, + { new: true } + ); + + if (widgetData) { + const floatDeleteData = { + floatWidgetID: findfloatWidget.floatWidgetID, + zoneId: findfloatWidget.zoneId, + zoneName: existingZone.zoneName, + }; + return { status: "Success", data: floatDeleteData }; + // return { + // success: true, + // message: "FloatingWidget deleted successfully", + // data: floatDeleteData, + // organization: organization, + // }; + } + return { status: "FloatWidget not deleted" }; + } catch (error: unknown) { + if (error instanceof Error) { + return { + status: error.message, + }; + } else { + return { + status: "An unexpected error occurred", + }; + } + } +}; + +export const DuplicateFloat = async ( + data: IDuplicateFloatData +): Promise => { + try { + const { organization, widget, zoneId, index, projectId, 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 { + // success: false, + // message: "Zone not found for the zoneId", + // organization: organization, + // }; + return { status: "Zone not found for the zoneId" }; + + const existingFloatWidget = await floatWidgetModel(organization).findOne({ + floatWidgetID: widget.id, + isArchive: false, + zoneId: zoneId, + }); + if (existingFloatWidget) { + const updateFloatWidget = await floatWidgetModel( + organization + ).findOneAndUpdate( + { + floatWidgetID: widget.id, + isArchive: false, + }, + { + $set: { + Data: { + measurements: widget?.Data?.measurements, + duration: widget?.Data?.duration, + }, + header: widget?.header, + position: widget?.position, + }, + }, + { + upsert: true, + new: true, + } + ); + if (!updateFloatWidget) { + // return { + // success: false, + // message: "FloatWidget update unsuccessfull", + // // data: updateWidget, + // organization: organization, + // }; + return { status: "FloatWidget update unsuccessfull" }; + } + const floatUpdateDatas = { + position: updateFloatWidget.position, + index: index, + zoneId: zoneId, + zoneName: existingZone.zoneName, + }; + return { + status: "Widget updated successfully", + data: floatUpdateDatas, + }; + // return { + // success: true, + // message: "Widget updated successfully", + // data: floatUpdateDatas, + // organization: organization, + // }; + } + const newFloadWidget = await floatWidgetModel(organization).create({ + className: widget.className, + header: widget.header, + floatWidgetID: widget.id, + position: widget.position, + per: widget.per, + value: widget.value, + zoneId: zoneId, + Data: { + measurements: widget?.Data?.measurements, + duration: widget?.Data?.duration, + }, + }); + if (newFloadWidget) { + const floatDatas = { + widget: { + position: newFloadWidget.position, + header: newFloadWidget.header, + value: newFloadWidget.value, + per: newFloadWidget.per, + className: newFloadWidget.className, + id: newFloadWidget.floatWidgetID, + }, + zoneId: zoneId, + zoneName: existingZone.zoneName, + index: index, + }; + return { + status: "Success", + data: floatDatas, + }; + // return { + // success: true, + // message: "duplicate FloatWidget created successfully", + // data: floatDatas, + // organization: organization, + // }; + } + return { + status: "Failed to duplicate FloatWidget", + }; + } catch (error: unknown) { + if (error instanceof Error) { + return { + status: error.message, + }; + } else { + return { + status: "An unexpected error occurred", + }; + } + } +}; + +export const GetFloatWidget = async (data: IGetZoneFloat): Promise => { + try { + const { organization, zoneId, projectId, 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" }; + const widgetData = await floatWidgetModel(organization) + .find({ + zoneId: zoneId, + isArchive: false, + }) + .select("-_id -zoneId -createdAt -updatedAt -__v"); + if (!widgetData || widgetData.length === 0) { + return { status: "All Datas" }; + // return res.send([]); + } + + const formattedWidgets = widgetData.map((widget) => ({ + Data: { + measurements: widget.Data?.measurements || {}, + duration: widget.Data?.duration || "1h", + }, + className: widget.className, + iconName: widget?.iconName, + header: widget.header, + id: widget.floatWidgetID, + position: widget.position, + per: widget.per, + value: widget.value, + })); + return { status: "Success", data: formattedWidgets }; + // return res.status(200).json(formattedWidgets); + } catch (error: unknown) { + if (error instanceof Error) { + return { + status: error.message, + }; + } else { + return { + status: "An unexpected error occurred", + }; + } + } +}; +export const SingleFloatWidget = async ( + data: ISingleFloat +): Promise => { + try { + const { organization, floatWidgetID, userId } = data; + const UserExists = await existingUser(userId, organization); + if (!UserExists) return { status: "User not found" }; + const widgetData = await floatWidgetModel(organization) + .findOne({ + floatWidgetID: floatWidgetID, + isArchive: false, + }) + .select("-_id -zoneId -createdAt -updatedAt -__v"); + if (!widgetData || widgetData.length === 0) { + return { status: "Widget not found" }; + // return res.status(204).json({ message: "Widget not found" }); + } + const Datastructure = { + measurements: widgetData?.Data?.measurements || {}, + duration: widgetData?.Data?.duration || "1h", + }; + const header = widgetData?.header; + return { status: "Success", data: { Datastructure, header } }; + // return res.status(200).json({ Data: Datastructure, header }); + } catch (error: unknown) { + if (error instanceof Error) { + return { + status: error.message, + }; + } else { + return { + status: "An unexpected error occurred", + }; + } + } +}; diff --git a/src/shared/services/visualization/panelService.ts b/src/shared/services/visualization/panelService.ts index e69de29..21b2975 100644 --- a/src/shared/services/visualization/panelService.ts +++ b/src/shared/services/visualization/panelService.ts @@ -0,0 +1,381 @@ +import panelModel from "../../V1Models/Vizualization/panelmodel.ts"; +import zoneModel from "../../V1Models/Builder/zoneModel.ts"; +import widgetModel from "../../V1Models/Vizualization/widgemodel.ts"; +import { existingUser } from "../helpers/v1projecthelperFns.ts"; +interface IResult { + status: string; + data?: object; +} +interface IAddPanel { + organization: string; + zoneId: string; + panelOrder: string[]; + userId: string; + projectId: string; +} +interface IPanel { + organization: string; + zoneId: string; + panelName: string; + userId: string; + projectId: string; +} +interface ILockedPanel { + organization: string; + zoneId: string; + lockedPanel: string[]; + userId: string; + projectId: string; +} +export const AddPanel = async (data: IAddPanel): Promise => { + try { + const { organization, zoneId, panelOrder, userId, projectId } = 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" }; + await zoneModel(organization).findOneAndUpdate( + { zoneId: zoneId, isArchive: false }, + { panelOrder: panelOrder }, + { new: true } + ); + const existingPanels = await panelModel(organization).find({ + zoneId: zoneId, + 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({ + zoneId: zoneId, + panelName: panelName, + widgets: [], + isArchive: false, + }); + createdPanels.push(newPanel); + } + + if (createdPanels.length === 0) { + return { status: "No new panels were created. All panels already exist" }; + // return { + // success: false, + // message: "No new panels were created. All panels already exist", + // organization: organization, + // }; + } + + const zoneAndPanelData = await getZoneAndPanelData( + organization, + zoneId, + projectId + ); + if (!zoneAndPanelData) { + return zoneAndPanelData; + } + return { status: "Success", data: zoneAndPanelData }; + // return { + // success: true, + // message: "Panels created successfully", + // data: zoneAndPanelData, + // organization: organization, + // }; + } 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, zoneId, panelName, userId, projectId } = 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" }; + const existingPanel = await panelModel(organization).findOne({ + zoneId: zoneId, + panelName: panelName, + isArchive: false, + }); + if (!existingPanel) return { status: "Panel Already Deleted" }; + // return { + // success: false, + // message: "Panel Already Deleted", + // organization: organization, + // }; + 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, + zoneId, + projectId + ); + if (!zoneAndPanelData) { + return zoneAndPanelData; + } + return { status: "Success", data: zoneAndPanelData }; + // return { + // success: true, + // message: "Panel deleted successfully", + // data: zoneAndPanelData, + // organization: organization, + // }; + } 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, zoneId, panelName, userId, projectId } = 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" }; + const existingPanel = await panelModel(organization).findOne({ + zoneId: zoneId, + panelName: panelName, + isArchive: false, + }); + if (!existingPanel) return { status: "Requested Panel not found" }; + // return { + // success: false, + // message: "Requested Panel not found", + // organization: organization, + // }; + + const existingWidgets = await widgetModel(organization).find({ + panelID: existingPanel._id, + isArchive: false, + }); + if (existingWidgets.length === 0) return { status: "No widgets to clear" }; + // return { + // success: false, + // message: "No widgets to clear", + // organization: organization, + // }; + 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" }; + // return { + // success: false, + // message: "Failed to clear widgets in panel", + // organization: organization, + // }; + + const zoneAndPanelData = await getZoneAndPanelData( + organization, + zoneId, + projectId + ); + if (!zoneAndPanelData) { + return zoneAndPanelData; + } + // return { + // success: true, + // data: zoneAndPanelData, + // message: "PanelWidgets cleared successfully", + // organization: organization, + // }; + 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, zoneId, lockedPanel, userId, projectId } = 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" }; + else { + const updateLockedPanel = await zoneModel(organization).findOneAndUpdate( + { zoneId: zoneId, isArchive: false }, + { + lockedPanel: lockedPanel, + }, + { new: true } + ); + const zoneAndPanelData = await getZoneAndPanelData( + organization, + zoneId, + projectId + ); + if (!zoneAndPanelData) { + return zoneAndPanelData; + } + if (updateLockedPanel) { + return { + status: "Success", + data: zoneAndPanelData, + }; + // return { + // success: true, + // message: "locked panel updated successfully", + // data: zoneAndPanelData, + // organization: organization, + // }; + } + 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, + zoneId: string, + projectId: string +) => { + try { + const existingZone = await zoneModel(organization) + .findOne({ + zoneId: zoneId, + isArchive: false, + projectId: projectId, + }) + .select( + "panelOrder zoneName zonePoints lockedPanel zoneId viewPortCenter viewPortposition" + ); + if (!existingZone) { + return { status: "Zone not found" }; + // return { + // success: false, + // message: "Zone not found", + // organization: organization, + // }; + } else { + const panelData = await panelModel(organization).find({ + zoneId: zoneId, + 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, + zoneId: existingZone.zoneId, + 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" }; + // return { + // success: false, + // message: "Panel not found", + // error, + // organization: organization, + // }; + } +}; diff --git a/src/shared/services/visualization/templateService.ts b/src/shared/services/visualization/templateService.ts index e69de29..52e032c 100644 --- a/src/shared/services/visualization/templateService.ts +++ b/src/shared/services/visualization/templateService.ts @@ -0,0 +1,337 @@ +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 => { + 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 => { + 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 => { + 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 => { + 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", + }; + } + } +}; diff --git a/src/shared/services/visualization/widget3dService.ts b/src/shared/services/visualization/widget3dService.ts index e69de29..bccaf3a 100644 --- a/src/shared/services/visualization/widget3dService.ts +++ b/src/shared/services/visualization/widget3dService.ts @@ -0,0 +1,333 @@ +import zoneModel from "../../V1Models/Builder/zoneModel.ts"; +import widget3dModel from "../../V1Models/Vizualization/3dwidget.ts"; +import { existingUser } from "../helpers/v1projecthelperFns.ts"; +interface IResult { + status: string; + data?: object; +} +interface IWidget3DAdd { + organization: string; + widget: { + id: string; + position: []; + type: string; + Data: { + measurements: {}; + duration: string; + }; + }; + projectId: string; + zoneId: string; + userId: string; +} +interface IWidget3dUpdate { + organization: string; + id: string; + projectId: string; + zoneId: string; + userId: string; +} +interface IWidgetUpdate { + organization: string; + id: string; + position: []; + rotation: []; + projectId: string; + zoneId: string; + userId: string; +} +interface I3dWidgetGet { + organization: string; + projectId: string; + zoneId: string; + userId: string; +} +export const Add3DWidget = async (data: IWidget3DAdd): Promise => { + try { + const { organization, widget, userId, zoneId, projectId } = 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 for the zoneId" }; + // return { + // success: false, + // message: "Zone not found for the zoneId", + // organization: organization, + // }; + const existing3Dwidget = await widget3dModel(organization).findOne({ + widgetID: widget.id, + isArchive: false, + }); + if (existing3Dwidget) { + const update3dwidget = await widget3dModel(organization).findOneAndUpdate( + { + widgetID: widget.id, + zoneId: zoneId, + isArchive: false, + }, + { position: widget.position }, + { upsert: true, new: true } + ); + if (update3dwidget) { + return { + status: "3dwidget update successfully", + }; + // return { + // success: true, + // message: "widget update successfully", + // organization: organization, + // }; + } else return { status: "3dWidget not updated" }; + // return { + // success: false, + // message: "Widget not updated", + // organization: organization, + // }; + } else { + const newWidget3d = await widget3dModel(organization).create({ + type: widget.type, + widgetID: widget.id, + position: widget.position, + zoneId, + Data: { + measurements: widget?.Data?.measurements || {}, + duration: widget?.Data?.duration || "1h", + }, + }); + if (newWidget3d) { + const widgemodel3D_Datas = { + widget: { + id: newWidget3d.widgetID, + type: newWidget3d.type, + position: newWidget3d.position, + }, + Data: newWidget3d.Data, + zoneId: zoneId, + }; + + return { + status: "Success", + data: widgemodel3D_Datas, + }; + } + return { status: "Widget 3d not created" }; + // return { + // success: true, + // message: "Widget created successfully", + // data: widgemodel3D_Datas, + // organization: organization, + // }; + } + } catch (error: unknown) { + if (error instanceof Error) { + return { + status: error.message, + }; + } else { + return { + status: "An unexpected error occurred", + }; + } + } +}; +export const Update3Dwidget = async (data: IWidgetUpdate): Promise => { + try { + const { organization, id, position, rotation, userId, zoneId, projectId } = + 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 existing3Dwidget = await widget3dModel(organization).findOne({ + widgetID: id, + zoneId: zoneId, + isArchive: false, + }); + if (existing3Dwidget) { + const update3dwidget = await widget3dModel(organization).findOneAndUpdate( + { + widgetID: id, + zoneId: zoneId, + isArchive: false, + }, + { position: position, rotation: rotation }, + { upsert: true, new: true } + ); + if (update3dwidget) { + const updateDatas = { + widget: { + id: update3dwidget.widgetID, + type: update3dwidget.type, + position: update3dwidget.position, + rotation: update3dwidget.rotation, + }, + zoneId: zoneId, + }; + return { + status: "Success", + data: updateDatas, + }; + // return { + // success: true, + // message: "widget update successfully", + // data: updateDatas, + // organization: organization, + // }; + } + return { status: "Widget not updated" }; + } else { + return { status: "widget not found" }; + // return { + // success: false, + // message: "widget not found", + // organization: organization, + // }; + } + } catch (error: unknown) { + if (error instanceof Error) { + return { + status: error.message, + }; + } else { + return { + status: "An unexpected error occurred", + }; + } + } +}; +export const Delete3Dwidget = async ( + data: IWidget3dUpdate +): Promise => { + try { + const { organization, id, userId, zoneId, projectId } = 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 existing3Dwidget = await widget3dModel(organization).findOne({ + widgetID: id, + isArchive: false, + zoneId: zoneId, + }); + if (!existing3Dwidget) { + return { status: "3D widget not found for the ID" }; + // return { + // success: false, + // message: "3D widget not found for the ID", + // organization: organization, + // }; + } + const updateWidget = await widget3dModel(organization).findOneAndUpdate( + { + widgetID: id, + zoneId: zoneId, + isArchive: false, + }, + { isArchive: true }, + { new: true } + ); + if (updateWidget) { + const delete_Datas = { + zoneId: zoneId, + id: existing3Dwidget.widgetID, + }; + return { + status: "Success", + data: delete_Datas, + }; + // return { + // success: true, + // data: delete_Datas, + // message: "3DWidget delete successfull", + // organization: organization, + // }; + } + return { status: "3DWidget delete unsuccessfull" }; + } catch (error: unknown) { + if (error instanceof Error) { + return { + status: error.message, + }; + } else { + return { + status: "An unexpected error occurred", + }; + } + } +}; +export const Get3Dwidget = async (data: I3dWidgetGet): Promise => { + try { + const { organization, userId, zoneId, projectId } = 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 res.send("Zone not found"); + const widgetData = await widget3dModel(organization).find({ + zoneId: zoneId, + isArchive: false, + }); + if (!widgetData || widgetData.length === 0) { + return { status: "All 3Dwidgets" }; + // return res.json([]); + } + + const zonebasedWidget = widgetData.map((widget) => ({ + Data: { + measurements: widget?.Data?.measurements || {}, + duration: widget?.Data?.duration || "1h", + }, + type: widget.type, + id: widget.widgetID, + position: widget.position, + rotation: widget?.rotation, + })); + return { status: "Success", data: zonebasedWidget }; + // return res.status(200).json(zonebasedWidget); + } catch (error: unknown) { + if (error instanceof Error) { + return { + status: error.message, + }; + } else { + return { + status: "An unexpected error occurred", + }; + } + } +}; diff --git a/src/shared/services/visualization/widgetService.ts b/src/shared/services/visualization/widgetService.ts index e69de29..0474c79 100644 --- a/src/shared/services/visualization/widgetService.ts +++ b/src/shared/services/visualization/widgetService.ts @@ -0,0 +1,245 @@ +import widgetModel from "../../V1Models/Vizualization/widgemodel.ts"; +import zoneModel from "../../V1Models/Builder/zoneModel.ts"; +import panelModel from "../../V1Models/Vizualization/panelmodel.ts"; +import { existingUser } from "../helpers/v1projecthelperFns.ts"; +interface IResult { + status: string; + data?: object; +} +interface IWidgetCreate { + organization: string; + userId: string; + zoneId: string; + projectId: string; + widget: { + type: string; + title: string; + panel: string; + id: string; + Data: { + measurements: {}; + duration: string; + }; + }; +} +interface IWidgetDelete { + organization: string; + userId: string; + zoneId: string; + projectId: string; + widgetID: string; +} +export const AddWidget = async (data: IWidgetCreate): Promise => { + try { + const { organization, widget, userId, zoneId, projectId } = 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 for the zoneId" }; + + const existingPanel = await panelModel(organization).findOne({ + panelName: widget.panel, + zoneId: zoneId, + isArchive: false, + }); + if (!existingPanel) return { status: "panelName not found" }; + // return { + // success: false, + // message: "panelName not found", + // organization: organization, + // }; + + if (existingPanel.panelName === widget.panel) { + const existingWidget = await widgetModel(organization).findOne({ + panelID: existingPanel._id, + widgetID: widget.id, + isArchive: false, + // widgetOrder: widget.widgetOrder, + }); + if (existingWidget) { + const updateWidget = await widgetModel(organization).findOneAndUpdate( + { + panelID: existingPanel._id, + widgetID: widget.id, + isArchive: false, + }, + { + $set: { + panelID: existingPanel._id, + widgetID: widget.id, + Data: { + measurements: widget?.Data?.measurements, + duration: widget?.Data?.duration, + }, + isArchive: false, + }, + }, + { upsert: true, new: true } // Upsert: create if not exists, new: return updated document + ); + if (!updateWidget) { + return { status: "Widget update unsuccessfull" }; + // return { + // success: false, + // message: "Widget update unsuccessfull", + // // data: updateWidget, + // organization: organization, + // }; + } + return { status: "Widget update successfully", data: updateWidget }; + // return { + // success: true, + // message: "Widget updated successfully", + // data: updateWidget, + // organization: organization, + // }; + } + const newWidget = await widgetModel(organization).create({ + widgetID: widget.id, + elementType: widget.type, + // widgetOrder: widgetOrder, + widgetName: widget.title, + panelID: existingPanel._id, + widgetside: widget.panel, + zoneId: zoneId, + Data: { + measurements: widget?.Data?.measurements || {}, + duration: widget?.Data?.duration || "1hr", + }, + }); + if (newWidget) { + existingPanel.widgets.push(newWidget._id); + await existingPanel.save(); + const widgetData = { + type: newWidget.elementType, + id: newWidget.widgetID, + panel: newWidget.widgetside, + title: newWidget.widgetName, + }; + const finaldata = { + widgetData: widgetData, + zoneId: existingZone.zoneId, + zoneName: existingZone.zoneName, + }; + return { + status: "Success", + data: finaldata, + }; + // return { + // success: true, + // message: "Widget created successfully", + // data: finaldata, + // organization: organization, + // }; + } + } + return { + status: "Type mismatch", + }; + // return { + // success: false, + // message: "Type mismatch", + // organization: organization, + // }; + } catch (error: unknown) { + if (error instanceof Error) { + return { + status: error.message, + }; + } else { + return { + status: "An unexpected error occurred", + }; + } + } +}; +export const WidgetDelete = async (data: IWidgetDelete): Promise => { + try { + const { organization, widgetID, userId, zoneId, projectId } = 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 for the zoneId" }; + + const findWidget = await widgetModel(organization).findOne({ + widgetID: widgetID, + zoneId: zoneId, + isArchive: false, + }); + if (!findWidget) return { status: "Widget not found" }; + // return { + // success: false, + // message: "Widget not found", + // organization: organization, + // }; + const widgetData = await widgetModel(organization).updateOne( + { _id: findWidget._id, isArchive: false, zoneId: zoneId }, + { $set: { isArchive: true } } + ); + + if (widgetData) { + await widgetModel(organization).find({ + panelID: findWidget.panelID, + zoneId: zoneId, + isArchive: false, + }); + + const panelData = await panelModel(organization).findOne({ + _id: findWidget.panelID, + isArchive: false, + zoneId: zoneId, + }); + if (panelData.widgets.includes(findWidget._id)) { + const index1 = panelData.widgets.indexOf(findWidget._id); + panelData.widgets.splice(index1, 1); + } + await panelData.save(); + + const activeWidgets = await widgetModel(organization).find({ + zoneId: zoneId, + isArchive: false, + }); + + const formattedWidgets = activeWidgets.map((widget) => ({ + id: widget.widgetID, + type: widget.elementType, + title: widget.widgetName, + panel: widget.widgetside, + data: { + duration: "1h", + measurements: {}, + }, + })); + const widgetData1 = { + widgetDeleteDatas: formattedWidgets, + zoneId: zoneId, + zoneName: existingZone.zoneName, + }; + return { status: "Success", data: widgetData1 }; + // return { + // success: true, + // message: "Widget deleted successfully", + // data: widgetData1, + // organization: organization, + // }; + } + return { status: "Widget not found" }; + } catch (error: unknown) { + if (error instanceof Error) { + return { + status: error.message, + }; + } else { + return { + status: "An unexpected error occurred", + }; + } + } +}; diff --git a/src/socket-server/services/visualization/widget-Services.ts b/src/socket-server/services/visualization/widget-Services.ts index a6cfc1f..a814f50 100644 --- a/src/socket-server/services/visualization/widget-Services.ts +++ b/src/socket-server/services/visualization/widget-Services.ts @@ -1,14 +1,14 @@ import panelSchema from "../../../shared/model/vizualization/panelmodel.ts"; import widgetSchema from "../../../shared/model/vizualization/widgemodel.ts"; import zoneSchema from "../../../shared/model/builder/lines/zone-Model.ts"; -export const addWidget = async (data: any,callback:any) => { +export const addWidget = async (data: any, callback: any) => { const { organization, panel, zoneId, widget } = data; try { const existingZone = await zoneSchema(organization).findOne({ zoneId: zoneId, isArchive: false, }); - + if (!existingZone) return { success: false, @@ -59,7 +59,7 @@ export const addWidget = async (data: any,callback:any) => { callback({ success: false, message: "Widget update unsuccessfull", - }) + }); } return { success: false, @@ -106,7 +106,7 @@ export const addWidget = async (data: any,callback:any) => { callback({ success: true, message: "Widget created successfully", - }) + }); } return { success: true, @@ -114,7 +114,6 @@ export const addWidget = async (data: any,callback:any) => { data: finaldata, organization: organization, }; - } } return { @@ -126,7 +125,7 @@ export const addWidget = async (data: any,callback:any) => { callback({ success: false, message: "widge not found", - }) + }); return { success: false, message: "widge not found",