@@ -1,26 +1,16 @@
|
||||
import { prisma } from "@/lib/api/db";
|
||||
import { CollectionIncludingMembersAndLinkCount } from "@/types/global";
|
||||
import createFolder from "@/lib/api/storage/createFolder";
|
||||
import {
|
||||
PostCollectionSchema,
|
||||
PostCollectionSchemaType,
|
||||
} from "@/lib/shared/schemaValidation";
|
||||
|
||||
export default async function postCollection(
|
||||
body: PostCollectionSchemaType,
|
||||
collection: CollectionIncludingMembersAndLinkCount,
|
||||
userId: number
|
||||
) {
|
||||
const dataValidation = PostCollectionSchema.safeParse(body);
|
||||
|
||||
if (!dataValidation.success) {
|
||||
if (!collection || collection.name.trim() === "")
|
||||
return {
|
||||
response: `Error: ${
|
||||
dataValidation.error.issues[0].message
|
||||
} [${dataValidation.error.issues[0].path.join(", ")}]`,
|
||||
response: "Please enter a valid collection.",
|
||||
status: 400,
|
||||
};
|
||||
}
|
||||
|
||||
const collection = dataValidation.data;
|
||||
|
||||
if (collection.parentId) {
|
||||
const findParentCollection = await prisma.collection.findUnique({
|
||||
@@ -44,11 +34,14 @@ export default async function postCollection(
|
||||
|
||||
const newCollection = await prisma.collection.create({
|
||||
data: {
|
||||
owner: {
|
||||
connect: {
|
||||
id: userId,
|
||||
},
|
||||
},
|
||||
name: collection.name.trim(),
|
||||
description: collection.description,
|
||||
color: collection.color,
|
||||
icon: collection.icon,
|
||||
iconWeight: collection.iconWeight,
|
||||
parent: collection.parentId
|
||||
? {
|
||||
connect: {
|
||||
@@ -56,16 +49,6 @@ export default async function postCollection(
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
owner: {
|
||||
connect: {
|
||||
id: userId,
|
||||
},
|
||||
},
|
||||
createdBy: {
|
||||
connect: {
|
||||
id: userId,
|
||||
},
|
||||
},
|
||||
},
|
||||
include: {
|
||||
_count: {
|
||||
|
||||
Reference in New Issue
Block a user