132 lines
3.0 KiB
TypeScript
132 lines
3.0 KiB
TypeScript
|
|
import { Response } from "express";
|
||
|
|
|
||
|
|
import { AuthenticatedRequest } from "../../../../shared/utils/token.ts";
|
||
|
|
import {
|
||
|
|
getEnvironment,
|
||
|
|
setEnvironment,
|
||
|
|
} from "../../../../shared/services/builder/EnvironmentService.ts";
|
||
|
|
|
||
|
|
export const SetEnvironmentController = async (
|
||
|
|
req: AuthenticatedRequest,
|
||
|
|
res: Response
|
||
|
|
): Promise<void> => {
|
||
|
|
try {
|
||
|
|
const { organization, userId } = req.user || {};
|
||
|
|
const {
|
||
|
|
roofVisibility,
|
||
|
|
renderDistance,
|
||
|
|
limitDistance,
|
||
|
|
wallVisibility,
|
||
|
|
shadowVisibility,
|
||
|
|
projectId,
|
||
|
|
} = req.body;
|
||
|
|
if (
|
||
|
|
!organization ||
|
||
|
|
!userId ||
|
||
|
|
!roofVisibility ||
|
||
|
|
!wallVisibility ||
|
||
|
|
!renderDistance ||
|
||
|
|
!limitDistance ||
|
||
|
|
!shadowVisibility ||
|
||
|
|
!projectId
|
||
|
|
) {
|
||
|
|
res.status(400).json({
|
||
|
|
message: "All fields are required",
|
||
|
|
});
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
const data = {
|
||
|
|
roofVisibility,
|
||
|
|
wallVisibility,
|
||
|
|
shadowVisibility,
|
||
|
|
organization,
|
||
|
|
projectId,
|
||
|
|
userId,
|
||
|
|
renderDistance,
|
||
|
|
limitDistance,
|
||
|
|
};
|
||
|
|
const result = await setEnvironment(data);
|
||
|
|
|
||
|
|
switch (result.status) {
|
||
|
|
case "User not found":
|
||
|
|
res.status(404).json({
|
||
|
|
message: "User not found",
|
||
|
|
});
|
||
|
|
break;
|
||
|
|
case "Project not found":
|
||
|
|
res.status(404).json({
|
||
|
|
message: "Project not found",
|
||
|
|
});
|
||
|
|
break;
|
||
|
|
case "environments updated":
|
||
|
|
res.status(200).json(result.data);
|
||
|
|
break;
|
||
|
|
case "Success":
|
||
|
|
res.status(201).json(result.data);
|
||
|
|
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 GetEnvironmentController = async (
|
||
|
|
req: AuthenticatedRequest,
|
||
|
|
res: Response
|
||
|
|
): Promise<void> => {
|
||
|
|
try {
|
||
|
|
const { organization, userId } = req.user || {};
|
||
|
|
const { projectId } = req.params;
|
||
|
|
if (!organization || !userId || !projectId) {
|
||
|
|
res.status(400).json({
|
||
|
|
message: "All fields are required",
|
||
|
|
});
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
const data = {
|
||
|
|
organization,
|
||
|
|
projectId,
|
||
|
|
userId,
|
||
|
|
};
|
||
|
|
const result = await getEnvironment(data);
|
||
|
|
|
||
|
|
switch (result.status) {
|
||
|
|
case "User not found":
|
||
|
|
res.status(404).json({
|
||
|
|
message: "User not found",
|
||
|
|
});
|
||
|
|
break;
|
||
|
|
case "Project not found":
|
||
|
|
res.status(404).json({
|
||
|
|
message: "Project not found",
|
||
|
|
});
|
||
|
|
break;
|
||
|
|
case "Environment Not found for the User":
|
||
|
|
res.status(404).json({
|
||
|
|
message: "Environment Not found for the User",
|
||
|
|
});
|
||
|
|
break;
|
||
|
|
case "Success":
|
||
|
|
res.status(200).json(result.data);
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
res.status(500).json({
|
||
|
|
message: "Internal server error",
|
||
|
|
});
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
} catch (error) {
|
||
|
|
console.log("error: ", error);
|
||
|
|
res.status(500).json({
|
||
|
|
message: "Unknown error",
|
||
|
|
});
|
||
|
|
}
|
||
|
|
};
|