From 38ac249f3b44f7d781fdba15456c3aeff29c11d8 Mon Sep 17 00:00:00 2001 From: Nivetharamesh Date: Wed, 27 Aug 2025 17:43:27 +0530 Subject: [PATCH] collection edge collaborated API --- .../controller/collectionNodeController.ts | 422 +++++++++--------- src/api-server/controller/edgeController.ts | 1 - .../controller/fileModelController.ts | 3 - .../controller/projectController.ts | 47 ++ src/api-server/routes/collectionRoutes.ts | 30 +- src/api-server/routes/projectRoutes.ts | 6 +- src/shared/services/collectionService.ts | 12 +- src/shared/services/projectService.ts | 51 +++ 8 files changed, 337 insertions(+), 235 deletions(-) diff --git a/src/api-server/controller/collectionNodeController.ts b/src/api-server/controller/collectionNodeController.ts index 1fbdfce..fa6d193 100644 --- a/src/api-server/controller/collectionNodeController.ts +++ b/src/api-server/controller/collectionNodeController.ts @@ -29,7 +29,6 @@ export const NodeCreationController = async ( position, }; const result = await Nodecreation(data); - console.log("result: ", result); switch (result.status) { case "project not found": @@ -115,210 +114,6 @@ export const SetCollectionNameController = async ( }); } }; -export const AddAttributesController = async ( - req: Request, - res: Response -): Promise => { - try { - const { organization, projectId, collectionNodeId, attributes } = req.body; - if (!organization || !projectId || !attributes || !collectionNodeId) { - res.status(400).json({ - message: "All fields are required", - }); - return; - } - const data = { - organization, - projectId, - collectionNodeId, - attributes, - }; - const result = await addAttributes(data); - switch (result.status) { - case "project not found": - res.status(200).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 Attributes Added", - }); - 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: Request, - res: Response -): Promise => { - try { - const { projectId, organization } = req.params; - if (!organization || !projectId) { - res.status(400).json({ - message: "All fields are required", - }); - return; - } - const data = { - organization, - projectId, - }; - const result = await GetNodesInProject(data); - switch (result.status) { - case "project not found": - res.status(200).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 updateAttributesCollections = async ( - req: Request, - res: Response -): Promise => { - try { - const { - organization, - projectId, - collectionNodeId, - AttributeId, - required, - defaultValue, - unique, - index, - key, - type, - } = req.body; - if (!organization || !projectId || !collectionNodeId || !AttributeId) { - res.status(400).json({ - message: "All fields are required", - }); - return; - } - const data = { - organization, - projectId, - collectionNodeId, - AttributeId, - required, - defaultValue, - unique, - index, - key, - type, - }; - const result = await UpdateAttributes(data); - switch (result.status) { - case "project not found": - res.status(200).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: Request, - res: Response -): Promise => { - try { - const { organization, projectId, collectionNodeId, AttributeId } = req.body; - if (!organization || !projectId || !collectionNodeId || !AttributeId) { - res.status(400).json({ - message: "All fields are required", - }); - return; - } - const data = { - organization, - projectId, - collectionNodeId, - AttributeId, - }; - const result = await DelAttributes(data); - console.log("result: ", result); - switch (result.status) { - case "project not found": - res.status(200).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({ 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 CollectionDatas = async ( req: Request, res: Response @@ -413,15 +208,9 @@ export const DuplicateNodeCollectionController = async ( res: Response ): Promise => { try { - const { - projectId, - organization, - collectionName, - position, - collectionNodeId, - attributes, - } = req.body; - console.log("req.body;: ", req.body); + const { collectionNodeId } = req.params; + const { projectId, organization, collectionName, position, attributes } = + req.body; if (!organization || !projectId || !collectionNodeId) { res.status(400).json({ message: "All fields are required", @@ -463,3 +252,208 @@ export const DuplicateNodeCollectionController = async ( }); } }; + + +export const NodesCollectionsBasedOnproject = async ( + req: Request, + res: Response +): Promise => { + try { + const { projectId, organization } = req.params; + if (!organization || !projectId) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const data = { + organization, + projectId, + }; + const result = await GetNodesInProject(data); + switch (result.status) { + case "project not found": + res.status(200).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: Request, + res: Response +): Promise => { + try { + const { collectionNodeId } = req.params; + const { organization, projectId, attributes } = req.body; + if (!organization || !projectId || !attributes || !collectionNodeId) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const data = { + organization, + projectId, + collectionNodeId, + attributes, + }; + const result = await addAttributes(data); + switch (result.status) { + case "project not found": + res.status(200).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 Attributes Added", + }); + 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: Request, + res: Response +): Promise => { + try { + const { collectionNodeId, attributeId } = req.params; + const { + organization, + projectId, + required, + defaultValue, + unique, + index, + key, + type, + } = req.body; + if (!organization || !projectId || !collectionNodeId || !attributeId) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const data = { + organization, + projectId, + collectionNodeId, + attributeId, + required, + defaultValue, + unique, + index, + key, + type, + }; + const result = await UpdateAttributes(data); + switch (result.status) { + case "project not found": + res.status(200).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: Request, + res: Response +): Promise => { + try { + const { collectionNodeId } = req.params; + const { organization, projectId, AttributeId } = req.body; + if (!organization || !projectId || !collectionNodeId || !AttributeId) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const data = { + organization, + projectId, + collectionNodeId, + AttributeId, + }; + const result = await DelAttributes(data); + switch (result.status) { + case "project not found": + res.status(200).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({ message: "Field deleted successfully" }); + break; + default: + res.status(500).json({ + message: "Internal server error", + }); + break; + } + } catch (error) { + res.status(500).json({ + message: "Unknown error", + }); + } +}; + diff --git a/src/api-server/controller/edgeController.ts b/src/api-server/controller/edgeController.ts index 0b2eac2..4163099 100644 --- a/src/api-server/controller/edgeController.ts +++ b/src/api-server/controller/edgeController.ts @@ -20,7 +20,6 @@ to, cardinality }; const result = await edgecreation(data); - console.log('result: ', result); switch (result.status) { case "project not found": diff --git a/src/api-server/controller/fileModelController.ts b/src/api-server/controller/fileModelController.ts index 76096b5..52de007 100644 --- a/src/api-server/controller/fileModelController.ts +++ b/src/api-server/controller/fileModelController.ts @@ -7,13 +7,10 @@ export const fileModelCreatecontroller = async ( req: Request, res: Response ): Promise => { - console.log("req.body: ", req.body); const { pathName, baseId, modelName, organization, attributes } = req.body; try { const findBaseName = await baseType(organization).findById(baseId); - console.log("baseId: ", baseId); - console.log("findBaseName: ", findBaseName); const attrToSchema = (attr: any) => { const mapType: any = { diff --git a/src/api-server/controller/projectController.ts b/src/api-server/controller/projectController.ts index edcbe5f..9771fd8 100644 --- a/src/api-server/controller/projectController.ts +++ b/src/api-server/controller/projectController.ts @@ -1,5 +1,6 @@ import { Request, Response } from "express"; import { + GetNodesInProject, projectCreationService, projectDatas, } from "../../shared/services/projectService"; @@ -119,3 +120,49 @@ export const getProjects = async ( }); } }; + + +export const NodesCollectionsBasedOnproject = async ( + req: Request, + res: Response +): Promise => { + try { + const { projectId, organization } = req.params; + if (!organization || !projectId) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const data = { + organization, + projectId, + }; + const result = await GetNodesInProject(data); + switch (result.status) { + case "project not found": + res.status(200).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", + }); + } +}; \ No newline at end of file diff --git a/src/api-server/routes/collectionRoutes.ts b/src/api-server/routes/collectionRoutes.ts index ca32ed8..2ccd0e2 100644 --- a/src/api-server/routes/collectionRoutes.ts +++ b/src/api-server/routes/collectionRoutes.ts @@ -13,34 +13,46 @@ import { const collectionNodeRoutes = express.Router(); //Node creation -collectionNodeRoutes.post("/NewNode", NodeCreationController); +collectionNodeRoutes.post("/nodes", NodeCreationController); //collection Added -collectionNodeRoutes.patch("/collectionName", SetCollectionNameController); +collectionNodeRoutes.patch( + "/nodes/collectionName", + SetCollectionNameController +); //duplicate collection collectionNodeRoutes.post( - "/duplicateCollection", + "/nodes/:collectionNodeId/duplicate", DuplicateNodeCollectionController ); //particular collection data collectionNodeRoutes.get( - "/collectionNodeById/:projectId/:collectionNodeId/:organization", + "/nodes/:organization/:projectId/:collectionNodeId", CollectionDatas ); //delete collection collectionNodeRoutes.patch( - "/delCollection/:projectId/:collectionNodeId/:organization", + "/nodes/:organization/:projectId/:collectionNodeId", DeleteCollectionsController ); //Add fields -collectionNodeRoutes.patch("/AddAttributes", AddAttributesController); +collectionNodeRoutes.patch( + "/nodes/:collectionNodeId/attributes", + AddAttributesController +); //Collections and fiels based on the project collectionNodeRoutes.get( - "/collectionNodes/:projectId/:organization", + "/nodes/:organization/:projectId", NodesCollectionsBasedOnproject ); //update fields -collectionNodeRoutes.patch("/Attributeupdate", updateAttributesCollections); +collectionNodeRoutes.patch( + "/nodes/:collectionNodeId/attributes/:attributeId", + updateAttributesCollections +); //delete fields -collectionNodeRoutes.patch("/softDelAttribute", delAttributesCollections); +collectionNodeRoutes.patch( + "/nodes/:collectionNodeId/attributes/softDelete", + delAttributesCollections +); export default collectionNodeRoutes; diff --git a/src/api-server/routes/projectRoutes.ts b/src/api-server/routes/projectRoutes.ts index 1676c04..8441c61 100644 --- a/src/api-server/routes/projectRoutes.ts +++ b/src/api-server/routes/projectRoutes.ts @@ -1,8 +1,12 @@ import express from "express"; -import { projectCreationController } from "../controller/projectController"; +import { NodesCollectionsBasedOnproject, projectCreationController } from "../controller/projectController"; const projectRoutes = express.Router(); projectRoutes.post("/Newproject", projectCreationController); +projectRoutes.get( + "/nodes/:organization/:projectId", + NodesCollectionsBasedOnproject +); // appRoutes.post("/createfileModel", fileModelCreatecontroller); export default projectRoutes; diff --git a/src/shared/services/collectionService.ts b/src/shared/services/collectionService.ts index d915ee6..3e54f16 100644 --- a/src/shared/services/collectionService.ts +++ b/src/shared/services/collectionService.ts @@ -40,7 +40,7 @@ interface IAttributesEdit { projectId: string; organization: string; collectionNodeId: string; - AttributeId: string; + attributeId: string; key?: string; type?: string; required?: boolean; @@ -179,12 +179,12 @@ export const addAttributes = async ( const updatedAttributesMap = new Map(); for (const attr of existingAttributes) { - updatedAttributesMap.set(attr.key, attr.type.toLocaleLowerCase()); + updatedAttributesMap.set(attr.key, attr); } for (const attr of newAttributes) { if (!updatedAttributesMap.has(attr.key)) { - updatedAttributesMap.set(attr.key, attr.type.toLocaleLowerCase()); + updatedAttributesMap.set(attr.key, attr); } } @@ -266,7 +266,7 @@ export const UpdateAttributes = async ( organization, projectId, collectionNodeId, - AttributeId, + attributeId, required, defaultValue, unique, @@ -296,7 +296,7 @@ export const UpdateAttributes = async ( projectId: projectId, isArchive: false, attributes: { - $elemMatch: { _id: new mongoose.Types.ObjectId(AttributeId) }, + $elemMatch: { _id: new mongoose.Types.ObjectId(attributeId) }, }, }, { @@ -311,7 +311,6 @@ export const UpdateAttributes = async ( }, { new: true } ); - console.log("editCollection: ", editCollection); return { status: "Success" }; } } catch (error: unknown) { @@ -491,7 +490,6 @@ const generateUniqueCollectionName = async ( } attempt++; nameToTry = `${baseName} (duplicate${attempt > 1 ? ` ${attempt}` : ""})`; - console.log("nameToTry: ", nameToTry); if (attempt > 10) { throw new Error("Too many duplicate project name attempts"); diff --git a/src/shared/services/projectService.ts b/src/shared/services/projectService.ts index 267eda3..bb3165b 100644 --- a/src/shared/services/projectService.ts +++ b/src/shared/services/projectService.ts @@ -1,5 +1,7 @@ import MVCarcModel from "../../shared/model/mvcModel"; import ProjectType from "../../shared/model/projectmodel"; +import collectionsModel from "../model/collectionModel"; +import edgeModel from "../model/edgeModel"; interface Iresponse { status: string; data?: any; @@ -132,3 +134,52 @@ export const projectDatas = async (data: IProject): Promise => { } } }; + +export const GetNodesInProject = async ( + data: IProjectstructure +): Promise => { + const { organization, projectId } = data; + try { + const existingProject = await ProjectType(organization).findOne({ + _id: projectId, + isArchive: false, + }); + if (!existingProject) { + return { status: "project not found" }; + } else { + const collectionNodesdata = await collectionsModel(organization) + .find({ projectId: projectId, isArchive: false }) + .select("collectionNodeName attributes position -_id "); + const edgeNodes = await edgeModel(organization) + .find({ projectId: projectId, isArchive: false }) + .select("cardinality from to"); + if(!edgeNodes) return {status:'No edge Nodes present',data:[]} + if (!collectionNodesdata) + return { status: "No collection Nodes present", data: [] }; + else { + const collectionNodes = collectionNodesdata.map((collection) => ({ + position: collection.position, + collectionNodeName: collection.collectionNodeName, + attributes: collection.attributes + .filter((attr: any) => !attr.isArchive) + .map((attr: any) => { + const { isArchive, ...rest } = attr.toObject(); + return { ...rest,edgeNodes }; + }), + + })); + return { status: "Success", data: {collectionNodes,edgeNodes} }; + } + } + } catch (error: unknown) { + if (error instanceof Error) { + return { + status: error.message, + }; + } else { + return { + status: "An unexpected error occurred", + }; + } + } +};