2023-03-08 15:31:24 -06:00
|
|
|
import { prisma } from "@/lib/api/db";
|
|
|
|
|
2023-10-22 23:28:39 -05:00
|
|
|
type Props = {
|
2023-11-19 07:12:37 -06:00
|
|
|
userId?: number;
|
2023-10-22 23:28:39 -05:00
|
|
|
collectionId?: number;
|
|
|
|
linkId?: number;
|
2023-11-19 07:12:37 -06:00
|
|
|
isPublic?: boolean;
|
2023-10-22 23:28:39 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
export default async function getPermission({
|
|
|
|
userId,
|
|
|
|
collectionId,
|
|
|
|
linkId,
|
2023-11-19 07:12:37 -06:00
|
|
|
isPublic,
|
2023-10-22 23:28:39 -05:00
|
|
|
}: Props) {
|
2023-06-24 16:54:35 -05:00
|
|
|
if (linkId) {
|
2023-10-22 23:28:39 -05:00
|
|
|
const check = await prisma.collection.findFirst({
|
2023-06-24 16:54:35 -05:00
|
|
|
where: {
|
2023-11-19 07:12:37 -06:00
|
|
|
[isPublic ? "OR" : "AND"]: [
|
|
|
|
{
|
|
|
|
id: collectionId,
|
|
|
|
OR: [{ ownerId: userId }, { members: { some: { userId } } }],
|
|
|
|
links: {
|
|
|
|
some: {
|
|
|
|
id: linkId,
|
|
|
|
},
|
|
|
|
},
|
2023-10-22 23:28:39 -05:00
|
|
|
},
|
2023-11-19 07:12:37 -06:00
|
|
|
{
|
|
|
|
isPublic: isPublic ? true : undefined,
|
|
|
|
},
|
|
|
|
],
|
2023-06-24 16:54:35 -05:00
|
|
|
},
|
2023-10-22 23:28:39 -05:00
|
|
|
include: { members: true },
|
2023-06-24 16:54:35 -05:00
|
|
|
});
|
|
|
|
|
2023-10-22 23:28:39 -05:00
|
|
|
return check;
|
|
|
|
} else if (collectionId) {
|
2023-06-24 16:54:35 -05:00
|
|
|
const check = await prisma.collection.findFirst({
|
|
|
|
where: {
|
2023-11-19 07:12:37 -06:00
|
|
|
[isPublic ? "OR" : "AND"]: [
|
|
|
|
{
|
|
|
|
id: collectionId,
|
|
|
|
OR: [{ ownerId: userId }, { members: { some: { userId } } }],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
isPublic: isPublic ? true : undefined,
|
|
|
|
},
|
|
|
|
],
|
2023-06-24 16:54:35 -05:00
|
|
|
},
|
|
|
|
include: { members: true },
|
|
|
|
});
|
2023-03-08 15:31:24 -06:00
|
|
|
|
2023-06-24 16:54:35 -05:00
|
|
|
return check;
|
|
|
|
}
|
2023-06-09 17:31:14 -05:00
|
|
|
}
|