el.xwx.moe/lib/api/controllers/links/updateLink.ts

118 lines
3.1 KiB
TypeScript
Raw Normal View History

2023-03-28 10:11:34 -05:00
import { prisma } from "@/lib/api/db";
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
2023-06-24 16:54:35 -05:00
import { Collection, Link, UsersAndCollections } from "@prisma/client";
import getPermission from "@/lib/api/getPermission";
2023-08-05 23:58:18 -05:00
import moveFile from "@/lib/api/storage/moveFile";
2023-03-28 10:11:34 -05:00
export default async function updateLink(
link: LinkIncludingShortenedCollectionAndTags,
2023-05-27 11:29:39 -05:00
userId: number
) {
console.log(link);
2023-06-24 16:54:35 -05:00
if (!link || !link.collection.id)
return {
response: "Please choose a valid link and collection.",
status: 401,
};
2023-03-28 10:11:34 -05:00
2023-06-24 16:54:35 -05:00
const targetLink = (await getPermission(
userId,
link.collection.id,
link.id
)) as
| (Link & {
collection: Collection & {
members: UsersAndCollections[];
};
})
| null;
2023-03-28 10:11:34 -05:00
2023-06-24 16:54:35 -05:00
const memberHasAccess = targetLink?.collection.members.some(
(e: UsersAndCollections) => e.userId === userId && e.canUpdate
);
2023-05-01 05:07:01 -05:00
2023-06-24 16:54:35 -05:00
const isCollectionOwner =
targetLink?.collection.ownerId === link.collection.ownerId &&
link.collection.ownerId === userId;
2023-05-01 05:07:01 -05:00
const unauthorizedSwitchCollection =
!isCollectionOwner && targetLink?.collection.id !== link.collection.id;
2023-08-21 23:43:34 -05:00
console.log(isCollectionOwner);
2023-08-21 23:43:34 -05:00
2023-06-24 16:54:35 -05:00
// Makes sure collection members (non-owners) cannot move a link to/from a collection.
if (unauthorizedSwitchCollection)
2023-06-24 16:54:35 -05:00
return {
response: "You can't move a link to/from a collection you don't own.",
status: 401,
};
else if (targetLink?.collection.ownerId !== userId && !memberHasAccess)
return {
response: "Collection is not accessible.",
status: 401,
};
else {
const updatedLink = await prisma.link.update({
where: {
id: link.id,
2023-03-28 10:11:34 -05:00
},
2023-06-24 16:54:35 -05:00
data: {
name: link.name,
description: link.description,
collection: {
connect: {
id: link.collection.id,
},
},
2023-06-24 16:54:35 -05:00
tags: {
set: [],
connectOrCreate: link.tags.map((tag) => ({
where: {
name_ownerId: {
name: tag.name,
ownerId: link.collection.ownerId,
},
2023-03-28 10:11:34 -05:00
},
2023-06-24 16:54:35 -05:00
create: {
name: tag.name,
owner: {
connect: {
id: link.collection.ownerId,
},
2023-03-28 10:11:34 -05:00
},
},
2023-06-24 16:54:35 -05:00
})),
},
pinnedBy:
link?.pinnedBy && link.pinnedBy[0]
? { connect: { id: userId } }
: { disconnect: { id: userId } },
2023-03-28 10:11:34 -05:00
},
2023-06-24 16:54:35 -05:00
include: {
tags: true,
collection: true,
pinnedBy: isCollectionOwner
? {
where: { id: userId },
select: { id: true },
}
: undefined,
2023-06-13 14:19:37 -05:00
},
2023-06-24 16:54:35 -05:00
});
2023-03-28 10:11:34 -05:00
if (targetLink?.collection.id !== link.collection.id) {
2023-08-05 23:58:18 -05:00
await moveFile(
`archives/${targetLink?.collection.id}/${link.id}.pdf`,
2023-08-05 23:58:18 -05:00
`archives/${link.collection.id}/${link.id}.pdf`
);
await moveFile(
`archives/${targetLink?.collection.id}/${link.id}.png`,
2023-08-05 23:58:18 -05:00
`archives/${link.collection.id}/${link.id}.png`
);
}
2023-06-24 16:54:35 -05:00
return { response: updatedLink, status: 200 };
}
2023-03-28 10:11:34 -05:00
}