el.xwx.moe/lib/api/controllers/links/linkId/updateLinkById.ts

141 lines
3.9 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";
import { 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
2023-10-28 23:57:24 -05:00
export default async function updateLinkById(
userId: number,
linkId: number,
data: LinkIncludingShortenedCollectionAndTags
2023-05-27 11:29:39 -05:00
) {
if (!data || !data.collection.id)
2023-06-24 16:54:35 -05:00
return {
response: "Please choose a valid link and collection.",
status: 401,
};
2023-03-28 10:11:34 -05:00
2023-11-19 15:22:27 -06:00
const collectionIsAccessible = await getPermission({ userId, linkId });
2023-03-28 10:11:34 -05:00
const memberHasAccess = collectionIsAccessible?.members.some(
2023-06-24 16:54:35 -05:00
(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 =
collectionIsAccessible?.ownerId === data.collection.ownerId &&
data.collection.ownerId === userId;
2023-05-01 05:07:01 -05:00
const unauthorizedSwitchCollection =
!isCollectionOwner && collectionIsAccessible?.id !== data.collection.id;
2023-08-21 23:43:34 -05:00
2024-02-07 08:48:40 -06:00
const canPinPermission = collectionIsAccessible?.members.some(
(e: UsersAndCollections) => e.userId === userId
);
// If the user is able to create a link, they can pin it to their dashboard only.
2024-02-07 08:48:40 -06:00
if (canPinPermission) {
const updatedLink = await prisma.link.update({
where: {
id: linkId,
},
data: {
2024-02-07 08:48:40 -06:00
pinnedBy:
data?.pinnedBy && data.pinnedBy[0]
? { connect: { id: userId } }
: { disconnect: { id: userId } },
},
include: {
collection: true,
pinnedBy: isCollectionOwner
? {
2024-02-10 18:34:25 -06:00
where: { id: userId },
select: { id: true },
}
: undefined,
},
});
return { response: updatedLink, status: 200 };
}
2024-02-07 08:48:40 -06:00
// Makes sure collection members (non-owners) cannot move a link to/from a collection.
if (unauthorizedSwitchCollection)
return {
response: "You can't move a link to/from a collection you don't own.",
status: 401,
};
else if (collectionIsAccessible?.ownerId !== userId && !memberHasAccess)
2023-06-24 16:54:35 -05:00
return {
response: "Collection is not accessible.",
status: 401,
};
else {
const updatedLink = await prisma.link.update({
where: {
id: linkId,
2023-03-28 10:11:34 -05:00
},
2023-06-24 16:54:35 -05:00
data: {
name: data.name,
description: data.description,
collection: {
connect: {
id: data.collection.id,
},
},
2023-06-24 16:54:35 -05:00
tags: {
set: [],
connectOrCreate: data.tags.map((tag) => ({
2023-06-24 16:54:35 -05:00
where: {
name_ownerId: {
name: tag.name,
ownerId: data.collection.ownerId,
2023-06-24 16:54:35 -05:00
},
2023-03-28 10:11:34 -05:00
},
2023-06-24 16:54:35 -05:00
create: {
name: tag.name,
owner: {
connect: {
id: data.collection.ownerId,
2023-06-24 16:54:35 -05:00
},
2023-03-28 10:11:34 -05:00
},
},
2023-06-24 16:54:35 -05:00
})),
},
pinnedBy:
data?.pinnedBy && data.pinnedBy[0]
2023-06-24 16:54:35 -05:00
? { 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
? {
2024-02-10 18:34:25 -06:00
where: { id: userId },
select: { id: true },
}
2023-06-24 16:54:35 -05:00
: 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 (collectionIsAccessible?.id !== data.collection.id) {
2023-08-05 23:58:18 -05:00
await moveFile(
`archives/${collectionIsAccessible?.id}/${linkId}.pdf`,
`archives/${data.collection.id}/${linkId}.pdf`
2023-08-05 23:58:18 -05:00
);
await moveFile(
`archives/${collectionIsAccessible?.id}/${linkId}.png`,
`archives/${data.collection.id}/${linkId}.png`
2023-08-05 23:58:18 -05:00
);
await moveFile(
2023-10-29 23:50:43 -05:00
`archives/${collectionIsAccessible?.id}/${linkId}_readability.json`,
`archives/${data.collection.id}/${linkId}_readability.json`
);
2023-08-05 23:58:18 -05:00
}
2023-06-24 16:54:35 -05:00
return { response: updatedLink, status: 200 };
}
2023-03-28 10:11:34 -05:00
}