Refactor edge creation and retrieval logic; remove debug logs and ensure collections are not archived

This commit is contained in:
2025-08-27 15:33:46 +05:30
parent 46956974a1
commit 831aa39c9a

View File

@@ -36,7 +36,7 @@ export const edgecreation = async (
data: IEdge
): Promise<Iresponse> => {
const { organization, projectId, from, to, cardinality } = data;
console.log('data: ', data);
try {
const existingProject = await ProjectType(organization).findOne({
_id: projectId,
@@ -49,34 +49,30 @@ export const edgecreation = async (
}
const existingFromCollection = await collectionsModel(organization).findOne({
_id: from.collection_id
_id: from.collection_id,isArchive: false,
})
if (!existingFromCollection) {
return { status: "From collection not found" };
}
const existingToCollection = await collectionsModel(organization).findOne({
_id: to.collection_id
_id: to.collection_id,isArchive: false
})
if (!existingToCollection) {
return { status: "To collection not found" };
}
// ✅ Generate new field name for TO collection
const capitalizeFirst = (str: string) => {
if (!str) return str;
return str.charAt(0).toUpperCase() + str.slice(1);
};
// Example variables (replace with your actual data)
const collectionName = existingFromCollection.collectionNodeName;
const fieldName = from.field;
// ✅ Generate new field key
const newFieldKey = `${collectionName}${capitalizeFirst(fieldName)}`;
console.log('newFieldKey: ', newFieldKey);
// Find matching field in FROM collection
const fromField = existingFromCollection.attributes.find(
(attr: any) => attr.key === from.field
);
@@ -86,15 +82,12 @@ export const edgecreation = async (
const fieldType = normalizeType(fromField?.type);
// ✅ Check if field already exists in TO collection
// Check if already exists in TO collection
const fieldExists = existingToCollection.attributes.some(
(attr: any) => attr.key === newFieldKey
);
if (!fieldExists) {
// ✅ Add field if not exists
const newAttribute: any = {
key: newFieldKey,
type: fieldType,
@@ -104,8 +97,6 @@ export const edgecreation = async (
}
};
// ✅ Push the object directly
console.log('newAttribute: ', newAttribute);
existingToCollection.attributes.push(newAttribute);
existingToCollection.attributes = existingToCollection.attributes.map((attr: any) => ({
@@ -126,9 +117,6 @@ export const edgecreation = async (
cardinality,
createdAt: new Date(),
};
// Here you should save into EdgeModel (need Edge Schema)
// Example:
const savedEdge = await edgeModel(organization).create(newEdge);
//
@@ -138,7 +126,7 @@ export const edgecreation = async (
};
}
else {
console.log("Field already exists 🚫");
console.log("Field already exists");
return {
status: "Field already exists"
}
@@ -166,7 +154,6 @@ export const Alledges = async (
data: IAllEdge
): Promise<Iresponse> => {
const { organization, projectId, } = data;
console.log('data: ', data);
try {
const existingProject = await ProjectType(organization).findOne({
_id: projectId,
@@ -177,8 +164,8 @@ export const Alledges = async (
if (!existingProject) {
return { status: "project not found" };
}
const edgeDatas = await edgeModel(organization).find()
console.log('edgeDatas: ', edgeDatas);
const edgeDatas = await edgeModel(organization).find({isArchive: false})
if (!edgeDatas || edgeDatas.length === 0) {
@@ -209,7 +196,7 @@ export const deleteEdge = async (
data: IEdgeDelete
): Promise<Iresponse> => {
const { organization, projectId, edgeId } = data;
console.log('data: ', data);
try {
const existingProject = await ProjectType(organization).findOne({
_id: projectId,
@@ -243,7 +230,6 @@ export const deleteEdge = async (
return false;
});
if (matchedAttr) {
console.log('Matched Attribute:', matchedAttr);
const index = attributes.indexOf(matchedAttr);
if (index > -1) {
attributes.splice(index, 1);