V1 folder created For auth Data project, trash, home page based on the token and role based
This commit is contained in:
@@ -0,0 +1,151 @@
|
|||||||
|
import { Request, Response } from "express";
|
||||||
|
import {
|
||||||
|
RecentlyAdded,
|
||||||
|
searchProject,
|
||||||
|
searchTrashProject,
|
||||||
|
} from "../../../../shared/services/v1home/v1homeservice.ts";
|
||||||
|
import { AuthenticatedRequest } from "../../../../shared/utils/token.ts";
|
||||||
|
|
||||||
|
export const recentDataController = async (
|
||||||
|
req: AuthenticatedRequest,
|
||||||
|
res: Response
|
||||||
|
): Promise<void> => {
|
||||||
|
try {
|
||||||
|
const { userId, organization, role } = req.user || {};
|
||||||
|
if (!userId || !organization || !role) {
|
||||||
|
res.status(400).json({
|
||||||
|
message: "All fields are required",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const result = await RecentlyAdded({ userId, organization, role });
|
||||||
|
|
||||||
|
switch (result.status) {
|
||||||
|
case "User not found":
|
||||||
|
res.status(404).json({
|
||||||
|
message: "User not found",
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
case "Datas were empty":
|
||||||
|
res.status(200).json({
|
||||||
|
RecentlyViewed: [],
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
case "Success":
|
||||||
|
res.status(200).json({
|
||||||
|
RecentlyViewed: 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 searchProjectController = async (
|
||||||
|
req: Request,
|
||||||
|
res: Response
|
||||||
|
): Promise<void> => {
|
||||||
|
try {
|
||||||
|
const { searchName, organization, userId } = req.query as {
|
||||||
|
organization: string;
|
||||||
|
searchName: string;
|
||||||
|
userId: string;
|
||||||
|
};
|
||||||
|
if (!userId || !organization || !searchName) {
|
||||||
|
res.status(400).json({
|
||||||
|
message: "All fields are required",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const result = await searchProject({
|
||||||
|
searchName,
|
||||||
|
organization,
|
||||||
|
userId,
|
||||||
|
});
|
||||||
|
switch (result?.status) {
|
||||||
|
case "Project not found":
|
||||||
|
res.status(200).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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
export const searchTrashProjectController = async (
|
||||||
|
req: Request,
|
||||||
|
res: Response
|
||||||
|
): Promise<void> => {
|
||||||
|
try {
|
||||||
|
const { searchName, organization, userId } = req.query as {
|
||||||
|
organization: string;
|
||||||
|
searchName: string;
|
||||||
|
userId: string;
|
||||||
|
};
|
||||||
|
if (!userId || !organization || !searchName) {
|
||||||
|
res.status(400).json({
|
||||||
|
message: "All fields are required",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const result = await searchTrashProject({
|
||||||
|
searchName,
|
||||||
|
organization,
|
||||||
|
userId,
|
||||||
|
});
|
||||||
|
switch (result?.status) {
|
||||||
|
case "Project not found":
|
||||||
|
res.status(200).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;
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,270 @@
|
|||||||
|
import { Request, Response } from "express";
|
||||||
|
import {
|
||||||
|
createProject,
|
||||||
|
DeleteProject,
|
||||||
|
GetAllProjects,
|
||||||
|
updateProject,
|
||||||
|
viewProject,
|
||||||
|
} from "../../../../shared/services/v1Project/v1projectservice.ts";
|
||||||
|
import { AuthenticatedRequest } from "../../../../shared/utils/token.ts";
|
||||||
|
|
||||||
|
export const createProjectController = async (
|
||||||
|
req: AuthenticatedRequest,
|
||||||
|
res: Response
|
||||||
|
): Promise<void> => {
|
||||||
|
try {
|
||||||
|
const { userId, organization } = req.user || {};
|
||||||
|
console.log("req.user: ", req.user);
|
||||||
|
const { projectUuid, thumbnail } = req.body;
|
||||||
|
if (!req.user || !req.user.userId || !req.user.organization) {
|
||||||
|
res.status(401).json({ message: "Unauthorized" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!projectUuid || !thumbnail) {
|
||||||
|
res.status(400).json({
|
||||||
|
message: "All fields are required",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const result = await createProject({ ...req.body, userId, organization });
|
||||||
|
|
||||||
|
switch (result.status) {
|
||||||
|
case "project_exists":
|
||||||
|
res.status(403).json({
|
||||||
|
message: "Project already exists",
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "user_not_found":
|
||||||
|
res.status(404).json({
|
||||||
|
message: "User not found",
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "Success":
|
||||||
|
res.status(201).json({
|
||||||
|
message: "Project created Successfully",
|
||||||
|
projectId: result.project._id,
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
res.status(500).json({
|
||||||
|
message: "Internal server error",
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({
|
||||||
|
message: "Unknown error",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
export const GetProjects = async (
|
||||||
|
req: AuthenticatedRequest,
|
||||||
|
res: Response
|
||||||
|
): Promise<void> => {
|
||||||
|
try {
|
||||||
|
const { userId, organization, role } = req.user || {};
|
||||||
|
// const { userId, organization } = req.params;
|
||||||
|
if (!userId || !organization || !role) {
|
||||||
|
res.status(400).json({
|
||||||
|
message: "All fields are required",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const result = await GetAllProjects({ userId, organization, role });
|
||||||
|
switch (result?.status) {
|
||||||
|
case "User not found":
|
||||||
|
res.status(404).json({
|
||||||
|
message: "User not found",
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "Success":
|
||||||
|
res.status(201).json({
|
||||||
|
Projects: result?.Datas,
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
res.status(500).json({
|
||||||
|
message: "Internal server error",
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({
|
||||||
|
message: "Unknown error",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
export const RemoveProject = async (
|
||||||
|
req: AuthenticatedRequest,
|
||||||
|
res: Response
|
||||||
|
): Promise<void> => {
|
||||||
|
try {
|
||||||
|
const { projectId } = req.params;
|
||||||
|
// const { organization, userId } = req.body;
|
||||||
|
const { organization, userId, role } = req.user || {};
|
||||||
|
if (
|
||||||
|
!req.user ||
|
||||||
|
!req.user.userId ||
|
||||||
|
!req.user.organization ||
|
||||||
|
!req.user.role
|
||||||
|
) {
|
||||||
|
res.status(401).json({ message: "Unauthorized" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!projectId || !organization || !userId || !role) {
|
||||||
|
res.status(400).json({
|
||||||
|
message: "All fields are required",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const result = await DeleteProject({
|
||||||
|
projectId,
|
||||||
|
organization,
|
||||||
|
userId,
|
||||||
|
role,
|
||||||
|
});
|
||||||
|
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(201).json({
|
||||||
|
message: "Project Deleted Successfully",
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
res.status(500).json({
|
||||||
|
message: "Internal server error",
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({
|
||||||
|
message: "Unknown error",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
export const updateProjectController = async (
|
||||||
|
req: AuthenticatedRequest,
|
||||||
|
res: Response
|
||||||
|
): Promise<void> => {
|
||||||
|
try {
|
||||||
|
const { userId, organization, role } = req.user || {};
|
||||||
|
const { projectId } = req.params;
|
||||||
|
const { projectName, thumbnail } = req.body;
|
||||||
|
if (!userId || !organization || !projectId || !role) {
|
||||||
|
res.status(400).json({
|
||||||
|
message: "All fields are required",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const result = await updateProject({
|
||||||
|
projectId,
|
||||||
|
organization,
|
||||||
|
userId,
|
||||||
|
projectName,
|
||||||
|
thumbnail,
|
||||||
|
role,
|
||||||
|
});
|
||||||
|
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: AuthenticatedRequest,
|
||||||
|
res: Response
|
||||||
|
): Promise<void> => {
|
||||||
|
try {
|
||||||
|
const { organization, userId, role } = req.user || {};
|
||||||
|
if (
|
||||||
|
!req.user ||
|
||||||
|
!req.user.userId ||
|
||||||
|
!req.user.organization ||
|
||||||
|
!req.user.role
|
||||||
|
) {
|
||||||
|
res.status(401).json({ message: "Unauthorized" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const { projectId } = req.params as {
|
||||||
|
projectId: string;
|
||||||
|
};
|
||||||
|
if (!userId || !organization || !projectId || !role) {
|
||||||
|
res.status(400).json({
|
||||||
|
message: "All fields are required",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const result = await viewProject({
|
||||||
|
projectId,
|
||||||
|
organization,
|
||||||
|
userId,
|
||||||
|
role,
|
||||||
|
});
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
import { Request, Response } from "express";
|
||||||
|
import {
|
||||||
|
TrashDatas,
|
||||||
|
RestoreTrashData,
|
||||||
|
} from "../../../../shared/services/v1trash/v1trashservice.ts";
|
||||||
|
import { AuthenticatedRequest } from "../../../../shared/utils/token.ts";
|
||||||
|
|
||||||
|
export const GetTrashList = async (
|
||||||
|
req: AuthenticatedRequest,
|
||||||
|
res: Response
|
||||||
|
): Promise<void> => {
|
||||||
|
try {
|
||||||
|
const { organization, role, userId } = req.user || {};
|
||||||
|
if (!organization || !role || !userId) {
|
||||||
|
res.status(400).json({
|
||||||
|
message: "All fields are required",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const result = await TrashDatas({ organization, role, userId });
|
||||||
|
|
||||||
|
switch (result.status) {
|
||||||
|
case "Trash is Empty":
|
||||||
|
res.status(200).json({
|
||||||
|
message: "Trash is Empty",
|
||||||
|
TrashDatas: [],
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "Success":
|
||||||
|
res.status(200).json({
|
||||||
|
TrashDatas: result.ListDatas,
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
res.status(500).json({
|
||||||
|
message: "Internal server error",
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log("error: ", error);
|
||||||
|
res.status(500).json({
|
||||||
|
message: "Unknown error",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const RestoreTrash = async (
|
||||||
|
req: AuthenticatedRequest,
|
||||||
|
res: Response
|
||||||
|
): Promise<void> => {
|
||||||
|
try {
|
||||||
|
const { organization, role, userId } = req.user || {};
|
||||||
|
const { projectId } = req.query as {
|
||||||
|
projectId: string;
|
||||||
|
};
|
||||||
|
if (!organization || !projectId || !role || !userId) {
|
||||||
|
res.status(400).json({
|
||||||
|
message: "All fields are required",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const result = await RestoreTrashData({
|
||||||
|
organization,
|
||||||
|
projectId,
|
||||||
|
role,
|
||||||
|
userId,
|
||||||
|
});
|
||||||
|
|
||||||
|
switch (result.status) {
|
||||||
|
case "Project not found":
|
||||||
|
res.status(404).json({
|
||||||
|
message: "Project not found",
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
case "Project Restore unsuccessfull":
|
||||||
|
res.status(200).json({
|
||||||
|
message: "Project Restore unsuccessfull",
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
case "Project Restored successfully":
|
||||||
|
res.status(200).json({
|
||||||
|
message: "Project Restored successfully",
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
res.status(500).json({
|
||||||
|
message: "Internal server error",
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log("error: ", error);
|
||||||
|
res.status(500).json({
|
||||||
|
message: "Unknown error",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -7,20 +7,6 @@ import {
|
|||||||
SignupController,
|
SignupController,
|
||||||
} from "../v1Controllers/authController/authControllers.ts";
|
} from "../v1Controllers/authController/authControllers.ts";
|
||||||
import { versioncontroller } from "../v1Controllers/versionController/versioncontroller.ts";
|
import { versioncontroller } from "../v1Controllers/versionController/versioncontroller.ts";
|
||||||
import {
|
|
||||||
createProjectController,
|
|
||||||
GetProjects,
|
|
||||||
RemoveProject,
|
|
||||||
updateProjectController,
|
|
||||||
ViewData,
|
|
||||||
} from "../../controller/project/projectController.ts";
|
|
||||||
import { tokenValidator } from "../../../shared/utils/token.ts";
|
|
||||||
import authorizedRoles from "../../../shared/middleware/rbacMiddleware.ts";
|
|
||||||
import { recentDataController } from "../../controller/home/homeControllers.ts";
|
|
||||||
import {
|
|
||||||
GetTrashList,
|
|
||||||
RestoreTrash,
|
|
||||||
} from "../../controller/trash/trashcontrollers.ts";
|
|
||||||
|
|
||||||
const Authrouter = express.Router();
|
const Authrouter = express.Router();
|
||||||
Authrouter.post("/Auth/signup", SignupController);
|
Authrouter.post("/Auth/signup", SignupController);
|
||||||
@@ -30,54 +16,4 @@ Authrouter.post("/Auth/forgetPassword", ForgetPasswordController);
|
|||||||
Authrouter.post("/Auth/reset-password/:resetToken", ResetPasswordController);
|
Authrouter.post("/Auth/reset-password/:resetToken", ResetPasswordController);
|
||||||
Authrouter.post("/Auth/versionData", versioncontroller);
|
Authrouter.post("/Auth/versionData", versioncontroller);
|
||||||
|
|
||||||
// project
|
|
||||||
Authrouter.post("/Auth/upsertProject", tokenValidator, createProjectController);
|
|
||||||
Authrouter.get(
|
|
||||||
"/Auth/Projects",
|
|
||||||
tokenValidator,
|
|
||||||
authorizedRoles("Admin", "User"),
|
|
||||||
GetProjects
|
|
||||||
);
|
|
||||||
Authrouter.patch(
|
|
||||||
"/Auth/Project/archive/:projectId",
|
|
||||||
tokenValidator,
|
|
||||||
authorizedRoles("Admin", "User"),
|
|
||||||
RemoveProject
|
|
||||||
);
|
|
||||||
|
|
||||||
Authrouter.patch(
|
|
||||||
"/Auth/Project/modify",
|
|
||||||
tokenValidator,
|
|
||||||
authorizedRoles("Admin", "User"),
|
|
||||||
updateProjectController
|
|
||||||
);
|
|
||||||
Authrouter.get(
|
|
||||||
"/Auth/Project/view",
|
|
||||||
tokenValidator,
|
|
||||||
authorizedRoles("Admin", "User"),
|
|
||||||
ViewData
|
|
||||||
);
|
|
||||||
|
|
||||||
//home-Page
|
|
||||||
Authrouter.get(
|
|
||||||
"/Auth/RecentlyViewed",
|
|
||||||
tokenValidator,
|
|
||||||
authorizedRoles("Admin", "User"),
|
|
||||||
recentDataController
|
|
||||||
);
|
|
||||||
|
|
||||||
//trash
|
|
||||||
Authrouter.get(
|
|
||||||
"/Auth/Trash/Lists",
|
|
||||||
tokenValidator,
|
|
||||||
authorizedRoles("Admin", "User"),
|
|
||||||
GetTrashList
|
|
||||||
);
|
|
||||||
|
|
||||||
Authrouter.patch(
|
|
||||||
"/Auth/restore",
|
|
||||||
tokenValidator,
|
|
||||||
authorizedRoles("Admin", "User"),
|
|
||||||
RestoreTrash
|
|
||||||
);
|
|
||||||
export default Authrouter;
|
export default Authrouter;
|
||||||
|
|||||||
16
src/api-server/V1/v1Routes/v1-homeRoutes.ts
Normal file
16
src/api-server/V1/v1Routes/v1-homeRoutes.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import express from "express";
|
||||||
|
import { tokenValidator } from "../../../shared/utils/token.ts";
|
||||||
|
import authorizedRoles from "../../../shared/middleware/rbacMiddleware.ts";
|
||||||
|
import { recentDataController } from "../../V1/v1Controllers/homeController/v1homeController.ts";
|
||||||
|
|
||||||
|
const v1homeRoutes = express.Router();
|
||||||
|
|
||||||
|
//home-Page
|
||||||
|
v1homeRoutes.get(
|
||||||
|
"/RecentlyViewed",
|
||||||
|
tokenValidator,
|
||||||
|
authorizedRoles("Admin", "User"),
|
||||||
|
recentDataController
|
||||||
|
);
|
||||||
|
|
||||||
|
export default v1homeRoutes;
|
||||||
42
src/api-server/V1/v1Routes/v1-projectRoutes.ts
Normal file
42
src/api-server/V1/v1Routes/v1-projectRoutes.ts
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import express from "express";
|
||||||
|
import {
|
||||||
|
createProjectController,
|
||||||
|
GetProjects,
|
||||||
|
RemoveProject,
|
||||||
|
updateProjectController,
|
||||||
|
ViewData,
|
||||||
|
} from "../../V1/v1Controllers/projectController/v1projectController.ts";
|
||||||
|
import { tokenValidator } from "../../../shared/utils/token.ts";
|
||||||
|
import authorizedRoles from "../../../shared/middleware/rbacMiddleware.ts";
|
||||||
|
|
||||||
|
const v1projectRouter = express.Router();
|
||||||
|
|
||||||
|
// project
|
||||||
|
v1projectRouter.post("/upsertProject", tokenValidator, createProjectController);
|
||||||
|
v1projectRouter.get(
|
||||||
|
"/Projects",
|
||||||
|
tokenValidator,
|
||||||
|
authorizedRoles("Admin", "User"),
|
||||||
|
GetProjects
|
||||||
|
);
|
||||||
|
v1projectRouter.patch(
|
||||||
|
"/Project/archive/:projectId",
|
||||||
|
tokenValidator,
|
||||||
|
authorizedRoles("Admin", "User"),
|
||||||
|
RemoveProject
|
||||||
|
);
|
||||||
|
|
||||||
|
v1projectRouter.patch(
|
||||||
|
"/Project/:projectId",
|
||||||
|
tokenValidator,
|
||||||
|
authorizedRoles("Admin", "User"),
|
||||||
|
updateProjectController
|
||||||
|
);
|
||||||
|
v1projectRouter.get(
|
||||||
|
"/Project/:projectId",
|
||||||
|
tokenValidator,
|
||||||
|
authorizedRoles("Admin", "User"),
|
||||||
|
ViewData
|
||||||
|
);
|
||||||
|
|
||||||
|
export default v1projectRouter;
|
||||||
25
src/api-server/V1/v1Routes/v1-trashRoutes.ts
Normal file
25
src/api-server/V1/v1Routes/v1-trashRoutes.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import express from "express";
|
||||||
|
import { tokenValidator } from "../../../shared/utils/token.ts";
|
||||||
|
import authorizedRoles from "../../../shared/middleware/rbacMiddleware.ts";
|
||||||
|
import {
|
||||||
|
GetTrashList,
|
||||||
|
RestoreTrash,
|
||||||
|
} from "../../V1/v1Controllers/trashController/v1trashController.ts";
|
||||||
|
|
||||||
|
const v1TrashRoutes = express.Router();
|
||||||
|
|
||||||
|
//trash
|
||||||
|
v1TrashRoutes.get(
|
||||||
|
"/TrashItems ",
|
||||||
|
tokenValidator,
|
||||||
|
authorizedRoles("Admin", "User"),
|
||||||
|
GetTrashList
|
||||||
|
);
|
||||||
|
|
||||||
|
v1TrashRoutes.patch(
|
||||||
|
"/Trash/restore",
|
||||||
|
tokenValidator,
|
||||||
|
authorizedRoles("Admin", "User"),
|
||||||
|
RestoreTrash
|
||||||
|
);
|
||||||
|
export default v1TrashRoutes;
|
||||||
@@ -22,6 +22,9 @@ import trashRouter from "./Routes/trashRoutes.ts";
|
|||||||
import homePageRouter from "./Routes/homepageRoutes.ts";
|
import homePageRouter from "./Routes/homepageRoutes.ts";
|
||||||
import redis from "../shared/redis/redis.ts";
|
import redis from "../shared/redis/redis.ts";
|
||||||
import Authrouter from "./V1/v1Routes/authRoutes.ts";
|
import Authrouter from "./V1/v1Routes/authRoutes.ts";
|
||||||
|
import v1TrashRoutes from "./V1/v1Routes/v1-trashRoutes.ts";
|
||||||
|
import v1homeRoutes from "./V1/v1Routes/v1-homeRoutes.ts";
|
||||||
|
import v1projectRouter from "./V1/v1Routes/v1-projectRoutes.ts";
|
||||||
// import productFlowRoutes from "./Routes/productFlowRouts.ts";
|
// import productFlowRoutes from "./Routes/productFlowRouts.ts";
|
||||||
redis;
|
redis;
|
||||||
const app = express();
|
const app = express();
|
||||||
@@ -90,7 +93,10 @@ app.use("/api/v1", projectRouter);
|
|||||||
app.use("/api/v1", trashRouter);
|
app.use("/api/v1", trashRouter);
|
||||||
app.use("/api/v1", homePageRouter);
|
app.use("/api/v1", homePageRouter);
|
||||||
|
|
||||||
//New versions
|
//New versions--based on the token and role based
|
||||||
app.use("/API/V1", Authrouter);
|
app.use("/api", Authrouter);
|
||||||
|
app.use("/api/v2", v1projectRouter);
|
||||||
|
app.use("/api/v2", v1TrashRoutes);
|
||||||
|
app.use("/api/v2", v1homeRoutes);
|
||||||
|
|
||||||
export default app;
|
export default app;
|
||||||
|
|||||||
@@ -1,20 +1,23 @@
|
|||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import { RecentlyAdded, searchProject, searchTrashProject } from "../../../shared/services/home/homeService.ts";
|
import {
|
||||||
import { AuthenticatedRequest } from "../../../shared/utils/token.ts";
|
RecentlyAdded,
|
||||||
|
searchProject,
|
||||||
|
searchTrashProject,
|
||||||
|
} from "../../../shared/services/home/homeService.ts";
|
||||||
|
|
||||||
export const recentDataController = async (
|
export const recentDataController = async (
|
||||||
req: AuthenticatedRequest,
|
req: Request,
|
||||||
res: Response
|
res: Response
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
const { userId, organization,role } = req.user||{};
|
const { userId, organization } = req.params;
|
||||||
if (!userId || !organization||!role) {
|
if (!userId || !organization) {
|
||||||
res.status(400).json({
|
res.status(400).json({
|
||||||
message: "All fields are required",
|
message: "All fields are required",
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const result = await RecentlyAdded({ userId, organization,role });
|
const result = await RecentlyAdded({ userId, organization });
|
||||||
|
|
||||||
switch (result.status) {
|
switch (result.status) {
|
||||||
case "User not found":
|
case "User not found":
|
||||||
@@ -45,7 +48,10 @@ export const recentDataController = async (
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
export const searchProjectController = async (req: Request, res: Response): Promise<void> => {
|
export const searchProjectController = async (
|
||||||
|
req: Request,
|
||||||
|
res: Response
|
||||||
|
): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
const { searchName, organization, userId } = req.query as {
|
const { searchName, organization, userId } = req.query as {
|
||||||
organization: string;
|
organization: string;
|
||||||
@@ -92,7 +98,10 @@ export const searchProjectController = async (req: Request, res: Response): Prom
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
export const searchTrashProjectController = async (req: Request, res: Response): Promise<void> => {
|
export const searchTrashProjectController = async (
|
||||||
|
req: Request,
|
||||||
|
res: Response
|
||||||
|
): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
const { searchName, organization, userId } = req.query as {
|
const { searchName, organization, userId } = req.query as {
|
||||||
organization: string;
|
organization: string;
|
||||||
@@ -138,4 +147,4 @@ export const searchTrashProjectController = async (req: Request, res: Response):
|
|||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -6,27 +6,20 @@ import {
|
|||||||
updateProject,
|
updateProject,
|
||||||
viewProject,
|
viewProject,
|
||||||
} from "../../../shared/services/project/project-Services.ts";
|
} from "../../../shared/services/project/project-Services.ts";
|
||||||
import { AuthenticatedRequest } from "../../../shared/utils/token.ts";
|
|
||||||
|
|
||||||
export const createProjectController = async (
|
export const createProjectController = async (
|
||||||
req: AuthenticatedRequest,
|
req: Request,
|
||||||
res: Response
|
res: Response
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
const { userId, organization } = req.user || {};
|
const { projectUuid, userId, thumbnail, organization } = req.body;
|
||||||
console.log("req.user: ", req.user);
|
if (!projectUuid || !userId || !thumbnail || !organization) {
|
||||||
const { projectUuid, thumbnail } = req.body;
|
|
||||||
if (!req.user || !req.user.userId || !req.user.organization) {
|
|
||||||
res.status(401).json({ message: "Unauthorized" });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!projectUuid || !thumbnail) {
|
|
||||||
res.status(400).json({
|
res.status(400).json({
|
||||||
message: "All fields are required",
|
message: "All fields are required",
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const result = await createProject({ ...req.body, userId, organization });
|
const result = await createProject(req.body);
|
||||||
|
|
||||||
switch (result.status) {
|
switch (result.status) {
|
||||||
case "project_exists":
|
case "project_exists":
|
||||||
@@ -61,19 +54,18 @@ export const createProjectController = async (
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
export const GetProjects = async (
|
export const GetProjects = async (
|
||||||
req: AuthenticatedRequest,
|
req: Request,
|
||||||
res: Response
|
res: Response
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
const { userId, organization, role } = req.user || {};
|
const { userId, organization } = req.params;
|
||||||
// const { userId, organization } = req.params;
|
if (!userId || !organization) {
|
||||||
if (!userId || !organization || !role) {
|
|
||||||
res.status(400).json({
|
res.status(400).json({
|
||||||
message: "All fields are required",
|
message: "All fields are required",
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const result = await GetAllProjects({ userId, organization, role });
|
const result = await GetAllProjects({ userId, organization });
|
||||||
switch (result?.status) {
|
switch (result?.status) {
|
||||||
case "User not found":
|
case "User not found":
|
||||||
res.status(404).json({
|
res.status(404).json({
|
||||||
@@ -100,34 +92,19 @@ export const GetProjects = async (
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
export const RemoveProject = async (
|
export const RemoveProject = async (
|
||||||
req: AuthenticatedRequest,
|
req: Request,
|
||||||
res: Response
|
res: Response
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
const { projectId } = req.params;
|
const { projectId } = req.params;
|
||||||
// const { organization, userId } = req.body;
|
const { organization, userId } = req.body;
|
||||||
const { organization, userId, role } = req.user || {};
|
if (!projectId || !organization || !userId) {
|
||||||
if (
|
|
||||||
!req.user ||
|
|
||||||
!req.user.userId ||
|
|
||||||
!req.user.organization ||
|
|
||||||
!req.user.role
|
|
||||||
) {
|
|
||||||
res.status(401).json({ message: "Unauthorized" });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!projectId || !organization || !userId || !role) {
|
|
||||||
res.status(400).json({
|
res.status(400).json({
|
||||||
message: "All fields are required",
|
message: "All fields are required",
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const result = await DeleteProject({
|
const result = await DeleteProject({ projectId, organization, userId });
|
||||||
projectId,
|
|
||||||
organization,
|
|
||||||
userId,
|
|
||||||
role,
|
|
||||||
});
|
|
||||||
switch (result?.status) {
|
switch (result?.status) {
|
||||||
case "Project not found":
|
case "Project not found":
|
||||||
res.status(404).json({
|
res.status(404).json({
|
||||||
@@ -158,13 +135,13 @@ export const RemoveProject = async (
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
export const updateProjectController = async (
|
export const updateProjectController = async (
|
||||||
req: AuthenticatedRequest,
|
req: Request,
|
||||||
res: Response
|
res: Response
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
const { userId, organization, role } = req.user || {};
|
const { projectId, organization, projectName, thumbnail, userId } =
|
||||||
const { projectId, projectName, thumbnail } = req.body;
|
req.body;
|
||||||
if (!userId || !organization || !projectId || !role) {
|
if (!userId || !organization || !projectId) {
|
||||||
res.status(400).json({
|
res.status(400).json({
|
||||||
message: "All fields are required",
|
message: "All fields are required",
|
||||||
});
|
});
|
||||||
@@ -176,7 +153,6 @@ export const updateProjectController = async (
|
|||||||
userId,
|
userId,
|
||||||
projectName,
|
projectName,
|
||||||
thumbnail,
|
thumbnail,
|
||||||
role,
|
|
||||||
});
|
});
|
||||||
switch (result?.status) {
|
switch (result?.status) {
|
||||||
case "Project not found":
|
case "Project not found":
|
||||||
@@ -208,25 +184,14 @@ export const updateProjectController = async (
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
export const ViewData = async (
|
export const ViewData = async (req: Request, res: Response): Promise<void> => {
|
||||||
req: AuthenticatedRequest,
|
|
||||||
res: Response
|
|
||||||
): Promise<void> => {
|
|
||||||
try {
|
try {
|
||||||
const { organization, userId, role } = req.user || {};
|
const { projectId, organization, userId } = req.query as {
|
||||||
if (
|
organization: string;
|
||||||
!req.user ||
|
|
||||||
!req.user.userId ||
|
|
||||||
!req.user.organization ||
|
|
||||||
!req.user.role
|
|
||||||
) {
|
|
||||||
res.status(401).json({ message: "Unauthorized" });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const { projectId } = req.query as {
|
|
||||||
projectId: string;
|
projectId: string;
|
||||||
|
userId: string;
|
||||||
};
|
};
|
||||||
if (!userId || !organization || !projectId || !role) {
|
if (!userId || !organization || !projectId) {
|
||||||
res.status(400).json({
|
res.status(400).json({
|
||||||
message: "All fields are required",
|
message: "All fields are required",
|
||||||
});
|
});
|
||||||
@@ -236,7 +201,6 @@ export const ViewData = async (
|
|||||||
projectId,
|
projectId,
|
||||||
organization,
|
organization,
|
||||||
userId,
|
userId,
|
||||||
role,
|
|
||||||
});
|
});
|
||||||
switch (result?.status) {
|
switch (result?.status) {
|
||||||
case "Project not found":
|
case "Project not found":
|
||||||
|
|||||||
@@ -3,21 +3,20 @@ import {
|
|||||||
TrashDatas,
|
TrashDatas,
|
||||||
RestoreTrashData,
|
RestoreTrashData,
|
||||||
} from "../../../shared/services/trash/trashService.ts";
|
} from "../../../shared/services/trash/trashService.ts";
|
||||||
import { AuthenticatedRequest } from "../../../shared/utils/token.ts";
|
|
||||||
|
|
||||||
export const GetTrashList = async (
|
export const GetTrashList = async (
|
||||||
req: AuthenticatedRequest,
|
req: Request,
|
||||||
res: Response
|
res: Response
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
const { organization, role, userId } = req.user || {};
|
const { organization } = req.query as { organization: string };
|
||||||
if (!organization || !role || !userId) {
|
if (!organization) {
|
||||||
res.status(400).json({
|
res.status(400).json({
|
||||||
message: "All fields are required",
|
message: "All fields are required",
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const result = await TrashDatas({ organization, role, userId });
|
const result = await TrashDatas({ organization });
|
||||||
|
|
||||||
switch (result.status) {
|
switch (result.status) {
|
||||||
case "Trash is Empty":
|
case "Trash is Empty":
|
||||||
@@ -29,6 +28,7 @@ export const GetTrashList = async (
|
|||||||
|
|
||||||
case "Success":
|
case "Success":
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
|
// message: "Project created Successfully",
|
||||||
TrashDatas: result.ListDatas,
|
TrashDatas: result.ListDatas,
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
@@ -47,26 +47,22 @@ export const GetTrashList = async (
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const RestoreTrash = async (
|
export const RestoreTrash = async (
|
||||||
req: AuthenticatedRequest,
|
req: Request,
|
||||||
res: Response
|
res: Response
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
const { organization, role, userId } = req.user || {};
|
const { organization, projectId } = req.query as {
|
||||||
const { projectId } = req.query as {
|
organization: string;
|
||||||
projectId: string;
|
projectId: string;
|
||||||
};
|
};
|
||||||
if (!organization || !projectId || !role || !userId) {
|
console.log("organization: ", organization);
|
||||||
|
if (!organization || !projectId) {
|
||||||
res.status(400).json({
|
res.status(400).json({
|
||||||
message: "All fields are required",
|
message: "All fields are required",
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const result = await RestoreTrashData({
|
const result = await RestoreTrashData({ organization, projectId });
|
||||||
organization,
|
|
||||||
projectId,
|
|
||||||
role,
|
|
||||||
userId,
|
|
||||||
});
|
|
||||||
|
|
||||||
switch (result.status) {
|
switch (result.status) {
|
||||||
case "Project not found":
|
case "Project not found":
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import projectModel from "../../model/project/project-model.ts";
|
|||||||
import userModel from "../../model/user-Model.ts";
|
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";
|
||||||
import AuthModel from "../../V1Models/Auth/userAuthModel.ts";
|
|
||||||
export const existingProject = async (
|
export const existingProject = async (
|
||||||
projectUuid: string,
|
projectUuid: string,
|
||||||
organization: string,
|
organization: string,
|
||||||
@@ -21,7 +20,7 @@ export const existingUser = async (userId: string, organization: string) => {
|
|||||||
console.log("Invalid ObjectId format");
|
console.log("Invalid ObjectId format");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const userData = await AuthModel(organization).findOne({
|
const userData = await userModel(organization).findOne({
|
||||||
_id: userId,
|
_id: userId,
|
||||||
});
|
});
|
||||||
return userData;
|
return userData;
|
||||||
|
|||||||
90
src/shared/services/helpers/v1projecthelperFns.ts
Normal file
90
src/shared/services/helpers/v1projecthelperFns.ts
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
import projectModel from "../../V1Models/Project/project-model.ts";
|
||||||
|
import { Types } from "mongoose";
|
||||||
|
import versionModel from "../../V1Models/Version/versionModel.ts";
|
||||||
|
import AuthModel from "../../V1Models/Auth/userAuthModel.ts";
|
||||||
|
export const existingProject = async (
|
||||||
|
projectUuid: string,
|
||||||
|
organization: string,
|
||||||
|
userId: string
|
||||||
|
) => {
|
||||||
|
const projectData = await projectModel(organization).findOne({
|
||||||
|
projectUuid: projectUuid,
|
||||||
|
createdBy: userId,
|
||||||
|
isArchive: false,
|
||||||
|
});
|
||||||
|
return projectData;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const existingUser = async (userId: string, organization: string) => {
|
||||||
|
if (!Types.ObjectId.isValid(userId)) {
|
||||||
|
console.log("Invalid ObjectId format");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const userData = await AuthModel(organization).findOne({
|
||||||
|
_id: userId,
|
||||||
|
});
|
||||||
|
return userData;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const archiveProject = async (
|
||||||
|
projectId: string,
|
||||||
|
organization: string
|
||||||
|
) => {
|
||||||
|
return await projectModel(organization).findByIdAndUpdate(
|
||||||
|
projectId,
|
||||||
|
{ isArchive: true },
|
||||||
|
{ new: true }
|
||||||
|
);
|
||||||
|
};
|
||||||
|
export const previousVersion = async (
|
||||||
|
projectId: string,
|
||||||
|
organization: string
|
||||||
|
) => {
|
||||||
|
const result = await versionModel(organization).findOne({
|
||||||
|
projectId: projectId,
|
||||||
|
isArchive: false,
|
||||||
|
});
|
||||||
|
// .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+))?$/);
|
||||||
|
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}`;
|
||||||
|
};
|
||||||
|
export const existingProjectById = async (
|
||||||
|
projectId: string,
|
||||||
|
organization: string,
|
||||||
|
userId: string
|
||||||
|
) => {
|
||||||
|
const projectData = await projectModel(organization).findOne({
|
||||||
|
_id: projectId,
|
||||||
|
createdBy: userId,
|
||||||
|
isArchive: false,
|
||||||
|
});
|
||||||
|
return projectData;
|
||||||
|
};
|
||||||
@@ -1,12 +1,10 @@
|
|||||||
import projectModel from "../../model/project/project-model.ts";
|
import projectModel from "../../model/project/project-model.ts";
|
||||||
import userModel from "../../model/user-Model.ts";
|
import userModel from "../../model/user-Model.ts";
|
||||||
import UsersDataModel from "../../V1Models/Auth/user.ts";
|
|
||||||
import { existingUser } from "../helpers/ProjecthelperFn.ts";
|
import { existingUser } from "../helpers/ProjecthelperFn.ts";
|
||||||
|
|
||||||
interface IRecentData {
|
interface IRecentData {
|
||||||
organization: string;
|
organization: string;
|
||||||
userId: string;
|
userId: string;
|
||||||
role: string;
|
|
||||||
}
|
}
|
||||||
interface IProject {
|
interface IProject {
|
||||||
_id: string;
|
_id: string;
|
||||||
@@ -21,31 +19,26 @@ interface searchProjectInterface {
|
|||||||
userId: string;
|
userId: string;
|
||||||
organization: string;
|
organization: string;
|
||||||
}
|
}
|
||||||
interface RoleFilter {
|
|
||||||
isArchive: boolean;
|
|
||||||
createdBy?: string;
|
|
||||||
}
|
|
||||||
export const RecentlyAdded = async (data: IRecentData) => {
|
export const RecentlyAdded = async (data: IRecentData) => {
|
||||||
try {
|
try {
|
||||||
const { userId, organization, role } = data;
|
const { userId, organization } = data;
|
||||||
const userExisting = await existingUser(userId, organization);
|
const userExisting = await existingUser(userId, organization);
|
||||||
if (!userExisting) return { status: "User not found" };
|
if (!userExisting) return { status: "User not found" };
|
||||||
const userRecentData = await UsersDataModel(organization)
|
const userRecentData = await userModel(organization)
|
||||||
.findOne({ userId: userId, isArchive: false })
|
.findOne({ _id: userId })
|
||||||
.populate({
|
.populate({
|
||||||
path: "recentlyViewed",
|
path: "recentlyViewed",
|
||||||
model: projectModel(organization),
|
model: projectModel(organization),
|
||||||
select: "_id",
|
select: "_id",
|
||||||
});
|
});
|
||||||
let filter = { isArchive: false } as RoleFilter;
|
|
||||||
if (role === "User") {
|
|
||||||
filter.createdBy = userId;
|
|
||||||
}
|
|
||||||
const populatedProjects = userRecentData.recentlyViewed as IProject[];
|
const populatedProjects = userRecentData.recentlyViewed as IProject[];
|
||||||
const RecentDatas = await Promise.all(
|
const RecentDatas = await Promise.all(
|
||||||
populatedProjects.map(async (project) => {
|
populatedProjects.map(async (project) => {
|
||||||
const projectExisting = await projectModel(organization)
|
const projectExisting = await projectModel(organization)
|
||||||
.findOne(filter)
|
.findOne({
|
||||||
|
_id: project._id,
|
||||||
|
isArchive: false,
|
||||||
|
})
|
||||||
.select("_id projectName createdBy thumbnail createdAt isViewed");
|
.select("_id projectName createdBy thumbnail createdAt isViewed");
|
||||||
return projectExisting;
|
return projectExisting;
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
import { ObjectId } from "mongoose";
|
|
||||||
import projectModel from "../../model/project/project-model.ts";
|
import projectModel from "../../model/project/project-model.ts";
|
||||||
import userModel from "../../model/user-Model.ts";
|
import userModel from "../../model/user-Model.ts";
|
||||||
|
import { Types } from "mongoose";
|
||||||
import versionModel from "../../model/version/versionModel.ts";
|
import versionModel from "../../model/version/versionModel.ts";
|
||||||
import { AuthenticatedRequest } from "../../utils/token.ts";
|
|
||||||
import {
|
import {
|
||||||
existingProject,
|
existingProject,
|
||||||
existingUser,
|
existingUser,
|
||||||
@@ -10,7 +9,6 @@ import {
|
|||||||
previousVersion,
|
previousVersion,
|
||||||
generateUntitledProjectName,
|
generateUntitledProjectName,
|
||||||
} from "../helpers/ProjecthelperFn.ts";
|
} from "../helpers/ProjecthelperFn.ts";
|
||||||
import UsersDataModel from "../../V1Models/Auth/user.ts";
|
|
||||||
interface CreateProjectInput {
|
interface CreateProjectInput {
|
||||||
projectName: string;
|
projectName: string;
|
||||||
projectUuid: string;
|
projectUuid: string;
|
||||||
@@ -26,26 +24,20 @@ interface updateProjectInput {
|
|||||||
thumbnail?: string;
|
thumbnail?: string;
|
||||||
sharedUsers?: string[];
|
sharedUsers?: string[];
|
||||||
organization: string;
|
organization: string;
|
||||||
role: string;
|
|
||||||
}
|
}
|
||||||
interface GetProjectsInterface {
|
interface GetProjectsInterface {
|
||||||
userId: string;
|
userId: string;
|
||||||
organization: string;
|
organization: string;
|
||||||
role: string;
|
|
||||||
}
|
}
|
||||||
interface ProjectInterface {
|
interface ProjectInterface {
|
||||||
projectId: string;
|
projectId: string;
|
||||||
userId: string;
|
userId: string;
|
||||||
organization: string;
|
organization: string;
|
||||||
role: string;
|
|
||||||
}
|
|
||||||
interface RoleFilter {
|
|
||||||
isArchive: boolean;
|
|
||||||
createdBy?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const createProject = async (data: CreateProjectInput) => {
|
export const createProject = async (data: CreateProjectInput) => {
|
||||||
try {
|
try {
|
||||||
const { thumbnail, sharedUsers, projectUuid, userId, organization } = data;
|
const { userId, thumbnail, sharedUsers, organization, projectUuid } = data;
|
||||||
const userExisting = await existingUser(userId, organization);
|
const userExisting = await existingUser(userId, organization);
|
||||||
if (!userExisting) {
|
if (!userExisting) {
|
||||||
return {
|
return {
|
||||||
@@ -102,16 +94,17 @@ export const createProject = async (data: CreateProjectInput) => {
|
|||||||
|
|
||||||
export const GetAllProjects = async (data: GetProjectsInterface) => {
|
export const GetAllProjects = async (data: GetProjectsInterface) => {
|
||||||
try {
|
try {
|
||||||
const { userId, organization, role } = data;
|
const { userId, organization } = data;
|
||||||
await existingUser(userId, organization);
|
await existingUser(userId, organization);
|
||||||
if (!existingUser) return { status: "User not found" };
|
if (!existingUser) return { status: "User not found" };
|
||||||
let filter = { isArchive: false } as RoleFilter;
|
|
||||||
if (role === "User") {
|
|
||||||
filter.createdBy = userId;
|
|
||||||
}
|
|
||||||
const projectDatas = await projectModel(organization)
|
const projectDatas = await projectModel(organization)
|
||||||
.find(filter)
|
.find({
|
||||||
.select("_id projectName createdBy thumbnail createdAt projectUuid");
|
createdBy: userId,
|
||||||
|
isArchive: false,
|
||||||
|
})
|
||||||
|
.select(
|
||||||
|
"_id projectName createdBy thumbnail createdAt projectUuid 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 };
|
||||||
@@ -120,17 +113,17 @@ export const GetAllProjects = async (data: GetProjectsInterface) => {
|
|||||||
|
|
||||||
export const DeleteProject = async (data: ProjectInterface) => {
|
export const DeleteProject = async (data: ProjectInterface) => {
|
||||||
try {
|
try {
|
||||||
const { projectId, organization, userId, role } = data;
|
const { projectId, organization, userId } = data;
|
||||||
const ExistingUser = await existingUser(userId, organization);
|
const ExistingUser = await existingUser(userId, organization);
|
||||||
if (!ExistingUser) return { status: "User not found" };
|
if (!ExistingUser) return { status: "User not found" };
|
||||||
let filter = { _id: projectId, isArchive: false } as RoleFilter;
|
const existingProject = await projectModel(organization).findOne({
|
||||||
if (role === "User") {
|
_id: projectId,
|
||||||
filter.createdBy = userId;
|
createdBy: userId,
|
||||||
}
|
isArchive: false,
|
||||||
const existingProject = await projectModel(organization).findOne(filter);
|
});
|
||||||
if (!existingProject) return { status: "Project not found" };
|
if (!existingProject) return { status: "Project not found" };
|
||||||
const updateProject = await projectModel(organization).findOneAndUpdate(
|
const updateProject = await projectModel(organization).findOneAndUpdate(
|
||||||
filter,
|
{ _id: projectId, isArchive: false },
|
||||||
{ isArchive: true, DeletedAt: new Date() },
|
{ isArchive: true, DeletedAt: new Date() },
|
||||||
{ new: true }
|
{ new: true }
|
||||||
);
|
);
|
||||||
@@ -141,25 +134,24 @@ export const DeleteProject = async (data: ProjectInterface) => {
|
|||||||
};
|
};
|
||||||
export const updateProject = async (data: updateProjectInput) => {
|
export const updateProject = async (data: updateProjectInput) => {
|
||||||
try {
|
try {
|
||||||
const { projectId, organization, userId, projectName, thumbnail, role } =
|
const { projectId, organization, userId, projectName, thumbnail } = data;
|
||||||
data;
|
|
||||||
const ExistingUser = await existingUser(userId, organization);
|
const ExistingUser = await existingUser(userId, organization);
|
||||||
if (!ExistingUser) return { status: "User not found" };
|
if (!ExistingUser) return { status: "User not found" };
|
||||||
let filter = { _id: projectId, isArchive: false } as RoleFilter;
|
const existingProject = await projectModel(organization).findOne({
|
||||||
if (role === "User") {
|
_id: projectId,
|
||||||
filter.createdBy = userId;
|
createdBy: userId,
|
||||||
}
|
isArchive: false,
|
||||||
const existingProject = await projectModel(organization).findOne(filter);
|
});
|
||||||
if (!existingProject) return { status: "Project not found" };
|
if (!existingProject) return { status: "Project not found" };
|
||||||
if (projectName !== undefined) projectName;
|
if (projectName !== undefined) projectName;
|
||||||
if (thumbnail !== undefined) thumbnail;
|
if (thumbnail !== undefined) thumbnail;
|
||||||
const updateProject = await projectModel(organization)
|
const updateProject = await projectModel(organization)
|
||||||
.findOneAndUpdate(
|
.findOneAndUpdate(
|
||||||
filter,
|
{ _id: projectId, isArchive: false },
|
||||||
{ projectName: projectName, thumbnail: thumbnail },
|
{ projectName: projectName, thumbnail: thumbnail },
|
||||||
{ new: true }
|
{ new: true }
|
||||||
)
|
)
|
||||||
.select("_id projectName createdBy thumbnail createdAt");
|
.select("_id projectName createdBy thumbnail createdAt projectUuid");
|
||||||
if (updateProject) return { status: "Success", data: updateProject };
|
if (updateProject) return { status: "Success", data: updateProject };
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
return { status: error };
|
return { status: error };
|
||||||
@@ -168,23 +160,18 @@ export const updateProject = async (data: updateProjectInput) => {
|
|||||||
const maxLength: number = 6;
|
const maxLength: number = 6;
|
||||||
export const viewProject = async (data: ProjectInterface) => {
|
export const viewProject = async (data: ProjectInterface) => {
|
||||||
try {
|
try {
|
||||||
const { projectId, organization, userId, role } = data;
|
const { projectId, organization, userId } = data;
|
||||||
const userExisting = await existingUser(userId, organization);
|
const userExisting = await existingUser(userId, organization);
|
||||||
if (!userExisting) return { status: "User not found" };
|
if (!userExisting) return { status: "User not found" };
|
||||||
const RecentUserDoc = await UsersDataModel(organization).findOne({
|
const existingProject = await projectModel(organization).findOne({
|
||||||
userId: userId,
|
_id: projectId,
|
||||||
|
createdBy: userId,
|
||||||
isArchive: false,
|
isArchive: false,
|
||||||
});
|
});
|
||||||
let filter = { _id: projectId, isArchive: false } as RoleFilter;
|
|
||||||
if (role === "User") {
|
|
||||||
filter.createdBy = userId;
|
|
||||||
}
|
|
||||||
const existingProject = await projectModel(organization).findOne(filter);
|
|
||||||
if (!existingProject) return { status: "Project not found" };
|
if (!existingProject) return { status: "Project not found" };
|
||||||
const newArr = RecentUserDoc?.recentlyViewed || [];
|
const newArr = userExisting?.recentlyViewed || [];
|
||||||
if (RecentUserDoc?.recentlyViewed.length === 0) {
|
if (userExisting?.recentlyViewed.length === 0) {
|
||||||
newArr.push(projectId);
|
newArr.push(projectId);
|
||||||
await RecentUserDoc.save();
|
|
||||||
} else {
|
} else {
|
||||||
const index = newArr.indexOf(projectId);
|
const index = newArr.indexOf(projectId);
|
||||||
if (index !== -1) {
|
if (index !== -1) {
|
||||||
@@ -196,13 +183,21 @@ export const viewProject = async (data: ProjectInterface) => {
|
|||||||
newArr.pop();
|
newArr.pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await UsersDataModel(organization).updateOne(
|
await userModel(organization).updateOne(
|
||||||
{ _id: userId },
|
{ _id: userId },
|
||||||
{ recentlyViewed: newArr },
|
{ recentlyViewed: newArr },
|
||||||
{ new: true }
|
{ new: true }
|
||||||
);
|
);
|
||||||
const projectData = await projectModel(organization)
|
const projectData = await projectModel(organization)
|
||||||
.findOneAndUpdate(filter, { isViewed: Date.now() }, { new: true })
|
.findOneAndUpdate(
|
||||||
|
{
|
||||||
|
_id: projectId,
|
||||||
|
createdBy: userId,
|
||||||
|
isArchive: false,
|
||||||
|
},
|
||||||
|
{ isViewed: Date.now() },
|
||||||
|
{ new: true }
|
||||||
|
)
|
||||||
.select("_id projectName createdBy thumbnail createdAt");
|
.select("_id projectName createdBy thumbnail createdAt");
|
||||||
return { status: "Success", data: projectData };
|
return { status: "Success", data: projectData };
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
|
|||||||
@@ -1,27 +1,19 @@
|
|||||||
import projectModel from "../../model/project/project-model.ts";
|
import projectModel from "../../model/project/project-model.ts";
|
||||||
interface IOrg {
|
interface IOrg {
|
||||||
organization: string;
|
organization: string;
|
||||||
role: string;
|
|
||||||
userId: string;
|
|
||||||
}
|
}
|
||||||
interface IRestore {
|
interface IRestore {
|
||||||
projectId: string;
|
projectId: string;
|
||||||
organization: string;
|
organization: string;
|
||||||
role: string;
|
|
||||||
userId: string;
|
|
||||||
}
|
|
||||||
interface RoleFilter {
|
|
||||||
isArchive: boolean;
|
|
||||||
createdBy?: string;
|
|
||||||
}
|
}
|
||||||
export const TrashDatas = async (data: IOrg) => {
|
export const TrashDatas = async (data: IOrg) => {
|
||||||
try {
|
try {
|
||||||
const { organization, role, userId } = data;
|
const { organization } = data;
|
||||||
let filter = { isArchive: true, isDeleted: false } as RoleFilter;
|
|
||||||
if (role === "User") {
|
const TrashLists = await projectModel(organization).find({
|
||||||
filter.createdBy = userId;
|
isArchive: true,
|
||||||
}
|
isDeleted: false,
|
||||||
const TrashLists = await projectModel(organization).find(filter);
|
});
|
||||||
if (!TrashLists) return { staus: "Trash is Empty" };
|
if (!TrashLists) return { staus: "Trash is Empty" };
|
||||||
const TrashDocs: any[] = [];
|
const TrashDocs: any[] = [];
|
||||||
for (const Trash of TrashLists) {
|
for (const Trash of TrashLists) {
|
||||||
@@ -55,15 +47,14 @@ export const TrashDatas = async (data: IOrg) => {
|
|||||||
};
|
};
|
||||||
export const RestoreTrashData = async (data: IRestore) => {
|
export const RestoreTrashData = async (data: IRestore) => {
|
||||||
try {
|
try {
|
||||||
const { projectId, organization, role, userId } = data;
|
const { projectId, organization } = data;
|
||||||
let filter = { isArchive: true, _id: projectId } as RoleFilter;
|
const findProject = await projectModel(organization).findOne({
|
||||||
if (role === "User") {
|
_id: projectId,
|
||||||
filter.createdBy = userId;
|
isArchive: true,
|
||||||
}
|
});
|
||||||
const findProject = await projectModel(organization).findOne(filter);
|
|
||||||
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(
|
||||||
filter,
|
{ _id: projectId, isArchive: true },
|
||||||
{ isArchive: false, DeletedAt: null },
|
{ isArchive: false, DeletedAt: null },
|
||||||
{ new: true }
|
{ new: true }
|
||||||
);
|
);
|
||||||
|
|||||||
208
src/shared/services/v1Project/v1projectservice.ts
Normal file
208
src/shared/services/v1Project/v1projectservice.ts
Normal file
@@ -0,0 +1,208 @@
|
|||||||
|
import projectModel from "../../V1Models/Project/project-model.ts";
|
||||||
|
import versionModel from "../../V1Models/Version/versionModel.ts";
|
||||||
|
import {
|
||||||
|
existingProject,
|
||||||
|
existingUser,
|
||||||
|
archiveProject,
|
||||||
|
previousVersion,
|
||||||
|
generateUntitledProjectName,
|
||||||
|
} from "../helpers/v1projecthelperFns.ts";
|
||||||
|
import UsersDataModel from "../../V1Models/Auth/user.ts";
|
||||||
|
interface CreateProjectInput {
|
||||||
|
projectName: string;
|
||||||
|
projectUuid: string;
|
||||||
|
userId: string; // user ID
|
||||||
|
thumbnail?: string;
|
||||||
|
sharedUsers?: string[];
|
||||||
|
organization: string;
|
||||||
|
}
|
||||||
|
interface updateProjectInput {
|
||||||
|
projectName: string;
|
||||||
|
projectId: string;
|
||||||
|
userId: string; // user ID
|
||||||
|
thumbnail?: string;
|
||||||
|
sharedUsers?: string[];
|
||||||
|
organization: string;
|
||||||
|
role: string;
|
||||||
|
}
|
||||||
|
interface GetProjectsInterface {
|
||||||
|
userId: string;
|
||||||
|
organization: string;
|
||||||
|
role: string;
|
||||||
|
}
|
||||||
|
interface ProjectInterface {
|
||||||
|
projectId: string;
|
||||||
|
userId: string;
|
||||||
|
organization: string;
|
||||||
|
role: string;
|
||||||
|
}
|
||||||
|
interface RoleFilter {
|
||||||
|
isArchive: boolean;
|
||||||
|
createdBy?: string;
|
||||||
|
}
|
||||||
|
export const createProject = async (data: CreateProjectInput) => {
|
||||||
|
try {
|
||||||
|
const { thumbnail, sharedUsers, projectUuid, userId, organization } = data;
|
||||||
|
const userExisting = await existingUser(userId, organization);
|
||||||
|
if (!userExisting) {
|
||||||
|
return {
|
||||||
|
status: "user_not_found",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
const projectExisting = await existingProject(
|
||||||
|
projectUuid,
|
||||||
|
organization,
|
||||||
|
userId
|
||||||
|
);
|
||||||
|
|
||||||
|
if (projectExisting) {
|
||||||
|
return {
|
||||||
|
status: "project_exists",
|
||||||
|
project: projectExisting,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
const newProjectName = await generateUntitledProjectName(
|
||||||
|
organization,
|
||||||
|
userId
|
||||||
|
);
|
||||||
|
const project = await projectModel(organization).create({
|
||||||
|
projectName: newProjectName,
|
||||||
|
projectUuid: projectUuid,
|
||||||
|
createdBy: userId,
|
||||||
|
thumbnail: thumbnail,
|
||||||
|
sharedUsers: sharedUsers || [],
|
||||||
|
isArchive: false,
|
||||||
|
});
|
||||||
|
const versionData = await previousVersion(project._id, organization);
|
||||||
|
if (!versionData || versionData.length === 0) {
|
||||||
|
const newVersion = await versionModel(organization).create({
|
||||||
|
projectId: project._id,
|
||||||
|
createdBy: userId,
|
||||||
|
version: 0.0,
|
||||||
|
});
|
||||||
|
await projectModel(organization).findByIdAndUpdate(
|
||||||
|
{ _id: project._id, isArchive: false },
|
||||||
|
{ total_versions: `v-${newVersion.version.toFixed(2)}` }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
status: "Success",
|
||||||
|
project: project,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.log("error: ", error);
|
||||||
|
return {
|
||||||
|
status: error,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const GetAllProjects = async (data: GetProjectsInterface) => {
|
||||||
|
try {
|
||||||
|
const { userId, organization, role } = data;
|
||||||
|
await existingUser(userId, organization);
|
||||||
|
if (!existingUser) return { status: "User not found" };
|
||||||
|
let filter = { isArchive: false } as RoleFilter;
|
||||||
|
if (role === "User") {
|
||||||
|
filter.createdBy = userId;
|
||||||
|
}
|
||||||
|
const projectDatas = await projectModel(organization)
|
||||||
|
.find(filter)
|
||||||
|
.select("_id projectName createdBy thumbnail createdAt projectUuid");
|
||||||
|
if (projectDatas) return { status: "Success", Datas: projectDatas };
|
||||||
|
} catch (error: unknown) {
|
||||||
|
return { status: error };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const DeleteProject = async (data: ProjectInterface) => {
|
||||||
|
try {
|
||||||
|
const { projectId, organization, userId, role } = data;
|
||||||
|
const ExistingUser = await existingUser(userId, organization);
|
||||||
|
if (!ExistingUser) return { status: "User not found" };
|
||||||
|
let filter = { _id: projectId, isArchive: false } as RoleFilter;
|
||||||
|
if (role === "User") {
|
||||||
|
filter.createdBy = userId;
|
||||||
|
}
|
||||||
|
const existingProject = await projectModel(organization).findOne(filter);
|
||||||
|
if (!existingProject) return { status: "Project not found" };
|
||||||
|
const updateProject = await projectModel(organization).findOneAndUpdate(
|
||||||
|
filter,
|
||||||
|
{ isArchive: true, DeletedAt: new Date() },
|
||||||
|
{ new: true }
|
||||||
|
);
|
||||||
|
if (updateProject) return { status: "Success", project: updateProject };
|
||||||
|
} catch (error: unknown) {
|
||||||
|
return { status: error };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
export const updateProject = async (data: updateProjectInput) => {
|
||||||
|
try {
|
||||||
|
const { projectId, organization, userId, projectName, thumbnail, role } =
|
||||||
|
data;
|
||||||
|
const ExistingUser = await existingUser(userId, organization);
|
||||||
|
if (!ExistingUser) return { status: "User not found" };
|
||||||
|
let filter = { _id: projectId, isArchive: false } as RoleFilter;
|
||||||
|
if (role === "User") {
|
||||||
|
filter.createdBy = userId;
|
||||||
|
}
|
||||||
|
const existingProject = await projectModel(organization).findOne(filter);
|
||||||
|
if (!existingProject) return { status: "Project not found" };
|
||||||
|
if (projectName !== undefined) projectName;
|
||||||
|
if (thumbnail !== undefined) thumbnail;
|
||||||
|
const updateProject = await projectModel(organization)
|
||||||
|
.findOneAndUpdate(
|
||||||
|
filter,
|
||||||
|
{ 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, role } = data;
|
||||||
|
const userExisting = await existingUser(userId, organization);
|
||||||
|
if (!userExisting) return { status: "User not found" };
|
||||||
|
const RecentUserDoc = await UsersDataModel(organization).findOne({
|
||||||
|
userId: userId,
|
||||||
|
isArchive: false,
|
||||||
|
});
|
||||||
|
let filter = { _id: projectId, isArchive: false } as RoleFilter;
|
||||||
|
if (role === "User") {
|
||||||
|
filter.createdBy = userId;
|
||||||
|
}
|
||||||
|
const existingProject = await projectModel(organization).findOne(filter);
|
||||||
|
if (!existingProject) return { status: "Project not found" };
|
||||||
|
const newArr = RecentUserDoc?.recentlyViewed || [];
|
||||||
|
if (RecentUserDoc?.recentlyViewed.length === 0) {
|
||||||
|
newArr.push(projectId);
|
||||||
|
await RecentUserDoc.save();
|
||||||
|
} else {
|
||||||
|
const index = newArr.indexOf(projectId);
|
||||||
|
if (index !== -1) {
|
||||||
|
newArr.splice(index, 1);
|
||||||
|
}
|
||||||
|
newArr.unshift(projectId);
|
||||||
|
|
||||||
|
if (newArr.length > maxLength) {
|
||||||
|
newArr.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await UsersDataModel(organization).updateOne(
|
||||||
|
{ _id: userId },
|
||||||
|
{ recentlyViewed: newArr },
|
||||||
|
{ new: true }
|
||||||
|
);
|
||||||
|
const projectData = await projectModel(organization)
|
||||||
|
.findOneAndUpdate(filter, { isViewed: Date.now() }, { new: true })
|
||||||
|
.select("_id projectName createdBy thumbnail createdAt");
|
||||||
|
return { status: "Success", data: projectData };
|
||||||
|
} catch (error: unknown) {
|
||||||
|
return { status: error };
|
||||||
|
}
|
||||||
|
};
|
||||||
92
src/shared/services/v1home/v1homeservice.ts
Normal file
92
src/shared/services/v1home/v1homeservice.ts
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
import projectModel from "../../V1Models/Project/project-model.ts";
|
||||||
|
import userModel from "../../model/user-Model.ts";
|
||||||
|
import UsersDataModel from "../../V1Models/Auth/user.ts";
|
||||||
|
import { existingUser } from "../helpers/ProjecthelperFn.ts";
|
||||||
|
|
||||||
|
interface IRecentData {
|
||||||
|
organization: string;
|
||||||
|
userId: string;
|
||||||
|
role: string;
|
||||||
|
}
|
||||||
|
interface IProject {
|
||||||
|
_id: string;
|
||||||
|
projectName: string;
|
||||||
|
createdBy: string;
|
||||||
|
thumbnail?: string;
|
||||||
|
createdAt: Date;
|
||||||
|
isViewed?: number;
|
||||||
|
}
|
||||||
|
interface searchProjectInterface {
|
||||||
|
searchName: string;
|
||||||
|
userId: string;
|
||||||
|
organization: string;
|
||||||
|
}
|
||||||
|
interface RoleFilter {
|
||||||
|
isArchive: boolean;
|
||||||
|
createdBy?: string;
|
||||||
|
}
|
||||||
|
export const RecentlyAdded = async (data: IRecentData) => {
|
||||||
|
try {
|
||||||
|
const { userId, organization, role } = data;
|
||||||
|
const userExisting = await existingUser(userId, organization);
|
||||||
|
if (!userExisting) return { status: "User not found" };
|
||||||
|
const userRecentData = await UsersDataModel(organization)
|
||||||
|
.findOne({ userId: userId, isArchive: false })
|
||||||
|
.populate({
|
||||||
|
path: "recentlyViewed",
|
||||||
|
model: projectModel(organization),
|
||||||
|
select: "_id",
|
||||||
|
});
|
||||||
|
let filter = { isArchive: false } as RoleFilter;
|
||||||
|
if (role === "User") {
|
||||||
|
filter.createdBy = userId;
|
||||||
|
}
|
||||||
|
const populatedProjects = userRecentData.recentlyViewed as IProject[];
|
||||||
|
const RecentDatas = await Promise.all(
|
||||||
|
populatedProjects.map(async (project) => {
|
||||||
|
const projectExisting = await projectModel(organization)
|
||||||
|
.findOne(filter)
|
||||||
|
.select("_id projectName createdBy thumbnail createdAt isViewed");
|
||||||
|
return projectExisting;
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
const filteredProjects = RecentDatas.filter(Boolean);
|
||||||
|
return { status: "Success", data: filteredProjects };
|
||||||
|
} catch (error) {
|
||||||
|
return { status: error };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
export const searchProject = async (data: searchProjectInterface) => {
|
||||||
|
try {
|
||||||
|
const { userId, organization, searchName } = data;
|
||||||
|
const userExisting = await existingUser(userId, organization);
|
||||||
|
if (!userExisting) return { status: "User not found" };
|
||||||
|
const findprojectName = await projectModel(organization).find({
|
||||||
|
projectName: { $regex: `${searchName}`, $options: "i" }, // 'i' makes it case-insensitive
|
||||||
|
isArchive: false,
|
||||||
|
});
|
||||||
|
if (!findprojectName || findprojectName.length === 0)
|
||||||
|
return { status: "Project not found" };
|
||||||
|
return { status: "Success", data: findprojectName };
|
||||||
|
} catch (error: unknown) {
|
||||||
|
return { status: error };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
export const searchTrashProject = async (data: searchProjectInterface) => {
|
||||||
|
try {
|
||||||
|
const { userId, organization, searchName } = data;
|
||||||
|
const userExisting = await existingUser(userId, organization);
|
||||||
|
if (!userExisting) return { status: "User not found" };
|
||||||
|
const findprojectName = await projectModel(organization).find({
|
||||||
|
projectName: { $regex: `${searchName}`, $options: "i" },
|
||||||
|
isArchive: true,
|
||||||
|
isDeleted: false,
|
||||||
|
});
|
||||||
|
if (!findprojectName || findprojectName.length === 0)
|
||||||
|
return { status: "Project not found" };
|
||||||
|
return { status: "Success", data: findprojectName };
|
||||||
|
} catch (error: unknown) {
|
||||||
|
return { status: error };
|
||||||
|
}
|
||||||
|
};
|
||||||
75
src/shared/services/v1trash/v1trashservice.ts
Normal file
75
src/shared/services/v1trash/v1trashservice.ts
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
import projectModel from "../../V1Models/Project/project-model.ts";
|
||||||
|
interface IOrg {
|
||||||
|
organization: string;
|
||||||
|
role: string;
|
||||||
|
userId: string;
|
||||||
|
}
|
||||||
|
interface IRestore {
|
||||||
|
projectId: string;
|
||||||
|
organization: string;
|
||||||
|
role: string;
|
||||||
|
userId: string;
|
||||||
|
}
|
||||||
|
interface RoleFilter {
|
||||||
|
isArchive: boolean;
|
||||||
|
createdBy?: string;
|
||||||
|
}
|
||||||
|
export const TrashDatas = async (data: IOrg) => {
|
||||||
|
try {
|
||||||
|
const { organization, role, userId } = data;
|
||||||
|
let filter = { isArchive: true, isDeleted: false } as RoleFilter;
|
||||||
|
if (role === "User") {
|
||||||
|
filter.createdBy = userId;
|
||||||
|
}
|
||||||
|
const TrashLists = await projectModel(organization).find(filter);
|
||||||
|
if (!TrashLists) return { staus: "Trash is Empty" };
|
||||||
|
const TrashDocs: any[] = [];
|
||||||
|
for (const Trash of TrashLists) {
|
||||||
|
const now = new Date();
|
||||||
|
const deletedPlus15 = new Date(
|
||||||
|
Trash.DeletedAt.getTime() + 15 * 24 * 60 * 60 * 1000
|
||||||
|
);
|
||||||
|
if (now > deletedPlus15) {
|
||||||
|
console.log("now > deletedPlus15: ", now > deletedPlus15);
|
||||||
|
await projectModel(organization).updateOne(
|
||||||
|
{ _id: Trash._id },
|
||||||
|
{ $set: { isDeleted: true } }
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
TrashDocs.push(Trash);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const ListDatas = TrashDocs.map((data) => {
|
||||||
|
return {
|
||||||
|
projectName: data.projectName,
|
||||||
|
thumbnail: data.thumbnail,
|
||||||
|
createdBy: data.createdBy,
|
||||||
|
_id: data._id,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
return { status: "Success", ListDatas };
|
||||||
|
} catch (error) {
|
||||||
|
return { status: error };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
export const RestoreTrashData = async (data: IRestore) => {
|
||||||
|
try {
|
||||||
|
const { projectId, organization, role, userId } = data;
|
||||||
|
let filter = { isArchive: true, _id: projectId } as RoleFilter;
|
||||||
|
if (role === "User") {
|
||||||
|
filter.createdBy = userId;
|
||||||
|
}
|
||||||
|
const findProject = await projectModel(organization).findOne(filter);
|
||||||
|
if (!findProject) return { status: "Project not found" };
|
||||||
|
const restoreData = await projectModel(organization).findOneAndUpdate(
|
||||||
|
filter,
|
||||||
|
{ isArchive: false, DeletedAt: null },
|
||||||
|
{ new: true }
|
||||||
|
);
|
||||||
|
if (!restoreData) return { status: "Project Restore unsuccessfull" };
|
||||||
|
return { status: "Project Restored successfully" };
|
||||||
|
} catch (error) {
|
||||||
|
return { status: error };
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -59,10 +59,10 @@ class VersionService {
|
|||||||
// Get all assets from previous version
|
// Get all assets from previous version
|
||||||
const previousVersion = parseFloat((newVersion.version - 0.1).toFixed(1));
|
const previousVersion = parseFloat((newVersion.version - 0.1).toFixed(1));
|
||||||
const previousVersionDoc = await versionModel(db).findOne({
|
const previousVersionDoc = await versionModel(db).findOne({
|
||||||
projectId,
|
projectId,
|
||||||
version: previousVersion,
|
version: previousVersion,
|
||||||
});
|
});
|
||||||
console.log('previousVersionDoc: ', previousVersionDoc);
|
console.log("previousVersionDoc: ", previousVersionDoc);
|
||||||
|
|
||||||
let previousAssets = [];
|
let previousAssets = [];
|
||||||
if (previousVersionDoc) {
|
if (previousVersionDoc) {
|
||||||
@@ -75,8 +75,8 @@ class VersionService {
|
|||||||
|
|
||||||
// Copy assets to new version
|
// Copy assets to new version
|
||||||
const newAssets = await Promise.all(
|
const newAssets = await Promise.all(
|
||||||
previousAssets.map(async (asset) => {
|
previousAssets.map(async (asset) => {
|
||||||
console.log('previousAssets: ', previousAssets);
|
console.log("previousAssets: ", previousAssets);
|
||||||
const newAsset = { ...asset.toObject(), versionId: newVersion._id };
|
const newAsset = { ...asset.toObject(), versionId: newVersion._id };
|
||||||
delete newAsset._id;
|
delete newAsset._id;
|
||||||
return await assetModel(db).create(newAsset);
|
return await assetModel(db).create(newAsset);
|
||||||
|
|||||||
Reference in New Issue
Block a user