diff --git a/src/api-server/controller/collectionNodeController.ts b/src/api-server/controller/collectionNodeController.ts index 347e1d3..db3a702 100644 --- a/src/api-server/controller/collectionNodeController.ts +++ b/src/api-server/controller/collectionNodeController.ts @@ -1,7 +1,11 @@ import { Request, Response } from "express"; -import { collectionNodecreation } from "../../shared/services/collectionService"; +import { + addAttributes, + Nodecreation, + SetCollectionName, +} from "../../shared/services/collectionService"; -export const collectionNodeCreationController = async ( +export const NodeCreationController = async ( req: Request, res: Response ): Promise => { @@ -18,7 +22,8 @@ export const collectionNodeCreationController = async ( projectId, position, }; - const result = await collectionNodecreation(data); + const result = await Nodecreation(data); + console.log('result: ', result); switch (result.status) { case "project not found": @@ -49,3 +54,109 @@ export const collectionNodeCreationController = async ( }); } }; + +export const SetCollectionNameController = async ( + req: Request, + res: Response +): Promise => { + try { + const { + organization, + projectId, + collectionNodeId, + collectionName, + position, + } = req.body; + if (!organization || !projectId || !collectionName || !collectionNodeId) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const data = { + organization, + projectId, + collectionNodeId, + collectionName, + position, + }; + const result = await SetCollectionName(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 name updated", + }); + 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 { organization, projectId, collectionNodeId, attributes } = req.body; + console.log('req.body: ', 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 name updated", + }); + 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/routes/collectionRoutes.ts b/src/api-server/routes/collectionRoutes.ts index 8fa06ab..54490c7 100644 --- a/src/api-server/routes/collectionRoutes.ts +++ b/src/api-server/routes/collectionRoutes.ts @@ -1,7 +1,13 @@ import express from "express"; -import { collectionNodeCreationController } from "../controller/collectionNodeController"; -collectionNodeCreationController; +import { + AddAttributesController, + NodeCreationController, + SetCollectionNameController, +} from "../controller/collectionNodeController"; + const collectionNodeRoutes = express.Router(); -collectionNodeRoutes.post("/NewNode", collectionNodeCreationController); +collectionNodeRoutes.post("/NewNode", NodeCreationController); +collectionNodeRoutes.patch("/collectionName", SetCollectionNameController); +collectionNodeRoutes.patch("/AddAttributes", AddAttributesController); export default collectionNodeRoutes; diff --git a/src/shared/model/collectionModel.ts b/src/shared/model/collectionModel.ts index d8c5a08..b140891 100644 --- a/src/shared/model/collectionModel.ts +++ b/src/shared/model/collectionModel.ts @@ -4,20 +4,32 @@ import { IProject } from "./projectmodel"; interface ICollectionNode extends Document { projectId: IProject["_id"]; collectionNodeName: string; - attributes: []; + attributes: [ + { + key: string; + type: any; + } + ]; isArchive: boolean; position: [number, number, number]; } -const collectionSchema: Schema = new Schema({ - projectId: { type: Schema.Types.ObjectId, ref: "Project" }, - collectionNodeName: { type: String, required: true }, - position: { type: [Number], required: true }, - isArchive: { type: Boolean, default: false }, - attributes: { type: [], required: true }, -}, +const collectionSchema: Schema = new Schema( + { + projectId: { type: Schema.Types.ObjectId, ref: "Project" }, + collectionNodeName: { type: String }, + position: { type: [Number], required: true }, + isArchive: { type: Boolean, default: false }, + attributes: [ + { + key: { type: String }, + type: { type: Schema.Types.Mixed }, + }, + ], + }, { timestamps: true, - }); + } +); const collectionsModel = (db: any) => { return MainModel(db, "collectionNode", collectionSchema, "collectionNode"); diff --git a/src/shared/services/collectionService.ts b/src/shared/services/collectionService.ts index aeeafe3..3aad469 100644 --- a/src/shared/services/collectionService.ts +++ b/src/shared/services/collectionService.ts @@ -9,7 +9,24 @@ interface IcollectionNode { organization: string; position: [number]; } -export const collectionNodecreation = async ( +interface IAttribute { + key: string; + type: any; +} +interface IcollectionNodeName { + projectId: string; + organization: string; + collectionNodeId: string; + collectionName: string; + position: [number]; +} +interface IcollectionAttributes { + projectId: string; + organization: string; + collectionNodeId: string; + attributes: IAttribute[]; +} +export const Nodecreation = async ( data: IcollectionNode ): Promise => { const { organization, projectId, position } = data; @@ -43,3 +60,200 @@ export const collectionNodecreation = async ( } } }; + +export const SetCollectionName = async ( + data: IcollectionNodeName +): Promise => { + const { + organization, + projectId, + position, + collectionNodeId, + collectionName, + } = 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 collectionNameupdate = await collectionsModel( + organization + ).findOneAndUpdate( + { + projectId: projectId, + isArchive: false, + _id: collectionNodeId, + }, + { collectionNodeName: collectionName }, + { new: true } + ); + return { status: "Success" }; + } + } catch (error: unknown) { + if (error instanceof Error) { + return { + status: error.message, + }; + } else { + return { + status: "An unexpected error occurred", + }; + } + } +}; + +export const addAttributes = async ( + data: IcollectionAttributes +): Promise => { + const { organization, projectId, collectionNodeId, attributes } = 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 existingAttributes = existingCollection.attributes || []; + const newAttributes = attributes; + const updatedAttributesMap = new Map(); + + for (const attr of existingAttributes) { + updatedAttributesMap.set(attr.key, attr); + } + + for (const attr of newAttributes) { + if (!updatedAttributesMap.has(attr.key)) { + updatedAttributesMap.set(attr.key, attr); + } + } + + const updatedAttributes = Array.from(updatedAttributesMap.values()); + const AttributesAdded = await collectionsModel( + organization + ).findOneAndUpdate( + { + projectId: projectId, + isArchive: false, + _id: collectionNodeId, + }, + { attributes: updatedAttributes }, + { new: true } + ); + return { status: "Success" }; + } + } catch (error: unknown) { + if (error instanceof Error) { + return { + status: error.message, + }; + } else { + return { + status: "An unexpected error occurred", + }; + } + } +}; + +// const attrToSchema = (attr: any): string => { +// const lines: string[] = []; +// // console.log('lines: ', lines); + +// for (const [key, config] of Object.entries(attr)) { +// const cfg: any = config; +// let typeValue = cfg.type; +// const line: string[] = []; + +// if (!typeValue && config !== null && typeof config === "object") { +// const nestedLines: string[] = []; + +// for (const [nestedKey, nestedConfig] of Object.entries(config)) { +// const nestedCfg: any = nestedConfig; +// const nestedLine: string[] = []; +// nestedLine.push(`${nestedKey}: {`); +// nestedLine.push(` type: ${nestedCfg.type},`); + +// if (nestedCfg.required) +// nestedLine.push(` required: ${nestedCfg.required},`); +// if (nestedCfg.default !== undefined) +// nestedLine.push( +// ` default: ${JSON.stringify(nestedCfg.default)},` +// ); + +// nestedLine.push("}"); +// nestedLines.push(nestedLine.join("\n")); +// } + +// line.push(`${key}: {`); +// line.push(nestedLines.join(",\n")); +// line.push("}"); +// lines.push(line.join("\n")); +// continue; +// } + +// line.push(`${key}: {`); + +// if (Array.isArray(typeValue)) { +// typeValue = `[${typeValue +// .map((t) => (typeof t === "string" ? t : JSON.stringify(t))) +// .join(", ")}]`; +// } + +// line.push(` type: ${typeValue},`); + +// if (cfg.ref) line.push(` ref: "${cfg.ref}",`); +// if (cfg.required) line.push(` required: ${cfg.required},`); +// if (cfg.default !== undefined) +// line.push(` default: ${JSON.stringify(cfg.default)},`); +// if (cfg.minLength) line.push(` minlength: ${cfg.minLength},`); +// if (cfg.maxLength) line.push(` maxlength: ${cfg.maxLength},`); +// if (cfg.enum) { +// const enumValues = (cfg.enum as string[]) +// .map((v) => `"${v}"`) +// .join(", "); +// line.push(` enum: [${enumValues}],`); +// } +// if (cfg.min !== undefined) line.push(` min: ${cfg.min},`); +// if (cfg.max !== undefined) line.push(` max: ${cfg.max},`); + +// line.push("}"); +// lines.push(line.join("\n")); +// } + +// return lines.join(",\n"); +// }; +// const AttributesToAdd = attrToSchema(attributes); +// console.log('AttributesToAdd: ', AttributesToAdd); +// const buildAttributesArray = (attr: Record): Record[] => { +// console.log('attr: ', attr); +// const result: Record[] = []; + +// for (const [key, config] of Object.entries(attr)) { +// if (config && typeof config === "object" && !config.type) { +// result.push({ [key]: buildAttributesArray(config) }); +// } else { +// result.push({ [key]: { ...config } }); +// } +// } +// return result; +// }; +// const AttributesToAdd = buildAttributesArray(attributes);