Files
Schema-Studio/src/api-server/controller/collectionNodeController.ts
2025-10-25 09:25:25 +05:30

853 lines
22 KiB
TypeScript

import { Request, Response } from "express";
import {
addAttributes,
DelAttributes,
delCollection,
DuplicateAttributes,
DuplicateCollection,
GetcollectionLists,
GetcollectionNode,
GetNodesInProject,
Nodecreation,
updatecollection,
UpdateAttributes,
GetcollectionGraphicData,
} from "../../shared/services/collectionService";
import { AuthenticatedRequest } from "../../shared/utils/token";
export const NodeCreationController = async (
req: AuthenticatedRequest,
res: Response
): Promise<void> => {
try {
const { organization, userId } = req.user || {};
const { projectId, position, collectionName, type } = req.body;
const missing = Object.entries({
organization,
projectId,
position,
userId,
collectionName,
})
.filter(([_, v]) => !v)
.map(([k]) => k);
if (missing.length) {
res.status(400).json({
message: `Missing field${missing.length > 1 ? "s" : ""}: ${missing.join(
", "
)}`,
});
return;
}
const data = {
organization: organization as string,
projectId,
position,
type,
collectionName,
userId: userId as string,
};
const result = await Nodecreation(data);
console.log("result:nodecreatge ", result);
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 "Collection node creation unsuccessfull":
res.status(200).json({
message: "Collection node creation unsuccessfull",
});
break;
case "CollectionName already exists":
res.status(200).json({
message: "CollectionName already exists",
});
break;
case "Success":
res.status(200).json({
message: "Node created successfully",
// collectionNodeId: result.data,
data: result.data,
});
break;
case "Validation Error":
res.status(400).json({ message: result.data || "Validation Error" });
break;
default:
res.status(500).json({
message: "Internal server error",
});
break;
}
} catch (error) {
res.status(500).json({
message: "Unknown error",
});
}
};
//casting error handled and missing fields handling updated for testing
export const updateCollectionController = async (
req: AuthenticatedRequest,
res: Response
): Promise<void> => {
try {
const { organization, userId } = req.user || {};
const { collectionNodeId, projectId } = req.params;
const {
collectionName,
position,
backgroundColor,
borderColor,
fontColor,
lineColor,
lineThickness,
fontName,
lineType,
borderThickness,
cornerRadius,
} = req.body;
const missing = Object.entries({
organization,
projectId,
userId,
collectionNodeId,
})
.filter(([_, v]) => !v)
.map(([k]) => k);
if (missing.length) {
res.status(400).json({
message: `Missing field${missing.length > 1 ? "s" : ""}: ${missing.join(
", "
)}`,
});
return;
}
const data = {
organization: organization as string,
projectId,
userId: userId as string,
backgroundColor,
borderColor,
fontColor,
lineColor,
lineThickness,
fontName,
lineType,
borderThickness,
cornerRadius,
collectionNodeId,
collectionName,
position,
};
const result = await updatecollection(data);
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 "Invalid ID":
res.status(400).json({ message: result.data || "Invalid ID provided" });
break;
case "Collection not found":
res.status(200).json({
message: "Collection not found",
});
break;
case "Success":
res.status(200).json({
message: "collection name updated",
});
break;
default:
res.status(500).json({
message: "Internal server error",
});
break;
}
} catch (error) {
res.status(500).json({
message: "Unknown error",
});
}
};
export const CollectionDatas = async (
req: AuthenticatedRequest,
res: Response
): Promise<void> => {
try {
const { organization, userId } = req.user || {};
const { projectId, collectionNodeId } = req.params;
const missing = Object.entries({
organization,
projectId,
userId,
collectionNodeId,
})
.filter(([_, v]) => !v)
.map(([k]) => k);
if (missing.length) {
res.status(400).json({
message: `Missing field${missing.length > 1 ? "s" : ""}: ${missing.join(
", "
)}`,
});
return;
}
const data = {
organization: organization as string,
projectId,
userId: userId as string,
collectionNodeId,
};
const result = await GetcollectionNode(data);
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 "No collection Nodes present":
res.status(200).json({
message: "No collection Nodes present",
Collections: result.data,
});
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",
});
}
};
export const graphicDatas = async (
req: AuthenticatedRequest,
res: Response
): Promise<void> => {
try {
const { organization, userId } = req.user || {};
const { projectId, collectionNodeId } = req.params;
const missing = Object.entries({
organization,
projectId,
userId,
collectionNodeId,
})
.filter(([_, v]) => !v)
.map(([k]) => k);
if (missing.length) {
res.status(400).json({
message: `Missing field${missing.length > 1 ? "s" : ""}: ${missing.join(
", "
)}`,
});
return;
}
const data = {
organization: organization as string,
projectId,
userId: userId as string,
collectionNodeId,
};
const result = await GetcollectionGraphicData(data);
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 "No collection Nodes present":
res.status(200).json({
message: "No collection Nodes present",
Collections: result.data,
});
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",
});
}
};
export const DeleteCollectionsController = async (
req: AuthenticatedRequest,
res: Response
): Promise<void> => {
try {
const { organization, userId } = req.user || {};
const { projectId, collectionNodeId } = req.params;
const missing = Object.entries({
organization,
projectId,
userId,
collectionNodeId,
})
.filter(([_, v]) => !v)
.map(([k]) => k);
if (missing.length) {
res.status(400).json({
message: `Missing field${missing.length > 1 ? "s" : ""}: ${missing.join(
", "
)}`,
});
return;
}
const data = {
organization: organization as string,
projectId,
userId: userId as string,
collectionNodeId,
};
const result = await delCollection(data);
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 "Collection not found":
res.status(200).json({
message: "Collection not found",
});
break;
case "Success":
res.status(200).json({ message: "Collection deleted successfully" });
break;
default:
res.status(500).json({
message: "Internal server error",
});
break;
}
} catch (error) {
res.status(500).json({
message: "Unknown error",
});
}
};
export const DuplicateNodeCollectionController = async (
req: AuthenticatedRequest,
res: Response
): Promise<void> => {
try {
const { organization, userId } = req.user || {};
const { collectionNodeId } = req.params;
const { projectId, collectionName, position, attributes } = req.body;
const missing = Object.entries({
organization,
projectId,
userId,
collectionNodeId,
})
.filter(([_, v]) => !v)
.map(([k]) => k);
if (missing.length) {
res.status(400).json({
message: `Missing field${missing.length > 1 ? "s" : ""}: ${missing.join(
", "
)}`,
});
return;
}
const data = {
organization: organization as string,
projectId,
userId: organization as string,
collectionName,
position,
attributes,
collectionNodeId,
};
const result = await DuplicateCollection(data);
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 "Duplication unsuccessfull":
res.status(200).json({
message: "Duplication unsuccessfull",
});
break;
case "Success":
res.status(200).json({ message: "Duplicated successfully" });
break;
default:
res.status(500).json({
message: "Internal server error",
});
break;
}
} catch (error) {
res.status(500).json({
message: "Unknown error",
});
}
};
export const NodesCollectionsBasedOnproject = async (
req: AuthenticatedRequest,
res: Response
): Promise<void> => {
try {
const { organization, userId } = req.user || {};
const { projectId } = req.params;
const missing = Object.entries({
organization,
projectId,
userId,
})
.filter(([_, v]) => !v)
.map(([k]) => k);
if (missing.length) {
res.status(400).json({
message: `Missing field${missing.length > 1 ? "s" : ""}: ${missing.join(
", "
)}`,
});
return;
}
const data = {
organization: organization as string,
projectId,
userId: userId as string,
};
const result = await GetNodesInProject(data);
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 "No collection Nodes present":
res.status(200).json({
message: "No collection Nodes present",
Collections: result.data,
});
break;
case "Success":
res.status(200).json({ Collections: result.data });
break;
default:
res.status(500).json({
message: "Internal server error",
});
break;
}
} catch (error) {
res.status(500).json({
message: "Unknown error",
});
}
};
export const AddAttributesController = async (
req: AuthenticatedRequest,
res: Response
): Promise<void> => {
try {
const { organization, userId } = req.user || {};
// const { collectionNodeId } = req.params;
const { projectId, attributes, collectionNodeId } = req.body;
const missing = Object.entries({
organization,
projectId,
userId,
attributes,
collectionNodeId,
})
.filter(([_, v]) => !v)
.map(([k]) => k);
if (missing.length) {
res.status(400).json({
message: `Missing field${missing.length > 1 ? "s" : ""}: ${missing.join(
", "
)}`,
});
return;
}
const data = {
organization: organization as string,
projectId,
userId: userId as string,
collectionNodeId,
attributes,
};
const result = await addAttributes(data);
console.log("result:addattri ", result);
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 "Subcollection already added for the object data":
res.status(200).json({
message: "Subcollection already added for the object data",
});
break;
case "Invalid ObjectId":
res.status(400).json({ message: "Invalid Id" });
break;
case "Collection not found":
res.status(200).json({
message: "Collection not found",
});
break;
case "Success":
res.status(200).json({
message: "collection Attributes Added",
fieldId: result.data,
});
break;
default:
res.status(500).json({
message: "Internal server error",
});
break;
}
} catch (error) {
res.status(500).json({
message: "Unknown error",
});
}
};
export const updateAttributesCollections = async (
req: AuthenticatedRequest,
res: Response
): Promise<void> => {
try {
const { organization, userId } = req.user || {};
const { collectionNodeId } = req.params;
const { projectId, attributes } = req.body;
const missing = Object.entries({
organization,
projectId,
userId,
collectionNodeId,
})
.filter(([_, v]) => !v)
.map(([k]) => k);
if (missing.length) {
res.status(400).json({
message: `Missing field${missing.length > 1 ? "s" : ""}: ${missing.join(
", "
)}`,
});
return;
}
const data = {
organization: organization as string,
projectId,
userId: userId as string,
collectionNodeId,
attributes,
};
const result = await UpdateAttributes(data);
console.log("result:d ", result);
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 "Collection not found":
res.status(200).json({
message: "Collection not found",
});
break;
case "Success":
res.status(200).json({ message: "Field updated successfully" });
break;
default:
res.status(500).json({
message: "Internal server error",
});
break;
}
} catch (error) {
res.status(500).json({
message: "Unknown error",
});
}
};
export const delAttributesCollections = async (
req: AuthenticatedRequest,
res: Response
): Promise<void> => {
try {
const { organization, userId } = req.user || {};
const { collectionNodeId } = req.params;
const { projectId, fieldId } = req.body;
const missing = Object.entries({
organization,
projectId,
userId,
collectionNodeId,
fieldId,
})
.filter(([_, v]) => !v)
.map(([k]) => k);
if (missing.length) {
res.status(400).json({
message: `Missing field${missing.length > 1 ? "s" : ""}: ${missing.join(
", "
)}`,
});
return;
}
const data = {
organization: organization as string,
userId: userId as string,
projectId,
collectionNodeId,
fieldId,
};
const result = await DelAttributes(data);
console.log("result:delatt ", result);
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 "Collection not found":
res.status(404).json({
message: "Collection not found",
});
break;
case "Attribute not found":
res.status(200).json({
message: "field not found",
});
break;
case "Success":
res.status(200).json({ message: "Field deleted successfully" });
break;
default:
res.status(500).json({
message: "Internal server error",
});
break;
}
} catch (error) {
res.status(500).json({
message: "Unknown error",
});
}
};
export const duplicateAttributesCollections = async (
req: AuthenticatedRequest,
res: Response
): Promise<void> => {
try {
const { organization, userId } = req.user || {};
const { projectId, attrKey, collectionNodeId } = req.body;
// if (
// !organization ||
// !projectId ||
// !collectionNodeId ||
// !attrKey ||
// !userId
// ) {
// res.status(400).json({
// message: "All fields are required",
// });
// return;
// }
const missing = Object.entries({
organization,
projectId,
userId,
collectionNodeId,
attrKey,
})
.filter(([_, v]) => !v)
.map(([k]) => k);
if (missing.length) {
res.status(400).json({
message: `Missing field${missing.length > 1 ? "s" : ""}: ${missing.join(
", "
)}`,
});
return;
}
const data = {
organization: organization as string,
userId: userId as string,
projectId,
collectionNodeId,
attrKey,
};
const result = await DuplicateAttributes(data);
console.log("result:duplicateatt ", result);
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 "CollectionId not found for duplication":
res.status(200).json({
message: "CollectionId not found for duplication",
});
break;
case "Attribute doesnot match":
res.status(200).json({
message: "Attribute doesnot match",
});
break;
case "Success":
res.status(200).json({
message: "Attributes duplicated successfully",
data: result.data,
});
break;
default:
res.status(500).json({
message: "Internal server error",
});
break;
}
} catch (error) {
res.status(500).json({
message: "Unknown error",
});
}
};
export const collectionListsController = async (
req: AuthenticatedRequest,
res: Response
): Promise<void> => {
try {
const { organization, userId } = req.user || {};
const { projectId } = req.params;
const missing = Object.entries({
organization,
projectId,
userId,
})
.filter(([_, v]) => !v)
.map(([k]) => k);
if (missing.length) {
res.status(400).json({
message: `Missing field${missing.length > 1 ? "s" : ""}: ${missing.join(
", "
)}`,
});
return;
}
const data = {
organization: organization as string,
projectId,
userId: userId as string,
};
const result = await GetcollectionLists(data);
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 "Collection not found":
res.status(200).json({
message: "Collection not found",
Collections: result.data,
});
break;
case "Success":
res.status(200).json({ Collections: result.data });
break;
default:
res.status(500).json({
message: "Internal server error",
});
break;
}
} catch (error) {
res.status(500).json({
message: "Unknown error",
});
}
};