136 lines
4.0 KiB
TypeScript
136 lines
4.0 KiB
TypeScript
import zoneModel from "../../../shared/model/builder/lines/zone-Model.ts";
|
|
import widget3dModel from "../../../shared/model/vizualization/3dwidget.ts";
|
|
import floatWidgetModel from "../../../shared/model/vizualization/floatWidget.ts";
|
|
import panelModel from "../../../shared/model/vizualization/panelmodel.ts";
|
|
import templateModel from "../../../shared/model/vizualization/templatemodel.ts";
|
|
import widgetModel from "../../../shared/model/vizualization/widgemodel.ts";
|
|
export const setZone = async (data: any) => {
|
|
const { organization, userId, zoneData } = data;
|
|
try {
|
|
const zoneId = zoneData.zoneId;
|
|
const points = zoneData.points;
|
|
const zoneName = zoneData.zoneName;
|
|
const layer = zoneData.layer;
|
|
const viewPortCenter = zoneData.viewPortCenter;
|
|
const viewPortposition = zoneData.viewPortposition;
|
|
const findZoneId = await zoneModel(organization).findOne({
|
|
zoneId: zoneId,
|
|
});
|
|
if (findZoneId) {
|
|
const updateZone = await zoneModel(organization)
|
|
.findOneAndUpdate(
|
|
{ zoneId: zoneId },
|
|
{
|
|
points: points,
|
|
viewPortposition: viewPortposition,
|
|
viewPortCenter: viewPortCenter,
|
|
},
|
|
{ new: true }
|
|
)
|
|
.select("-_id -__v");
|
|
return {
|
|
success: true,
|
|
message: "zone updated",
|
|
data: updateZone,
|
|
organization: organization,
|
|
};
|
|
} else {
|
|
const zoneCreate = await zoneModel(organization).create({
|
|
zoneId,
|
|
createdBy: userId,
|
|
zoneName: zoneName,
|
|
points,
|
|
layer,
|
|
viewPortCenter,
|
|
viewPortposition,
|
|
});
|
|
const createdZone = await zoneModel(organization)
|
|
.findById(zoneCreate._id)
|
|
.select("-_id -__v")
|
|
.lean();
|
|
return {
|
|
success: true,
|
|
message: "zone created",
|
|
data: createdZone,
|
|
organization: organization,
|
|
};
|
|
}
|
|
} catch (error) {
|
|
console.log("error: ", error);
|
|
return {
|
|
success: false,
|
|
message: "Zone not found",
|
|
error,
|
|
organization: organization,
|
|
};
|
|
}
|
|
};
|
|
|
|
export const deleteZone = async (data: any) => {
|
|
const { organization, userId, zoneId } = data;
|
|
try {
|
|
const findZoneId = await zoneModel(organization).findOne({
|
|
zoneId: zoneId,
|
|
});
|
|
if (findZoneId) {
|
|
const deleteZone = await zoneModel(organization)
|
|
.findOneAndDelete({ zoneId: zoneId, createdBy: userId })
|
|
.select("-_id -__v");
|
|
if (deleteZone) {
|
|
const panels = await panelModel(organization).find({ zoneId });
|
|
console.log("panels: ", panels);
|
|
|
|
const allWidgetIds = panels.reduce((ids: string[], panel: any) => {
|
|
return ids.concat(panel.widgets || []);
|
|
}, []);
|
|
|
|
await widgetModel(organization).updateMany(
|
|
{ _id: { $in: allWidgetIds } },
|
|
{ $set: { isArchive: true } }
|
|
);
|
|
|
|
await panelModel(organization).updateMany(
|
|
{ zoneId },
|
|
{ $set: { isArchive: true } }
|
|
);
|
|
|
|
await Promise.all([
|
|
widget3dModel(organization).updateMany(
|
|
{ zoneId },
|
|
{ $set: { isArchive: true } }
|
|
),
|
|
templateModel(organization).updateMany(
|
|
{ zoneId },
|
|
{ $set: { isArchive: true } }
|
|
),
|
|
floatWidgetModel(organization).updateMany(
|
|
{ zoneId },
|
|
{ $set: { isArchive: true } }
|
|
),
|
|
]);
|
|
}
|
|
|
|
return {
|
|
success: true,
|
|
message: "zone deleted",
|
|
data: deleteZone,
|
|
organization: organization,
|
|
};
|
|
} else {
|
|
return {
|
|
success: true,
|
|
message: "Invalid zone ID",
|
|
organization: organization,
|
|
};
|
|
}
|
|
} catch (error) {
|
|
console.log("error: ", error);
|
|
return {
|
|
success: false,
|
|
message: "Zone not found",
|
|
error,
|
|
organization: organization,
|
|
};
|
|
}
|
|
};
|