View project, update project Data, Usermodel modified Based on the recentlyViewed
This commit is contained in:
@@ -3,10 +3,14 @@ import {
|
|||||||
createProjectController,
|
createProjectController,
|
||||||
GetProjects,
|
GetProjects,
|
||||||
RemoveProject,
|
RemoveProject,
|
||||||
|
updateProjectController,
|
||||||
|
ViewData,
|
||||||
} from "../controller/project/projectController.ts";
|
} from "../controller/project/projectController.ts";
|
||||||
|
|
||||||
const projectRouter = express.Router();
|
const projectRouter = express.Router();
|
||||||
projectRouter.post("/upsertProject", createProjectController);
|
projectRouter.post("/upsertProject", createProjectController);
|
||||||
projectRouter.get("/Projects/:userId/:organization", GetProjects);
|
projectRouter.get("/Projects/:userId/:organization", GetProjects);
|
||||||
projectRouter.patch("/Project/archive/:projectId", RemoveProject);
|
projectRouter.patch("/Project/archive/:projectId", RemoveProject);
|
||||||
|
projectRouter.patch("/Project/modify", updateProjectController);
|
||||||
|
projectRouter.get("/Project/view", ViewData);
|
||||||
export default projectRouter;
|
export default projectRouter;
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ import {
|
|||||||
createProject,
|
createProject,
|
||||||
DeleteProject,
|
DeleteProject,
|
||||||
GetAllProjects,
|
GetAllProjects,
|
||||||
|
updateProject,
|
||||||
|
viewProject,
|
||||||
} from "../../../shared/services/project/project-Services.ts";
|
} from "../../../shared/services/project/project-Services.ts";
|
||||||
|
|
||||||
export const createProjectController = async (
|
export const createProjectController = async (
|
||||||
@@ -10,11 +12,13 @@ export const createProjectController = async (
|
|||||||
res: Response
|
res: Response
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
const { projectName, userId, thumbnail, organization } = req.body;
|
const { projectUuid, userId, thumbnail, organization } = req.body;
|
||||||
if (!projectName || !userId || !thumbnail || !organization)
|
if (!projectUuid || !userId || !thumbnail || !organization) {
|
||||||
res.status(400).json({
|
res.status(400).json({
|
||||||
message: "All fields are required",
|
message: "All fields are required",
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
const result = await createProject(req.body);
|
const result = await createProject(req.body);
|
||||||
|
|
||||||
switch (result.status) {
|
switch (result.status) {
|
||||||
@@ -46,6 +50,7 @@ export const createProjectController = async (
|
|||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
message: "Unknown error",
|
message: "Unknown error",
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
export const GetProjects = async (
|
export const GetProjects = async (
|
||||||
@@ -54,10 +59,12 @@ export const GetProjects = async (
|
|||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
const { userId, organization } = req.params;
|
const { userId, organization } = req.params;
|
||||||
if (!userId || !organization)
|
if (!userId || !organization) {
|
||||||
res.status(400).json({
|
res.status(400).json({
|
||||||
message: "All fields are required",
|
message: "All fields are required",
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
const result = await GetAllProjects({ userId, organization });
|
const result = await GetAllProjects({ userId, organization });
|
||||||
switch (result?.status) {
|
switch (result?.status) {
|
||||||
case "User not found":
|
case "User not found":
|
||||||
@@ -81,6 +88,7 @@ export const GetProjects = async (
|
|||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
message: "Unknown error",
|
message: "Unknown error",
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
export const RemoveProject = async (
|
export const RemoveProject = async (
|
||||||
@@ -90,10 +98,12 @@ export const RemoveProject = async (
|
|||||||
try {
|
try {
|
||||||
const { projectId } = req.params;
|
const { projectId } = req.params;
|
||||||
const { organization, userId } = req.body;
|
const { organization, userId } = req.body;
|
||||||
if (!projectId || !organization || !userId)
|
if (!projectId || !organization || !userId) {
|
||||||
res.status(400).json({
|
res.status(400).json({
|
||||||
message: "All fields are required",
|
message: "All fields are required",
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
const result = await DeleteProject({ projectId, organization, userId });
|
const result = await DeleteProject({ projectId, organization, userId });
|
||||||
switch (result?.status) {
|
switch (result?.status) {
|
||||||
case "Project not found":
|
case "Project not found":
|
||||||
@@ -121,5 +131,103 @@ export const RemoveProject = async (
|
|||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
message: "Unknown error",
|
message: "Unknown error",
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
export const updateProjectController = async (
|
||||||
|
req: Request,
|
||||||
|
res: Response
|
||||||
|
): Promise<void> => {
|
||||||
|
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<void> => {
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -9,8 +9,7 @@ export const GetTrashList = async (
|
|||||||
res: Response
|
res: Response
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
const { organization } = req.query as { organization?: string };
|
const { organization } = req.query as { organization: string };
|
||||||
console.log("organization: ", organization);
|
|
||||||
if (!organization) {
|
if (!organization) {
|
||||||
res.status(400).json({
|
res.status(400).json({
|
||||||
message: "All fields are required",
|
message: "All fields are required",
|
||||||
@@ -53,8 +52,8 @@ export const RestoreTrash = async (
|
|||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
const { organization, projectId } = req.query as {
|
const { organization, projectId } = req.query as {
|
||||||
organization?: string;
|
organization: string;
|
||||||
projectId?: string;
|
projectId: string;
|
||||||
};
|
};
|
||||||
console.log("organization: ", organization);
|
console.log("organization: ", organization);
|
||||||
if (!organization || !projectId) {
|
if (!organization || !projectId) {
|
||||||
|
|||||||
@@ -4,12 +4,11 @@ export interface User extends Document {
|
|||||||
userName: String;
|
userName: String;
|
||||||
email: String;
|
email: String;
|
||||||
password: String;
|
password: String;
|
||||||
|
recentlyViewed: string[];
|
||||||
role: String;
|
role: String;
|
||||||
profilePicture: String;
|
profilePicture: String;
|
||||||
isShare:Boolean,
|
isShare: Boolean;
|
||||||
activeStatus:string
|
activeStatus: string;
|
||||||
|
|
||||||
}
|
}
|
||||||
const signupschema: Schema = new Schema({
|
const signupschema: Schema = new Schema({
|
||||||
userName: {
|
userName: {
|
||||||
@@ -35,16 +34,19 @@ const signupschema: Schema = new Schema({
|
|||||||
type: String,
|
type: String,
|
||||||
// default: "default-profile-picture.jpg"
|
// default: "default-profile-picture.jpg"
|
||||||
},
|
},
|
||||||
isShare:{
|
isShare: {
|
||||||
type:Boolean,
|
type: Boolean,
|
||||||
default:false
|
default: false,
|
||||||
},
|
},
|
||||||
activeStatus:{
|
activeStatus: {
|
||||||
type:String,
|
type: String,
|
||||||
enum: ["online", "offline"],
|
enum: ["online", "offline"],
|
||||||
default: "offline"
|
default: "offline",
|
||||||
}
|
},
|
||||||
|
recentlyViewed: {
|
||||||
|
type: [String],
|
||||||
|
default: [],
|
||||||
|
},
|
||||||
});
|
});
|
||||||
// const userModel = (db: string) => {
|
// const userModel = (db: string) => {
|
||||||
// const mongoUrl = process.env.MONGO_URI || "";
|
// const mongoUrl = process.env.MONGO_URI || "";
|
||||||
@@ -63,7 +65,7 @@ const signupschema: Schema = new Schema({
|
|||||||
// };
|
// };
|
||||||
|
|
||||||
// export default userModel;
|
// export default userModel;
|
||||||
const userModel = (db:string) => {
|
const userModel = (db: string) => {
|
||||||
return MainModel(db, "Users", signupschema, "Users")
|
return MainModel(db, "Users", signupschema, "Users");
|
||||||
};
|
};
|
||||||
export default userModel;
|
export default userModel;
|
||||||
@@ -3,12 +3,12 @@ import userModel from "../../model/user-Model.ts";
|
|||||||
import { Types } from "mongoose";
|
import { Types } from "mongoose";
|
||||||
import versionModel from "../../model/version/versionModel.ts";
|
import versionModel from "../../model/version/versionModel.ts";
|
||||||
export const existingProject = async (
|
export const existingProject = async (
|
||||||
projectName: string,
|
projectUuid: string,
|
||||||
organization: string,
|
organization: string,
|
||||||
userId: string
|
userId: string
|
||||||
) => {
|
) => {
|
||||||
const projectData = await projectModel(organization).findOne({
|
const projectData = await projectModel(organization).findOne({
|
||||||
projectName: projectName,
|
projectUuid: projectUuid,
|
||||||
createdBy: userId,
|
createdBy: userId,
|
||||||
isArchive: false,
|
isArchive: false,
|
||||||
});
|
});
|
||||||
@@ -47,3 +47,33 @@ export const previousVersion = async (
|
|||||||
// .sort({ version: -1 });
|
// .sort({ version: -1 });
|
||||||
return result;
|
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,
|
existingUser,
|
||||||
archiveProject,
|
archiveProject,
|
||||||
previousVersion,
|
previousVersion,
|
||||||
|
generateUntitledProjectName,
|
||||||
} from "../helpers/ProjecthelperFn.ts";
|
} from "../helpers/ProjecthelperFn.ts";
|
||||||
interface CreateProjectInput {
|
interface CreateProjectInput {
|
||||||
projectName: string;
|
projectName: string;
|
||||||
@@ -16,25 +17,26 @@ interface CreateProjectInput {
|
|||||||
sharedUsers?: string[];
|
sharedUsers?: string[];
|
||||||
organization: string;
|
organization: string;
|
||||||
}
|
}
|
||||||
|
interface updateProjectInput {
|
||||||
|
projectName: string;
|
||||||
|
projectId: string;
|
||||||
|
userId: string; // user ID
|
||||||
|
thumbnail?: string;
|
||||||
|
sharedUsers?: string[];
|
||||||
|
organization: string;
|
||||||
|
}
|
||||||
interface GetProjectsInterface {
|
interface GetProjectsInterface {
|
||||||
userId: string;
|
userId: string;
|
||||||
organization: string;
|
organization: string;
|
||||||
}
|
}
|
||||||
interface DeleteProjectInterface {
|
interface ProjectInterface {
|
||||||
projectId: string;
|
projectId: string;
|
||||||
userId: string;
|
userId: string;
|
||||||
organization: string;
|
organization: string;
|
||||||
}
|
}
|
||||||
export const createProject = async (data: CreateProjectInput) => {
|
export const createProject = async (data: CreateProjectInput) => {
|
||||||
try {
|
try {
|
||||||
const {
|
const { userId, thumbnail, sharedUsers, organization, projectUuid } = data;
|
||||||
projectName,
|
|
||||||
projectUuid,
|
|
||||||
userId,
|
|
||||||
thumbnail,
|
|
||||||
sharedUsers,
|
|
||||||
organization,
|
|
||||||
} = data;
|
|
||||||
const userExisting = await existingUser(userId, organization);
|
const userExisting = await existingUser(userId, organization);
|
||||||
if (!userExisting) {
|
if (!userExisting) {
|
||||||
return {
|
return {
|
||||||
@@ -42,7 +44,7 @@ export const createProject = async (data: CreateProjectInput) => {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
const projectExisting = await existingProject(
|
const projectExisting = await existingProject(
|
||||||
projectName,
|
projectUuid,
|
||||||
organization,
|
organization,
|
||||||
userId
|
userId
|
||||||
);
|
);
|
||||||
@@ -53,9 +55,12 @@ export const createProject = async (data: CreateProjectInput) => {
|
|||||||
project: projectExisting,
|
project: projectExisting,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
const newProjectName = await generateUntitledProjectName(
|
||||||
|
organization,
|
||||||
|
userId
|
||||||
|
);
|
||||||
const project = await projectModel(organization).create({
|
const project = await projectModel(organization).create({
|
||||||
projectName: projectName,
|
projectName: newProjectName,
|
||||||
projectUuid: projectUuid,
|
projectUuid: projectUuid,
|
||||||
createdBy: userId,
|
createdBy: userId,
|
||||||
thumbnail: thumbnail,
|
thumbnail: thumbnail,
|
||||||
@@ -95,14 +100,14 @@ export const GetAllProjects = async (data: GetProjectsInterface) => {
|
|||||||
.find({
|
.find({
|
||||||
isArchive: false,
|
isArchive: false,
|
||||||
})
|
})
|
||||||
.select("_id projectName createdBy thumbnail");
|
.select("_id projectName createdBy thumbnail createdAt");
|
||||||
if (projectDatas) return { status: "Success", Datas: projectDatas };
|
if (projectDatas) return { status: "Success", Datas: projectDatas };
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
return { status: error };
|
return { status: error };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const DeleteProject = async (data: DeleteProjectInterface) => {
|
export const DeleteProject = async (data: ProjectInterface) => {
|
||||||
try {
|
try {
|
||||||
const { projectId, organization, userId } = data;
|
const { projectId, organization, userId } = data;
|
||||||
const ExistingUser = await existingUser(userId, organization);
|
const ExistingUser = await existingUser(userId, organization);
|
||||||
@@ -123,3 +128,63 @@ export const DeleteProject = async (data: DeleteProjectInterface) => {
|
|||||||
return { status: error };
|
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) => {
|
export const RestoreTrashData = async (data: IRestore) => {
|
||||||
try {
|
try {
|
||||||
const { projectId, organization } = data;
|
const { projectId, organization } = data;
|
||||||
console.log("projectId: ", projectId);
|
|
||||||
const findProject = await projectModel(organization).findOne({
|
const findProject = await projectModel(organization).findOne({
|
||||||
_id: projectId,
|
_id: projectId,
|
||||||
isArchive: true,
|
isArchive: true,
|
||||||
});
|
});
|
||||||
console.log("findProject: ", findProject);
|
|
||||||
if (!findProject) return { status: "Project not found" };
|
if (!findProject) return { status: "Project not found" };
|
||||||
const restoreData = await projectModel(organization).findOneAndUpdate(
|
const restoreData = await projectModel(organization).findOneAndUpdate(
|
||||||
{ _id: projectId, isArchive: true },
|
{ _id: projectId, isArchive: true },
|
||||||
|
|||||||
Reference in New Issue
Block a user