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,45 +67,12 @@ 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,
select: { null
collections: { );
where: {
name: folder.textContent.trim(),
},
},
},
});
const checkIfCollectionExists = findCollection?.collections[0];
let collectionId = findCollection?.collections[0]?.id;
if (!checkIfCollectionExists || !collectionId) {
const newCollection = await prisma.collection.create({
data: {
name: folder.textContent.trim(),
description: "",
color: "#0ea5e9",
isPublic: false,
ownerId: userId,
},
});
createFolder({ filePath: `archives/${newCollection.id}` });
collectionId = newCollection.id;
}
createFolder({ filePath: `archives/${collectionId}` });
const bookmarks = folder.nextElementSibling.querySelectorAll("A");
for (const bookmark of bookmarks) {
createBookmark(userId, bookmark, collectionId);
}
} }
} }
}, },
@ -116,6 +83,52 @@ export default async function importFromHTMLFile(
return { response: "Success.", status: 200 }; 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;
let collectionId = findCollection?.id;
if (!checkIfCollectionExists || !collectionId) {
const newCollection = await prisma.collection.create({
data: {
name: folder.textContent.trim(),
description: "",
color: "#0ea5e9",
isPublic: false,
ownerId: userId,
parentId
},
});
createFolder({ filePath: `archives/${newCollection.id}` });
collectionId = newCollection.id;
}
createFolder({ filePath: `archives/${collectionId}` });
const bookmarks = folderContent.querySelectorAll("A");
for (const bookmark of bookmarks) {
createBookmark(userId, bookmark, collectionId);
}
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,
bookmark: any, bookmark: any,
@ -136,39 +149,52 @@ const createBookmark = async (
description = nextSibling.textContent.trim(); description = nextSibling.textContent.trim();
} }
await prisma.link.create({ const linkName = bookmark.textContent.trim();
data: { const linkURL = bookmark.getAttribute("HREF");
name: bookmark.textContent.trim(),
url: bookmark.getAttribute("HREF"), const existingLink = await prisma.link.findFirst({
tags: bookmark.getAttribute("TAGS") where: {
? { url: linkURL,
collectionId
},
});
// Create the link only if it doesn't already exist
if (!existingLink) {
await prisma.link.create({
data: {
name: linkName,
url: linkURL,
tags: bookmark.getAttribute("TAGS")
? {
connectOrCreate: bookmark connectOrCreate: bookmark
.getAttribute("TAGS") .getAttribute("TAGS")
.split(",") .split(",")
.map((tag: string) => .map((tag: string) =>
tag tag
? { ? {
where: { where: {
name_ownerId: { data: {
name: tag.trim(),
ownerId: userId,
},
},
create: {
name: tag.trim(), name: tag.trim(),
owner: { ownerId: userId,
connect: { },
id: userId, },
}, create: {
name: tag.trim(),
owner: {
connect: {
id: userId,
}, },
}, },
} },
}
: undefined : undefined
), ),
} }
: undefined, : undefined,
description, description,
collectionId, collectionId,
}, },
}); });
}; }
};