fix(tags): retreive tags affected even when only affected to deleted documents (#477)

* fix(tags): retreive tags affected even when only affected to deleted documents

* Update apps/papra-server/src/modules/tags/tags.repository.test.ts

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update .changeset/lazy-tables-cover.md

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Corentin Thomasset
2025-08-21 13:50:48 +02:00
committed by GitHub
parent ea9d90d6cf
commit a62d376772
3 changed files with 27 additions and 11 deletions

View File

@@ -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 list

View File

@@ -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: 'user-1@example.com' }],
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', () => {

View File

@@ -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: str
const 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 };