el.xwx.moe/lib/api/controllers/links/linkId/getLinkById.ts

45 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-10-28 23:57:24 -05:00
import { prisma } from "@/lib/api/db";
2023-11-19 07:12:37 -06:00
import { Collection, UsersAndCollections } from "@prisma/client";
2023-10-28 23:57:24 -05:00
import getPermission from "@/lib/api/getPermission";
export default async function getLinkById(userId: number, linkId: number) {
if (!linkId)
return {
response: "Please choose a valid link.",
status: 401,
};
2023-11-19 15:22:27 -06:00
const collectionIsAccessible = await getPermission({ userId, linkId });
2023-10-28 23:57:24 -05:00
const memberHasAccess = collectionIsAccessible?.members.some(
2023-10-30 14:20:15 -05:00
(e: UsersAndCollections) => e.userId === userId
2023-10-28 23:57:24 -05:00
);
const isCollectionOwner = collectionIsAccessible?.ownerId === userId;
if (collectionIsAccessible?.ownerId !== userId && !memberHasAccess)
return {
response: "Collection is not accessible.",
status: 401,
};
else {
2023-11-19 07:12:37 -06:00
const link = await prisma.link.findUnique({
2023-10-28 23:57:24 -05:00
where: {
id: linkId,
},
include: {
tags: true,
collection: true,
pinnedBy: isCollectionOwner
? {
where: { id: userId },
select: { id: true },
}
: undefined,
},
});
2023-11-19 07:12:37 -06:00
return { response: link, status: 200 };
2023-10-28 23:57:24 -05:00
}
}