From cdc05f85a6dc676e8af1227a56f65d6452488d82 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Wed, 7 Feb 2024 22:12:18 +0000 Subject: [feature] Render tags in the link card --- web/lib/services/links.ts | 65 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 18 deletions(-) (limited to 'web/lib/services') diff --git a/web/lib/services/links.ts b/web/lib/services/links.ts index dbcbe9c4..d273b118 100644 --- a/web/lib/services/links.ts +++ b/web/lib/services/links.ts @@ -1,5 +1,43 @@ 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({ @@ -16,6 +54,7 @@ export async function bookmarkLink(url: string, userId: string) { url, userId, }, + select: defaultLinkFields, }); // Enqueue crawling request @@ -24,26 +63,16 @@ export async function bookmarkLink(url: string, userId: string) { url: link.url, }); - return link; + return toZodSchema(link); } export async function getLinks(userId: string) { - return await prisma.bookmarkedLink.findMany({ - where: { - userId, - }, - select: { - id: true, - url: true, - createdAt: true, - details: { - select: { - title: true, - description: true, - imageUrl: true, - favicon: true, - }, + return ( + await prisma.bookmarkedLink.findMany({ + where: { + userId, }, - }, - }); + select: defaultLinkFields, + }) + ).map(toZodSchema); } -- cgit v1.2.3-70-g09d2