2023-02-24 11:32:28 -06:00
|
|
|
import { prisma } from "@/lib/api/db";
|
2023-06-14 17:34:54 -05:00
|
|
|
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
|
2023-07-01 09:11:39 -05:00
|
|
|
import getTitle from "@/lib/api/getTitle";
|
|
|
|
import archive from "@/lib/api/archive";
|
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-18 10:59:57 -05:00
|
|
|
import createFolder from "@/lib/api/storage/createFolder";
|
2023-02-24 11:32:28 -06:00
|
|
|
|
2023-06-09 17:31:14 -05:00
|
|
|
export default async function postLink(
|
2023-06-14 17:34:54 -05:00
|
|
|
link: LinkIncludingShortenedCollectionAndTags,
|
2023-05-27 11:29:39 -05:00
|
|
|
userId: number
|
|
|
|
) {
|
2023-03-28 02:31:50 -05:00
|
|
|
link.collection.name = link.collection.name.trim();
|
2023-02-24 11:32:28 -06:00
|
|
|
|
|
|
|
if (!link.name) {
|
2023-06-24 16:54:35 -05:00
|
|
|
return { response: "Please enter a valid name for the link.", status: 400 };
|
2023-03-28 02:31:50 -05:00
|
|
|
} else if (!link.collection.name) {
|
2023-07-18 11:06:47 -05:00
|
|
|
link.collection.name = "Unnamed Collection";
|
2023-02-24 11:32:28 -06:00
|
|
|
}
|
|
|
|
|
2023-05-27 11:29:39 -05:00
|
|
|
if (link.collection.id) {
|
2023-06-24 16:54:35 -05:00
|
|
|
const collectionIsAccessible = (await getPermission(
|
2023-03-28 02:31:50 -05:00
|
|
|
userId,
|
|
|
|
link.collection.id
|
2023-06-24 16:54:35 -05:00
|
|
|
)) as
|
|
|
|
| (Collection & {
|
|
|
|
members: UsersAndCollections[];
|
|
|
|
})
|
|
|
|
| null;
|
2023-02-24 11:32:28 -06:00
|
|
|
|
2023-03-28 02:31:50 -05:00
|
|
|
const memberHasAccess = collectionIsAccessible?.members.some(
|
|
|
|
(e: UsersAndCollections) => e.userId === userId && e.canCreate
|
|
|
|
);
|
2023-02-24 11:32:28 -06:00
|
|
|
|
2023-03-28 02:31:50 -05:00
|
|
|
if (!(collectionIsAccessible?.ownerId === userId || memberHasAccess))
|
|
|
|
return { response: "Collection is not accessible.", status: 401 };
|
|
|
|
} else {
|
|
|
|
link.collection.ownerId = userId;
|
2023-02-24 11:32:28 -06:00
|
|
|
}
|
|
|
|
|
2023-06-20 09:39:03 -05:00
|
|
|
const description =
|
|
|
|
link.description && link.description !== ""
|
|
|
|
? link.description
|
|
|
|
: await getTitle(link.url);
|
2023-02-24 11:32:28 -06:00
|
|
|
|
2023-03-08 15:31:24 -06:00
|
|
|
const newLink: Link = await prisma.link.create({
|
2023-02-24 11:32:28 -06:00
|
|
|
data: {
|
|
|
|
name: link.name,
|
2023-03-05 15:03:20 -06:00
|
|
|
url: link.url,
|
2023-06-20 09:39:03 -05:00
|
|
|
description,
|
2023-02-24 11:32:28 -06:00
|
|
|
collection: {
|
2023-03-28 02:31:50 -05:00
|
|
|
connectOrCreate: {
|
|
|
|
where: {
|
|
|
|
name_ownerId: {
|
|
|
|
ownerId: link.collection.ownerId,
|
|
|
|
name: link.collection.name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
create: {
|
2023-07-23 13:27:12 -05:00
|
|
|
name: link.collection.name.trim(),
|
2023-03-28 02:31:50 -05:00
|
|
|
ownerId: userId,
|
|
|
|
},
|
2023-02-24 11:32:28 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
tags: {
|
2023-03-28 02:31:50 -05:00
|
|
|
connectOrCreate: link.tags.map((tag) => ({
|
2023-02-24 11:32:28 -06:00
|
|
|
where: {
|
2023-03-28 02:31:50 -05:00
|
|
|
name_ownerId: {
|
|
|
|
name: tag.name,
|
|
|
|
ownerId: link.collection.ownerId,
|
2023-02-24 11:32:28 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
create: {
|
2023-03-28 02:31:50 -05:00
|
|
|
name: tag.name,
|
|
|
|
owner: {
|
2023-02-24 11:32:28 -06:00
|
|
|
connect: {
|
2023-03-28 02:31:50 -05:00
|
|
|
id: link.collection.ownerId,
|
2023-02-24 11:32:28 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
},
|
2023-03-10 13:25:33 -06:00
|
|
|
include: { tags: true, collection: true },
|
2023-02-24 11:32:28 -06:00
|
|
|
});
|
|
|
|
|
2023-07-18 10:59:57 -05:00
|
|
|
createFolder({ filePath: `archives/${newLink.collectionId}` });
|
|
|
|
|
2023-06-13 09:30:52 -05:00
|
|
|
archive(newLink.url, newLink.collectionId, newLink.id);
|
2023-03-08 15:31:24 -06:00
|
|
|
|
2023-06-13 09:30:52 -05:00
|
|
|
return { response: newLink, status: 200 };
|
2023-02-24 11:32:28 -06:00
|
|
|
}
|