many breaking changes/fixes

This commit is contained in:
Daniel
2023-03-28 11:01:50 +03:30
parent 3a5ae28f86
commit b9567ca3c2
43 changed files with 1180 additions and 466 deletions
@@ -1,19 +1,11 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { prisma } from "@/lib/api/db";
import { Session } from "next-auth";
export default async function (
req: NextApiRequest,
res: NextApiResponse,
session: Session
) {
export default async function (userId: number) {
const collections = await prisma.collection.findMany({
where: {
ownerId: session?.user.id,
ownerId: userId,
},
});
return res.status(200).json({
response: collections || [],
});
return { response: collections, status: 200 };
}
@@ -1,29 +1,16 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { prisma } from "@/lib/api/db";
import { Session } from "next-auth";
import { existsSync, mkdirSync } from "fs";
export default async function (
req: NextApiRequest,
res: NextApiResponse,
session: Session
) {
if (!session?.user?.email) {
return res.status(401).json({ response: "You must be logged in." });
}
export default async function (collectionName: string, userId: number) {
if (!collectionName)
return {
response: "Please enter a valid name for the collection.",
status: 400,
};
const email: string = session.user.email;
const collectionName: string = req?.body?.collectionName;
if (!collectionName) {
return res
.status(401)
.json({ response: "Please enter a valid name for the collection." });
}
const findCollection = await prisma.user.findFirst({
const findCollection = await prisma.user.findUnique({
where: {
email,
id: userId,
},
select: {
collections: {
@@ -36,15 +23,14 @@ export default async function (
const checkIfCollectionExists = findCollection?.collections[0];
if (checkIfCollectionExists) {
return res.status(400).json({ response: "Collection already exists." });
}
if (checkIfCollectionExists)
return { response: "Collection already exists.", status: 400 };
const newCollection = await prisma.collection.create({
data: {
owner: {
connect: {
id: session.user.id,
id: userId,
},
},
name: collectionName,
@@ -55,7 +41,5 @@ export default async function (
if (!existsSync(collectionPath))
mkdirSync(collectionPath, { recursive: true });
return res.status(200).json({
response: newCollection,
});
return { response: newCollection, status: 200 };
}
+7 -23
View File
@@ -1,37 +1,23 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { prisma } from "@/lib/api/db";
import { Session } from "next-auth";
import { ExtendedLink } from "@/types/global";
import fs from "fs";
import { Link, UsersAndCollections } from "@prisma/client";
import hasAccessToCollection from "@/lib/api/hasAccessToCollection";
export default async function (
req: NextApiRequest,
res: NextApiResponse,
session: Session
) {
if (!session?.user?.email) {
return res.status(401).json({ response: "You must be logged in." });
}
const link: ExtendedLink = req?.body;
if (!link) {
return res.status(401).json({ response: "Please choose a valid link." });
}
export default async function (link: ExtendedLink, userId: number) {
if (!link) return { response: "Please choose a valid link.", status: 401 };
const collectionIsAccessible = await hasAccessToCollection(
session.user.id,
userId,
link.collectionId
);
const memberHasAccess = collectionIsAccessible?.members.some(
(e: UsersAndCollections) => e.userId === session.user.id && e.canDelete
(e: UsersAndCollections) => e.userId === userId && e.canDelete
);
if (!(collectionIsAccessible?.ownerId === session.user.id || memberHasAccess))
return res.status(401).json({ response: "Collection is not accessible." });
if (!(collectionIsAccessible?.ownerId === userId || memberHasAccess))
return { response: "Collection is not accessible.", status: 401 };
const deleteLink: Link = await prisma.link.delete({
where: {
@@ -47,7 +33,5 @@ export default async function (
if (err) console.log(err);
});
return res.status(200).json({
response: deleteLink,
});
return { response: deleteLink, status: 200 };
}
+5 -14
View File
@@ -1,23 +1,16 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { prisma } from "@/lib/api/db";
import { Session } from "next-auth";
export default async function (
req: NextApiRequest,
res: NextApiResponse,
session: Session
) {
const tags = await prisma.link.findMany({
export default async function (userId: number) {
const links = await prisma.link.findMany({
where: {
collection: {
OR: [
{
ownerId: session?.user.id,
ownerId: userId,
},
{
members: {
some: {
userId: session?.user.id,
userId,
},
},
},
@@ -30,7 +23,5 @@ export default async function (
},
});
return res.status(200).json({
response: tags || [],
});
return { response: links, status: 200 };
}
+42 -82
View File
@@ -1,7 +1,5 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { prisma } from "@/lib/api/db";
import { Session } from "next-auth";
import { ExtendedLink, NewLink } from "@/types/global";
import { ExtendedLink } from "@/types/global";
import { existsSync, mkdirSync } from "fs";
import getTitle from "../../getTitle";
import archive from "../../archive";
@@ -9,77 +7,31 @@ import { Link, UsersAndCollections } from "@prisma/client";
import AES from "crypto-js/aes";
import hasAccessToCollection from "@/lib/api/hasAccessToCollection";
export default async function (
req: NextApiRequest,
res: NextApiResponse,
session: Session
) {
if (!session?.user?.email) {
return res.status(401).json({ response: "You must be logged in." });
}
const email: string = session.user.email;
const link: NewLink = req?.body;
export default async function (link: ExtendedLink, userId: number) {
link.collection.name = link.collection.name.trim();
if (!link.name) {
return res
.status(401)
.json({ response: "Please enter a valid name for the link." });
return { response: "Please enter a valid name for the link.", status: 401 };
} else if (!link.collection.name) {
return { response: "Please enter a valid collection name.", status: 401 };
}
if (link.collection.isNew) {
const collectionId = link.collection.id as string;
if (link.collection.ownerId) {
const collectionIsAccessible = await hasAccessToCollection(
userId,
link.collection.id
);
const findCollection = await prisma.user.findFirst({
where: {
email,
},
select: {
collections: {
where: {
name: collectionId,
},
},
},
});
const memberHasAccess = collectionIsAccessible?.members.some(
(e: UsersAndCollections) => e.userId === userId && e.canCreate
);
const checkIfCollectionExists = findCollection?.collections[0];
if (checkIfCollectionExists)
return res.status(400).json({ response: "Collection already exists." });
const newCollection = await prisma.collection.create({
data: {
owner: {
connect: {
id: session.user.id,
},
},
name: collectionId,
},
});
const collectionPath = `data/archives/${newCollection.id}`;
if (!existsSync(collectionPath))
mkdirSync(collectionPath, { recursive: true });
link.collection.id = newCollection.id;
if (!(collectionIsAccessible?.ownerId === userId || memberHasAccess))
return { response: "Collection is not accessible.", status: 401 };
} else {
link.collection.ownerId = userId;
}
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({
@@ -87,35 +39,45 @@ export default async function (
name: link.name,
url: link.url,
collection: {
connect: {
id: collectionId,
},
},
tags: {
connectOrCreate: link.tags.map((name) => ({
connectOrCreate: {
where: {
name_collectionId: {
name,
collectionId,
name_ownerId: {
ownerId: link.collection.ownerId,
name: link.collection.name,
},
},
create: {
name,
collections: {
name: link.collection.name,
ownerId: userId,
},
},
},
tags: {
connectOrCreate: link.tags.map((tag) => ({
where: {
name_ownerId: {
name: tag.name,
ownerId: link.collection.ownerId,
},
},
create: {
name: tag.name,
owner: {
connect: {
id: collectionId,
id: link.collection.ownerId,
},
},
},
})),
},
title,
starred: false,
screenshotPath: "",
pdfPath: "",
},
});
console.log(newLink);
const AES_SECRET = process.env.AES_SECRET as string;
const screenshotHashedPath = AES.encrypt(
@@ -136,7 +98,5 @@ export default async function (
archive(updatedLink.url, updatedLink.collectionId, updatedLink.id);
return res.status(200).json({
response: updatedLink,
});
return { response: updatedLink, status: 200 };
}
+13 -14
View File
@@ -1,12 +1,6 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { prisma } from "@/lib/api/db";
import { Session } from "next-auth";
export default async function (
req: NextApiRequest,
res: NextApiResponse,
session: Session
) {
export default async function (userId: number) {
// tag cleanup
await prisma.tag.deleteMany({
where: {
@@ -18,15 +12,22 @@ export default async function (
const tags = await prisma.tag.findMany({
where: {
collections: {
ownerId: userId,
owner: {
OR: [
{
ownerId: session?.user.id,
id: userId,
},
{
members: {
collections: {
some: {
userId: session?.user.id,
members: {
some: {
user: {
id: userId,
},
},
},
},
},
},
@@ -35,7 +36,5 @@ export default async function (
},
});
return res.status(200).json({
response: tags || [],
});
return { response: tags, status: 200 };
}