The minimalistic document archiving platform.
refactor(demo-api-mock): simplify document and tag retrieval using utility functions (#99)
Corentin THOMASSET510e8622b593
diff --git a/apps/papra-client/src/modules/demo/demo-api-mock.ts b/apps/papra-client/src/modules/demo/demo-api-mock.tsindex a7eee8ae..f16d5d48 100644--- a/apps/papra-client/src/modules/demo/demo-api-mock.ts+++ b/apps/papra-client/src/modules/demo/demo-api-mock.ts@@ -3,6 +3,7 @@ import { FetchError } from 'ofetch';import { createRouter } from 'radix3';import { defineHandler } from './demo-api-mock.models';import { documentFileStorage, documentStorage, organizationStorage, tagDocumentStorage, tagStorage } from './demo.storage';+import { findMany, getValues } from './demo.storage.models';function assert(condition: unknown, { message = 'Error', status }: { message?: string; status?: number } = {}): asserts condition {if (!condition) {@@ -71,8 +72,7 @@ const inMemoryApiMock: Record<string, { handler: any }> = {path: '/api/organizations',method: 'GET',handler: async () => {- const keys = await organizationStorage.getKeys();- const organizations = await Promise.all(keys.map(key => organizationStorage.getItem(key)));+ const organizations = await getValues(organizationStorage);return { organizations };},@@ -102,33 +102,20 @@ const inMemoryApiMock: Record<string, { handler: any }> = {const organization = organizationStorage.getItem(organizationId);assert(organization, { status: 403 });- const allKeys = await documentStorage.getKeys();- const keys = allKeys.filter(key => key.startsWith(organizationId));-- const documents = await Promise.all(keys.map(key => documentStorage.getItem(key)));+ const documents = await findMany(documentStorage, document => document.organizationId === organizationId && !document.deletedAt);const filteredDocuments = await Promise.all(- documents- .filter(document => !document?.deletedAt)- .map(async (document) => {- const tagDocumentsIds = await tagDocumentStorage.getKeys();- const tagDocuments = await Promise.all(tagDocumentsIds.map(tagDocumentId => tagDocumentStorage.getItem(tagDocumentId)));-- const tags = await Promise.all(- tagDocuments- .filter(tagDocument => tagDocument?.documentId === document?.id)- .map(async (tagDocument) => {- const tag = await tagStorage.getItem(tagDocument?.tagId);-- return tag;- }),- );-- return {- ...document,- tags,- };- }),+ documents.map(async (document) => {+ const tagDocuments = await findMany(tagDocumentStorage, tagDocument => tagDocument?.documentId === document?.id);+ const allTags = await getValues(tagStorage);++ const tags = allTags.filter(tag => tagDocuments.some(tagDocument => tagDocument?.tagId === tag?.id));++ return {+ ...document,+ tags,+ };+ }),);const {@@ -187,10 +174,7 @@ const inMemoryApiMock: Record<string, { handler: any }> = {const organization = organizationStorage.getItem(organizationId);assert(organization, { status: 403 });- const allKeys = await documentStorage.getKeys();- const keys = allKeys.filter(key => key.startsWith(organizationId));-- const documents = await Promise.all(keys.map(key => documentStorage.getItem(key)));+ const documents = await findMany(documentStorage, document => document?.organizationId === organizationId);const filteredDocuments = documents.filter(document => document?.name.includes(searchQuery) && !document?.deletedAt);@@ -208,12 +192,10 @@ const inMemoryApiMock: Record<string, { handler: any }> = {const organization = organizationStorage.getItem(organizationId);assert(organization, { status: 403 });- const allKeys = await documentStorage.getKeys();- const keys = allKeys.filter(key => key.startsWith(organizationId));-- const documents = await Promise.all(keys.map(key => documentStorage.getItem(key)));-- const deletedDocuments = documents.filter(document => document?.deletedAt);+ const deletedDocuments = await findMany(+ documentStorage,+ document => document.organizationId === organizationId && document.deletedAt !== undefined,+ );return {documents: deletedDocuments,@@ -229,14 +211,11 @@ const inMemoryApiMock: Record<string, { handler: any }> = {const key = `${organizationId}:${documentId}`;const document = await documentStorage.getItem(key);- const tagsIds = await tagDocumentStorage.getKeys();- const documentTags = await Promise.all(tagsIds.map(tagId => tagDocumentStorage.getItem(tagId)));- const filteredDocumentTags = documentTags.filter(documentTag => documentTag?.documentId === documentId);-- const tags = await Promise.all(filteredDocumentTags.map(tag => tagStorage.getItem(tag?.tagId)));-assert(document, { status: 404 });+ const tagDocuments = await findMany(tagDocumentStorage, tagDocument => tagDocument.documentId === documentId);+ const tags = await findMany(tagStorage, tag => tagDocuments.some(tagDocument => tagDocument.tagId === tag.id));+return {document: {...document,@@ -344,27 +323,16 @@ const inMemoryApiMock: Record<string, { handler: any }> = {assert(organization, { status: 403 });- const tagsItemsIds = await tagStorage.getKeys();- const allTags = await Promise.all(tagsItemsIds.map(tagId => tagStorage.getItem(tagId)));+ const tags = await findMany(tagStorage, tag => tag.organizationId === organizationId);+ const documents = await findMany(documentStorage, document => document.organizationId === organizationId);- const filteredTags = await Promise.all(- allTags- .filter(tag => tag?.organizationId === organizationId)- .map(async (tag) => {- const tagDocumentsIds = await tagDocumentStorage.getKeys();- const tagDocuments = await Promise.all(tagDocumentsIds.map(tagDocumentId => tagDocumentStorage.getItem(tagDocumentId)));-- const tagDocumentsFiltered = tagDocuments.filter(tagDocument => tagDocument?.tagId === tag?.id);-- return {- ...tag,- documentsCount: tagDocumentsFiltered.length,- };- }),- );+ const tagsWithDocumentsCount = tags.map(tag => ({+ ...tag,+ documentsCount: documents.filter(document => document.tags.some(t => t.id === tag.id)).length,+ }));return {- tags: filteredTags,+ tags: tagsWithDocumentsCount,};},}),@@ -421,9 +389,7 @@ const inMemoryApiMock: Record<string, { handler: any }> = {await tagStorage.removeItem(tagId);- const tagDocumentsIds = await tagDocumentStorage.getKeys();- const allTagDocuments = await Promise.all(tagDocumentsIds.map(tagDocumentId => tagDocumentStorage.getItem(tagDocumentId)));- const tagDocuments = allTagDocuments.filter(tagDocument => tagDocument?.tagId === tagId);+ const tagDocuments = await findMany(tagDocumentStorage, tagDocument => tagDocument.tagId === tagId);await Promise.all(tagDocuments.map(tagDocument => tagDocumentStorage.removeItem(tagDocument.id)));},@@ -460,13 +426,9 @@ const inMemoryApiMock: Record<string, { handler: any }> = {assert(organization, { status: 403 });- const tagDocumentsIds = await tagDocumentStorage.getKeys();- const tagDocuments = await Promise.all(tagDocumentsIds.map(tagDocumentId => tagDocumentStorage.getItem(tagDocumentId)));- const tagDocument = tagDocuments.find(tagDocument => tagDocument?.tagId === tagId && tagDocument?.documentId === documentId);-- assert(tagDocument, { status: 404 });+ const tagDocuments = await findMany(tagDocumentStorage, tagDocument => tagDocument.tagId === tagId && tagDocument.documentId === documentId);- await tagDocumentStorage.removeItem(tagDocument.id);+ await Promise.all(tagDocuments.map(tagDocument => tagDocumentStorage.removeItem(tagDocument.id)));},}),diff --git a/apps/papra-client/src/modules/demo/demo.storage.models.ts b/apps/papra-client/src/modules/demo/demo.storage.models.tsnew file mode 100644index 00000000..9683ee0c--- /dev/null+++ b/apps/papra-client/src/modules/demo/demo.storage.models.ts@@ -0,0 +1,23 @@+import type { Storage, StorageValue } from 'unstorage';++export async function getValues<T extends StorageValue>(storage: Storage<T>): Promise<T[]> {+ const keys = await storage.getKeys();++ const values = await Promise.all(keys.map(key => storage.getItem(key))) as T[];++ return values;+}++export async function findOne<T extends StorageValue>(storage: Storage<T>, predicate: (value: T) => boolean): Promise< T | null> {+ const values = await getValues(storage);+ const found = values.find(predicate);++ return found ?? null;+}++export async function findMany<T extends StorageValue>(storage: Storage<T>, predicate: (value: T) => boolean): Promise<T[]> {+ const values = await getValues(storage);+ const found = values.filter(predicate);++ return found;+}diff --git a/apps/papra-client/src/modules/demo/demo.storage.ts b/apps/papra-client/src/modules/demo/demo.storage.tsindex 877c6cff..4536f817 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 { Document } from '../documents/documents.types';import type { Organization } from '../organizations/organizations.types';+import type { Tag } from '../tags/tags.types';import { createStorage, prefixStorage } from 'unstorage';import localStorageDriver from 'unstorage/drivers/localstorage';@@ -10,8 +11,8 @@ const storage = createStorage<any>({export const organizationStorage = prefixStorage<Organization>(storage, 'organizations');export const documentStorage = prefixStorage<Document>(storage, 'documents');export const documentFileStorage = prefixStorage(storage, 'documentFiles');-export const tagStorage = prefixStorage(storage, 'tags');-export const tagDocumentStorage = prefixStorage(storage, 'tagDocuments');+export const tagStorage = prefixStorage<Omit<Tag, 'documentsCount'>>(storage, 'tags');+export const tagDocumentStorage = prefixStorage<{ documentId: string; tagId: string; id: string }>(storage, 'tagDocuments');export async function clearDemoStorage() {await storage.clear();diff --git a/apps/papra-client/src/modules/tags/tags.types.ts b/apps/papra-client/src/modules/tags/tags.types.tsindex 4c49a16b..5ca5b522 100644--- a/apps/papra-client/src/modules/tags/tags.types.ts+++ b/apps/papra-client/src/modules/tags/tags.types.ts@@ -4,6 +4,7 @@ export type Tag = {color: string;description: string | null;documentsCount: number;+ organizationId: string;createdAt: Date;updatedAt: Date;};