Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(tags): add error handling for existing tags (#315)

Corentin Thomassetfdb90fa16452
diff --git a/apps/papra-client/src/locales/en.yml b/apps/papra-client/src/locales/en.yml
index 85487125..22b8f93d 100644
--- a/apps/papra-client/src/locales/en.yml
+++ b/apps/papra-client/src/locales/en.yml
@@ -498,6 +498,7 @@ api-errors.organization.invitation_already_exists: An invitation for this email
api-errors.user.already_in_organization: This user is already in this organization.
api-errors.user.organization_invitation_limit_reached: The maximum number of invitations has been reached for today. Please try again tomorrow.
api-errors.demo.not_available: This feature is not available in demo
+api-errors.tags.already_exists: A tag with this name already exists for this organization
# Not found
diff --git a/apps/papra-client/src/locales/fr.yml b/apps/papra-client/src/locales/fr.yml
index e94bb066..7ee15f36 100644
--- a/apps/papra-client/src/locales/fr.yml
+++ b/apps/papra-client/src/locales/fr.yml
@@ -498,6 +498,7 @@ api-errors.organization.invitation_already_exists: Une invitation pour cet email
api-errors.user.already_in_organization: Cet utilisateur est déjà dans cette organisation.
api-errors.user.organization_invitation_limit_reached: Le nombre maximum d'invitations a été atteint pour aujourd'hui. Veuillez réessayer demain.
api-errors.demo.not_available: Cette fonctionnalité n'est pas disponible dans la démo
+api-errors.tags.already_exists: Un tag avec ce nom existe déjà pour cette organisation
# Not found
diff --git a/apps/papra-client/src/modules/i18n/locales.types.ts b/apps/papra-client/src/modules/i18n/locales.types.ts
index e0c31f1e..89d2479c 100644
--- a/apps/papra-client/src/modules/i18n/locales.types.ts
+++ b/apps/papra-client/src/modules/i18n/locales.types.ts
@@ -443,6 +443,7 @@ export type LocaleKeys =
| 'api-errors.user.already_in_organization'
| 'api-errors.user.organization_invitation_limit_reached'
| 'api-errors.demo.not_available'
+ | 'api-errors.tags.already_exists'
| 'not-found.title'
| 'not-found.description'
| 'not-found.back-to-home'
diff --git a/apps/papra-client/src/modules/tags/pages/tags.page.tsx b/apps/papra-client/src/modules/tags/pages/tags.page.tsx
index c416cdba..e3b8680f 100644
--- a/apps/papra-client/src/modules/tags/pages/tags.page.tsx
+++ b/apps/papra-client/src/modules/tags/pages/tags.page.tsx
@@ -1,6 +1,7 @@
import type { DialogTriggerProps } from '@kobalte/core/dialog';
import type { Component, JSX } from 'solid-js';
import type { Tag as TagType } from '../tags.types';
+import { safely } from '@corentinth/chisels';
import { getValues } from '@modular-forms/solid';
import { A, useParams } from '@solidjs/router';
import { createQuery } from '@tanstack/solid-query';
@@ -10,6 +11,7 @@ import { useI18n } from '@/modules/i18n/i18n.provider';
import { useConfirmModal } from '@/modules/shared/confirm';
import { timeAgo } from '@/modules/shared/date/time-ago';
import { createForm } from '@/modules/shared/form/form';
+import { useI18nApiErrors } from '@/modules/shared/http/composables/i18n-api-errors';
import { queryClient } from '@/modules/shared/query/query-client';
import { Button } from '@/modules/ui/components/button';
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from '@/modules/ui/components/dialog';
@@ -112,14 +114,23 @@ export const CreateTagModal: Component<{
}> = (props) => {
const [getIsModalOpen, setIsModalOpen] = createSignal(false);
const { t } = useI18n();
+ const { getErrorMessage } = useI18nApiErrors({ t });
const onSubmit = async ({ name, color, description }: { name: string; color: string; description: string }) => {
- await createTag({
+ const [,error] = await safely(createTag({
name,
color,
description,
organizationId: props.organizationId,
- });
+ }));
+
+ if (error) {
+ createToast({
+ message: getErrorMessage({ error }),
+ type: 'error',
+ });
+ return;
+ }
await queryClient.invalidateQueries({
queryKey: ['organizations', props.organizationId],
diff --git a/apps/papra-server/src/modules/tags/tags.errors.ts b/apps/papra-server/src/modules/tags/tags.errors.ts
index 314afd70..1d75b3bf 100644
--- a/apps/papra-server/src/modules/tags/tags.errors.ts
+++ b/apps/papra-server/src/modules/tags/tags.errors.ts
@@ -5,3 +5,9 @@ export const createDocumentAlreadyHasTagError = createErrorFactory({
code: 'documents.already_has_tag',
statusCode: 400,
});
+
+export const createTagAlreadyExistsError = createErrorFactory({
+ message: 'Tag already exists',
+ code: 'tags.already_exists',
+ statusCode: 400,
+});
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 e0b66d86..d64c6896 100644
--- a/apps/papra-server/src/modules/tags/tags.repository.test.ts
+++ b/apps/papra-server/src/modules/tags/tags.repository.test.ts
@@ -1,7 +1,7 @@
import { describe, expect, test } from 'vitest';
import { createInMemoryDatabase } from '../app/database/database.test-utils';
import { ORGANIZATION_ROLES } from '../organizations/organizations.constants';
-import { createDocumentAlreadyHasTagError } from './tags.errors';
+import { createDocumentAlreadyHasTagError, createTagAlreadyExistsError } from './tags.errors';
import { createTagsRepository } from './tags.repository';
describe('tags repository', () => {
@@ -151,4 +151,22 @@ describe('tags repository', () => {
]);
});
});
+
+ describe('createTag', () => {
+ test('when a tag with the same name already exists for the same organization, it throws an error', async () => {
+ const { db } = await createInMemoryDatabase({
+ organizations: [{ id: 'organization-1', name: 'Organization 1' }],
+ });
+
+ const tagsRepository = createTagsRepository({ db });
+
+ await tagsRepository.createTag({
+ tag: { organizationId: 'organization-1', name: 'Tag 1', color: '#aa0000' },
+ });
+
+ await expect(
+ tagsRepository.createTag({ tag: { organizationId: 'organization-1', name: 'Tag 1', color: '#aa0000' } }),
+ ).rejects.toThrow(createTagAlreadyExistsError());
+ });
+ });
});
diff --git a/apps/papra-server/src/modules/tags/tags.repository.ts b/apps/papra-server/src/modules/tags/tags.repository.ts
index 8c885471..7acdf266 100644
--- a/apps/papra-server/src/modules/tags/tags.repository.ts
+++ b/apps/papra-server/src/modules/tags/tags.repository.ts
@@ -4,8 +4,9 @@ import { injectArguments, safely } from '@corentinth/chisels';
import { and, count, eq, getTableColumns, isNull, or } from 'drizzle-orm';
import { get } from 'lodash-es';
import { documentsTable } from '../documents/documents.table';
+import { isUniqueConstraintError } from '../shared/db/constraints.models';
import { omitUndefined } from '../shared/utils';
-import { createDocumentAlreadyHasTagError } from './tags.errors';
+import { createDocumentAlreadyHasTagError, createTagAlreadyExistsError } from './tags.errors';
import { documentsTagsTable, tagsTable } from './tags.table';
export type TagsRepository = ReturnType<typeof createTagsRepository>;
@@ -50,7 +51,17 @@ async function getOrganizationTags({ organizationId, db }: { organizationId: str
}
async function createTag({ tag, db }: { tag: DbInsertableTag; db: Database }) {
- const [createdTag] = await db.insert(tagsTable).values(tag).returning();
+ const [result, error] = await safely(db.insert(tagsTable).values(tag).returning());
+
+ if (isUniqueConstraintError({ error })) {
+ throw createTagAlreadyExistsError();
+ }
+
+ if (error) {
+ throw error;
+ }
+
+ const [createdTag] = result;
return { tag: createdTag };
}