From 08a5694e451218f1bcb2ad9eb42fd93250afbb96 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Fri, 9 Feb 2024 01:50:35 +0000 Subject: [refactor] Extract the bookmark model to be a high level model to support other type of bookmarks --- packages/web/lib/services/bookmarks.ts | 92 ++++++++++++++++++++++++++++++++++ packages/web/lib/services/links.ts | 78 ---------------------------- 2 files changed, 92 insertions(+), 78 deletions(-) create mode 100644 packages/web/lib/services/bookmarks.ts delete mode 100644 packages/web/lib/services/links.ts (limited to 'packages/web/lib/services') diff --git a/packages/web/lib/services/bookmarks.ts b/packages/web/lib/services/bookmarks.ts new file mode 100644 index 00000000..3c9929bc --- /dev/null +++ b/packages/web/lib/services/bookmarks.ts @@ -0,0 +1,92 @@ +import { LinkCrawlerQueue } from "@remember/shared/queues"; +import prisma from "@remember/db"; +import { ZBookmark, ZBookmarkContent } from "@/lib/types/api/bookmarks"; + +const defaultBookmarkFields = { + id: true, + favourited: true, + archived: true, + createdAt: true, + link: { + select: { + url: true, + title: true, + description: true, + imageUrl: true, + favicon: true, + }, + }, + tags: { + include: { + tag: true, + }, + }, +}; + +async function dummyPrismaReturnType() { + const x = await prisma.bookmark.findFirstOrThrow({ + select: defaultBookmarkFields, + }); + return x; +} + +function toZodSchema( + bookmark: Awaited>, +): ZBookmark { + const { tags, link, ...rest } = bookmark; + + let content: ZBookmarkContent; + if (link) { + content = { type: "link", ...link }; + } else { + throw new Error("Unknown content type"); + } + + return { + tags: tags.map((t) => t.tag), + content, + ...rest, + }; +} + +export async function deleteBookmark(bookmarkId: string, userId: string) { + await prisma.bookmark.delete({ + where: { + id: bookmarkId, + userId, + }, + }); +} + +export async function bookmarkLink(url: string, userId: string) { + const bookmark = await prisma.bookmark.create({ + data: { + link: { + create: { + url, + }, + }, + userId, + }, + select: defaultBookmarkFields, + }); + + // Enqueue crawling request + await LinkCrawlerQueue.add("crawl", { + bookmarkId: bookmark.id, + url: url, + }); + + return toZodSchema(bookmark); +} + +export async function getBookmarks(userId: string) { + return ( + await prisma.bookmark.findMany({ + where: { + userId, + }, + select: defaultBookmarkFields, + }) + ).map(toZodSchema); +} diff --git a/packages/web/lib/services/links.ts b/packages/web/lib/services/links.ts deleted file mode 100644 index d273b118..00000000 --- a/packages/web/lib/services/links.ts +++ /dev/null @@ -1,78 +0,0 @@ -import { LinkCrawlerQueue } from "@remember/shared/queues"; -import prisma from "@remember/db"; -import { ZBookmarkedLink } from "@/lib/types/api/links"; - -const defaultLinkFields = { - id: true, - url: true, - createdAt: true, - details: { - select: { - title: true, - description: true, - imageUrl: true, - favicon: true, - }, - }, - tags: { - include: { - tag: true, - }, - }, -}; - -async function dummyPrismaReturnType() { - return await prisma.bookmarkedLink.findFirstOrThrow({ - select: defaultLinkFields, - }); -} - -function toZodSchema( - link: Awaited>, -): ZBookmarkedLink { - return { - id: link.id, - url: link.url, - createdAt: link.createdAt, - details: link.details, - tags: link.tags.map((t) => t.tag), - }; -} - -export async function unbookmarkLink(linkId: string, userId: string) { - await prisma.bookmarkedLink.delete({ - where: { - id: linkId, - userId, - }, - }); -} - -export async function bookmarkLink(url: string, userId: string) { - const link = await prisma.bookmarkedLink.create({ - data: { - url, - userId, - }, - select: defaultLinkFields, - }); - - // Enqueue crawling request - await LinkCrawlerQueue.add("crawl", { - linkId: link.id, - url: link.url, - }); - - return toZodSchema(link); -} - -export async function getLinks(userId: string) { - return ( - await prisma.bookmarkedLink.findMany({ - where: { - userId, - }, - select: defaultLinkFields, - }) - ).map(toZodSchema); -} -- cgit v1.2.3-70-g09d2