@@ -1,31 +1,15 @@
|
||||
import { prisma } from "@/lib/api/db";
|
||||
import { CollectionIncludingMembersAndLinkCount } from "@/types/global";
|
||||
import getPermission from "@/lib/api/getPermission";
|
||||
import {
|
||||
UpdateCollectionSchema,
|
||||
UpdateCollectionSchemaType,
|
||||
} from "@/lib/shared/schemaValidation";
|
||||
|
||||
export default async function updateCollection(
|
||||
userId: number,
|
||||
collectionId: number,
|
||||
body: UpdateCollectionSchemaType
|
||||
data: CollectionIncludingMembersAndLinkCount
|
||||
) {
|
||||
if (!collectionId)
|
||||
return { response: "Please choose a valid collection.", status: 401 };
|
||||
|
||||
const dataValidation = UpdateCollectionSchema.safeParse(body);
|
||||
|
||||
if (!dataValidation.success) {
|
||||
return {
|
||||
response: `Error: ${
|
||||
dataValidation.error.issues[0].message
|
||||
} [${dataValidation.error.issues[0].path.join(", ")}]`,
|
||||
status: 400,
|
||||
};
|
||||
}
|
||||
|
||||
const data = dataValidation.data;
|
||||
|
||||
const collectionIsAccessible = await getPermission({
|
||||
userId,
|
||||
collectionId,
|
||||
@@ -34,8 +18,10 @@ export default async function updateCollection(
|
||||
if (!(collectionIsAccessible?.ownerId === userId))
|
||||
return { response: "Collection is not accessible.", status: 401 };
|
||||
|
||||
console.log(data);
|
||||
|
||||
if (data.parentId) {
|
||||
if (data.parentId !== "root") {
|
||||
if (data.parentId !== ("root" as any)) {
|
||||
const findParentCollection = await prisma.collection.findUnique({
|
||||
where: {
|
||||
id: data.parentId,
|
||||
@@ -58,12 +44,6 @@ export default async function updateCollection(
|
||||
}
|
||||
}
|
||||
|
||||
const uniqueMembers = data.members.filter(
|
||||
(e, i, a) =>
|
||||
a.findIndex((el) => el.userId === e.userId) === i &&
|
||||
e.userId !== collectionIsAccessible.ownerId
|
||||
);
|
||||
|
||||
const updatedCollection = await prisma.$transaction(async () => {
|
||||
await prisma.usersAndCollections.deleteMany({
|
||||
where: {
|
||||
@@ -81,24 +61,22 @@ export default async function updateCollection(
|
||||
name: data.name.trim(),
|
||||
description: data.description,
|
||||
color: data.color,
|
||||
icon: data.icon,
|
||||
iconWeight: data.iconWeight,
|
||||
isPublic: data.isPublic,
|
||||
parent:
|
||||
data.parentId && data.parentId !== "root"
|
||||
data.parentId && data.parentId !== ("root" as any)
|
||||
? {
|
||||
connect: {
|
||||
id: data.parentId,
|
||||
},
|
||||
}
|
||||
: data.parentId === "root"
|
||||
: data.parentId === ("root" as any)
|
||||
? {
|
||||
disconnect: true,
|
||||
}
|
||||
: undefined,
|
||||
members: {
|
||||
create: uniqueMembers.map((e) => ({
|
||||
user: { connect: { id: e.userId } },
|
||||
create: data.members.map((e) => ({
|
||||
user: { connect: { id: e.user.id || e.userId } },
|
||||
canCreate: e.canCreate,
|
||||
canUpdate: e.canUpdate,
|
||||
canDelete: e.canDelete,
|
||||
|
||||
Reference in New Issue
Block a user