Skip to content

Commit

Permalink
[Bug Report] Importing bookmarks adds all tags to all bookmarks hoard…
Browse files Browse the repository at this point in the history
…er-app#421

fixed an issue that caused all existing tags to be assigned to a new bookmark
  • Loading branch information
kamtschatka committed Sep 25, 2024
1 parent e1feece commit b21f2f7
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions packages/trpc/routers/bookmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -767,36 +767,42 @@ export const bookmarksAppRouter = router({
.returning();
}

const allIds = (
await tx.query.bookmarkTags.findMany({
where: and(
eq(bookmarkTags.userId, ctx.user.id),
or(
toAddTagIds.length > 0
? inArray(bookmarkTags.id, toAddTagIds)
: undefined,
toAddTagNames.length > 0
? inArray(bookmarkTags.name, toAddTagNames)
: undefined,
let allIds: string[] = [];

// If there is nothing to add, the "or" statement will become useless and
// the query below will simply select all the existing tags for this user and assign them to the bookmark
if (toAddTagNames.length > 0 || toAddTagIds.length > 0) {
allIds = (
await tx.query.bookmarkTags.findMany({
where: and(
eq(bookmarkTags.userId, ctx.user.id),
or(
toAddTagIds.length > 0
? inArray(bookmarkTags.id, toAddTagIds)
: undefined,
toAddTagNames.length > 0
? inArray(bookmarkTags.name, toAddTagNames)
: undefined,
),
),
),
columns: {
id: true,
},
})
).map((t) => t.id);

await tx
.insert(tagsOnBookmarks)
.values(
allIds.map((i) => ({
tagId: i,
bookmarkId: input.bookmarkId,
attachedBy: "human" as const,
userId: ctx.user.id,
})),
)
.onConflictDoNothing();
columns: {
id: true,
},
})
).map((t) => t.id);

await tx
.insert(tagsOnBookmarks)
.values(
allIds.map((i) => ({
tagId: i,
bookmarkId: input.bookmarkId,
attachedBy: "human" as const,
userId: ctx.user.id,
})),
)
.onConflictDoNothing();
}
await triggerSearchReindex(input.bookmarkId);
return {
bookmarkId: input.bookmarkId,
Expand Down

0 comments on commit b21f2f7

Please sign in to comment.