Files
Dwinzo-Backend-V0.0/src/api-server/V1/v1Controllers/vizualizationController/v1widgetController.ts

272 lines
6.5 KiB
TypeScript

import { Response } from "express";
import { AuthenticatedRequest } from "../../../../shared/utils/token.ts";
import {
AddWidget,
GetWidget,
UpdateWidget,
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, zoneUuid } = req.body;
if (!userId || !organization || !widget || !projectId || !zoneUuid) {
res.status(400).json({
message: "All fields are required",
});
return;
}
const result = await AddWidget({
organization,
widget,
projectId,
userId,
zoneUuid,
});
switch (result.status) {
case "User not found":
res.status(404).json({
message: "User not found",
});
break;
case "Project not found":
res.status(404).json({
message: "Project not found",
});
break;
case "Zone not found for the zoneUuid":
res.status(404).json({
message: "Zone not found",
});
break;
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, zoneUuid } = req.body;
if (!userId || !organization || !widgetID || !projectId || !zoneUuid) {
res.status(400).json({
message: "All fields are required",
});
return;
}
const result = await WidgetDelete({
organization,
widgetID,
zoneUuid,
projectId,
userId,
});
switch (result.status) {
case "User not found":
res.status(404).json({
message: "User not found",
});
break;
case "Project not found":
res.status(404).json({
message: "Project not found",
});
break;
case "Zone not found for the zoneUuid":
res.status(404).json({
message: "Zone not found for the zoneUuid",
});
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;
}
};
export const WidgetUpdateController = async (
req: AuthenticatedRequest,
res: Response
): Promise<void> => {
try {
const { userId, organization } = req.user || {};
const { values, projectId, zoneUuid, widgetID } = req.body;
if (
!userId ||
!organization ||
!widgetID ||
!values ||
!projectId ||
!zoneUuid
) {
res.status(400).json({
message: "All fields are required",
});
return;
}
const result = await UpdateWidget({
organization,
values,
zoneUuid,
widgetID,
projectId,
userId,
});
switch (result.status) {
case "User not found":
res.status(404).json({
message: "User not found",
});
break;
case "Project not found":
res.status(404).json({
message: "Project not found",
});
break;
case "Zone not found for the zoneUuid":
res.status(404).json({
message: "Zone not found for the zoneUuid",
});
break;
case "Data not found":
res.status(409).json({
message: "Data not found",
});
break;
case "Success":
res.status(200).json({
message: "Widget updated successfully",
});
break;
default:
res.status(500).json({
message: "Internal server error",
});
break;
}
} catch (error) {
res.status(500).json({
message: "Unknown error",
});
return;
}
};
export const GetWidgetController = async (
req: AuthenticatedRequest,
res: Response
): Promise<void> => {
try {
const { userId, organization } = req.user || {};
const { projectId, zoneUuid, widgetID } = req.query as {
projectId: string;
zoneUuid: string;
widgetID: string;
};
if (!userId || !organization || !widgetID || !projectId || !zoneUuid) {
res.status(400).json({
message: "All fields are required",
});
return;
}
const result = await GetWidget({
organization,
zoneUuid,
widgetID,
projectId,
userId,
});
switch (result.status) {
case "User not found":
res.status(404).json({
message: "User not found",
});
break;
case "Project not found":
res.status(404).json({
message: "Project not found",
});
break;
case "Zone not found for the zoneUuid":
res.status(404).json({
message: "Zone not found for the zoneUuid",
});
break;
case "Widget not found for the widgetID":
res.status(409).json({
message: "Widget not found for the widgetID",
});
break;
case "Success":
res.status(200).json(result.data);
break;
default:
res.status(500).json({
message: "Internal server error",
});
break;
}
} catch (error) {
res.status(500).json({
message: "Unknown error",
});
return;
}
};