Role nd token Based Routing completed for Project,trash,home which is in Controller. Token, Auth Purpose,Rolebased middlewares created. Auth API,Project token Based API, Home Token Based API, Trash Token Based API In v1 AuthRoutes

This commit is contained in:
2025-05-19 16:06:09 +05:30
parent 2aa8c479fa
commit ac8de5d33d
28 changed files with 1748 additions and 224 deletions

View File

@@ -1,19 +1,20 @@
import { Request, Response } from "express";
import { RecentlyAdded, searchProject, searchTrashProject } from "../../../shared/services/home/homeService.ts";
import { AuthenticatedRequest } from "../../../shared/utils/token.ts";
export const recentDataController = async (
req: Request,
req: AuthenticatedRequest,
res: Response
): Promise<void> => {
try {
const { userId, organization } = req.params;
if (!userId || !organization) {
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 });
const result = await RecentlyAdded({ userId, organization,role });
switch (result.status) {
case "User not found":

View File

@@ -6,20 +6,27 @@ import {
updateProject,
viewProject,
} from "../../../shared/services/project/project-Services.ts";
import { AuthenticatedRequest } from "../../../shared/utils/token.ts";
export const createProjectController = async (
req: Request,
req: AuthenticatedRequest,
res: Response
): Promise<void> => {
try {
const { projectUuid, userId, thumbnail, organization } = req.body;
if (!projectUuid || !userId || !thumbnail || !organization) {
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);
const result = await createProject({ ...req.body, userId, organization });
switch (result.status) {
case "project_exists":
@@ -54,18 +61,19 @@ export const createProjectController = async (
}
};
export const GetProjects = async (
req: Request,
req: AuthenticatedRequest,
res: Response
): Promise<void> => {
try {
const { userId, organization } = req.params;
if (!userId || !organization) {
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 });
const result = await GetAllProjects({ userId, organization, role });
switch (result?.status) {
case "User not found":
res.status(404).json({
@@ -92,19 +100,34 @@ export const GetProjects = async (
}
};
export const RemoveProject = async (
req: Request,
req: AuthenticatedRequest,
res: Response
): Promise<void> => {
try {
const { projectId } = req.params;
const { organization, userId } = req.body;
if (!projectId || !organization || !userId) {
// 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 });
const result = await DeleteProject({
projectId,
organization,
userId,
role,
});
switch (result?.status) {
case "Project not found":
res.status(404).json({
@@ -135,13 +158,13 @@ export const RemoveProject = async (
}
};
export const updateProjectController = async (
req: Request,
req: AuthenticatedRequest,
res: Response
): Promise<void> => {
try {
const { projectId, organization, projectName, thumbnail, userId } =
req.body;
if (!userId || !organization || !projectId) {
const { userId, organization, role } = req.user || {};
const { projectId, projectName, thumbnail } = req.body;
if (!userId || !organization || !projectId || !role) {
res.status(400).json({
message: "All fields are required",
});
@@ -153,6 +176,7 @@ export const updateProjectController = async (
userId,
projectName,
thumbnail,
role,
});
switch (result?.status) {
case "Project not found":
@@ -184,14 +208,25 @@ export const updateProjectController = async (
return;
}
};
export const ViewData = async (req: Request, res: Response): Promise<void> => {
export const ViewData = async (
req: AuthenticatedRequest,
res: Response
): Promise<void> => {
try {
const { projectId, organization, userId } = req.query as {
organization: string;
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.query as {
projectId: string;
userId: string;
};
if (!userId || !organization || !projectId) {
if (!userId || !organization || !projectId || !role) {
res.status(400).json({
message: "All fields are required",
});
@@ -201,6 +236,7 @@ export const ViewData = async (req: Request, res: Response): Promise<void> => {
projectId,
organization,
userId,
role,
});
switch (result?.status) {
case "Project not found":

View File

@@ -3,20 +3,21 @@ import {
TrashDatas,
RestoreTrashData,
} from "../../../shared/services/trash/trashService.ts";
import { AuthenticatedRequest } from "../../../shared/utils/token.ts";
export const GetTrashList = async (
req: Request,
req: AuthenticatedRequest,
res: Response
): Promise<void> => {
try {
const { organization } = req.query as { organization: string };
if (!organization) {
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 });
const result = await TrashDatas({ organization, role, userId });
switch (result.status) {
case "Trash is Empty":
@@ -28,7 +29,6 @@ export const GetTrashList = async (
case "Success":
res.status(200).json({
// message: "Project created Successfully",
TrashDatas: result.ListDatas,
});
break;
@@ -47,22 +47,26 @@ export const GetTrashList = async (
};
export const RestoreTrash = async (
req: Request,
req: AuthenticatedRequest,
res: Response
): Promise<void> => {
try {
const { organization, projectId } = req.query as {
organization: string;
const { organization, role, userId } = req.user || {};
const { projectId } = req.query as {
projectId: string;
};
console.log("organization: ", organization);
if (!organization || !projectId) {
if (!organization || !projectId || !role || !userId) {
res.status(400).json({
message: "All fields are required",
});
return;
}
const result = await RestoreTrashData({ organization, projectId });
const result = await RestoreTrashData({
organization,
projectId,
role,
userId,
});
switch (result.status) {
case "Project not found":

View File

@@ -1,6 +1,6 @@
import { Request, Response } from "express";
import userModel from "../../shared/model/user-Model.ts";
import {hashGenerate,hashValidator} from "../../shared/security/Hasing.ts"
import {hashGenerate,hashValidator} from "../../shared/utils/Hasing.ts"
let serverAlive = true;
export class User {