From 649151d613babe902e82aa384e7e18d553f36aee Mon Sep 17 00:00:00 2001 From: Saimon8420 Date: Sat, 8 Feb 2025 15:35:18 +0600 Subject: [PATCH] some bugs fixed --- src/api/project/project.controller.ts | 70 +++++++++++---------------- src/helper/projects/createProject.ts | 4 +- 2 files changed, 30 insertions(+), 44 deletions(-) diff --git a/src/api/project/project.controller.ts b/src/api/project/project.controller.ts index 07d1eeb..e729d72 100644 --- a/src/api/project/project.controller.ts +++ b/src/api/project/project.controller.ts @@ -39,24 +39,7 @@ export const getAllProjects = async (userId: string, token: string) => { return { status: 404, message: "No projects found", token }; } - // Filter out projects where `object` is empty (null or an empty object) - const validProjects = []; - for (const project of allProjects) { - if (!project.object || Object.keys(project.object).length === 0) { - // Remove the project from the database - await db.delete(projects).where(eq(projects.id, project.id)); - - // Remove the associated MinIO bucket - await removeBucket(project.id); - } else { - validProjects.push(project); - } - } - - if (validProjects.length === 0) { - return { status: 404, message: "No projects found", token }; - } - return { status: 200, message: "Projects fetched successfully", data: validProjects, token }; + return { status: 200, message: "Projects fetched successfully", data: allProjects, token }; } catch (error: any) { console.log(error.message); return { status: 500, message: "An error occurred while fetching projects", token }; @@ -111,34 +94,34 @@ export const updateProject = async (id: string, body: any, token: string) => { export const deleteProject = async (id: string, token: string) => { try { - // First, delete related records from the 'uploads' table - await db.delete(uploads).where(eq(uploads.projectId, id)); + const deletedUploads = await db + .delete(uploads) + .where(eq(uploads.projectId, id)) + .returning({ id: uploads.id }); - // Now delete the project - const deleteProject = await db - .delete(projects) - .where(eq(projects.id, id)) - .returning({ id: projects.id }); + if (deletedUploads.length >= 0) { + // Step 4: Delete the project + const deletedProject = await db + .delete(projects) + .where(eq(projects.id, id)) + .returning({ id: projects.id }); - if (deleteProject.length === 0) { - return { status: 404, message: "Project not found", token }; + if (deletedProject.length === 0) { + return { status: 404, message: "Project not found", token }; + } + + // Step 5: Delete the associated bucket + const bucketDeletionResult = await removeBucket(id); + + if (bucketDeletionResult.status !== 200) { + return { + status: bucketDeletionResult.status, + message: `Error deleting bucket: ${bucketDeletionResult.message}`, + token + }; + } + return { status: 200, message: "Project and associated bucket deleted successfully", token }; } - - const projectId = deleteProject[0].id; - - // Delete the bucket associated with the project - const bucketDeletionResult = await removeBucket(projectId); - - if (bucketDeletionResult.status !== 200) { - return { - status: bucketDeletionResult.status, - message: `Error deleting bucket: ${bucketDeletionResult.message}`, - token - }; - } - - return { status: 200, message: "Project and associated bucket deleted successfully", 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 }; @@ -146,3 +129,4 @@ export const deleteProject = async (id: string, token: string) => { }; + diff --git a/src/helper/projects/createProject.ts b/src/helper/projects/createProject.ts index a41b989..d52c2cc 100644 --- a/src/helper/projects/createProject.ts +++ b/src/helper/projects/createProject.ts @@ -11,6 +11,8 @@ export const createEmptyProject = async (userId: string): Promise<{ id: string } object: {}, // Empty object as default name: "", // Empty name description: "", // Empty description + preview_url: "", // Empty preview URL + is_public: false, // Add default value for is_public }) .returning({ id: projects.id }); // Returning the ID of the created project // Return the newly created project's ID @@ -19,4 +21,4 @@ export const createEmptyProject = async (userId: string): Promise<{ id: string } console.error("Error creating an empty project:", error); throw new Error("Failed to create an empty project"); } -}; \ No newline at end of file +};