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";
|
2024-02-10 15:53:46 -06:00
|
|
|
import { UsersAndCollections } from "@prisma/client";
|
2023-04-26 15:40:48 -05:00
|
|
|
import getPermission from "@/lib/api/getPermission";
|
2024-09-12 14:03:14 -05:00
|
|
|
import { moveFiles, removeFiles } from "@/lib/api/manageLinkFiles";
|
|
|
|
import isValidUrl from "@/lib/shared/isValidUrl";
|
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-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
|
|
|
|
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
|
|
|
|
2024-09-04 21:19:40 -05:00
|
|
|
// If the user is part of a collection, they can pin it to their dashboard
|
|
|
|
if (canPinPermission && data.pinnedBy && data.pinnedBy[0]) {
|
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:
|
2024-09-04 21:19:40 -05:00
|
|
|
data?.pinnedBy && data.pinnedBy[0].id === userId
|
2024-02-07 08:48:40 -06:00
|
|
|
? { connect: { id: userId } }
|
|
|
|
: { disconnect: { id: userId } },
|
2024-02-04 23:43:59 -06:00
|
|
|
},
|
|
|
|
include: {
|
|
|
|
collection: true,
|
|
|
|
pinnedBy: isCollectionOwner
|
|
|
|
? {
|
2024-02-10 18:34:25 -06:00
|
|
|
where: { id: userId },
|
|
|
|
select: { id: true },
|
|
|
|
}
|
2024-02-04 23:43:59 -06:00
|
|
|
: undefined,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-09-04 21:19:40 -05:00
|
|
|
return { response: updatedLink, status: 200 };
|
2024-02-04 23:43:59 -06:00
|
|
|
}
|
|
|
|
|
2024-03-05 11:11:40 -06:00
|
|
|
const targetCollectionIsAccessible = await getPermission({
|
|
|
|
userId,
|
|
|
|
collectionId: data.collection.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
const memberHasAccess = collectionIsAccessible?.members.some(
|
|
|
|
(e: UsersAndCollections) => e.userId === userId && e.canUpdate
|
|
|
|
);
|
|
|
|
|
|
|
|
const targetCollectionMatchesData = data.collection.id
|
|
|
|
? data.collection.id === targetCollectionIsAccessible?.id
|
|
|
|
: true && data.collection.name
|
|
|
|
? data.collection.name === targetCollectionIsAccessible?.name
|
|
|
|
: true && data.collection.ownerId
|
|
|
|
? data.collection.ownerId === targetCollectionIsAccessible?.ownerId
|
|
|
|
: true;
|
|
|
|
|
2024-07-18 09:46:21 -05:00
|
|
|
if (!targetCollectionMatchesData)
|
2024-03-05 11:11:40 -06:00
|
|
|
return {
|
|
|
|
response: "Target collection does not match the data.",
|
|
|
|
status: 401,
|
|
|
|
};
|
|
|
|
|
|
|
|
const unauthorizedSwitchCollection =
|
|
|
|
!isCollectionOwner && collectionIsAccessible?.id !== data.collection.id;
|
|
|
|
|
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 {
|
2024-09-12 14:03:14 -05:00
|
|
|
const oldLink = await prisma.link.findUnique({
|
|
|
|
where: {
|
|
|
|
id: linkId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-09-12 14:30:15 -05:00
|
|
|
if (
|
|
|
|
data.url &&
|
|
|
|
oldLink &&
|
|
|
|
oldLink?.url !== data.url &&
|
|
|
|
isValidUrl(data.url)
|
|
|
|
) {
|
2024-09-12 14:03:14 -05:00
|
|
|
await removeFiles(oldLink.id, oldLink.collectionId);
|
2024-09-12 14:30:15 -05:00
|
|
|
} else
|
|
|
|
return {
|
|
|
|
response: "Invalid URL.",
|
|
|
|
status: 401,
|
|
|
|
};
|
2024-09-12 14:03:14 -05:00
|
|
|
|
2023-06-24 16:54:35 -05:00
|
|
|
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,
|
2024-09-12 14:03:14 -05:00
|
|
|
url: data.url,
|
2023-10-22 23:28:39 -05:00
|
|
|
description: data.description,
|
2024-08-24 14:50:29 -05:00
|
|
|
icon: data.icon,
|
|
|
|
iconWeight: data.iconWeight,
|
|
|
|
color: data.color,
|
2024-09-12 14:03:14 -05:00
|
|
|
image: oldLink?.url !== data.url ? null : undefined,
|
|
|
|
pdf: oldLink?.url !== data.url ? null : undefined,
|
|
|
|
readable: oldLink?.url !== data.url ? null : undefined,
|
|
|
|
monolith: oldLink?.url !== data.url ? null : undefined,
|
|
|
|
preview: oldLink?.url !== data.url ? null : undefined,
|
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:
|
2024-09-11 01:29:50 -05:00
|
|
|
data?.pinnedBy && data.pinnedBy[0]?.id === userId
|
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
|
|
|
|
2023-10-22 23:28:39 -05:00
|
|
|
if (collectionIsAccessible?.id !== data.collection.id) {
|
2024-04-08 18:35:06 -05:00
|
|
|
await moveFiles(linkId, collectionIsAccessible?.id, data.collection.id);
|
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
|
|
|
}
|