131 lines
3.2 KiB
TypeScript
131 lines
3.2 KiB
TypeScript
|
|
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<void> => {
|
||
|
|
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<void> => {
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
};
|