try to solve swagger issue
This commit is contained in:
parent
1f57c95a4f
commit
d68db66cc4
2 changed files with 274 additions and 188 deletions
|
|
@ -6,156 +6,225 @@ import { createBucket } from "../../helper/upload/createBucket";
|
||||||
import { removeBucket } from "../../helper/upload/removeBucket";
|
import { removeBucket } from "../../helper/upload/removeBucket";
|
||||||
|
|
||||||
export const getAllProjects = async (userId: string, token: string) => {
|
export const getAllProjects = async (userId: string, token: string) => {
|
||||||
try {
|
try {
|
||||||
// Fetch all projects for the given user
|
// Fetch all projects for the given user
|
||||||
const allProjects = await db.select({
|
const allProjects = await db
|
||||||
id: projects.id,
|
.select({
|
||||||
name: projects.name,
|
id: projects.id,
|
||||||
description: projects.description,
|
name: projects.name,
|
||||||
preview_url: projects.preview_url,
|
description: projects.description,
|
||||||
object: projects.object,
|
preview_url: projects.preview_url,
|
||||||
}).from(projects).where(eq(projects.userId, userId));
|
object: projects.object,
|
||||||
|
})
|
||||||
|
.from(projects)
|
||||||
|
.where(eq(projects.userId, userId));
|
||||||
|
|
||||||
// Identify projects where 'object' is empty or 'object.objects' is empty
|
// Identify projects where 'object' is empty or 'object.objects' is empty
|
||||||
const projectsToDelete = allProjects.filter(proj =>
|
const projectsToDelete = allProjects.filter(
|
||||||
(proj.object && typeof proj.object === "object" && Object.keys(proj.object).length === 0) ||
|
(proj) =>
|
||||||
(proj.object?.objects && Array.isArray(proj.object.objects) && proj.object.objects.length === 0)
|
(proj.object &&
|
||||||
);
|
typeof proj.object === "object" &&
|
||||||
|
Object.keys(proj.object).length === 0) ||
|
||||||
|
(proj.object?.objects &&
|
||||||
|
Array.isArray(proj.object.objects) &&
|
||||||
|
proj.object.objects.length === 0)
|
||||||
|
);
|
||||||
|
|
||||||
// Delete projects with empty 'object' or empty 'object.objects'
|
// Delete projects with empty 'object' or empty 'object.objects'
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
projectsToDelete.map(async (proj) => {
|
projectsToDelete.map(async (proj) => {
|
||||||
// Step 1: Delete associated uploads first
|
// Step 1: Delete associated uploads first
|
||||||
await db.delete(uploads).where(eq(uploads.projectId, proj.id));
|
await db.delete(uploads).where(eq(uploads.projectId, proj.id));
|
||||||
|
|
||||||
// Step 2: Delete the project itself
|
// Step 2: Delete the project itself
|
||||||
await db.delete(projects).where(eq(projects.id, proj.id));
|
await db.delete(projects).where(eq(projects.id, proj.id));
|
||||||
|
|
||||||
// Step 3: Delete the associated bucket
|
// Step 3: Delete the associated bucket
|
||||||
await removeBucket(proj.id);
|
await removeBucket(proj.id);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
// Get remaining projects
|
// Get remaining projects
|
||||||
const remainingProjects = allProjects.filter(proj =>
|
const remainingProjects = allProjects.filter(
|
||||||
!(
|
(proj) =>
|
||||||
(proj.object && typeof proj.object === "object" && Object.keys(proj.object).length === 0) ||
|
!(
|
||||||
(proj.object?.objects && Array.isArray(proj.object.objects) && proj.object.objects.length === 0)
|
(proj.object &&
|
||||||
)
|
typeof proj.object === "object" &&
|
||||||
);
|
Object.keys(proj.object).length === 0) ||
|
||||||
|
(proj.object?.objects &&
|
||||||
|
Array.isArray(proj.object.objects) &&
|
||||||
|
proj.object.objects.length === 0)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
if (remainingProjects.length === 0) {
|
if (remainingProjects.length === 0) {
|
||||||
return { status: 404, message: "No projects found", token };
|
return { status: 404, message: "No projects found", token };
|
||||||
}
|
|
||||||
|
|
||||||
return { status: 200, message: "Projects fetched successfully", data: remainingProjects, token };
|
|
||||||
} catch (error: any) {
|
|
||||||
console.log(error.message);
|
|
||||||
return { status: 500, message: "An error occurred while fetching projects", token };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
status: 200,
|
||||||
|
message: "Projects fetched successfully",
|
||||||
|
data: remainingProjects,
|
||||||
|
token,
|
||||||
|
};
|
||||||
|
} catch (error: any) {
|
||||||
|
console.log(error.message);
|
||||||
|
return {
|
||||||
|
status: 500,
|
||||||
|
message: "An error occurred while fetching projects",
|
||||||
|
token,
|
||||||
|
};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getEachProjects = async (id: string, token: string) => {
|
export const getEachProjects = async (id: string, token: string) => {
|
||||||
try {
|
try {
|
||||||
const project = await db.select({
|
const project = await db
|
||||||
id: projects.id,
|
.select({
|
||||||
name: projects.name,
|
id: projects.id,
|
||||||
description: projects.description,
|
name: projects.name,
|
||||||
preview_url: projects.preview_url,
|
description: projects.description,
|
||||||
object: projects.object,
|
preview_url: projects.preview_url,
|
||||||
}).from(projects).where(eq(projects.id, id)).limit(1);
|
object: projects.object,
|
||||||
if (project.length === 0) {
|
})
|
||||||
return { status: 404, message: "Project not found", token };
|
.from(projects)
|
||||||
}
|
.where(eq(projects.id, id))
|
||||||
return { status: 200, message: "Project fetched successfully", data: project[0], token };
|
.limit(1);
|
||||||
} catch (error: any) {
|
if (project.length === 0) {
|
||||||
console.log(error.message);
|
return { status: 404, message: "Project not found", token };
|
||||||
return { status: 500, message: "An error occurred while fetching projects", token };
|
|
||||||
}
|
}
|
||||||
|
return {
|
||||||
|
status: 200,
|
||||||
|
message: "Project fetched successfully",
|
||||||
|
data: project[0],
|
||||||
|
token,
|
||||||
|
};
|
||||||
|
} catch (error: any) {
|
||||||
|
console.log(error.message);
|
||||||
|
return {
|
||||||
|
status: 500,
|
||||||
|
message: "An error occurred while fetching projects",
|
||||||
|
token,
|
||||||
|
};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const createProject = async (userId: string, token: string) => {
|
export const createProject = async (userId: string, token: string) => {
|
||||||
try {
|
try {
|
||||||
const { id } = await createEmptyProject(userId);
|
const { id } = await createEmptyProject(userId);
|
||||||
const bucket = await createBucket(id);
|
const bucket = await createBucket(id);
|
||||||
return { status: 200, message: "New project created successfully", data: { id, bucketName: bucket }, token };
|
return {
|
||||||
|
status: 200,
|
||||||
} catch (error: any) {
|
message: "New project created successfully",
|
||||||
console.log(error.message);
|
data: { id, bucketName: bucket },
|
||||||
return { status: 500, message: "An error occurred while creating projects", token }
|
token,
|
||||||
}
|
};
|
||||||
|
} catch (error: any) {
|
||||||
|
console.log(error.message);
|
||||||
|
return {
|
||||||
|
status: 500,
|
||||||
|
message: "An error occurred while creating projects",
|
||||||
|
token,
|
||||||
|
};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const updateProject = async (id: string, body: any, token: string, user_id: string) => {
|
export const updateProject = async (
|
||||||
try {
|
id: string,
|
||||||
// 1. Validate if project exists
|
body: any,
|
||||||
const existingProject = await db.select().from(projects).where(eq(projects.id, id));
|
token: string,
|
||||||
if (existingProject.length === 0) {
|
user_id: string
|
||||||
return { status: 404, message: "Project not found", token };
|
) => {
|
||||||
}
|
try {
|
||||||
|
// 1. Validate if project exists
|
||||||
const { object, name, description, preview_url } = body;
|
const existingProject = await db
|
||||||
// The preview_url will come from client-side as well, where before updating the project a project capture will be taken and uploaded to the bucket. than the url will be sent to the server.And rest of them are normal process
|
.select()
|
||||||
|
.from(projects)
|
||||||
const updatedProject = await db.update(projects).set({
|
.where(eq(projects.id, id));
|
||||||
object,
|
if (existingProject.length === 0) {
|
||||||
name,
|
return { status: 404, message: "Project not found", token };
|
||||||
description,
|
|
||||||
preview_url,
|
|
||||||
userId: user_id,
|
|
||||||
}).where(eq(projects.id, id)).returning({
|
|
||||||
id: projects.id,
|
|
||||||
object: projects.object,
|
|
||||||
name: projects.name,
|
|
||||||
description: projects.description,
|
|
||||||
preview_url: projects.preview_url
|
|
||||||
});
|
|
||||||
|
|
||||||
if (updatedProject.length === 0) {
|
|
||||||
return { status: 500, message: "Failed to update the project", token };
|
|
||||||
}
|
|
||||||
return { status: 200, message: "Project updated successfully", data: updatedProject[0], token };
|
|
||||||
} catch (error: any) {
|
|
||||||
console.log("Error updating project:", error.message || error.toString());
|
|
||||||
return { status: 500, message: "An error occurred while updating the project", token };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { object, name, description, preview_url } = body;
|
||||||
|
// The preview_url will come from client-side as well, where before updating the project a project capture will be taken and uploaded to the bucket. than the url will be sent to the server.And rest of them are normal process
|
||||||
|
|
||||||
|
const updatedProject = await db
|
||||||
|
.update(projects)
|
||||||
|
.set({
|
||||||
|
object,
|
||||||
|
name,
|
||||||
|
description,
|
||||||
|
preview_url,
|
||||||
|
userId: user_id,
|
||||||
|
})
|
||||||
|
.where(eq(projects.id, id))
|
||||||
|
.returning({
|
||||||
|
id: projects.id,
|
||||||
|
object: projects.object,
|
||||||
|
name: projects.name,
|
||||||
|
description: projects.description,
|
||||||
|
preview_url: projects.preview_url,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (updatedProject.length === 0) {
|
||||||
|
return { status: 500, message: "Failed to update the project", token };
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
status: 200,
|
||||||
|
message: "Project updated successfully",
|
||||||
|
data: updatedProject[0],
|
||||||
|
token,
|
||||||
|
};
|
||||||
|
} catch (error: any) {
|
||||||
|
console.log("Error updating project:", error.message || error.toString());
|
||||||
|
return {
|
||||||
|
status: 500,
|
||||||
|
message: "An error occurred while updating the project",
|
||||||
|
token,
|
||||||
|
};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const deleteProject = async (id: string, token: string) => {
|
export const deleteProject = async (id: string, token: string) => {
|
||||||
try {
|
try {
|
||||||
const deletedUploads = await db
|
const deletedUploads = await db
|
||||||
.delete(uploads)
|
.delete(uploads)
|
||||||
.where(eq(uploads.projectId, id))
|
.where(eq(uploads.projectId, id))
|
||||||
.returning({ id: uploads.id });
|
.returning({ id: uploads.id });
|
||||||
|
|
||||||
if (deletedUploads.length >= 0) {
|
if (deletedUploads.length >= 0) {
|
||||||
// Step 4: Delete the project
|
// Step 4: Delete the project
|
||||||
const deletedProject = await db
|
const deletedProject = await db
|
||||||
.delete(projects)
|
.delete(projects)
|
||||||
.where(eq(projects.id, id))
|
.where(eq(projects.id, id))
|
||||||
.returning({ id: projects.id });
|
.returning({ id: projects.id });
|
||||||
|
|
||||||
if (deletedProject.length === 0) {
|
if (deletedProject.length === 0) {
|
||||||
return { status: 404, message: "Project not found", token };
|
return { status: 404, message: "Project not found", token };
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 5: Delete the associated bucket
|
// Step 5: Delete the associated bucket
|
||||||
const bucketDeletionResult = await removeBucket(id);
|
const bucketDeletionResult = await removeBucket(id);
|
||||||
|
|
||||||
if (bucketDeletionResult.status !== 200) {
|
if (bucketDeletionResult.status !== 200) {
|
||||||
return {
|
return {
|
||||||
status: bucketDeletionResult.status,
|
status: bucketDeletionResult.status,
|
||||||
message: `Error deleting bucket: ${bucketDeletionResult.message}`,
|
message: `Error deleting bucket: ${bucketDeletionResult.message}`,
|
||||||
token
|
token,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return { status: 200, message: "Project and associated bucket deleted successfully", token };
|
return {
|
||||||
}
|
status: 200,
|
||||||
} catch (error: any) {
|
message: "Project and associated bucket deleted successfully",
|
||||||
console.log("Error in deleteProject:", error.message || error.toString());
|
token,
|
||||||
return { status: 500, message: "An error occurred while deleting the project", token };
|
};
|
||||||
}
|
}
|
||||||
|
} catch (error: any) {
|
||||||
|
console.log("Error in deleteProject:", error.message || error.toString());
|
||||||
|
return {
|
||||||
|
status: 500,
|
||||||
|
message: "An error occurred while deleting the project",
|
||||||
|
token,
|
||||||
|
};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,87 +1,104 @@
|
||||||
import { Elysia, t } from "elysia";
|
import { Elysia, t } from "elysia";
|
||||||
import { createProject, deleteProject, getAllProjects, getEachProjects, updateProject } from "./project.controller";
|
import {
|
||||||
|
createProject,
|
||||||
|
deleteProject,
|
||||||
|
getAllProjects,
|
||||||
|
getEachProjects,
|
||||||
|
updateProject,
|
||||||
|
} from "./project.controller";
|
||||||
import { verifyAuth } from "../../middlewares/auth.middlewares";
|
import { verifyAuth } from "../../middlewares/auth.middlewares";
|
||||||
|
|
||||||
export const projectRoutes = new Elysia({
|
export const projectRoutes = new Elysia({
|
||||||
prefix: "/projects",
|
prefix: "/projects",
|
||||||
tags: ["Projects"],
|
tags: ["Projects"],
|
||||||
detail: {
|
detail: {
|
||||||
description: "Routes for managing projects",
|
description: "Routes for managing projects",
|
||||||
}
|
},
|
||||||
}).derive(async ({ cookie }) => {
|
}).derive(async ({ cookie }) => {
|
||||||
const authData = await verifyAuth(cookie);
|
const authData = await verifyAuth(cookie);
|
||||||
return { authData }; // Inject into context
|
return { authData }; // Inject into context
|
||||||
});
|
});
|
||||||
|
|
||||||
projectRoutes.get("/each/:project_id", async ({ params: { project_id }, authData }) => {
|
projectRoutes.get(
|
||||||
if (authData.status !== 200)
|
"/each/:project_id",
|
||||||
return authData;
|
async ({ params: { project_id }, authData }) => {
|
||||||
|
if (authData.status !== 200) return authData;
|
||||||
else {
|
else {
|
||||||
const token = authData.token;
|
const token = authData.token;
|
||||||
const response = await getEachProjects(project_id, token);
|
const response = await getEachProjects(project_id, token);
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
}, {
|
},
|
||||||
|
{
|
||||||
params: t.Object({
|
params: t.Object({
|
||||||
project_id: t.String()
|
project_id: t.String(),
|
||||||
})
|
}),
|
||||||
});
|
}
|
||||||
|
);
|
||||||
|
|
||||||
projectRoutes.get("/", async ({ authData }: any) => {
|
projectRoutes.get("/", async ({ authData }: any) => {
|
||||||
if (authData.status !== 200)
|
if (authData.status !== 200) return authData;
|
||||||
return authData;
|
else {
|
||||||
else {
|
const userId = authData.userId;
|
||||||
const userId = authData.userId;
|
const token = authData.token;
|
||||||
const token = authData.token;
|
const response = await getAllProjects(userId, token);
|
||||||
const response = await getAllProjects(userId, token);
|
return response;
|
||||||
return response;
|
}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
projectRoutes.post("/create", async ({ authData }: any) => {
|
projectRoutes.post("/create", async ({ authData }: any) => {
|
||||||
if (authData.status !== 200)
|
if (authData.status !== 200) return authData;
|
||||||
return authData;
|
else {
|
||||||
else {
|
const userId = authData.userId;
|
||||||
const userId = authData.userId;
|
const token = authData.token;
|
||||||
const token = authData.token;
|
const response = await createProject(userId, token);
|
||||||
const response = await createProject(userId, token);
|
return response;
|
||||||
return response;
|
}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
projectRoutes.put("/update/:project_id", async ({ body, params: { project_id }, authData }) => {
|
projectRoutes.put(
|
||||||
if (authData.status !== 200)
|
"/update/:project_id",
|
||||||
return authData;
|
async ({ body, params: { project_id }, authData }) => {
|
||||||
|
if (authData.status !== 200) return authData;
|
||||||
else {
|
else {
|
||||||
const token = authData.token;
|
const token = authData.token;
|
||||||
const user_id = authData?.userId;
|
const user_id = authData?.userId;
|
||||||
// sending user_id to the controller to update the project with the user_id, when user tried to design a existing project from the design project panel
|
// sending user_id to the controller to update the project with the user_id, when user tried to design a existing project from the design project panel
|
||||||
const response = await updateProject(project_id, body, token, user_id as string);
|
const response = await updateProject(
|
||||||
return response;
|
project_id,
|
||||||
|
body,
|
||||||
|
token,
|
||||||
|
user_id as string
|
||||||
|
);
|
||||||
|
return response;
|
||||||
}
|
}
|
||||||
}, {
|
},
|
||||||
|
{
|
||||||
params: t.Object({
|
params: t.Object({
|
||||||
project_id: t.String()
|
project_id: t.String(),
|
||||||
}),
|
}),
|
||||||
body: t.Object({
|
body: t.Object({
|
||||||
object: t.Record(t.String(), t.Any()), // Allows any JSON object
|
object: t.Record(t.String(), t.Any()), // Allows any JSON object
|
||||||
name: t.String(),
|
name: t.String(),
|
||||||
description: t.String(),
|
description: t.String(),
|
||||||
preview_url: t.String(),
|
preview_url: t.String(),
|
||||||
})
|
}),
|
||||||
});
|
}
|
||||||
|
);
|
||||||
|
|
||||||
projectRoutes.delete("/delete/:project_id", async ({ params: { project_id }, authData }) => {
|
projectRoutes.delete(
|
||||||
if (authData.status !== 200)
|
"/delete/:project_id",
|
||||||
return authData;
|
async ({ params: { project_id }, authData }) => {
|
||||||
|
if (authData.status !== 200) return authData;
|
||||||
else {
|
else {
|
||||||
const token = authData.token;
|
const token = authData.token;
|
||||||
const response = await deleteProject(project_id, token);
|
const response = await deleteProject(project_id, token);
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
}, {
|
},
|
||||||
|
{
|
||||||
params: t.Object({
|
params: t.Object({
|
||||||
project_id: t.String()
|
project_id: t.String(),
|
||||||
})
|
}),
|
||||||
});
|
}
|
||||||
|
);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue