177 lines
5.9 KiB
TypeScript
177 lines
5.9 KiB
TypeScript
import { Request, Response } from "express";
|
|
import zoneSchema from "../../../shared/model/builder/lines/zone-Model.ts";
|
|
import widget3dModel from "../../../shared/model/vizualization/3dwidget.ts";
|
|
export class Widget3dService {
|
|
static async add3Dwidget(req: Request, res: Response): Promise<any> {
|
|
try {
|
|
const { organization, widget, zoneId } = req.body;
|
|
const existingZone = await zoneSchema(organization).findOne({
|
|
zoneId: zoneId,
|
|
isArchive: false,
|
|
});
|
|
if (!existingZone)
|
|
return res.status(404).json({ message: "Zone not found" });
|
|
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,
|
|
Data: {
|
|
measurements: widget?.Data.measurements,
|
|
duration: widget?.Data.duration,
|
|
},
|
|
widgetName: widget?.widgetName,
|
|
},
|
|
{ upsert: true, new: true }
|
|
);
|
|
if (update3dwidget)
|
|
return res
|
|
.status(200)
|
|
.json({ message: "widget update successfully" });
|
|
else return res.send("Widget not updated");
|
|
}
|
|
const newWidget3d = await widget3dModel(organization).create({
|
|
type: widget.type,
|
|
widgetID: widget.id,
|
|
position: widget.position,
|
|
rotation: widget.rotation,
|
|
zoneId,
|
|
});
|
|
if (newWidget3d)
|
|
return res.status(201).json({ message: "Widget created successfully" });
|
|
} catch (error: any) {
|
|
return res.status(500).send(error.message);
|
|
}
|
|
}
|
|
static async get3Dwiget(req: Request, res: Response): Promise<any> {
|
|
try {
|
|
const { organization, zoneId } = req.params;
|
|
const existingZone = await zoneSchema(organization).findOne({
|
|
zoneId: zoneId,
|
|
isArchive: false,
|
|
});
|
|
if (!existingZone) return res.send("Zone not found");
|
|
const widgetData = await widget3dModel(organization).find({
|
|
zoneId: zoneId,
|
|
isArchive: false,
|
|
});
|
|
if (!widgetData || widgetData.length === 0) {
|
|
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 res.status(200).json(zonebasedWidget);
|
|
} catch (error: any) {
|
|
return res.status(500).send(error.message);
|
|
}
|
|
}
|
|
static async update3DpositionRotation(
|
|
req: Request,
|
|
res: Response
|
|
): Promise<any> {
|
|
try {
|
|
const { organization, id, position, rotation, zoneId } = req.body;
|
|
const existingZone = await zoneSchema(organization).findOne({
|
|
zoneId: zoneId,
|
|
isArchive: false,
|
|
});
|
|
if (!existingZone)
|
|
return res.status(404).json({ message: "Zone not found" });
|
|
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)
|
|
return res.status(200).json("widget3D update successfully");
|
|
} else {
|
|
return res.send("widget not found");
|
|
}
|
|
} catch (error: any) {
|
|
return res.status(500).send(error.message);
|
|
}
|
|
}
|
|
static async getSingle3Dwidget(req: Request, res: Response): Promise<any> {
|
|
const { organization, id } = req.params;
|
|
try {
|
|
const widgetData = await widget3dModel(organization).findOne({
|
|
widgetID: id,
|
|
isArchive: false,
|
|
});
|
|
if (!widgetData) return res.status(204).json({ message: "Widget not found" });
|
|
const structureData = {
|
|
measurements: widgetData?.Data?.measurements,
|
|
duration: widgetData?.Data?.duration,
|
|
};
|
|
const widgetName = widgetData.widgetName;
|
|
return res.status(200).json({ Data: structureData, widgetName });
|
|
} catch (error: any) {
|
|
return res.status(500).send(error.message);
|
|
}
|
|
}
|
|
static async delete3Dwidget(req: Request, res: Response): Promise<any> {
|
|
const { organization, id, zoneId } = req.body;
|
|
try {
|
|
const existingZone = await zoneSchema(organization).findOne({
|
|
zoneId: zoneId,
|
|
isArchive: false,
|
|
});
|
|
if (!existingZone)
|
|
return res.status(404).json({ message: "Zone not found" });
|
|
const existing3Dwidget = await widget3dModel(organization).findOne({
|
|
widgetID: id,
|
|
isArchive: false,
|
|
zoneId: zoneId,
|
|
});
|
|
if (!existing3Dwidget) {
|
|
return res.send("3D widget not found for the ID");
|
|
}
|
|
const updateWidget = await widget3dModel(organization).findOneAndUpdate(
|
|
{
|
|
widgetID: id,
|
|
zoneId: zoneId,
|
|
isArchive: false,
|
|
},
|
|
{ isArchive: true },
|
|
{ new: true }
|
|
);
|
|
if (!updateWidget)
|
|
return res.json({ message: "3DWidget delete unsuccessfull" });
|
|
return res.status(200).json({ message: "3DWidget deleted successfully" });
|
|
} catch (error: any) {
|
|
return res.status(500).send(error.message);
|
|
}
|
|
}
|
|
}
|