Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

fix(tags): exclude deleted documents from tags docs count (#234)

Corentin Thomasset21a2c95e56e0
diff --git a/apps/papra-server/src/modules/tags/tags.repository.test.ts b/apps/papra-server/src/modules/tags/tags.repository.test.ts
index 3a2352e0..e0b66d86 100644
--- a/apps/papra-server/src/modules/tags/tags.repository.test.ts
+++ b/apps/papra-server/src/modules/tags/tags.repository.test.ts
@@ -90,4 +90,65 @@ describe('tags repository', () => {
).rejects.toThrow(createDocumentAlreadyHasTagError());
});
});
+
+ describe('getOrganizationTags', () => {
+ test('retrieves all tags for an organization along with the number of non-deleted documents associated with each tag', 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' },
+ { id: 'document-2', organizationId: 'organization-1', createdBy: 'user-1', name: 'Document 2', originalName: 'document-2.pdf', content: 'lorem ipsum', originalStorageKey: '', mimeType: 'application/pdf', originalSha256Hash: 'hash-2', isDeleted: true },
+ ],
+ tags: [
+ { id: 'tag-1', organizationId: 'organization-1', name: 'Tag 1', color: '#aa0000', createdAt: new Date('2021-01-01'), updatedAt: new Date('2021-01-01') },
+ { id: 'tag-2', organizationId: 'organization-1', name: 'Tag 2', color: '#00aa00', createdAt: new Date('2021-01-02'), updatedAt: new Date('2021-01-02') },
+ { id: 'tag-3', organizationId: 'organization-1', name: 'Tag 3', color: '#0000aa', createdAt: new Date('2021-01-03'), updatedAt: new Date('2021-01-03') },
+ ],
+ documentsTags: [
+ { documentId: 'document-1', tagId: 'tag-1' },
+ { documentId: 'document-1', tagId: 'tag-2' },
+ { documentId: 'document-2', tagId: 'tag-1' },
+ ],
+ });
+
+ const tagsRepository = createTagsRepository({ db });
+
+ const { tags } = await tagsRepository.getOrganizationTags({ organizationId: 'organization-1' });
+
+ expect(tags).to.eql([
+ {
+ id: 'tag-1',
+ organizationId: 'organization-1',
+ name: 'Tag 1',
+ description: null,
+ color: '#aa0000',
+ documentsCount: 1,
+ createdAt: new Date('2021-01-01'),
+ updatedAt: new Date('2021-01-01'),
+ },
+ {
+ id: 'tag-2',
+ organizationId: 'organization-1',
+ name: 'Tag 2',
+ description: null,
+ color: '#00aa00',
+ documentsCount: 1,
+ createdAt: new Date('2021-01-02'),
+ updatedAt: new Date('2021-01-02'),
+ },
+ {
+ id: 'tag-3',
+ organizationId: 'organization-1',
+ name: 'Tag 3',
+ description: null,
+ color: '#0000aa',
+ documentsCount: 0,
+ createdAt: new Date('2021-01-03'),
+ updatedAt: new Date('2021-01-03'),
+ },
+ ]);
+ });
+ });
});
diff --git a/apps/papra-server/src/modules/tags/tags.repository.ts b/apps/papra-server/src/modules/tags/tags.repository.ts
index aa26016f..8c885471 100644
--- a/apps/papra-server/src/modules/tags/tags.repository.ts
+++ b/apps/papra-server/src/modules/tags/tags.repository.ts
@@ -1,8 +1,9 @@
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 } from 'drizzle-orm';
+import { and, count, eq, getTableColumns, isNull, or } from 'drizzle-orm';
import { get } from 'lodash-es';
+import { documentsTable } from '../documents/documents.table';
import { omitUndefined } from '../shared/utils';
import { createDocumentAlreadyHasTagError } from './tags.errors';
import { documentsTagsTable, tagsTable } from './tags.table';
@@ -33,8 +34,15 @@ async function getOrganizationTags({ organizationId, db }: { organizationId: str
})
.from(tagsTable)
.leftJoin(documentsTagsTable, eq(tagsTable.id, documentsTagsTable.tagId))
+ .leftJoin(documentsTable, eq(documentsTagsTable.documentId, documentsTable.id))
.where(
- eq(tagsTable.organizationId, organizationId),
+ and(
+ eq(tagsTable.organizationId, organizationId),
+ or(
+ isNull(documentsTable.id),
+ eq(documentsTable.isDeleted, false),
+ ),
+ ),
)
.groupBy(tagsTable.id);