Merge pull request #472 from IsaacWise06/fix/imports

Importing sub-collections fix
This commit is contained in:
Daniel 2024-02-18 04:33:28 +03:30 committed by GitHub
commit c3c74b8162
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -67,22 +67,37 @@ export default async function importFromHTMLFile(
} else {
// @ts-ignore
for (const folder of folders) {
const findCollection = await prisma.user.findUnique({
where: {
id: userId,
await createCollectionAndBookmarks(
userId,
folder,
folder.nextElementSibling,
null
);
}
}
},
select: {
collections: {
{ timeout: 30000 }
)
.catch((err) => console.log(err));
return { response: "Success.", status: 200 };
}
const createCollectionAndBookmarks = async (
userId: number,
folder: any,
folderContent: any,
parentId: number | null
) => {
const findCollection = await prisma.collection.findFirst({
where: {
name: folder.textContent.trim(),
},
},
ownerId: userId,
},
});
const checkIfCollectionExists = findCollection?.collections[0];
let collectionId = findCollection?.collections[0]?.id;
const checkIfCollectionExists = findCollection;
let collectionId = findCollection?.id;
if (!checkIfCollectionExists || !collectionId) {
const newCollection = await prisma.collection.create({
@ -92,6 +107,7 @@ export default async function importFromHTMLFile(
color: "#0ea5e9",
isPublic: false,
ownerId: userId,
parentId
},
});
@ -102,19 +118,16 @@ export default async function importFromHTMLFile(
createFolder({ filePath: `archives/${collectionId}` });
const bookmarks = folder.nextElementSibling.querySelectorAll("A");
const bookmarks = folderContent.querySelectorAll("A");
for (const bookmark of bookmarks) {
createBookmark(userId, bookmark, collectionId);
}
}
}
},
{ timeout: 30000 }
)
.catch((err) => console.log(err));
return { response: "Success.", status: 200 };
const subfolders = folderContent.querySelectorAll("H3");
for (const subfolder of subfolders) {
await createCollectionAndBookmarks(userId, subfolder, subfolder.nextElementSibling, collectionId);
}
};
const createBookmark = async (
userId: number,
@ -136,10 +149,22 @@ const createBookmark = async (
description = nextSibling.textContent.trim();
}
const linkName = bookmark.textContent.trim();
const linkURL = bookmark.getAttribute("HREF");
const existingLink = await prisma.link.findFirst({
where: {
url: linkURL,
collectionId
},
});
// Create the link only if it doesn't already exist
if (!existingLink) {
await prisma.link.create({
data: {
name: bookmark.textContent.trim(),
url: bookmark.getAttribute("HREF"),
name: linkName,
url: linkURL,
tags: bookmark.getAttribute("TAGS")
? {
connectOrCreate: bookmark
@ -149,7 +174,7 @@ const createBookmark = async (
tag
? {
where: {
name_ownerId: {
data: {
name: tag.trim(),
ownerId: userId,
},
@ -171,4 +196,5 @@ const createBookmark = async (
collectionId,
},
});
}
};