2023-03-28 10:11:34 -05:00
|
|
|
import { prisma } from "@/lib/api/db";
|
2023-06-14 17:34:54 -05:00
|
|
|
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
|
2023-06-24 16:54:35 -05:00
|
|
|
import { Collection, Link, UsersAndCollections } from "@prisma/client";
|
2023-04-26 15:40:48 -05:00
|
|
|
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(
|
2023-10-22 23:28:39 -05:00
|
|
|
userId: number,
|
|
|
|
linkId: number,
|
|
|
|
data: LinkIncludingShortenedCollectionAndTags
|
2023-05-27 11:29:39 -05:00
|
|
|
) {
|
2023-10-22 23:28: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
|
|
|
|
2023-10-22 23:28:39 -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 =
|
2023-10-22 23:28:39 -05:00
|
|
|
collectionIsAccessible?.ownerId === data.collection.ownerId &&
|
|
|
|
data.collection.ownerId === userId;
|
2023-05-01 05:07:01 -05:00
|
|
|
|
2023-08-28 13:03:06 -05:00
|
|
|
const unauthorizedSwitchCollection =
|
2023-10-22 23:28:39 -05:00
|
|
|
!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
|
|
|
|
);
|
2024-02-04 23:43:59 -06:00
|
|
|
|
|
|
|
// 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) {
|
2024-02-04 23:43:59 -06:00
|
|
|
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 } },
|
2024-02-04 23:43:59 -06:00
|
|
|
},
|
|
|
|
include: {
|
|
|
|
collection: true,
|
|
|
|
pinnedBy: isCollectionOwner
|
|
|
|
? {
|
2024-02-07 08:48:40 -06:00
|
|
|
where: { id: userId },
|
|
|
|
select: { id: true },
|
|
|
|
}
|
2024-02-04 23:43:59 -06:00
|
|
|
: 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,
|
|
|
|
};
|
2023-10-22 23:28:39 -05:00
|
|
|
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: {
|
2023-10-22 23:28:39 -05:00
|
|
|
id: linkId,
|
2023-03-28 10:11:34 -05:00
|
|
|
},
|
2023-06-24 16:54:35 -05:00
|
|
|
data: {
|
2023-10-22 23:28:39 -05:00
|
|
|
name: data.name,
|
|
|
|
description: data.description,
|
2023-08-28 13:03:06 -05:00
|
|
|
collection: {
|
|
|
|
connect: {
|
2023-10-22 23:28:39 -05:00
|
|
|
id: data.collection.id,
|
2023-08-28 13:03:06 -05:00
|
|
|
},
|
|
|
|
},
|
2023-06-24 16:54:35 -05:00
|
|
|
tags: {
|
|
|
|
set: [],
|
2023-10-22 23:28:39 -05:00
|
|
|
connectOrCreate: data.tags.map((tag) => ({
|
2023-06-24 16:54:35 -05:00
|
|
|
where: {
|
|
|
|
name_ownerId: {
|
|
|
|
name: tag.name,
|
2023-10-22 23:28:39 -05:00
|
|
|
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: {
|
2023-10-22 23:28:39 -05:00
|
|
|
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:
|
2023-10-22 23:28:39 -05:00
|
|
|
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-07 08:48:40 -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
|
|
|
|
2023-10-22 23:28:39 -05:00
|
|
|
if (collectionIsAccessible?.id !== data.collection.id) {
|
2023-08-05 23:58:18 -05:00
|
|
|
await moveFile(
|
2023-10-22 23:28:39 -05:00
|
|
|
`archives/${collectionIsAccessible?.id}/${linkId}.pdf`,
|
|
|
|
`archives/${data.collection.id}/${linkId}.pdf`
|
2023-08-05 23:58:18 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
await moveFile(
|
2023-10-22 23:28:39 -05:00
|
|
|
`archives/${collectionIsAccessible?.id}/${linkId}.png`,
|
|
|
|
`archives/${data.collection.id}/${linkId}.png`
|
2023-08-05 23:58:18 -05:00
|
|
|
);
|
2023-10-29 23:30:45 -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-10-29 23:30:45 -05:00
|
|
|
);
|
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
|
|
|
}
|