diff options
Diffstat (limited to 'packages/trpc/routers/bookmarks.ts')
| -rw-r--r-- | packages/trpc/routers/bookmarks.ts | 55 |
1 files changed, 43 insertions, 12 deletions
diff --git a/packages/trpc/routers/bookmarks.ts b/packages/trpc/routers/bookmarks.ts index d2aa36bb..e685d5c2 100644 --- a/packages/trpc/routers/bookmarks.ts +++ b/packages/trpc/routers/bookmarks.ts @@ -665,8 +665,13 @@ export const bookmarksAppRouter = router({ tagName: z.string().optional(), }), ), - // Detach by tag ids - detach: z.array(z.object({ tagId: z.string() })), + detach: z.array( + z.object({ + // At least one of the two must be set + tagId: z.string().optional(), + tagName: z.string().optional(), // Also allow removing by tagName, to make CLI usage easier + }), + ), }), ) .output( @@ -679,23 +684,49 @@ export const bookmarksAppRouter = router({ .mutation(async ({ input, ctx }) => { return ctx.db.transaction(async (tx) => { // Detaches + const idsToRemove: string[] = []; if (input.detach.length > 0) { - await tx.delete(tagsOnBookmarks).where( - and( - eq(tagsOnBookmarks.bookmarkId, input.bookmarkId), - inArray( - tagsOnBookmarks.tagId, - input.detach.map((t) => t.tagId), + const namesToRemove: string[] = []; + input.detach.forEach((detachInfo) => { + if (detachInfo.tagId) { + idsToRemove.push(detachInfo.tagId); + } + if (detachInfo.tagName) { + namesToRemove.push(detachInfo.tagName); + } + }); + + if (namesToRemove.length > 0) { + ( + await tx.query.bookmarkTags.findMany({ + where: and( + eq(bookmarkTags.userId, ctx.user.id), + inArray(bookmarkTags.name, namesToRemove), + ), + columns: { + id: true, + }, + }) + ).forEach((tag) => { + idsToRemove.push(tag.id); + }); + } + + await tx + .delete(tagsOnBookmarks) + .where( + and( + eq(tagsOnBookmarks.bookmarkId, input.bookmarkId), + inArray(tagsOnBookmarks.tagId, idsToRemove), ), - ), - ); + ); } if (input.attach.length == 0) { return { bookmarkId: input.bookmarkId, attached: [], - detached: input.detach.map((t) => t.tagId), + detached: idsToRemove, }; } @@ -751,7 +782,7 @@ export const bookmarksAppRouter = router({ return { bookmarkId: input.bookmarkId, attached: allIds, - detached: input.detach.map((t) => t.tagId), + detached: idsToRemove, }; }); }), |
