429 lines
11 KiB
TypeScript
429 lines
11 KiB
TypeScript
|
|
import collectionsModel ,{ICollectionNode} from "../model/collectionModel";
|
|
import edgeModel ,{IEdgeModel}from "../model/edgeModel";
|
|
import ProjectType from "../model/projectmodel";
|
|
import userDataModel from "../model/userDataModel";
|
|
import userModel from "../model/userModel";
|
|
import versionModel from "../model/versionModel";
|
|
import { existingProjectById, existingUser } from "../utils/functionality";
|
|
type VersionData = {
|
|
Project: {
|
|
edges: IEdgeModel[];
|
|
collectionNode: ICollectionNode[];
|
|
};
|
|
};
|
|
interface IVersionSave {
|
|
organization: string;
|
|
hierarchyVersion: string;
|
|
versionName: string;
|
|
projectId: string;
|
|
userId: string;
|
|
createdBy: string;
|
|
description?: string;
|
|
}
|
|
interface IVersionRollback {
|
|
organization: string;
|
|
versionId: string;
|
|
projectId: string;
|
|
userId: string;
|
|
description?: string;
|
|
}
|
|
|
|
interface IVersionHistory {
|
|
organization: string;
|
|
projectId: string;
|
|
userId: string;
|
|
page: number;
|
|
limit: number;
|
|
sortBy?: string;
|
|
sortOrder?: string;
|
|
}
|
|
interface IVersionUpdate {
|
|
organization: string;
|
|
projectId: string;
|
|
userId: string;
|
|
page: number;
|
|
versionId: string;
|
|
versionName?: string;
|
|
description?: string;
|
|
}
|
|
interface IVersionById {
|
|
organization: string;
|
|
projectId: string;
|
|
versionId: string;
|
|
userId: string;
|
|
}
|
|
interface IResult {
|
|
status: string;
|
|
data?: object;
|
|
}
|
|
export const CreateVersion = async (data: IVersionSave): Promise<IResult> => {
|
|
try {
|
|
const {
|
|
hierarchyVersion,
|
|
projectId,
|
|
versionName,
|
|
description,
|
|
userId,
|
|
organization,
|
|
createdBy,
|
|
} = data;
|
|
const ExistingUser = await userModel(organization).findOne({
|
|
_id: userId,
|
|
isArchive: false,
|
|
});
|
|
if (!ExistingUser) return { status: "User not found" };
|
|
const existingProject = await ProjectType(organization).findOne({
|
|
_id: projectId,
|
|
isArchive: false,
|
|
});
|
|
if (!existingProject) {
|
|
return { status: "project not found" };
|
|
}
|
|
const versionData = await versionModel(organization).findOne({
|
|
_id: hierarchyVersion,
|
|
projectId: projectId,
|
|
isArchive: false,
|
|
});
|
|
if (!versionData) return { status: "Parent Version not found" };
|
|
const versionDataprevious = await versionModel(organization)
|
|
.findOne({
|
|
projectId: projectId,
|
|
isArchive: false,
|
|
})
|
|
.sort({ version: -1 });
|
|
const currentVersion = versionDataprevious?.version ?? 0;
|
|
const newVersion = parseFloat((currentVersion + 0.1).toFixed(2));
|
|
const saveVersion = await versionModel(organization).create({
|
|
versionName: versionName,
|
|
parentVersionID: versionData._id,
|
|
projectId: projectId,
|
|
description: description,
|
|
createdBy: createdBy,
|
|
previous_Version: currentVersion,
|
|
version: newVersion,
|
|
createdAt: new Date(),
|
|
});
|
|
const find_User = await userDataModel(organization).findById({
|
|
_id: saveVersion.createdBy,
|
|
isArchive: false,
|
|
});
|
|
const responseVersionData = {
|
|
versionId: saveVersion._id,
|
|
version: saveVersion.version,
|
|
description: saveVersion.description,
|
|
versionName: saveVersion.versionName,
|
|
createdAt: saveVersion.createdAt,
|
|
createdBy: {
|
|
userId: find_User._id,
|
|
userName: find_User.userName,
|
|
},
|
|
};
|
|
await existingProject.updateOne({
|
|
Present_version: saveVersion._id,
|
|
total_versions: `v-${saveVersion.version.toFixed(1)}`,
|
|
});
|
|
|
|
await AllCloneFornewVersions(
|
|
projectId,
|
|
organization,
|
|
saveVersion._id,
|
|
hierarchyVersion
|
|
);
|
|
return { status: "Success", data: responseVersionData };
|
|
} catch (error: unknown) {
|
|
if (error instanceof Error) {
|
|
return {
|
|
status: error.message,
|
|
};
|
|
} else {
|
|
return {
|
|
status: "An unexpected error occurred",
|
|
};
|
|
}
|
|
}
|
|
};
|
|
|
|
export const GetVersionById = async (data: IVersionById): Promise<IResult> => {
|
|
try {
|
|
const { projectId, userId, organization, versionId } = data;
|
|
const userExisting = await existingUser(userId, organization);
|
|
if (!userExisting) {
|
|
return {
|
|
status: "User not found",
|
|
};
|
|
}
|
|
const LivingProject = await existingProjectById(
|
|
projectId,
|
|
organization,
|
|
userId
|
|
);
|
|
if (!LivingProject) return { status: "Project not found" };
|
|
const existingVersion = await versionModel(organization)
|
|
.findOne({
|
|
_id: versionId,
|
|
projectId: projectId,
|
|
isArchive: false,
|
|
})
|
|
.select("version previous_Version");
|
|
if (!existingVersion) {
|
|
return { status: "Version not found for the project" };
|
|
}
|
|
|
|
const [collectionNode, edges] =
|
|
await Promise.all([
|
|
collectionsModel(organization).find({
|
|
versionId,
|
|
projectId,
|
|
isArchive: false,
|
|
}),
|
|
edgeModel(organization).find({
|
|
versionId,
|
|
projectId,
|
|
isArchive: false,
|
|
}),
|
|
|
|
]);
|
|
|
|
const versionData: VersionData = {
|
|
Project: {
|
|
collectionNode,
|
|
edges
|
|
},
|
|
};
|
|
|
|
return {
|
|
status: "Success",
|
|
data: {
|
|
version: existingVersion,
|
|
entities: versionData,
|
|
},
|
|
};
|
|
} catch (error: unknown) {
|
|
if (error instanceof Error) {
|
|
return {
|
|
status: error.message,
|
|
};
|
|
} else {
|
|
return {
|
|
status: "An unexpected error occurred",
|
|
};
|
|
}
|
|
}
|
|
};
|
|
|
|
export const RollBackversion = async (
|
|
data: IVersionRollback
|
|
): Promise<IResult> => {
|
|
try {
|
|
const { versionId, projectId, userId, organization } = data;
|
|
const userExisting = await existingUser(userId, organization);
|
|
if (!userExisting) {
|
|
return {
|
|
status: "User not found",
|
|
};
|
|
}
|
|
const LivingProject = await existingProjectById(
|
|
projectId,
|
|
organization,
|
|
userId
|
|
);
|
|
if (!LivingProject) return { status: "Project not found" };
|
|
const rollbackversion = await versionModel(organization).findOne({
|
|
_id: versionId,
|
|
projectId: projectId,
|
|
isArchive: false,
|
|
});
|
|
if (!rollbackversion)
|
|
return { status: "Mentioned Version not found for the Rollback" };
|
|
const versionDataprevious = await versionModel(organization)
|
|
.findOne({
|
|
projectId: projectId,
|
|
isArchive: false,
|
|
})
|
|
.sort({ version: -1 });
|
|
const currentVersion = versionDataprevious?.version ?? 0;
|
|
const newVersion = parseFloat((currentVersion + 0.01).toFixed(2));
|
|
const saveVersion = await versionModel(organization).create({
|
|
parentVersionID: rollbackversion._id,
|
|
projectId: projectId,
|
|
createdBy: userId,
|
|
previous_Version: currentVersion,
|
|
version: newVersion,
|
|
createdAt: new Date(),
|
|
rollBackComment: `RollBack from the version ${rollbackversion.version}`,
|
|
});
|
|
LivingProject.Present_version = saveVersion._id;
|
|
LivingProject.total_versions = `v-${saveVersion.version.toFixed(2)}`;
|
|
await LivingProject.save();
|
|
|
|
await AllCloneFornewVersions(
|
|
projectId,
|
|
organization,
|
|
saveVersion._id,
|
|
rollbackversion._id
|
|
);
|
|
return { status: "Success", data: saveVersion._id };
|
|
} catch (error: unknown) {
|
|
if (error instanceof Error) {
|
|
return {
|
|
status: error.message,
|
|
};
|
|
} else {
|
|
return {
|
|
status: "An unexpected error occurred",
|
|
};
|
|
}
|
|
}
|
|
};
|
|
|
|
export const VersionHistory = async (
|
|
data: IVersionHistory
|
|
): Promise<IResult> => {
|
|
try {
|
|
const { organization, userId, projectId, page, limit, sortOrder } = data;
|
|
|
|
const userExisting = await existingUser(userId, organization);
|
|
if (!userExisting) return { status: "User not found" };
|
|
|
|
const LivingProject = await existingProjectById(
|
|
projectId,
|
|
organization,
|
|
userId
|
|
);
|
|
if (!LivingProject) return { status: "Project not found" };
|
|
|
|
const filter: object = { projectId, isArchive: false };
|
|
const total = await versionModel(organization).countDocuments(filter);
|
|
const versiondatas = await versionModel(organization)
|
|
.find(filter)
|
|
.sort({ ["version"]: sortOrder === "asc" ? 1 : -1 })
|
|
.skip((page - 1) * limit)
|
|
.limit(limit)
|
|
.select("version createdBy createdAt description versionName")
|
|
.populate({
|
|
path: "createdBy",
|
|
model: userDataModel(organization),
|
|
select: "userName",
|
|
});
|
|
// .populate({
|
|
// path: "createdBy",
|
|
// model: UsersDataModel(organization),
|
|
// select: "profilePicture",
|
|
// });
|
|
// console.log('versiondatas: ', versiondatas);
|
|
if (!versiondatas)
|
|
return {
|
|
status: "Versions not found",
|
|
};
|
|
const versions = versiondatas.map((version) => {
|
|
return {
|
|
versionId: version._id,
|
|
version: version.version,
|
|
createdAt: version.createdAt,
|
|
description: version.description,
|
|
versionName: version.versionName,
|
|
createdBy: {
|
|
userId: version.createdBy._id,
|
|
userName: version.createdBy.userName,
|
|
},
|
|
};
|
|
});
|
|
return {
|
|
status: "Success",
|
|
data: {
|
|
versions,
|
|
Metadatas: {
|
|
total,
|
|
page,
|
|
limit,
|
|
hasNextPage: page * limit < total,
|
|
},
|
|
},
|
|
};
|
|
} catch (error: unknown) {
|
|
if (error instanceof Error) {
|
|
return {
|
|
status: error.message,
|
|
};
|
|
} else {
|
|
return {
|
|
status: "An unexpected error occurred",
|
|
};
|
|
}
|
|
}
|
|
};
|
|
export const updateVersion = async (data: IVersionUpdate): Promise<IResult> => {
|
|
try {
|
|
const {
|
|
organization,
|
|
userId,
|
|
projectId,
|
|
versionId,
|
|
versionName,
|
|
description,
|
|
} = data;
|
|
|
|
const userExisting = await existingUser(userId, organization);
|
|
if (!userExisting) return { status: "User not found" };
|
|
|
|
const LivingProject = await existingProjectById(
|
|
projectId,
|
|
organization,
|
|
userId
|
|
);
|
|
if (!LivingProject) return { status: "Project not found" };
|
|
const existingVersionId = await versionModel(organization).findOne({
|
|
_id: versionId,
|
|
isArchive: false,
|
|
projectId: projectId,
|
|
});
|
|
if (!existingVersionId) {
|
|
return { status: "VersionId not match" };
|
|
} else {
|
|
existingVersionId.versionName = versionName;
|
|
existingVersionId.description = description;
|
|
await existingVersionId.save();
|
|
return { status: "Version Updated successfully" };
|
|
}
|
|
} catch (error: unknown) {
|
|
if (error instanceof Error) {
|
|
return {
|
|
status: error.message,
|
|
};
|
|
} else {
|
|
return {
|
|
status: "An unexpected error occurred",
|
|
};
|
|
}
|
|
}
|
|
};
|
|
|
|
export const AllCloneFornewVersions = async (
|
|
projectId: string,
|
|
organization: string,
|
|
versionId: string,
|
|
oldeVersionId: string
|
|
) => {
|
|
const cloneDocuments = async (model: any, modelName: string) => {
|
|
const docs = await model(organization).find({
|
|
projectId,
|
|
isArchive: false,
|
|
versionId: oldeVersionId,
|
|
});
|
|
|
|
if (!docs.length) return;
|
|
|
|
const clonedDocs = docs.map((doc: any) => {
|
|
const { _id, __v, ...rest } = doc.toObject();
|
|
return { ...rest, versionId };
|
|
});
|
|
|
|
const created = await model(organization).create(clonedDocs);
|
|
};
|
|
await Promise.all([
|
|
cloneDocuments(collectionsModel, "collections"),
|
|
cloneDocuments(edgeModel, "edges"),
|
|
]);
|
|
};
|