View project, update project Data, Usermodel modified Based on the recentlyViewed
This commit is contained in:
@@ -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 };
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user