2023-03-23 10:25:17 -05:00
|
|
|
import { prisma } from "@/lib/api/db";
|
2023-06-24 16:54:35 -05:00
|
|
|
import { Collection, Link, UsersAndCollections } from "@prisma/client";
|
2023-04-26 15:40:48 -05:00
|
|
|
import getPermission from "@/lib/api/getPermission";
|
2023-07-01 09:11:39 -05:00
|
|
|
import removeFile from "@/lib/api/storage/removeFile";
|
2023-03-23 10:25:17 -05:00
|
|
|
|
2023-10-22 23:28:39 -05:00
|
|
|
export default async function deleteLink(userId: number, linkId: number) {
|
|
|
|
if (!linkId) return { response: "Please choose a valid link.", status: 401 };
|
2023-03-23 10:25:17 -05:00
|
|
|
|
2023-10-22 23:28:39 -05:00
|
|
|
const collectionIsAccessible = (await getPermission({ userId, linkId })) as
|
2023-06-24 16:54:35 -05:00
|
|
|
| (Collection & {
|
|
|
|
members: UsersAndCollections[];
|
|
|
|
})
|
|
|
|
| null;
|
2023-03-23 10:25:17 -05:00
|
|
|
|
|
|
|
const memberHasAccess = collectionIsAccessible?.members.some(
|
2023-03-28 02:31:50 -05:00
|
|
|
(e: UsersAndCollections) => e.userId === userId && e.canDelete
|
2023-03-23 10:25:17 -05:00
|
|
|
);
|
|
|
|
|
2023-03-28 02:31:50 -05:00
|
|
|
if (!(collectionIsAccessible?.ownerId === userId || memberHasAccess))
|
|
|
|
return { response: "Collection is not accessible.", status: 401 };
|
2023-03-23 10:25:17 -05:00
|
|
|
|
|
|
|
const deleteLink: Link = await prisma.link.delete({
|
|
|
|
where: {
|
2023-10-22 23:28:39 -05:00
|
|
|
id: linkId,
|
2023-03-23 10:25:17 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-10-22 23:28:39 -05:00
|
|
|
removeFile({
|
|
|
|
filePath: `archives/${collectionIsAccessible?.id}/${linkId}.pdf`,
|
|
|
|
});
|
|
|
|
removeFile({
|
|
|
|
filePath: `archives/${collectionIsAccessible?.id}/${linkId}.png`,
|
|
|
|
});
|
2023-10-29 23:30:45 -05:00
|
|
|
removeFile({
|
2023-10-29 23:50:43 -05:00
|
|
|
filePath: `archives/${collectionIsAccessible?.id}/${linkId}_readability.json`,
|
2023-10-29 23:30:45 -05:00
|
|
|
});
|
2023-03-25 09:17:34 -05:00
|
|
|
|
2023-03-28 02:31:50 -05:00
|
|
|
return { response: deleteLink, status: 200 };
|
2023-03-23 10:25:17 -05:00
|
|
|
}
|