zone duplication and Deleted Time updated
This commit is contained in:
@@ -134,7 +134,6 @@ export const DeleteProject = async (data: ProjectDelInterface) => {
|
||||
let filter = { _id: projectId, isArchive: false } as RoleFilter;
|
||||
|
||||
const existingProject = await projectModel(organization).findOne(filter);
|
||||
console.log("existingProject: ", existingProject);
|
||||
if (!existingProject) return { status: "Project not found" };
|
||||
const updateProject = await projectModel(organization).findOneAndUpdate(
|
||||
filter,
|
||||
@@ -148,8 +147,7 @@ export const DeleteProject = async (data: ProjectDelInterface) => {
|
||||
};
|
||||
export const updateProject = async (data: UpdateProjectInput) => {
|
||||
try {
|
||||
const { projectId, organization, userId, projectName, thumbnail } =
|
||||
data;
|
||||
const { projectId, organization, userId, projectName, thumbnail } = data;
|
||||
const ExistingUser = await existingUser(userId, organization);
|
||||
if (!ExistingUser) return { status: "User not found" };
|
||||
let filter = { _id: projectId, isArchive: false } as RoleFilter;
|
||||
@@ -199,14 +197,43 @@ export const DuplicateProject = async (data: IProjectDuplicate) => {
|
||||
project: projectExisting,
|
||||
};
|
||||
}
|
||||
const uniqeName = await generateUniqueProjectName(
|
||||
projectName,
|
||||
organization,
|
||||
userId
|
||||
);
|
||||
const project = await projectModel(organization).create({
|
||||
projectName: projectName,
|
||||
projectName: uniqeName,
|
||||
projectUuid: projectUuid,
|
||||
createdBy: userId,
|
||||
thumbnail: thumbnail,
|
||||
sharedUsers: sharedUsers || [],
|
||||
isArchive: false,
|
||||
});
|
||||
const RecentUserDoc = await UsersDataModel(organization).findOne({
|
||||
userId: userId,
|
||||
isArchive: false,
|
||||
});
|
||||
const newArr = RecentUserDoc?.recentlyViewed || [];
|
||||
if (RecentUserDoc?.recentlyViewed.length === 0) {
|
||||
newArr.push(project._id);
|
||||
await RecentUserDoc.save();
|
||||
} else {
|
||||
const index = newArr.indexOf(project._id);
|
||||
if (index !== -1) {
|
||||
newArr.splice(index, 1);
|
||||
}
|
||||
newArr.unshift(project._id);
|
||||
|
||||
if (newArr.length > maxLength) {
|
||||
newArr.pop();
|
||||
}
|
||||
}
|
||||
await UsersDataModel(organization).findOneAndUpdate(
|
||||
{ userId: userId, isArchive: false },
|
||||
{ recentlyViewed: newArr },
|
||||
{ new: true }
|
||||
);
|
||||
const versionData = await previousVersion(project._id, organization);
|
||||
if (!versionData || versionData.length === 0) {
|
||||
const newVersion = await versionModel(organization).create({
|
||||
@@ -227,6 +254,33 @@ export const DuplicateProject = async (data: IProjectDuplicate) => {
|
||||
return { status: error };
|
||||
}
|
||||
};
|
||||
|
||||
const generateUniqueProjectName = async (
|
||||
baseName: string,
|
||||
organization: string,
|
||||
userId: string
|
||||
): Promise<string> => {
|
||||
let nameToTry = baseName;
|
||||
let suffix = "";
|
||||
let attempt = 0;
|
||||
|
||||
while (true) {
|
||||
const existing = await projectModel(organization).findOne({
|
||||
projectName: nameToTry.trim(),
|
||||
createdBy: userId,
|
||||
isArchive: false,
|
||||
});
|
||||
|
||||
if (!existing) return nameToTry;
|
||||
|
||||
suffix += " (copy)";
|
||||
nameToTry = `${baseName}${suffix}`;
|
||||
attempt++;
|
||||
|
||||
if (attempt > 10)
|
||||
throw new Error("Too many duplicate project name attempts");
|
||||
}
|
||||
};
|
||||
const maxLength: number = 6;
|
||||
export const viewProject = async (data: ProjectInterface) => {
|
||||
try {
|
||||
|
||||
@@ -45,6 +45,7 @@ export const TrashDatas = async (data: IOrg) => {
|
||||
projectName: data.projectName,
|
||||
thumbnail: data.thumbnail,
|
||||
createdBy: data.createdBy,
|
||||
DeletedAt: data.DeletedAt,
|
||||
_id: data._id,
|
||||
};
|
||||
});
|
||||
@@ -90,7 +91,7 @@ export const TrashDelete = async (data: IRestore) => {
|
||||
{ new: true }
|
||||
);
|
||||
if (!DeleteTrashData) return { status: "Project Already Deleted" };
|
||||
return { status: "Success",data:DeleteTrashData };
|
||||
return { status: "Success", data: DeleteTrashData };
|
||||
} catch (error) {
|
||||
return { status: error };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user