el.xwx.moe/lib/api/controllers/collections/collectionId/updateCollectionById.ts
2024-02-26 23:59:10 -05:00

108 lines
2.5 KiB
TypeScript

import { prisma } from "@/lib/api/db";
import { CollectionIncludingMembersAndLinkCount } from "@/types/global";
import getPermission from "@/lib/api/getPermission";
export default async function updateCollection(
userId: number,
collectionId: number,
data: CollectionIncludingMembersAndLinkCount
) {
if (!collectionId)
return { response: "Please choose a valid collection.", status: 401 };
const collectionIsAccessible = await getPermission({
userId,
collectionId,
});
if (!(collectionIsAccessible?.ownerId === userId))
return { response: "Collection is not accessible.", status: 401 };
if (data.parentId) {
const findParentCollection = await prisma.collection.findUnique({
where: {
id: data.parentId,
},
select: {
ownerId: true,
},
});
if (
findParentCollection?.ownerId !== userId ||
typeof data.parentId !== "number"
)
return {
response: "You are not authorized to create a sub-collection here.",
status: 403,
};
}
const updatedCollection = await prisma.$transaction(async () => {
await prisma.usersAndCollections.deleteMany({
where: {
collection: {
id: collectionId,
},
},
});
await prisma.user.update({
where: {
id: userId,
},
data: {
collectionOrder: {
push: collectionId,
},
},
});
return await prisma.collection.update({
where: {
id: collectionId,
},
data: {
name: data.name.trim(),
description: data.description,
color: data.color,
isPublic: data.isPublic,
parent: data.parentId
? {
connect: {
id: data.parentId,
},
}
: undefined,
members: {
create: data.members.map((e) => ({
user: { connect: { id: e.user.id || e.userId } },
canCreate: e.canCreate,
canUpdate: e.canUpdate,
canDelete: e.canDelete,
})),
},
},
include: {
_count: {
select: { links: true },
},
members: {
include: {
user: {
select: {
image: true,
username: true,
name: true,
id: true,
},
},
},
},
},
});
});
return { response: updatedCollection, status: 200 };
}