Pass the entire link to the store & fix bulk update function

This commit is contained in:
Isaac Wise
2024-02-10 15:53:46 -06:00
parent da0533ac36
commit 582607e726
9 changed files with 52 additions and 107 deletions
@@ -0,0 +1,27 @@
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
import updateLinkById from "../linkId/updateLinkById";
// Need to fix this
export default async function updateLinks(userId: number, links: LinkIncludingShortenedCollectionAndTags[], newData: Pick<LinkIncludingShortenedCollectionAndTags, "tags" | "collectionId">) {
let allUpdatesSuccessful = true;
for (const link of links) {
const updatedData: LinkIncludingShortenedCollectionAndTags = {
...link,
tags: [...link.tags, ...(newData.tags ?? [])],
collectionId: newData.collectionId ?? link.collectionId,
}
const updatedLink = await updateLinkById(userId, link.id, updatedData);
if (updatedLink.status !== 200) {
allUpdatesSuccessful = false;
}
}
if (allUpdatesSuccessful) {
return { response: "All links updated successfully", status: 200 };
} else {
return { response: "Some links failed to update", status: 400 };
}
}
@@ -1,65 +0,0 @@
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
import { prisma } from "@/lib/api/db";
import getPermission from "@/lib/api/getPermission";
import { UsersAndCollections } from "@prisma/client";
// Need to fix this
export default async function updateLinksById(userId: number, linkIds: number[], data: LinkIncludingShortenedCollectionAndTags) {
if (!linkIds || linkIds.length === 0) {
return { response: "Please choose valid links.", status: 401 };
}
// Check if the user has access to the collection of each link
// if any of the links are not accessible, return an error
for (const linkId of linkIds) {
const linkIsAccessible = await getPermission({ userId, linkId });
const memberHasAccess = linkIsAccessible?.members.some(
(e: UsersAndCollections) => e.userId === userId && e.canUpdate
);
if (!(linkIsAccessible?.ownerId === userId || memberHasAccess)) {
return { response: "Link is not accessible.", status: 401 };
}
}
const updateData = {
collection: {
connect: {
id: data.collection.id,
},
},
tags: {
set: [],
connectOrCreate: data.tags.map((tag) => ({
where: {
name_ownerId: {
name: tag.name,
ownerId: data.collection.ownerId,
},
},
create: {
name: tag.name,
owner: {
connect: {
id: data.collection.ownerId,
},
},
},
})),
},
include: {
tags: true,
collection: true,
}
};
const updatedLinks = await prisma.link.updateMany({
where: {
id: { in: linkIds },
},
data: updateData,
});
return { response: updatedLinks, status: 200 };
}
@@ -1,6 +1,6 @@
import { prisma } from "@/lib/api/db";
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
import { Collection, Link, UsersAndCollections } from "@prisma/client";
import { UsersAndCollections } from "@prisma/client";
import getPermission from "@/lib/api/getPermission";
import moveFile from "@/lib/api/storage/moveFile";
@@ -48,9 +48,9 @@ export default async function updateLinkById(
collection: true,
pinnedBy: isCollectionOwner
? {
where: { id: userId },
select: { id: true },
}
where: { id: userId },
select: { id: true },
}
: undefined,
},
});
@@ -111,9 +111,9 @@ export default async function updateLinkById(
collection: true,
pinnedBy: isCollectionOwner
? {
where: { id: userId },
select: { id: true },
}
where: { id: userId },
select: { id: true },
}
: undefined,
},
});