added support for nested collection (backend)

This commit is contained in:
daniel31x13
2024-02-03 07:57:29 -05:00
parent 8534572662
commit dba2453453
9 changed files with 158 additions and 25 deletions
@@ -37,6 +37,8 @@ export default async function deleteCollection(
}
const deletedCollection = await prisma.$transaction(async () => {
await deleteSubCollections(collectionId);
await prisma.usersAndCollections.deleteMany({
where: {
collection: {
@@ -53,7 +55,7 @@ export default async function deleteCollection(
},
});
removeFolder({ filePath: `archives/${collectionId}` });
await removeFolder({ filePath: `archives/${collectionId}` });
return await prisma.collection.delete({
where: {
@@ -64,3 +66,35 @@ export default async function deleteCollection(
return { response: deletedCollection, status: 200 };
}
async function deleteSubCollections(collectionId: number) {
const subCollections = await prisma.collection.findMany({
where: { parentId: collectionId },
});
for (const subCollection of subCollections) {
await deleteSubCollections(subCollection.id);
await prisma.usersAndCollections.deleteMany({
where: {
collection: {
id: subCollection.id,
},
},
});
await prisma.link.deleteMany({
where: {
collection: {
id: subCollection.id,
},
},
});
await prisma.collection.delete({
where: { id: subCollection.id },
});
await removeFolder({ filePath: `archives/${subCollection.id}` });
}
}