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,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;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user