The minimalistic document archiving platform.
fix(tags): retreive tags affected even when only affected to deleted documents (#477)
Corentin Thomasseta62d3767729a
diff --git a/.changeset/lazy-tables-cover.md b/.changeset/lazy-tables-cover.mdnew file mode 100644index 00000000..00ee9245--- /dev/null+++ b/.changeset/lazy-tables-cover.md@@ -0,0 +1,5 @@+---+"@papra/app-server": patch+---++Fixed an issue where tags assigned to only deleted documents won't show up in the tag listdiff --git a/apps/papra-server/src/modules/tags/tags.repository.test.ts b/apps/papra-server/src/modules/tags/tags.repository.test.tsindex 3b1d1307..7ebe425b 100644--- a/apps/papra-server/src/modules/tags/tags.repository.test.ts+++ b/apps/papra-server/src/modules/tags/tags.repository.test.ts@@ -156,6 +156,25 @@ describe('tags repository', () => {},]);});++ test('when a tag is assigned to only deleted documents, it is retrieved with 0 documents count', async () => {+ const { db } = await createInMemoryDatabase({+ users: [{ id: 'user-1', email: '[email protected]' }],+ organizations: [{ id: 'organization-1', name: 'Organization 1' }],+ organizationMembers: [{ organizationId: 'organization-1', userId: 'user-1', role: ORGANIZATION_ROLES.OWNER }],+ documents: [+ { id: 'document-1', organizationId: 'organization-1', createdBy: 'user-1', name: 'Document 1', originalName: 'document-1.pdf', content: 'lorem ipsum', originalStorageKey: '', mimeType: 'application/pdf', originalSha256Hash: 'hash-1', isDeleted: true },+ ],+ tags: [{ id: 'tag-1', organizationId: 'organization-1', name: 'Tag 1', color: '#aa0000' }],+ documentsTags: [{ documentId: 'document-1', tagId: 'tag-1' }],+ });++ const tagsRepository = createTagsRepository({ db });++ const { tags } = await tagsRepository.getOrganizationTags({ organizationId: 'organization-1' });++ expect(tags.map(({ documentsCount, id }) => ({ documentsCount, id }))).to.eql([{ documentsCount: 0, id: 'tag-1' }]);+ });});describe('createTag', () => {diff --git a/apps/papra-server/src/modules/tags/tags.repository.ts b/apps/papra-server/src/modules/tags/tags.repository.tsindex 5602241b..c58ed7be 100644--- a/apps/papra-server/src/modules/tags/tags.repository.ts+++ b/apps/papra-server/src/modules/tags/tags.repository.ts@@ -1,7 +1,7 @@import type { Database } from '../app/database/database.types';import type { DbInsertableTag } from './tags.types';import { injectArguments, safely } from '@corentinth/chisels';-import { and, count, eq, getTableColumns, isNull, or } from 'drizzle-orm';+import { and, eq, getTableColumns, sql } from 'drizzle-orm';import { get } from 'lodash-es';import { documentsTable } from '../documents/documents.table';import { isUniqueConstraintError } from '../shared/db/constraints.models';@@ -32,20 +32,12 @@ async function getOrganizationTags({ organizationId, db }: { organizationId: strconst tags = await db.select({...getTableColumns(tagsTable),- documentsCount: count(documentsTagsTable.documentId),+ documentsCount: sql<number>`COUNT(${documentsTagsTable.documentId}) FILTER (WHERE ${documentsTable.isDeleted} = false)`.as('documentsCount'),}).from(tagsTable).leftJoin(documentsTagsTable, eq(tagsTable.id, documentsTagsTable.tagId)).leftJoin(documentsTable, eq(documentsTagsTable.documentId, documentsTable.id))- .where(- and(- eq(tagsTable.organizationId, organizationId),- or(- isNull(documentsTable.id),- eq(documentsTable.isDeleted, false),- ),- ),- )+ .where(eq(tagsTable.organizationId, organizationId)).groupBy(tagsTable.id);return { tags };