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 };
|
||||
}
|
||||
|
||||
// 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) => {
|
|||
};
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue