some bugs fixed
This commit is contained in:
parent
55b0a0300c
commit
649151d613
2 changed files with 30 additions and 44 deletions
|
|
@ -39,24 +39,7 @@ export const getAllProjects = async (userId: string, token: string) => {
|
||||||
return { status: 404, message: "No projects found", token };
|
return { status: 404, message: "No projects found", token };
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter out projects where `object` is empty (null or an empty object)
|
return { status: 200, message: "Projects fetched successfully", data: allProjects, token };
|
||||||
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 };
|
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.log(error.message);
|
console.log(error.message);
|
||||||
return { status: 500, message: "An error occurred while fetching projects", token };
|
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) => {
|
export const deleteProject = async (id: string, token: string) => {
|
||||||
try {
|
try {
|
||||||
// First, delete related records from the 'uploads' table
|
const deletedUploads = await db
|
||||||
await db.delete(uploads).where(eq(uploads.projectId, id));
|
.delete(uploads)
|
||||||
|
.where(eq(uploads.projectId, id))
|
||||||
|
.returning({ id: uploads.id });
|
||||||
|
|
||||||
// Now delete the project
|
if (deletedUploads.length >= 0) {
|
||||||
const deleteProject = await db
|
// Step 4: Delete the project
|
||||||
.delete(projects)
|
const deletedProject = await db
|
||||||
.where(eq(projects.id, id))
|
.delete(projects)
|
||||||
.returning({ id: projects.id });
|
.where(eq(projects.id, id))
|
||||||
|
.returning({ id: projects.id });
|
||||||
|
|
||||||
if (deleteProject.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
|
||||||
|
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) {
|
} catch (error: any) {
|
||||||
console.log("Error in deleteProject:", error.message || error.toString());
|
console.log("Error in deleteProject:", error.message || error.toString());
|
||||||
return { status: 500, message: "An error occurred while deleting the project", token };
|
return { status: 500, message: "An error occurred while deleting the project", token };
|
||||||
|
|
@ -146,3 +129,4 @@ export const deleteProject = async (id: string, token: string) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,8 @@ export const createEmptyProject = async (userId: string): Promise<{ id: string }
|
||||||
object: {}, // Empty object as default
|
object: {}, // Empty object as default
|
||||||
name: "", // Empty name
|
name: "", // Empty name
|
||||||
description: "", // Empty description
|
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
|
.returning({ id: projects.id }); // Returning the ID of the created project
|
||||||
// Return the newly created project's ID
|
// Return the newly created project's ID
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue