The minimalistic document archiving platform.
feat(demo): add document views demo enpoints (#1134)
Corentin Thomasset70aec2087a48
diff --git a/apps/papra-client/src/modules/demo/demo-api-mock.ts b/apps/papra-client/src/modules/demo/demo-api-mock.tsindex 91696d43..1a31feba 100644--- a/apps/papra-client/src/modules/demo/demo-api-mock.ts+++ b/apps/papra-client/src/modules/demo/demo-api-mock.ts@@ -17,6 +17,7 @@ import {documentCustomPropertyValueStorage,documentFileStorage,documentStorage,+ documentViewStorage,organizationStorage,tagDocumentStorage,taggingRuleStorage,@@ -1284,6 +1285,123 @@ const inMemoryApiMock: Record<string, { handler: any }> = {},}),+ ...defineHandler({+ path: '/api/organizations/:organizationId/document-views',+ method: 'GET',+ handler: async ({ params: { organizationId } }) => {+ const organization = await organizationStorage.getItem(organizationId);++ assert(organization, { status: 403 });++ const documentViews = await findMany(documentViewStorage, view => view.organizationId === organizationId);++ return {+ documentViews: documentViews.toSorted((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()),+ };+ },+ }),++ ...defineHandler({+ path: '/api/organizations/:organizationId/document-views',+ method: 'POST',+ handler: async ({ params: { organizationId }, body }) => {+ const organization = await organizationStorage.getItem(organizationId);++ assert(organization, { status: 403 });++ const name = get(body, ['name']) as string;+ const query = get(body, ['query']) as string;+ const description = (get(body, ['description']) ?? null) as string | null;++ const existingViewsWithSameName = await findMany(+ documentViewStorage,+ view => view.organizationId === organizationId && view.name.toLowerCase() === name.toLowerCase(),+ );++ if (existingViewsWithSameName.length > 0) {+ throw Object.assign(new FetchError('A view with this name already exists'), {+ status: 400,+ data: { error: { code: 'document_views.already_exists' } },+ });+ }++ const documentView = {+ id: createId({ prefix: 'dv' }),+ organizationId,+ name,+ query,+ description,+ createdAt: new Date(),+ updatedAt: new Date(),+ };++ await documentViewStorage.setItem(documentView.id, documentView);++ return { documentView };+ },+ }),++ ...defineHandler({+ path: '/api/organizations/:organizationId/document-views/:documentViewId',+ method: 'PUT',+ handler: async ({ params: { organizationId, documentViewId }, body }) => {+ const organization = await organizationStorage.getItem(organizationId);++ assert(organization, { status: 403 });++ const documentView = await documentViewStorage.getItem(documentViewId);++ assert(documentView && documentView.organizationId === organizationId, {+ status: 404,+ message: 'Document view not found',+ });++ const newName = get(body, ['name']) as string | undefined;++ if (newName) {+ const existingViewsWithSameName = await findMany(+ documentViewStorage,+ view =>+ view.organizationId === organizationId+ && view.id !== documentViewId+ && view.name.toLowerCase() === newName.toLowerCase(),+ );++ if (existingViewsWithSameName.length > 0) {+ throw Object.assign(new FetchError('A view with this name already exists'), {+ status: 400,+ data: { error: { code: 'document_views.already_exists' } },+ });+ }+ }++ const updatedDocumentView = Object.assign(documentView, body, { updatedAt: new Date() });++ await documentViewStorage.setItem(documentViewId, updatedDocumentView);++ return { documentView: updatedDocumentView };+ },+ }),++ ...defineHandler({+ path: '/api/organizations/:organizationId/document-views/:documentViewId',+ method: 'DELETE',+ handler: async ({ params: { organizationId, documentViewId } }) => {+ const organization = await organizationStorage.getItem(organizationId);++ assert(organization, { status: 403 });++ const documentView = await documentViewStorage.getItem(documentViewId);++ assert(documentView && documentView.organizationId === organizationId, {+ status: 404,+ message: 'Document view not found',+ });++ await documentViewStorage.removeItem(documentViewId);+ },+ }),+...defineHandler({path: '/api/organizations/:organizationId/share-links',method: 'GET',diff --git a/apps/papra-client/src/modules/demo/demo.storage.ts b/apps/papra-client/src/modules/demo/demo.storage.tsindex 92fb87fa..fe40f5a5 100644--- a/apps/papra-client/src/modules/demo/demo.storage.ts+++ b/apps/papra-client/src/modules/demo/demo.storage.ts@@ -1,5 +1,6 @@import type { ApiKey } from '../api-keys/api-keys.types';import type { CustomPropertyDefinition } from '../custom-properties/custom-properties.types';+import type { DocumentView } from '../document-views/document-views.types';import type { Document } from '../documents/documents.types';import type { Organization } from '../organizations/organizations.types';import type { TaggingRule } from '../tagging-rules/tagging-rules.types';@@ -39,6 +40,7 @@ export const apiKeyStorage = prefixStorage<ApiKey>(storage, 'apiKeys');export const webhooksStorage = prefixStorage<Webhook>(storage, 'webhooks');export const customPropertyDefinitionStorage = prefixStorage<CustomPropertyDefinition>(storage, 'customPropertyDefinitions');export const documentCustomPropertyValueStorage = prefixStorage<DocumentCustomPropertyValueStorage>(storage, 'documentCustomPropertyValues');+export const documentViewStorage = prefixStorage<DocumentView>(storage, 'documentViews');export async function clearDemoStorage() {await storage.clear();@@ -119,6 +121,23 @@ export async function seedDemoStorage() {customPropertyDefinitions.map(def => ({ key: def.id, value: def })),);+ // Create stored document views+ const documentViews = [+ { name: 'Receipts', query: 'tag:Receipts', description: 'Payment receipts and bills' },+ ].map(view => ({+ id: createId({ prefix: 'dv' }),+ organizationId,+ name: view.name,+ query: view.query,+ description: view.description,+ createdAt: lastMonth,+ updatedAt: lastMonth,+ }));++ const documentViewsPromises = documentViewStorage.setItems(+ documentViews.map(view => ({ key: view.id, value: view })),+ );+const documentsPromises = documentFixtures.flatMap((fixture) => {const documentId = createId({ prefix: 'doc' });@@ -179,6 +198,7 @@ export async function seedDemoStorage() {await Promise.all([tagsPromises,customPropertyDefinitionsPromises,+ documentViewsPromises,...documentsPromises,]);}