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