edges node collection and project api processed

This commit is contained in:
2025-08-27 15:36:01 +05:30
parent 831aa39c9a
commit 0cdfd21bbb
7 changed files with 729 additions and 86 deletions

View File

@@ -1,8 +1,14 @@
import { Request, Response } from "express"; import { Request, Response } from "express";
import { import {
addAttributes, addAttributes,
DelAttributes,
delCollection,
DuplicateCollection,
GetcollectionNode,
GetNodesInProject,
Nodecreation, Nodecreation,
SetCollectionName, SetCollectionName,
UpdateAttributes,
} from "../../shared/services/collectionService"; } from "../../shared/services/collectionService";
export const NodeCreationController = async ( export const NodeCreationController = async (
@@ -23,7 +29,7 @@ export const NodeCreationController = async (
position, position,
}; };
const result = await Nodecreation(data); const result = await Nodecreation(data);
console.log('result: ', result); console.log("result: ", result);
switch (result.status) { switch (result.status) {
case "project not found": case "project not found":
@@ -54,7 +60,6 @@ export const NodeCreationController = async (
}); });
} }
}; };
export const SetCollectionNameController = async ( export const SetCollectionNameController = async (
req: Request, req: Request,
res: Response res: Response
@@ -110,14 +115,12 @@ export const SetCollectionNameController = async (
}); });
} }
}; };
export const AddAttributesController = async ( export const AddAttributesController = async (
req: Request, req: Request,
res: Response res: Response
): Promise<void> => { ): Promise<void> => {
try { try {
const { organization, projectId, collectionNodeId, attributes } = req.body; const { organization, projectId, collectionNodeId, attributes } = req.body;
console.log('req.body: ', req.body);
if (!organization || !projectId || !attributes || !collectionNodeId) { if (!organization || !projectId || !attributes || !collectionNodeId) {
res.status(400).json({ res.status(400).json({
message: "All fields are required", message: "All fields are required",
@@ -131,7 +134,6 @@ export const AddAttributesController = async (
attributes, attributes,
}; };
const result = await addAttributes(data); const result = await addAttributes(data);
switch (result.status) { switch (result.status) {
case "project not found": case "project not found":
res.status(200).json({ res.status(200).json({
@@ -145,7 +147,7 @@ export const AddAttributesController = async (
break; break;
case "Success": case "Success":
res.status(200).json({ res.status(200).json({
message: "collection name updated", message: "collection Attributes Added",
}); });
break; break;
default: default:
@@ -160,3 +162,304 @@ export const AddAttributesController = async (
}); });
} }
}; };
export const NodesCollectionsBasedOnproject = async (
req: Request,
res: Response
): Promise<void> => {
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<void> => {
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<void> => {
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
): Promise<void> => {
try {
const { projectId, organization, collectionNodeId } = req.params;
if (!organization || !projectId || !collectionNodeId) {
res.status(400).json({
message: "All fields are required",
});
return;
}
const data = {
organization,
projectId,
collectionNodeId,
};
const result = await GetcollectionNode(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",
});
}
};
export const DeleteCollectionsController = async (
req: Request,
res: Response
): Promise<void> => {
try {
const { organization, projectId, collectionNodeId } = req.params;
if (!organization || !projectId || !collectionNodeId) {
res.status(400).json({
message: "All fields are required",
});
return;
}
const data = {
organization,
projectId,
collectionNodeId,
};
const result = await delCollection(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 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: Request,
res: Response
): Promise<void> => {
try {
const {
projectId,
organization,
collectionName,
position,
collectionNodeId,
attributes,
} = req.body;
console.log("req.body;: ", req.body);
if (!organization || !projectId || !collectionNodeId) {
res.status(400).json({
message: "All fields are required",
});
return;
}
const data = {
organization,
projectId,
collectionName,
position,
attributes,
collectionNodeId,
};
const result = await DuplicateCollection(data);
switch (result.status) {
case "project not found":
res.status(200).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",
});
}
};

View File

@@ -15,6 +15,7 @@ export const projectCreationController = async (
projectName, projectName,
userName, userName,
apiType, apiType,
application,
architecture, architecture,
description, description,
} = req.body; } = req.body;
@@ -24,7 +25,7 @@ export const projectCreationController = async (
!projectName || !projectName ||
!userName || !userName ||
!apiType || !apiType ||
!architecture !architecture|| !application
) { ) {
res.status(400).json({ res.status(400).json({
message: "All fields are required", message: "All fields are required",
@@ -35,7 +36,7 @@ export const projectCreationController = async (
organization, organization,
projectName, projectName,
useableLanguage, useableLanguage,
description, description,application,
userName, userName,
apiType, apiType,
architecture, architecture,

View File

@@ -1,13 +1,46 @@
import express from "express"; import express from "express";
import { import {
AddAttributesController, AddAttributesController,
CollectionDatas,
delAttributesCollections,
DeleteCollectionsController,
DuplicateNodeCollectionController,
NodeCreationController, NodeCreationController,
NodesCollectionsBasedOnproject,
SetCollectionNameController, SetCollectionNameController,
updateAttributesCollections,
} from "../controller/collectionNodeController"; } from "../controller/collectionNodeController";
const collectionNodeRoutes = express.Router(); const collectionNodeRoutes = express.Router();
//Node creation
collectionNodeRoutes.post("/NewNode", NodeCreationController); collectionNodeRoutes.post("/NewNode", NodeCreationController);
//collection Added
collectionNodeRoutes.patch("/collectionName", SetCollectionNameController); collectionNodeRoutes.patch("/collectionName", SetCollectionNameController);
//duplicate collection
collectionNodeRoutes.post(
"/duplicateCollection",
DuplicateNodeCollectionController
);
//particular collection data
collectionNodeRoutes.get(
"/collectionNodeById/:projectId/:collectionNodeId/:organization",
CollectionDatas
);
//delete collection
collectionNodeRoutes.patch(
"/delCollection/:projectId/:collectionNodeId/:organization",
DeleteCollectionsController
);
//Add fields
collectionNodeRoutes.patch("/AddAttributes", AddAttributesController); collectionNodeRoutes.patch("/AddAttributes", AddAttributesController);
//Collections and fiels based on the project
collectionNodeRoutes.get(
"/collectionNodes/:projectId/:organization",
NodesCollectionsBasedOnproject
);
//update fields
collectionNodeRoutes.patch("/Attributeupdate", updateAttributesCollections);
//delete fields
collectionNodeRoutes.patch("/softDelAttribute", delAttributesCollections);
export default collectionNodeRoutes; export default collectionNodeRoutes;

View File

@@ -13,7 +13,6 @@ const MainModel = <T>(
collectionName: string collectionName: string
): Model<T> => { ): Model<T> => {
const db1_url = `${process.env.MONGO_URI}${db}`; const db1_url = `${process.env.MONGO_URI}${db}`;
console.log('process.env.MONGO_URI: ', process.env.MONGO_URI);
const authOptions = { const authOptions = {
user: process.env.MONGO_USER, user: process.env.MONGO_USER,
pass: process.env.MONGO_PASSWORD, pass: process.env.MONGO_PASSWORD,

View File

@@ -2,6 +2,7 @@ import { Schema, Document } from "mongoose";
import MainModel from "../connection/connection"; import MainModel from "../connection/connection";
export interface IProject extends Document { export interface IProject extends Document {
projectName: string; projectName: string;
appType: string;
slug: string; slug: string;
isArchive: boolean; isArchive: boolean;
createdBy: string; createdBy: string;
@@ -16,6 +17,7 @@ export interface IProject extends Document {
const projectSchema: Schema = new Schema( const projectSchema: Schema = new Schema(
{ {
projectName: { type: String }, projectName: { type: String },
appType:{type:String,enum:["Backend","Frontend"]},
slug: { type: String }, slug: { type: String },
isArchive: { type: Boolean, default: false }, isArchive: { type: Boolean, default: false },
createdBy: { type: String }, createdBy: { type: String },

View File

@@ -1,3 +1,4 @@
import mongoose from "mongoose";
import ProjectType from "../../shared/model/projectmodel"; import ProjectType from "../../shared/model/projectmodel";
import collectionsModel from "../model/collectionModel"; import collectionsModel from "../model/collectionModel";
interface Iresponse { interface Iresponse {
@@ -26,6 +27,47 @@ interface IcollectionAttributes {
collectionNodeId: string; collectionNodeId: string;
attributes: IAttribute[]; attributes: IAttribute[];
} }
interface IcollectionNodes {
projectId: string;
organization: string;
}
interface IcollectionNodeById {
projectId: string;
organization: string;
collectionNodeId: string;
}
interface IAttributesEdit {
projectId: string;
organization: string;
collectionNodeId: string;
AttributeId: string;
key?: string;
type?: string;
required?: boolean;
defaultValue?: any;
unique?: boolean;
index?: boolean;
}
interface IAttributesDel {
projectId: string;
organization: string;
collectionNodeId: string;
AttributeId: string;
}
interface ICollectionDelete {
projectId: string;
organization: string;
collectionNodeId: string;
}
interface IDuplicateCollectionNode {
projectId: string;
collectionNodeId: string;
organization: string;
collectionName: string;
position: [number];
attributes: [];
}
export const Nodecreation = async ( export const Nodecreation = async (
data: IcollectionNode data: IcollectionNode
): Promise<Iresponse> => { ): Promise<Iresponse> => {
@@ -33,7 +75,6 @@ export const Nodecreation = async (
try { try {
const existingProject = await ProjectType(organization).findOne({ const existingProject = await ProjectType(organization).findOne({
_id: projectId, _id: projectId,
// createdBy: userName,
isArchive: false, isArchive: false,
}); });
if (!existingProject) { if (!existingProject) {
@@ -138,12 +179,12 @@ export const addAttributes = async (
const updatedAttributesMap = new Map<string, any>(); const updatedAttributesMap = new Map<string, any>();
for (const attr of existingAttributes) { for (const attr of existingAttributes) {
updatedAttributesMap.set(attr.key, attr); updatedAttributesMap.set(attr.key, attr.type.toLocaleLowerCase());
} }
for (const attr of newAttributes) { for (const attr of newAttributes) {
if (!updatedAttributesMap.has(attr.key)) { if (!updatedAttributesMap.has(attr.key)) {
updatedAttributesMap.set(attr.key, attr); updatedAttributesMap.set(attr.key, attr.type.toLocaleLowerCase());
} }
} }
@@ -174,86 +215,347 @@ export const addAttributes = async (
} }
}; };
// const attrToSchema = (attr: any): string => { export const GetNodesInProject = async (
// const lines: string[] = []; data: IcollectionNodes
// // console.log('lines: ', lines); ): Promise<Iresponse> => {
const { organization, projectId } = data;
try {
const existingProject = await ProjectType(organization).findOne({
_id: projectId,
isArchive: false,
});
if (!existingProject) {
return { status: "project not found" };
} else {
const collectionNodes = await collectionsModel(organization)
.find({ projectId: projectId, isArchive: false })
.select("collectionNodeName attributes position -_id ");
if (!collectionNodes)
return { status: "No collection Nodes present", data: [] };
else {
const formattedCollections = collectionNodes.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 };
}),
}));
return { status: "Success", data: formattedCollections };
}
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
// for (const [key, config] of Object.entries(attr)) { export const UpdateAttributes = async (
// const cfg: any = config; data: IAttributesEdit
// let typeValue = cfg.type; ): Promise<Iresponse> => {
// const line: string[] = []; const {
organization,
projectId,
collectionNodeId,
AttributeId,
required,
defaultValue,
unique,
index,
key,
type,
} = data;
try {
const existingProject = await ProjectType(organization).findOne({
_id: projectId,
isArchive: false,
});
if (!existingProject) {
return { status: "project not found" };
} else {
const existingCollection = await collectionsModel(organization).findOne({
projectId: projectId,
_id: collectionNodeId,
isArchive: false,
});
if (!existingCollection) return { status: "Collection not found" };
// if (!typeValue && config !== null && typeof config === "object") { const editCollection = await collectionsModel(
// const nestedLines: string[] = []; organization
).findOneAndUpdate(
{
projectId: projectId,
isArchive: false,
attributes: {
$elemMatch: { _id: new mongoose.Types.ObjectId(AttributeId) },
},
},
{
$set: {
"attributes.$.required": required,
"attributes.$.default": defaultValue,
"attributes.$.index": index,
"attributes.$.unique": unique,
"attributes.$.key": key,
"attributes.$.type": type,
},
},
{ new: true }
);
console.log("editCollection: ", editCollection);
return { status: "Success" };
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
// for (const [nestedKey, nestedConfig] of Object.entries(config)) { export const DelAttributes = async (
// const nestedCfg: any = nestedConfig; data: IAttributesDel
// const nestedLine: string[] = []; ): Promise<Iresponse> => {
// nestedLine.push(`${nestedKey}: {`); const { organization, projectId, collectionNodeId, AttributeId } = data;
// nestedLine.push(` type: ${nestedCfg.type},`); try {
const existingProject = await ProjectType(organization).findOne({
_id: projectId,
isArchive: false,
});
if (!existingProject) {
return { status: "project not found" };
} else {
const existingCollection = await collectionsModel(organization).findOne({
projectId: projectId,
_id: collectionNodeId,
isArchive: false,
});
if (!existingCollection) return { status: "Collection not found" };
// if (nestedCfg.required) const softDeleteAttribute = await collectionsModel(
// nestedLine.push(` required: ${nestedCfg.required},`); organization
// if (nestedCfg.default !== undefined) ).findOneAndUpdate(
// nestedLine.push( {
// ` default: ${JSON.stringify(nestedCfg.default)},` projectId: projectId,
// ); isArchive: false,
attributes: {
$elemMatch: {
_id: new mongoose.Types.ObjectId(AttributeId),
isArchive: false,
},
},
},
{
$set: {
"attributes.$.isArchive": true,
},
},
{ new: true }
);
return { status: "Success" };
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
// nestedLine.push("}"); export const delCollection = async (
// nestedLines.push(nestedLine.join("\n")); data: ICollectionDelete
// } ): Promise<Iresponse> => {
const { organization, projectId, collectionNodeId } = data;
try {
const existingProject = await ProjectType(organization).findOne({
_id: projectId,
isArchive: false,
});
if (!existingProject) {
return { status: "project not found" };
} else {
const existingCollection = await collectionsModel(organization).findOne({
projectId: projectId,
isArchive: false,
_id: collectionNodeId,
});
if (!existingCollection) {
return { status: "Collection not found" };
}
const collectionSoftDelete = await collectionsModel(
organization
).findOneAndUpdate(
{
projectId: projectId,
isArchive: false,
_id: collectionNodeId,
},
{ isArchive: true },
{ new: true }
);
return { status: "Success" };
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
// line.push(`${key}: {`); export const GetcollectionNode = async (
// line.push(nestedLines.join(",\n")); data: IcollectionNodeById
// line.push("}"); ): Promise<Iresponse> => {
// lines.push(line.join("\n")); const { organization, projectId, collectionNodeId } = data;
// continue; try {
// } const existingProject = await ProjectType(organization).findOne({
_id: projectId,
isArchive: false,
});
if (!existingProject) {
return { status: "project not found" };
} else {
const existingCollection = await collectionsModel(organization).findOne({
projectId: projectId,
isArchive: false,
_id: collectionNodeId,
});
if (!existingCollection) {
return { status: "Collection not found" };
}
const formattedCollection = {
position: existingCollection.position,
collectionNodeName: existingCollection.collectionNodeName,
attributes: existingCollection.attributes
.filter((attr: any) => !attr.isArchive)
.map((attr: any) => {
const { isArchive, ...rest } = attr.toObject();
return rest;
}),
};
return { status: "Success", data: formattedCollection };
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
// line.push(`${key}: {`); const generateUniqueCollectionName = async (
baseName: string,
organization: string,
projectId: string
): Promise<string> => {
let nameToTry = baseName;
let attempt = 0;
// if (Array.isArray(typeValue)) { while (true) {
// typeValue = `[${typeValue const existingCollection = await collectionsModel(organization).findOne({
// .map((t) => (typeof t === "string" ? t : JSON.stringify(t))) projectId,
// .join(", ")}]`; isArchive: false,
// } collectionNodeName: nameToTry,
});
if (!existingCollection) {
return nameToTry;
}
attempt++;
nameToTry = `${baseName} (duplicate${attempt > 1 ? ` ${attempt}` : ""})`;
console.log("nameToTry: ", nameToTry);
// line.push(` type: ${typeValue},`); if (attempt > 10) {
throw new Error("Too many duplicate project name attempts");
}
}
};
export const DuplicateCollection = async (
data: IDuplicateCollectionNode
): Promise<Iresponse> => {
const { organization, projectId, position, collectionNodeId } = data;
try {
const existingProject = await ProjectType(organization).findOne({
_id: projectId,
isArchive: false,
});
if (!existingProject) {
return { status: "project not found" };
} else {
const existingCollection = await collectionsModel(organization).findOne({
projectId: projectId,
isArchive: false,
_id: collectionNodeId,
});
if (!existingCollection)
return { status: "CollectionId not found for duplication" };
else {
const NewduplicateName = await generateUniqueCollectionName(
existingCollection.collectionNodeName,
organization,
projectId
);
// if (cfg.ref) line.push(` ref: "${cfg.ref}",`); const NewCollectionDuplicate = await collectionsModel(
// if (cfg.required) line.push(` required: ${cfg.required},`); organization
// if (cfg.default !== undefined) ).create({
// line.push(` default: ${JSON.stringify(cfg.default)},`); projectId,
// if (cfg.minLength) line.push(` minlength: ${cfg.minLength},`); isArchive: false,
// if (cfg.maxLength) line.push(` maxlength: ${cfg.maxLength},`); position,
// if (cfg.enum) { collectionNodeName: NewduplicateName,
// const enumValues = (cfg.enum as string[]) });
// .map((v) => `"${v}"`) if (!NewCollectionDuplicate)
// .join(", "); return { status: "Duplication unsuccessfull" };
// line.push(` enum: [${enumValues}],`); else {
// } const data = existingCollection.attributes
// if (cfg.min !== undefined) line.push(` min: ${cfg.min},`); .filter((attr: any) => !attr.isArchive)
// if (cfg.max !== undefined) line.push(` max: ${cfg.max},`); .map((attr: any) => {
const { isArchive, _id, ...rest } = attr.toObject();
// line.push("}"); return rest;
// lines.push(line.join("\n")); });
// } NewCollectionDuplicate.attributes = data;
NewCollectionDuplicate.save();
// return lines.join(",\n"); return { status: "Success" };
// }; }
// const AttributesToAdd = attrToSchema(attributes); }
// console.log('AttributesToAdd: ', AttributesToAdd); }
// const buildAttributesArray = (attr: Record<string, any>): Record<string, any>[] => { } catch (error: unknown) {
// console.log('attr: ', attr); if (error instanceof Error) {
// const result: Record<string, any>[] = []; return {
status: error.message,
// for (const [key, config] of Object.entries(attr)) { };
// if (config && typeof config === "object" && !config.type) { } else {
// result.push({ [key]: buildAttributesArray(config) }); return {
// } else { status: "An unexpected error occurred",
// result.push({ [key]: { ...config } }); };
// } }
// } }
// return result; };
// };
// const AttributesToAdd = buildAttributesArray(attributes);

View File

@@ -10,6 +10,7 @@ interface IProject {
projectName: string; projectName: string;
userName: string; userName: string;
apiType: string; apiType: string;
application: string;
architecture: string; architecture: string;
description: string; description: string;
} }
@@ -27,6 +28,7 @@ export const projectCreationService = async (
description, description,
userName, userName,
apiType, apiType,
application,
architecture, architecture,
} = data; } = data;
try { try {
@@ -45,6 +47,7 @@ export const projectCreationService = async (
useableLanguage, useableLanguage,
architecture, architecture,
apiType: apiType, apiType: apiType,
appType: application,
description, description,
}); });
if (!newProject) return { status: "Project creation unsuccessfull" }; if (!newProject) return { status: "Project creation unsuccessfull" };