collection model updated based on the Attributes
This commit is contained in:
@@ -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<void> => {
|
||||
@@ -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<void> => {
|
||||
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<void> => {
|
||||
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",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<ICollectionNode> = 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<ICollectionNode> = 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");
|
||||
|
||||
@@ -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<Iresponse> => {
|
||||
const { organization, projectId, position } = data;
|
||||
@@ -43,3 +60,200 @@ export const collectionNodecreation = async (
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const SetCollectionName = async (
|
||||
data: IcollectionNodeName
|
||||
): Promise<Iresponse> => {
|
||||
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<Iresponse> => {
|
||||
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<string, any>();
|
||||
|
||||
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<string, any>): Record<string, any>[] => {
|
||||
// console.log('attr: ', attr);
|
||||
// const result: Record<string, any>[] = [];
|
||||
|
||||
// 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);
|
||||
|
||||
Reference in New Issue
Block a user