312 lines
7.3 KiB
TypeScript
312 lines
7.3 KiB
TypeScript
import { Response } from "express";
|
|
import {
|
|
createProject,
|
|
DeleteProject,
|
|
DuplicateProject,
|
|
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 || {};
|
|
const { projectUuid, thumbnail } = req.body;
|
|
|
|
if (!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 } = req.user || {};
|
|
if (!userId || !organization) {
|
|
res.status(400).json({
|
|
message: "All fields are required",
|
|
});
|
|
return;
|
|
}
|
|
const result = await GetAllProjects({ userId, organization });
|
|
switch (result?.status) {
|
|
case "User not found":
|
|
res.status(404).json({
|
|
message: "User not found",
|
|
});
|
|
break;
|
|
|
|
case "Success":
|
|
res.status(200).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.user || {};
|
|
if (!req.user?.userId || !req.user?.organization) {
|
|
res.status(401).json({ message: "Unauthorized" });
|
|
return;
|
|
}
|
|
if (!projectId || !organization || !userId) {
|
|
res.status(400).json({
|
|
message: "All fields are required",
|
|
});
|
|
return;
|
|
}
|
|
const result = await DeleteProject({
|
|
projectId,
|
|
organization,
|
|
userId,
|
|
});
|
|
switch (result?.status) {
|
|
case "Project not found":
|
|
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) {
|
|
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: AuthenticatedRequest,
|
|
res: Response
|
|
): Promise<void> => {
|
|
try {
|
|
const { organization, userId, role } = req.user || {};
|
|
if (!req.user?.userId || !req.user?.organization) {
|
|
res.status(401).json({ message: "Unauthorized" });
|
|
return;
|
|
}
|
|
const { projectId } = req.params as {
|
|
projectId: 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;
|
|
}
|
|
};
|
|
export const ProjectDuplicateController = async (
|
|
req: AuthenticatedRequest,
|
|
res: Response
|
|
): Promise<void> => {
|
|
try {
|
|
const { userId, organization } = req.user || {};
|
|
const { projectUuid, thumbnail, projectName } = req.body;
|
|
if (!req.user?.userId || !req.user?.organization) {
|
|
res.status(401).json({ message: "Unauthorized" });
|
|
return;
|
|
}
|
|
if (!projectUuid || !thumbnail || !projectName) {
|
|
res.status(400).json({
|
|
message: "All fields are required",
|
|
});
|
|
return;
|
|
}
|
|
const result = await DuplicateProject({
|
|
...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 Duplicated 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;
|
|
}
|
|
};
|