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

31 lines
961 B
TypeScript
Raw Normal View History

2023-03-23 10:25:17 -05:00
import { prisma } from "@/lib/api/db";
2024-02-10 00:37:48 -06:00
import { Link, UsersAndCollections } from "@prisma/client";
import getPermission from "@/lib/api/getPermission";
2024-04-08 18:35:06 -05:00
import { removeFiles } from "@/lib/api/manageLinkFiles";
2023-03-23 10:25:17 -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-11-19 15:22:27 -06:00
const collectionIsAccessible = await getPermission({ userId, linkId });
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
);
2024-04-08 18:35:06 -05:00
if (
!collectionIsAccessible ||
!(collectionIsAccessible?.ownerId === userId || memberHasAccess)
)
2023-03-28 02:31:50 -05:00
return { response: "Collection is not accessible.", status: 401 };
2023-03-23 10:25:17 -05:00
const deleteLink: Link = await prisma.link.delete({
where: {
id: linkId,
2023-03-23 10:25:17 -05:00
},
});
2024-04-08 18:35:06 -05:00
removeFiles(linkId, collectionIsAccessible.id);
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
}