216 lines
5.1 KiB
TypeScript
216 lines
5.1 KiB
TypeScript
import { Response } from "express";
|
|
import { AuthenticatedRequest } from "../../../../shared/utils/token.ts";
|
|
import {
|
|
AddTemplate,
|
|
AddTemplateToZone,
|
|
GetAllTemplates,
|
|
TemplateDelete,
|
|
} from "../../../../shared/services/visualization/templateService.ts";
|
|
|
|
export const AddTemplateController = async (
|
|
req: AuthenticatedRequest,
|
|
res: Response
|
|
): Promise<void> => {
|
|
try {
|
|
const { userId, organization } = req.user || {};
|
|
const { template, projectId } = req.body;
|
|
if (!userId || !organization || !template || !projectId) {
|
|
res.status(400).json({
|
|
message: "All fields are required",
|
|
});
|
|
return;
|
|
}
|
|
const result = await AddTemplate({
|
|
organization,
|
|
template,
|
|
projectId,
|
|
userId,
|
|
});
|
|
|
|
switch (result.status) {
|
|
case "User not found":
|
|
res.status(404).json({
|
|
message: "User not found",
|
|
});
|
|
break;
|
|
case "TemplateID alreay exists":
|
|
res.status(200).json({
|
|
message: "TemplateID alreay exists",
|
|
});
|
|
break;
|
|
case "Template not saved":
|
|
res.status(200).json({
|
|
message: "Template not saved",
|
|
});
|
|
break;
|
|
case "Success":
|
|
res.status(200).json({
|
|
message: "Template saved successfully",
|
|
});
|
|
break;
|
|
default:
|
|
res.status(500).json({
|
|
message: "Internal server error",
|
|
});
|
|
break;
|
|
}
|
|
} catch (error) {
|
|
res.status(500).json({
|
|
message: "Unknown error",
|
|
});
|
|
return;
|
|
}
|
|
};
|
|
export const AddTemToZoneController = async (
|
|
req: AuthenticatedRequest,
|
|
res: Response
|
|
): Promise<void> => {
|
|
try {
|
|
const { userId, organization } = req.user || {};
|
|
const { templateID, projectId, zoneId } = req.body;
|
|
if (!userId || !organization || !templateID || !projectId || !zoneId) {
|
|
res.status(400).json({
|
|
message: "All fields are required",
|
|
});
|
|
return;
|
|
}
|
|
const result = await AddTemplateToZone({
|
|
organization,
|
|
templateID,
|
|
zoneId,
|
|
projectId,
|
|
userId,
|
|
});
|
|
switch (result.status) {
|
|
case "User not found":
|
|
res.status(404).json({
|
|
message: "User not found",
|
|
});
|
|
break;
|
|
case "Zone not found":
|
|
res.status(404).json({
|
|
message: "Zone not found",
|
|
});
|
|
break;
|
|
case "TemplateID not found":
|
|
res.status(409).json({
|
|
message: "TemplateID not found",
|
|
});
|
|
break;
|
|
case "Success":
|
|
res.status(201).json({
|
|
message: "Template placed in Zone",
|
|
data: result.data,
|
|
});
|
|
break;
|
|
default:
|
|
res.status(500).json({
|
|
message: "Internal server error",
|
|
});
|
|
break;
|
|
}
|
|
} catch (error) {
|
|
res.status(500).json({
|
|
message: "Unknown error",
|
|
});
|
|
return;
|
|
}
|
|
};
|
|
export const TemplateDeleteController = async (
|
|
req: AuthenticatedRequest,
|
|
res: Response
|
|
): Promise<void> => {
|
|
try {
|
|
const { userId, organization } = req.user || {};
|
|
const { templateID, projectId } = req.body;
|
|
if (!userId || !organization || !templateID || !projectId) {
|
|
res.status(400).json({
|
|
message: "All fields are required",
|
|
});
|
|
return;
|
|
}
|
|
const result = await TemplateDelete({
|
|
userId,
|
|
organization,
|
|
templateID,
|
|
projectId,
|
|
});
|
|
switch (result.status) {
|
|
case "User not found":
|
|
res.status(404).json({
|
|
message: "User not found",
|
|
});
|
|
break;
|
|
case "Template not found":
|
|
res.status(404).json({
|
|
message: "Template not found",
|
|
});
|
|
break;
|
|
case "Template not Deleted":
|
|
res.status(200).json({
|
|
message: "Template not Deleted",
|
|
});
|
|
break;
|
|
case "Success":
|
|
res.status(200).json({
|
|
message: "Template 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 GetTemplateController = async (
|
|
req: AuthenticatedRequest,
|
|
res: Response
|
|
): Promise<void> => {
|
|
try {
|
|
const { userId, organization } = req.user || {};
|
|
const { projectId } = req.params;
|
|
if (!userId || !organization || !projectId) {
|
|
res.status(400).json({
|
|
message: "All fields are required",
|
|
});
|
|
return;
|
|
}
|
|
const result = await GetAllTemplates({
|
|
organization,
|
|
userId,
|
|
projectId,
|
|
});
|
|
switch (result.status) {
|
|
case "User not found":
|
|
res.status(404).json({
|
|
message: "User not found",
|
|
});
|
|
break;
|
|
case "All Datas":
|
|
res.status(404).json([]);
|
|
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;
|
|
}
|
|
};
|