107 lines
3.5 KiB
TypeScript
107 lines
3.5 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 },
|
|
{ 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({
|
|
widgetName: widget.type,
|
|
widgetID: widget.id,
|
|
position: widget.position,
|
|
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.widgetName,
|
|
id: widget.widgetID,
|
|
position: widget.position,
|
|
}));
|
|
return res.status(200).json(zonebasedWidget);
|
|
} catch (error: any) {
|
|
return res.status(500).send(error.message);
|
|
}
|
|
}
|
|
// static async update3Dposition(req: Request, res: Response): Promise<any> {
|
|
// try {
|
|
// const { organization, id, position, zoneId } = req.body;
|
|
// const existing3Dwidget = await widget3dModel(organization).findOne({
|
|
// widgetID: id,
|
|
// isArchive: false,
|
|
// });
|
|
// if (existing3Dwidget) {
|
|
// const update3dwidget = await widget3dModel(
|
|
// organization
|
|
// ).findOneAndUpdate(
|
|
// {
|
|
// widgetID: id,
|
|
// zoneId: zoneId,
|
|
// isArchive: false,
|
|
// },
|
|
// { position: position },
|
|
// { upsert: true, new: true }
|
|
// );
|
|
// if (update3dwidget)
|
|
// return res.status(200).send("widget update successfully");
|
|
// } else {
|
|
// return res.status(404).send("widget not found");
|
|
// }
|
|
// } catch (error: any) {
|
|
// return res.status(500).send(error.message);
|
|
// }
|
|
// }
|
|
}
|