updated route + bug fixed

This commit is contained in:
daniel31x13
2023-11-19 16:22:27 -05:00
parent bd16136946
commit 0c6911aaf0
13 changed files with 98 additions and 103 deletions
@@ -10,14 +10,10 @@ export default async function deleteCollection(
if (!collectionId)
return { response: "Please choose a valid collection.", status: 401 };
const collectionIsAccessible = (await getPermission({
const collectionIsAccessible = await getPermission({
userId,
collectionId,
})) as
| (Collection & {
members: UsersAndCollections[];
})
| null;
});
const memberHasAccess = collectionIsAccessible?.members.some(
(e: UsersAndCollections) => e.userId === userId
@@ -11,14 +11,10 @@ export default async function updateCollection(
if (!collectionId)
return { response: "Please choose a valid collection.", status: 401 };
const collectionIsAccessible = (await getPermission({
const collectionIsAccessible = await getPermission({
userId,
collectionId,
})) as
| (Collection & {
members: UsersAndCollections[];
})
| null;
});
if (!(collectionIsAccessible?.ownerId === userId))
return { response: "Collection is not accessible.", status: 401 };
@@ -6,11 +6,7 @@ import removeFile from "@/lib/api/storage/removeFile";
export default async function deleteLink(userId: number, linkId: number) {
if (!linkId) return { response: "Please choose a valid link.", status: 401 };
const collectionIsAccessible = (await getPermission({ userId, linkId })) as
| (Collection & {
members: UsersAndCollections[];
})
| null;
const collectionIsAccessible = await getPermission({ userId, linkId });
const memberHasAccess = collectionIsAccessible?.members.some(
(e: UsersAndCollections) => e.userId === userId && e.canDelete
@@ -9,11 +9,7 @@ export default async function getLinkById(userId: number, linkId: number) {
status: 401,
};
const collectionIsAccessible = (await getPermission({ userId, linkId })) as
| (Collection & {
members: UsersAndCollections[];
})
| null;
const collectionIsAccessible = await getPermission({ userId, linkId });
const memberHasAccess = collectionIsAccessible?.members.some(
(e: UsersAndCollections) => e.userId === userId
@@ -15,11 +15,7 @@ export default async function updateLinkById(
status: 401,
};
const collectionIsAccessible = (await getPermission({ userId, linkId })) as
| (Collection & {
members: UsersAndCollections[];
})
| null;
const collectionIsAccessible = await getPermission({ userId, linkId });
const memberHasAccess = collectionIsAccessible?.members.some(
(e: UsersAndCollections) => e.userId === userId && e.canUpdate
+3 -7
View File
@@ -2,7 +2,7 @@ import { prisma } from "@/lib/api/db";
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
import getTitle from "@/lib/api/getTitle";
import archive from "@/lib/api/archive";
import { Collection, UsersAndCollections } from "@prisma/client";
import { UsersAndCollections } from "@prisma/client";
import getPermission from "@/lib/api/getPermission";
import createFolder from "@/lib/api/storage/createFolder";
@@ -27,14 +27,10 @@ export default async function postLink(
link.collection.name = link.collection.name.trim();
if (link.collection.id) {
const collectionIsAccessible = (await getPermission({
const collectionIsAccessible = await getPermission({
userId,
collectionId: link.collection.id,
})) as
| (Collection & {
members: UsersAndCollections[];
})
| null;
});
const memberHasAccess = collectionIsAccessible?.members.some(
(e: UsersAndCollections) => e.userId === userId && e.canCreate
+7 -25
View File
@@ -1,35 +1,24 @@
import { prisma } from "@/lib/api/db";
type Props = {
userId?: number;
userId: number;
collectionId?: number;
linkId?: number;
isPublic?: boolean;
};
export default async function getPermission({
userId,
collectionId,
linkId,
isPublic,
}: Props) {
if (linkId) {
const check = await prisma.collection.findFirst({
where: {
[isPublic ? "OR" : "AND"]: [
{
id: collectionId,
OR: [{ ownerId: userId }, { members: { some: { userId } } }],
links: {
some: {
id: linkId,
},
},
links: {
some: {
id: linkId,
},
{
isPublic: isPublic ? true : undefined,
},
],
},
},
include: { members: true },
});
@@ -38,15 +27,8 @@ export default async function getPermission({
} else if (collectionId) {
const check = await prisma.collection.findFirst({
where: {
[isPublic ? "OR" : "AND"]: [
{
id: collectionId,
OR: [{ ownerId: userId }, { members: { some: { userId } } }],
},
{
isPublic: isPublic ? true : undefined,
},
],
id: collectionId,
OR: [{ ownerId: userId }, { members: { some: { userId } } }],
},
include: { members: true },
});