feat: created the Link component

This commit is contained in:
Daniel
2023-03-10 22:55:33 +03:30
parent 0d5579b56d
commit 48bdf29161
10 changed files with 144 additions and 33 deletions
+4 -1
View File
@@ -24,7 +24,10 @@ export default async function (
],
},
},
include: { tags: true },
include: {
tags: true,
collection: true,
},
});
return res.status(200).json({
+18 -6
View File
@@ -1,12 +1,13 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { prisma } from "@/lib/api/db";
import { Session } from "next-auth";
import { LinkAndTags, NewLink } from "@/types/global";
import { ExtendedLink, NewLink } from "@/types/global";
import { existsSync, mkdirSync } from "fs";
import getTitle from "../../getTitle";
import archive from "../../archive";
import { Link } from "@prisma/client";
import { Link, UsersAndCollections } from "@prisma/client";
import AES from "crypto-js/aes";
import hasAccessToCollection from "@/lib/api/hasAccessToCollection";
export default async function (
req: NextApiRequest,
@@ -44,9 +45,8 @@ export default async function (
const checkIfCollectionExists = findCollection?.collections[0];
if (checkIfCollectionExists) {
if (checkIfCollectionExists)
return res.status(400).json({ response: "Collection already exists." });
}
const newCollection = await prisma.collection.create({
data: {
@@ -68,6 +68,18 @@ export default async function (
const collectionId = link.collection.id as number;
const collectionIsAccessible = await hasAccessToCollection(
session.user.id,
collectionId
);
const memberHasAccess = collectionIsAccessible?.members.some(
(e: UsersAndCollections) => e.userId === session.user.id && e.canCreate
);
if (!(collectionIsAccessible?.ownerId === session.user.id || memberHasAccess))
return res.status(401).json({ response: "Collection is not accessible." });
const title = await getTitle(link.url);
const newLink: Link = await prisma.link.create({
@@ -116,10 +128,10 @@ export default async function (
AES_SECRET
).toString();
const updatedLink: LinkAndTags = await prisma.link.update({
const updatedLink: ExtendedLink = await prisma.link.update({
where: { id: newLink.id },
data: { screenshotPath: screenShotHashedPath, pdfPath: pdfHashedPath },
include: { tags: true },
include: { tags: true, collection: true },
});
archive(updatedLink.url, updatedLink.collectionId, updatedLink.id);