2023-02-18 21:32:02 -06:00
|
|
|
import { prisma } from "@/lib/api/db";
|
2023-06-16 07:55:21 -05:00
|
|
|
import { CollectionIncludingMembersAndLinkCount } from "@/types/global";
|
2023-07-01 09:11:39 -05:00
|
|
|
import createFolder from "@/lib/api/storage/createFolder";
|
2023-02-08 15:11:33 -06:00
|
|
|
|
2023-06-09 17:31:14 -05:00
|
|
|
export default async function postCollection(
|
2023-06-16 07:55:21 -05:00
|
|
|
collection: CollectionIncludingMembersAndLinkCount,
|
2023-05-26 23:11:29 -05:00
|
|
|
userId: number
|
|
|
|
) {
|
2023-04-25 07:39:46 -05:00
|
|
|
if (!collection || collection.name.trim() === "")
|
2023-03-28 02:31:50 -05:00
|
|
|
return {
|
2023-04-24 16:30:40 -05:00
|
|
|
response: "Please enter a valid collection.",
|
2023-03-28 02:31:50 -05:00
|
|
|
status: 400,
|
|
|
|
};
|
2023-02-08 15:11:33 -06:00
|
|
|
|
2023-03-28 02:31:50 -05:00
|
|
|
const findCollection = await prisma.user.findUnique({
|
2023-02-08 15:11:33 -06:00
|
|
|
where: {
|
2023-03-28 02:31:50 -05:00
|
|
|
id: userId,
|
2023-02-08 15:11:33 -06:00
|
|
|
},
|
2023-02-13 18:09:13 -06:00
|
|
|
select: {
|
2023-02-08 15:11:33 -06:00
|
|
|
collections: {
|
|
|
|
where: {
|
2023-04-24 16:30:40 -05:00
|
|
|
name: collection.name,
|
2023-02-08 15:11:33 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const checkIfCollectionExists = findCollection?.collections[0];
|
|
|
|
|
2023-03-28 02:31:50 -05:00
|
|
|
if (checkIfCollectionExists)
|
|
|
|
return { response: "Collection already exists.", status: 400 };
|
2023-02-08 15:11:33 -06:00
|
|
|
|
2023-03-08 15:31:24 -06:00
|
|
|
const newCollection = await prisma.collection.create({
|
2023-02-08 15:11:33 -06:00
|
|
|
data: {
|
2023-02-18 21:32:02 -06:00
|
|
|
owner: {
|
|
|
|
connect: {
|
2023-03-28 02:31:50 -05:00
|
|
|
id: userId,
|
2023-02-18 21:32:02 -06:00
|
|
|
},
|
2023-02-08 15:11:33 -06:00
|
|
|
},
|
2023-07-23 13:27:12 -05:00
|
|
|
name: collection.name.trim(),
|
2023-04-24 16:30:40 -05:00
|
|
|
description: collection.description,
|
2023-06-02 06:59:52 -05:00
|
|
|
color: collection.color,
|
2023-04-26 15:40:48 -05:00
|
|
|
members: {
|
|
|
|
create: collection.members.map((e) => ({
|
2023-07-19 11:14:52 -05:00
|
|
|
user: { connect: { id: e.user.id } },
|
2023-04-26 15:40:48 -05:00
|
|
|
canCreate: e.canCreate,
|
|
|
|
canUpdate: e.canUpdate,
|
|
|
|
canDelete: e.canDelete,
|
|
|
|
})),
|
|
|
|
},
|
2023-02-08 15:11:33 -06:00
|
|
|
},
|
2023-05-01 15:00:23 -05:00
|
|
|
include: {
|
2023-06-16 07:55:21 -05:00
|
|
|
_count: {
|
|
|
|
select: { links: true },
|
|
|
|
},
|
2023-05-01 15:00:23 -05:00
|
|
|
members: {
|
|
|
|
include: {
|
|
|
|
user: {
|
|
|
|
select: {
|
2023-07-08 05:35:43 -05:00
|
|
|
username: true,
|
2023-05-01 15:00:23 -05:00
|
|
|
name: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-02-08 15:11:33 -06:00
|
|
|
});
|
|
|
|
|
2023-07-01 09:11:39 -05:00
|
|
|
createFolder({ filePath: `archives/${newCollection.id}` });
|
2023-03-08 15:31:24 -06:00
|
|
|
|
2023-03-28 02:31:50 -05:00
|
|
|
return { response: newCollection, status: 200 };
|
2023-02-08 15:11:33 -06:00
|
|
|
}
|