zone datas delete function updates

This commit is contained in:
2025-04-07 17:30:41 +05:30
parent eaaf6a672c
commit e7216d9116

View File

@@ -1,4 +1,9 @@
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 {
@@ -39,6 +44,35 @@ export const deleteZone = async (data: any) => {
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);
// Collect all widget IDs from all panels
const allWidgetIds = panels.reduce((ids: string[], panel: any) => {
return ids.concat(panel.widgets || []);
}, []);
// Soft delete all widgets
await widgetModel(organization).updateMany(
{ _id: { $in: allWidgetIds } },
{ $set: { isArchive: true } }
);
// Soft delete all panels for the zone
await panelModel(organization).updateMany(
{ zoneId },
{ $set: { isArchive: true } }
);
// Soft delete from other models
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 {
@@ -48,4 +82,60 @@ export const deleteZone = async (data: any) => {
console.log('error: ', error);
return { success: false, message: 'Zone not found', error, organization: organization }
}
}
}
// export const zoneDatasDeleted = async (data: { organization: string; userId: string; zoneId: string }) => {
// const { organization, userId, zoneId } = data;
// console.log('data: ', data);
// try {
// const findZone = await zoneModel(organization).findOne({ zoneId });
// if (!findZone) {
// return { success: false, message: 'Invalid zone ID', organization };
// }
// // Soft delete the zone
// const deleteZone = await zoneModel(organization).findOneAndDelete(
// { zoneId, createdBy: userId },
// );
// if (deleteZone) {
// const panels = await panelModel(organization).find({ zoneId });
// console.log('panels: ', panels);
// // Collect all widget IDs from all panels
// const allWidgetIds = panels.reduce((ids: string[], panel: any) => {
// return ids.concat(panel.widgets || []);
// }, []);
// console.log('allWidgetIds: ', allWidgetIds);
// // Soft delete all widgets
// await widgetModel(organization).updateMany(
// { _id: { $in: allWidgetIds } },
// { $set: { isArchive: true } }
// );
// // Soft delete all panels for the zone
// await panelModel(organization).updateMany(
// { zoneId },
// { $set: { isArchive: true } }
// );
// // Soft delete from other models
// await Promise.all([
// widget3dModel(organization).updateMany({ zoneId }, { $set: { isArchive: true } }),
// templateModel(organization).updateMany({ zoneId }, { $set: { isArchive: true } }),
// floatWidgetModel(organization).updateMany({ zoneId }, { $set: { isArchive: true } }),
// ]);
// }
// console.log("Zone and associated data archived");
// return { success: true, message: 'Zone and associated data archived', data: deleteZone, organization };
// return { success: true, message: 'Zone and associated data deleted', data: deleteZone, organization };
// } catch (error) {
// console.error('Zone deletion error:', error);
// return { success: false, message: 'An error occurred while deleting the zone', error, organization };
// }
// };