Files
Schema-Studio/src/shared/services/dummyService.ts
2025-10-25 09:25:25 +05:30

681 lines
19 KiB
TypeScript

import mongoose from "mongoose";
import ProjectType from "../../shared/model/projectmodel";
import collectionsModel from "../model/collectionModel";
import dummyModel from "../model/dummycollectionmodel";
interface Iresponse {
status: string;
data?: any;
}
interface IcollectionNode {
projectId: string;
userId: string;
organization: string;
collectionName: string;
position: [number];
}
interface IAttribute {
key: string;
type: any;
}
interface IcollectionNodeName {
projectId: string;
userId: string;
organization: string;
collectionNodeId: string;
collectionName: string;
position: [number];
}
interface IcollectionAttributes {
projectId: string;
userId: string;
organization: string;
collectionNodeId: string;
attributes: IAttribute[];
}
interface IcollectionNodes {
projectId: string;
userId: string;
organization: string;
}
interface IcollectionNodeById {
projectId: string;
organization: string;
userId: string;
collectionNodeId: string;
}
interface IcollectionNodeRenameKey {
projectId: string;
organization: string;
userId: string;
collectionNodeId: string;
newKeyName: string;
oldKeyName: string;
}
interface IAttributesEdit {
projectId: string;
userId: string;
organization: string;
collectionNodeId: string;
fieldId: string;
key?: string;
type?: string;
required?: boolean;
defaultValue?: any;
unique?: boolean;
index?: boolean;
}
interface IAttributesDel {
projectId: string;
userId: string;
organization: string;
collectionNodeId: string;
fieldId: string;
}
interface ICollectionDelete {
projectId: string;
organization: string;
userId: string;
collectionNodeId: string;
}
interface IDuplicateCollectionNode {
projectId: string;
userId: string;
collectionNodeId: string;
organization: string;
collectionName: string;
position: [number];
attributes: [];
}
export const Nodecreationdummy = async (
data: IcollectionNode
): Promise<Iresponse> => {
const { organization, projectId, position, userId, collectionName } = data;
try {
const existingProject = await ProjectType(organization).findOne({
_id: projectId,
isArchive: false,
});
if (!existingProject) {
return { status: "project not found" };
} else {
const newCollectionnode = await dummyModel(organization).create({
projectId: projectId,
isArchive: false,
position: position,
collectionName: collectionName,
});
if (!newCollectionnode)
return { status: "Collection node creation unsuccessfull" };
else return { status: "Success", data: newCollectionnode._id };
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const SetCollectionNamedummy = async (
data: IcollectionNodeName
): Promise<Iresponse> => {
const {
organization,
projectId,
position,
collectionNodeId,
collectionName,
userId,
} = data;
try {
const existingProject = await ProjectType(organization).findOne({
_id: projectId,
isArchive: false,
});
if (!existingProject) {
return { status: "project not found" };
} else {
const existingCollection = await dummyModel(organization).findOne({
projectId: projectId,
isArchive: false,
_id: collectionNodeId,
});
if (!existingCollection) {
return { status: "Collection not found" };
}
const collectionNameupdate = await dummyModel(
organization
).findOneAndUpdate(
{
projectId: projectId,
isArchive: false,
_id: collectionNodeId,
},
{ collectionName: 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 addAttributesdummy = async (
data: IcollectionAttributes
): Promise<Iresponse> => {
const { organization, projectId, userId, collectionNodeId, attributes } =
data;
try {
const existingProject = await ProjectType(organization).findOne({
_id: projectId,
isArchive: false,
});
if (!existingProject) {
return { status: "project not found" };
}
const existingCollection = await dummyModel(organization).findOne({
_id: collectionNodeId,
projectId,
isArchive: false,
});
if (!existingCollection) {
return { status: "Collection not found" };
}
let collections = existingCollection.collections || [];
if (collections.length === 0) {
collections.push({ fields: new Map<string, any>(), isArchive: false });
// collections.push({ fields: {}, isArchive: false });
}
const targetCollection = collections[0];
for (const attr of attributes) {
const key = attr.key.trim();
if (!targetCollection.fields.has(key)) {
(targetCollection.fields as Map<string, any>).set(key, {
type: attr.type,
isArchive: false,
});
}
}
existingCollection.collections = collections;
await existingCollection.save();
for (const [key, field] of targetCollection.fields.entries()) {
if (field.type === "object") {
const existingsubcollection = await dummyModel(organization).findOne({
projectId,
parentCollectionNodeId: collectionNodeId,
attributeparentId: (field as any)._id,
isArchive: false,
});
if (!existingsubcollection) {
const newCollection = await dummyModel(organization).create({
isArchive: false,
projectId,
collectionName: key,
parentCollectionNodeId: collectionNodeId,
attributeparentId: (field as any)._id,
attributes: [],
position: [0, 0, 0],
});
existingCollection.isSubCollection = true;
await existingCollection.save();
}
}
}
return { status: "Success" };
} catch (error: unknown) {
if (error instanceof Error) {
return { status: error.message };
}
return { status: "An unexpected error occurred" };
}
};
// export const GetNodesInProject = async (
// data: IcollectionNodes
// ): Promise<Iresponse> => {
// const { organization, userId, 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("collectionName attributes position -_id ");
// if (!collectionNodes)
// return { status: "No collection Nodes present", data: [] };
// else {
// const formattedCollections = collectionNodes.map((collection) => ({
// position: collection.position,
// collectionName: collection.collectionName,
// 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",
// };
// }
// }
// };
// export const UpdateAttributes = async (
// data: IAttributesEdit
// ): Promise<Iresponse> => {
// const {
// organization,
// userId,
// projectId,
// collectionNodeId,
// fieldId,
// 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" };
// const editCollection = await collectionsModel(
// organization
// ).findOneAndUpdate(
// {
// projectId: projectId,
// isArchive: false,
// attributes: {
// $elemMatch: { _id: new mongoose.Types.ObjectId(fieldId) },
// },
// },
// {
// $set: {
// "attributes.$.required": required,
// "attributes.$.default": defaultValue,
// "attributes.$.index": index,
// "attributes.$.unique": unique,
// "attributes.$.key": key,
// "attributes.$.type": type,
// },
// },
// { 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 DelAttributes = async (
// data: IAttributesDel
// ): Promise<Iresponse> => {
// const { organization, userId, projectId, collectionNodeId, fieldId } =
// 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" };
// const softDeleteAttribute = await collectionsModel(
// organization
// ).findOneAndUpdate(
// {
// projectId: projectId,
// isArchive: false,
// attributes: {
// $elemMatch: {
// _id: new mongoose.Types.ObjectId(fieldId),
// 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",
// };
// }
// }
// };
// export const delCollection = async (
// data: ICollectionDelete
// ): Promise<Iresponse> => {
// const { organization, userId, 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",
// };
// }
// }
// };
export const GetcollectionNodedummy = async (
data: IcollectionNodeById
): Promise<Iresponse> => {
const { organization, userId, 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 dummyModel(organization).findOne({
projectId: projectId,
isArchive: false,
_id: collectionNodeId,
});
if (!existingCollection) {
return { status: "Collection not found" };
}
const fieldsMap = existingCollection.collections[0]?.fields || new Map();
console.log("fieldsMap: ", fieldsMap);
const formattedAttributes: Record<string, any> = {};
for (const [key, value] of fieldsMap.entries()) {
formattedAttributes[key] = value.type;
if (value.type === "object") {
const childrenData = await dummyModel(organization).findOne({
attributeparentId: (value as any)._id,
parentCollectionNodeId: collectionNodeId,
isArchive: false,
});
if (!childrenData) {
return { status: "subCollection not found" };
}
const fieldsChildrenMap =
childrenData.collections[0]?.fields || new Map();
const formattedChildrenAttributes: Record<string, any> = {};
for (const [childkey, childvalue] of fieldsChildrenMap.entries()) {
formattedChildrenAttributes[childkey] = childvalue.type;
}
formattedAttributes[key] = formattedChildrenAttributes;
}
}
const formattedCollection = {
position: existingCollection.position,
collectionName: existingCollection.collectionName,
collections: formattedAttributes,
};
return { status: "Success", data: formattedCollection };
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const collectionKeyRenamedummy = async (
data: IcollectionNodeRenameKey
): Promise<Iresponse> => {
const {
organization,
userId,
projectId,
collectionNodeId,
oldKeyName,
newKeyName,
} = data;
try {
const existingProject = await await ProjectType(organization).findOne({
_id: projectId,
isArchive: false,
});
if (!existingProject) {
return { status: "project not found" };
} else {
const existingCollection = await dummyModel(organization).findOne({
projectId: projectId,
isArchive: false,
_id: collectionNodeId,
});
if (!existingCollection) {
return { status: "Collection not found" };
}
const fieldsMap = existingCollection.collections[0]?.fields || new Map();
console.log("fieldsMap: ", fieldsMap);
if (!fieldsMap.has(oldKeyName)) {
return { status: `Key "${oldKeyName}" not found` };
}
const fieldValue = fieldsMap.get(oldKeyName);
if (fieldValue) {
fieldsMap.delete(oldKeyName);
fieldsMap.set(newKeyName, fieldValue);
}
existingCollection.collections[0].fields = fieldsMap;
await existingCollection.save();
}
return { status: "Success" };
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
// const generateUniqueCollectionName = async (
// baseName: string,
// organization: string,
// projectId: string
// ): Promise<string> => {
// let nameToTry = baseName;
// let attempt = 0;
// while (true) {
// const existingCollection = await collectionsModel(organization).findOne({
// projectId,
// isArchive: false,
// collectionName: nameToTry,
// });
// if (!existingCollection) {
// return nameToTry;
// }
// attempt++;
// nameToTry = `${baseName} (duplicate${attempt > 1 ? ` ${attempt}` : ""})`;
// if (attempt > 10) {
// throw new Error("Too many duplicate project name attempts");
// }
// }
// };
// export const DuplicateCollection = async (
// data: IDuplicateCollectionNode
// ): Promise<Iresponse> => {
// const { organization, userId, 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.collectionName,
// organization,
// projectId
// );
// const NewCollectionDuplicate = await collectionsModel(
// organization
// ).create({
// projectId,
// isArchive: false,
// position,
// collectionName: NewduplicateName,
// });
// if (!NewCollectionDuplicate)
// return { status: "Duplication unsuccessfull" };
// else {
// const data = existingCollection.attributes
// .filter((attr: any) => !attr.isArchive)
// .map((attr: any) => {
// const { isArchive, _id, ...rest } = attr.toObject();
// return rest;
// });
// NewCollectionDuplicate.attributes = data;
// NewCollectionDuplicate.save();
// return { status: "Success" };
// }
// }
// }
// } catch (error: unknown) {
// if (error instanceof Error) {
// return {
// status: error.message,
// };
// } else {
// return {
// status: "An unexpected error occurred",
// };
// }
// }
// };