el.xwx.moe/lib/api/controllers/links/bulk/updateLinks.ts

51 lines
1.4 KiB
TypeScript
Raw Normal View History

import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
import updateLinkById from "../linkId/updateLinkById";
2024-02-10 18:34:25 -06:00
export default async function updateLinks(
userId: number,
links: LinkIncludingShortenedCollectionAndTags[],
2024-02-11 01:21:25 -06:00
removePreviousTags: boolean,
2024-02-10 18:34:25 -06:00
newData: Pick<
LinkIncludingShortenedCollectionAndTags,
"tags" | "collectionId"
>
) {
let allUpdatesSuccessful = true;
2024-02-10 18:34:25 -06:00
// Have to use a loop here rather than updateMany, see the following:
// https://github.com/prisma/prisma/issues/3143
for (const link of links) {
2024-02-11 01:21:25 -06:00
let updatedTags = [...link.tags, ...(newData.tags ?? [])];
if (removePreviousTags) {
// If removePreviousTags is true, replace the existing tags with new tags
updatedTags = [...(newData.tags ?? [])];
}
2024-02-10 18:34:25 -06:00
const updatedData: LinkIncludingShortenedCollectionAndTags = {
...link,
2024-02-11 01:21:25 -06:00
tags: updatedTags,
2024-02-10 18:34:25 -06:00
collection: {
...link.collection,
id: newData.collectionId ?? link.collection.id,
},
};
2024-02-10 18:34:25 -06:00
const updatedLink = await updateLinkById(
userId,
link.id as number,
updatedData
);
2024-02-10 18:34:25 -06:00
if (updatedLink.status !== 200) {
allUpdatesSuccessful = false;
}
}
2024-02-10 18:34:25 -06:00
if (allUpdatesSuccessful) {
return { response: "All links updated successfully", status: 200 };
} else {
return { response: "Some links failed to update", status: 400 };
}
}