From bd0c5013a91fbacb19fe98b165b39822d34fe21f Mon Sep 17 00:00:00 2001 From: Nivetharamesh Date: Thu, 15 May 2025 18:53:18 +0530 Subject: [PATCH] View project, update project Data, Usermodel modified Based on the recentlyViewed --- src/api-server/Routes/projectRoutes.ts | 4 + .../controller/project/projectController.ts | 116 +++++++++++++++++- .../controller/trash/trashcontrollers.ts | 7 +- src/shared/model/user-Model.ts | 34 ++--- .../services/helpers/ProjecthelperFn.ts | 34 ++++- .../services/project/project-Services.ts | 93 +++++++++++--- src/shared/services/trash/trashService.ts | 2 - 7 files changed, 248 insertions(+), 42 deletions(-) diff --git a/src/api-server/Routes/projectRoutes.ts b/src/api-server/Routes/projectRoutes.ts index 95d4373..5ddb6fa 100644 --- a/src/api-server/Routes/projectRoutes.ts +++ b/src/api-server/Routes/projectRoutes.ts @@ -3,10 +3,14 @@ import { createProjectController, GetProjects, RemoveProject, + updateProjectController, + ViewData, } from "../controller/project/projectController.ts"; const projectRouter = express.Router(); projectRouter.post("/upsertProject", createProjectController); projectRouter.get("/Projects/:userId/:organization", GetProjects); projectRouter.patch("/Project/archive/:projectId", RemoveProject); +projectRouter.patch("/Project/modify", updateProjectController); +projectRouter.get("/Project/view", ViewData); export default projectRouter; diff --git a/src/api-server/controller/project/projectController.ts b/src/api-server/controller/project/projectController.ts index 790861c..a061d11 100644 --- a/src/api-server/controller/project/projectController.ts +++ b/src/api-server/controller/project/projectController.ts @@ -3,6 +3,8 @@ import { createProject, DeleteProject, GetAllProjects, + updateProject, + viewProject, } from "../../../shared/services/project/project-Services.ts"; export const createProjectController = async ( @@ -10,11 +12,13 @@ export const createProjectController = async ( res: Response ): Promise => { try { - const { projectName, userId, thumbnail, organization } = req.body; - if (!projectName || !userId || !thumbnail || !organization) + const { projectUuid, userId, thumbnail, organization } = req.body; + if (!projectUuid || !userId || !thumbnail || !organization) { res.status(400).json({ message: "All fields are required", }); + return; + } const result = await createProject(req.body); switch (result.status) { @@ -46,6 +50,7 @@ export const createProjectController = async ( res.status(500).json({ message: "Unknown error", }); + return; } }; export const GetProjects = async ( @@ -54,10 +59,12 @@ export const GetProjects = async ( ): Promise => { try { const { userId, organization } = req.params; - if (!userId || !organization) + if (!userId || !organization) { res.status(400).json({ message: "All fields are required", }); + return; + } const result = await GetAllProjects({ userId, organization }); switch (result?.status) { case "User not found": @@ -81,6 +88,7 @@ export const GetProjects = async ( res.status(500).json({ message: "Unknown error", }); + return; } }; export const RemoveProject = async ( @@ -90,10 +98,12 @@ export const RemoveProject = async ( try { const { projectId } = req.params; const { organization, userId } = req.body; - if (!projectId || !organization || !userId) + if (!projectId || !organization || !userId) { res.status(400).json({ message: "All fields are required", }); + return; + } const result = await DeleteProject({ projectId, organization, userId }); switch (result?.status) { case "Project not found": @@ -121,5 +131,103 @@ export const RemoveProject = async ( res.status(500).json({ message: "Unknown error", }); + return; + } +}; +export const updateProjectController = async ( + req: Request, + res: Response +): Promise => { + try { + const { projectId, organization, projectName, thumbnail, userId } = + req.body; + if (!userId || !organization || !projectId) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const result = await updateProject({ + projectId, + organization, + userId, + projectName, + thumbnail, + }); + switch (result?.status) { + case "Project not found": + res.status(404).json({ + message: "Project not found", + }); + break; + case "User not found": + res.status(404).json({ + message: "User not found", + }); + break; + case "Success": + res.status(200).json({ + message: "Project updated Successfully", + projectData: result.data, + }); + break; + default: + res.status(500).json({ + message: "Internal server error", + }); + break; + } + } catch (error) { + res.status(500).json({ + message: "Unknown error", + }); + return; + } +}; +export const ViewData = async (req: Request, res: Response): Promise => { + try { + const { projectId, organization, userId } = req.query as { + organization: string; + projectId: string; + userId: string; + }; + if (!userId || !organization || !projectId) { + res.status(400).json({ + message: "All fields are required", + }); + return; + } + const result = await viewProject({ + projectId, + organization, + userId, + }); + switch (result?.status) { + case "Project not found": + res.status(404).json({ + message: "Project not found", + }); + break; + case "User not found": + res.status(404).json({ + message: "User not found", + }); + break; + case "Success": + res.status(200).json({ + projectData: result.data, + }); + break; + default: + res.status(500).json({ + message: "Internal server error", + }); + break; + } + } catch (error) { + res.status(500).json({ + message: "Unknown error", + }); + return; } }; diff --git a/src/api-server/controller/trash/trashcontrollers.ts b/src/api-server/controller/trash/trashcontrollers.ts index 2e7b1d4..7b02590 100644 --- a/src/api-server/controller/trash/trashcontrollers.ts +++ b/src/api-server/controller/trash/trashcontrollers.ts @@ -9,8 +9,7 @@ export const GetTrashList = async ( res: Response ): Promise => { try { - const { organization } = req.query as { organization?: string }; - console.log("organization: ", organization); + const { organization } = req.query as { organization: string }; if (!organization) { res.status(400).json({ message: "All fields are required", @@ -53,8 +52,8 @@ export const RestoreTrash = async ( ): Promise => { try { const { organization, projectId } = req.query as { - organization?: string; - projectId?: string; + organization: string; + projectId: string; }; console.log("organization: ", organization); if (!organization || !projectId) { diff --git a/src/shared/model/user-Model.ts b/src/shared/model/user-Model.ts index 191a1a7..2f1e13d 100644 --- a/src/shared/model/user-Model.ts +++ b/src/shared/model/user-Model.ts @@ -4,12 +4,11 @@ export interface User extends Document { userName: String; email: String; password: String; - + recentlyViewed: string[]; role: String; profilePicture: String; - isShare:Boolean, - activeStatus:string - + isShare: Boolean; + activeStatus: string; } const signupschema: Schema = new Schema({ userName: { @@ -35,16 +34,19 @@ const signupschema: Schema = new Schema({ type: String, // default: "default-profile-picture.jpg" }, - isShare:{ - type:Boolean, - default:false - }, - activeStatus:{ - type:String, + isShare: { + type: Boolean, + default: false, + }, + activeStatus: { + type: String, enum: ["online", "offline"], - default: "offline" - } - + default: "offline", + }, + recentlyViewed: { + type: [String], + default: [], + }, }); // const userModel = (db: string) => { // const mongoUrl = process.env.MONGO_URI || ""; @@ -63,7 +65,7 @@ const signupschema: Schema = new Schema({ // }; // export default userModel; -const userModel = (db:string) => { - return MainModel(db, "Users", signupschema, "Users") +const userModel = (db: string) => { + return MainModel(db, "Users", signupschema, "Users"); }; -export default userModel; \ No newline at end of file +export default userModel; diff --git a/src/shared/services/helpers/ProjecthelperFn.ts b/src/shared/services/helpers/ProjecthelperFn.ts index 1b16f94..5749781 100644 --- a/src/shared/services/helpers/ProjecthelperFn.ts +++ b/src/shared/services/helpers/ProjecthelperFn.ts @@ -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 => { + const projects = await projectModel(organization) + .find({ + createdBy: userId, + isArchive: false, + projectName: { $regex: /^Untitled(?: \s?\d+)?$/, $options: "i" }, + }) + .select("projectName"); + + const usedNumbers = new Set(); + + 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}`; +}; diff --git a/src/shared/services/project/project-Services.ts b/src/shared/services/project/project-Services.ts index 1b8c409..85b7495 100644 --- a/src/shared/services/project/project-Services.ts +++ b/src/shared/services/project/project-Services.ts @@ -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 }; + } +}; diff --git a/src/shared/services/trash/trashService.ts b/src/shared/services/trash/trashService.ts index c1c1511..2976f95 100644 --- a/src/shared/services/trash/trashService.ts +++ b/src/shared/services/trash/trashService.ts @@ -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 },