Subcollections when importing

This commit is contained in:
Isaac Wise 2024-02-15 11:26:42 -06:00
parent c3f72c4be8
commit 0e60dee47d

View File

@ -67,22 +67,37 @@ export default async function importFromHTMLFile(
} else { } else {
// @ts-ignore // @ts-ignore
for (const folder of folders) { for (const folder of folders) {
const findCollection = await prisma.user.findUnique({ await createCollectionAndBookmarks(
where: { userId,
id: userId, folder,
folder.nextElementSibling,
null
);
}
}
}, },
select: { { timeout: 30000 }
collections: { )
.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: { where: {
name: folder.textContent.trim(), name: folder.textContent.trim(),
}, ownerId: userId,
},
}, },
}); });
const checkIfCollectionExists = findCollection?.collections[0]; const checkIfCollectionExists = findCollection;
let collectionId = findCollection?.id;
let collectionId = findCollection?.collections[0]?.id;
if (!checkIfCollectionExists || !collectionId) { if (!checkIfCollectionExists || !collectionId) {
const newCollection = await prisma.collection.create({ const newCollection = await prisma.collection.create({
@ -92,6 +107,7 @@ export default async function importFromHTMLFile(
color: "#0ea5e9", color: "#0ea5e9",
isPublic: false, isPublic: false,
ownerId: userId, ownerId: userId,
parentId
}, },
}); });
@ -102,19 +118,16 @@ export default async function importFromHTMLFile(
createFolder({ filePath: `archives/${collectionId}` }); createFolder({ filePath: `archives/${collectionId}` });
const bookmarks = folder.nextElementSibling.querySelectorAll("A"); const bookmarks = folderContent.querySelectorAll("A");
for (const bookmark of bookmarks) { for (const bookmark of bookmarks) {
createBookmark(userId, bookmark, collectionId); 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 ( const createBookmark = async (
userId: number, userId: number,
@ -136,10 +149,22 @@ const createBookmark = async (
description = nextSibling.textContent.trim(); 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({ await prisma.link.create({
data: { data: {
name: bookmark.textContent.trim(), name: linkName,
url: bookmark.getAttribute("HREF"), url: linkURL,
tags: bookmark.getAttribute("TAGS") tags: bookmark.getAttribute("TAGS")
? { ? {
connectOrCreate: bookmark connectOrCreate: bookmark
@ -149,7 +174,7 @@ const createBookmark = async (
tag tag
? { ? {
where: { where: {
name_ownerId: { data: {
name: tag.trim(), name: tag.trim(),
ownerId: userId, ownerId: userId,
}, },
@ -171,4 +196,5 @@ const createBookmark = async (
collectionId, collectionId,
}, },
}); });
}
}; };