View project, update project Data, Usermodel modified Based on the recentlyViewed
This commit is contained in:
@@ -3,12 +3,12 @@ import userModel from "../../model/user-Model.ts";
|
||||
import { Types } from "mongoose";
|
||||
import versionModel from "../../model/version/versionModel.ts";
|
||||
export const existingProject = async (
|
||||
projectName: string,
|
||||
projectUuid: string,
|
||||
organization: string,
|
||||
userId: string
|
||||
) => {
|
||||
const projectData = await projectModel(organization).findOne({
|
||||
projectName: projectName,
|
||||
projectUuid: projectUuid,
|
||||
createdBy: userId,
|
||||
isArchive: false,
|
||||
});
|
||||
@@ -47,3 +47,33 @@ export const previousVersion = async (
|
||||
// .sort({ version: -1 });
|
||||
return result;
|
||||
};
|
||||
export const generateUntitledProjectName = async (
|
||||
organization: string,
|
||||
userId: string
|
||||
): Promise<string> => {
|
||||
const projects = await projectModel(organization)
|
||||
.find({
|
||||
createdBy: userId,
|
||||
isArchive: false,
|
||||
projectName: { $regex: /^Untitled(?: \s?\d+)?$/, $options: "i" },
|
||||
})
|
||||
.select("projectName");
|
||||
|
||||
const usedNumbers = new Set<number>();
|
||||
|
||||
for (const proj of projects) {
|
||||
const match = proj.projectName.match(/^Untitled(?:\s?(\d+))?$/);
|
||||
console.log("match: ", match);
|
||||
if (match) {
|
||||
const num = match[1] ? parseInt(match[1], 10) : 0;
|
||||
usedNumbers.add(num);
|
||||
}
|
||||
}
|
||||
|
||||
let newNumber = 0;
|
||||
while (usedNumbers.has(newNumber)) {
|
||||
newNumber++;
|
||||
}
|
||||
|
||||
return newNumber === 0 ? "Untitled" : `Untitled ${newNumber}`;
|
||||
};
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
existingUser,
|
||||
archiveProject,
|
||||
previousVersion,
|
||||
generateUntitledProjectName,
|
||||
} from "../helpers/ProjecthelperFn.ts";
|
||||
interface CreateProjectInput {
|
||||
projectName: string;
|
||||
@@ -16,25 +17,26 @@ interface CreateProjectInput {
|
||||
sharedUsers?: string[];
|
||||
organization: string;
|
||||
}
|
||||
interface updateProjectInput {
|
||||
projectName: string;
|
||||
projectId: string;
|
||||
userId: string; // user ID
|
||||
thumbnail?: string;
|
||||
sharedUsers?: string[];
|
||||
organization: string;
|
||||
}
|
||||
interface GetProjectsInterface {
|
||||
userId: string;
|
||||
organization: string;
|
||||
}
|
||||
interface DeleteProjectInterface {
|
||||
interface ProjectInterface {
|
||||
projectId: string;
|
||||
userId: string;
|
||||
organization: string;
|
||||
}
|
||||
export const createProject = async (data: CreateProjectInput) => {
|
||||
try {
|
||||
const {
|
||||
projectName,
|
||||
projectUuid,
|
||||
userId,
|
||||
thumbnail,
|
||||
sharedUsers,
|
||||
organization,
|
||||
} = data;
|
||||
const { userId, thumbnail, sharedUsers, organization, projectUuid } = data;
|
||||
const userExisting = await existingUser(userId, organization);
|
||||
if (!userExisting) {
|
||||
return {
|
||||
@@ -42,7 +44,7 @@ export const createProject = async (data: CreateProjectInput) => {
|
||||
};
|
||||
}
|
||||
const projectExisting = await existingProject(
|
||||
projectName,
|
||||
projectUuid,
|
||||
organization,
|
||||
userId
|
||||
);
|
||||
@@ -53,9 +55,12 @@ export const createProject = async (data: CreateProjectInput) => {
|
||||
project: projectExisting,
|
||||
};
|
||||
}
|
||||
|
||||
const newProjectName = await generateUntitledProjectName(
|
||||
organization,
|
||||
userId
|
||||
);
|
||||
const project = await projectModel(organization).create({
|
||||
projectName: projectName,
|
||||
projectName: newProjectName,
|
||||
projectUuid: projectUuid,
|
||||
createdBy: userId,
|
||||
thumbnail: thumbnail,
|
||||
@@ -95,14 +100,14 @@ export const GetAllProjects = async (data: GetProjectsInterface) => {
|
||||
.find({
|
||||
isArchive: false,
|
||||
})
|
||||
.select("_id projectName createdBy thumbnail");
|
||||
.select("_id projectName createdBy thumbnail createdAt");
|
||||
if (projectDatas) return { status: "Success", Datas: projectDatas };
|
||||
} catch (error: unknown) {
|
||||
return { status: error };
|
||||
}
|
||||
};
|
||||
|
||||
export const DeleteProject = async (data: DeleteProjectInterface) => {
|
||||
export const DeleteProject = async (data: ProjectInterface) => {
|
||||
try {
|
||||
const { projectId, organization, userId } = data;
|
||||
const ExistingUser = await existingUser(userId, organization);
|
||||
@@ -123,3 +128,63 @@ export const DeleteProject = async (data: DeleteProjectInterface) => {
|
||||
return { status: error };
|
||||
}
|
||||
};
|
||||
export const updateProject = async (data: updateProjectInput) => {
|
||||
try {
|
||||
const { projectId, organization, userId, projectName, thumbnail } = data;
|
||||
const ExistingUser = await existingUser(userId, organization);
|
||||
if (!ExistingUser) return { status: "User not found" };
|
||||
const existingProject = await projectModel(organization).findOne({
|
||||
_id: projectId,
|
||||
createdBy: userId,
|
||||
isArchive: false,
|
||||
});
|
||||
if (!existingProject) return { status: "Project not found" };
|
||||
if (projectName !== undefined) projectName;
|
||||
if (thumbnail !== undefined) thumbnail;
|
||||
const updateProject = await projectModel(organization)
|
||||
.findOneAndUpdate(
|
||||
{ _id: projectId, isArchive: false },
|
||||
{ projectName: projectName, thumbnail: thumbnail },
|
||||
{ new: true }
|
||||
)
|
||||
.select("_id projectName createdBy thumbnail createdAt");
|
||||
if (updateProject) return { status: "Success", data: updateProject };
|
||||
} catch (error: unknown) {
|
||||
return { status: error };
|
||||
}
|
||||
};
|
||||
const maxLength: number = 6;
|
||||
export const viewProject = async (data: ProjectInterface) => {
|
||||
try {
|
||||
const { projectId, organization, userId } = data;
|
||||
const userExisting = await existingUser(userId, organization);
|
||||
if (!userExisting) return { status: "User not found" };
|
||||
const existingProject = await projectModel(organization).findOne({
|
||||
_id: projectId,
|
||||
createdBy: userId,
|
||||
isArchive: false,
|
||||
});
|
||||
if (!existingProject) return { status: "Project not found" };
|
||||
const newArr = userExisting?.recentlyViewed || [];
|
||||
if (userExisting?.recentlyViewed.length === 0) {
|
||||
newArr.push(projectId);
|
||||
} else {
|
||||
const index = newArr.indexOf(projectId);
|
||||
if (index !== -1) {
|
||||
newArr.splice(index, 1);
|
||||
}
|
||||
newArr.unshift(projectId);
|
||||
|
||||
if (newArr.length > maxLength) {
|
||||
newArr.pop();
|
||||
}
|
||||
}
|
||||
await userExisting.updateOne(
|
||||
{ _id: userId, isArchive: false },
|
||||
{ recentlyViewed: newArr }
|
||||
);
|
||||
return { data: existingProject };
|
||||
} catch (error: unknown) {
|
||||
return { status: error };
|
||||
}
|
||||
};
|
||||
|
||||
@@ -48,12 +48,10 @@ export const TrashDatas = async (data: IOrg) => {
|
||||
export const RestoreTrashData = async (data: IRestore) => {
|
||||
try {
|
||||
const { projectId, organization } = data;
|
||||
console.log("projectId: ", projectId);
|
||||
const findProject = await projectModel(organization).findOne({
|
||||
_id: projectId,
|
||||
isArchive: true,
|
||||
});
|
||||
console.log("findProject: ", findProject);
|
||||
if (!findProject) return { status: "Project not found" };
|
||||
const restoreData = await projectModel(organization).findOneAndUpdate(
|
||||
{ _id: projectId, isArchive: true },
|
||||
|
||||
Reference in New Issue
Block a user