From 071c41cb007f109cde69fd60f326eb2e6b1e3bf6 Mon Sep 17 00:00:00 2001 From: Daniel Naab Date: Fri, 1 Nov 2024 15:50:46 -0500 Subject: [PATCH] Fix test and typing errors; implement localStorage version of addDocument --- .../forms/src/context/browser/form-repo.ts | 28 ++++++++++++++++++- packages/forms/src/context/test/index.ts | 2 ++ .../patterns/package-download/submit.test.ts | 1 + .../forms/src/repository/get-form.test.ts | 3 +- .../forms/src/repository/save-form.test.ts | 3 +- .../forms/src/services/initialize-form.ts | 3 +- packages/forms/src/testing.ts | 1 - packages/server/src/config/services.ts | 2 ++ packages/server/src/lib/api-client.ts | 13 +++++++++ 9 files changed, 51 insertions(+), 5 deletions(-) diff --git a/packages/forms/src/context/browser/form-repo.ts b/packages/forms/src/context/browser/form-repo.ts index c39566ec..bc141976 100644 --- a/packages/forms/src/context/browser/form-repo.ts +++ b/packages/forms/src/context/browser/form-repo.ts @@ -1,8 +1,15 @@ import { type Result, type VoidResult, failure } from '@atj/common'; -import { FormSession, FormSessionId, type Blueprint } from '../../index.js'; +import { + FormSession, + FormSessionId, + type Blueprint, + type DocumentFieldMap, +} from '../../index.js'; import { FormRepository } from '../../repository/index.js'; +import type { ParsedPdf } from '../../documents/pdf/parsing-api.js'; +const documentKey = (id: string) => `documents/${id}`; const formKey = (formId: string) => `forms/${formId}`; const isFormKey = (key: string) => key.startsWith('forms/'); const getFormIdFromKey = (key: string) => { @@ -119,6 +126,25 @@ export class BrowserFormRepository implements FormRepository { } return { success: true }; } + + addDocument(document: { + fileName: string; + data: Uint8Array; + extract: { parsedPdf: ParsedPdf; fields: DocumentFieldMap }; + }) { + const documentId = crypto.randomUUID(); + this.storage.setItem( + documentKey(documentId), + JSON.stringify({ + id: documentId, + type: 'pdf', + file_name: document.fileName, + data: Buffer.from(document.data), + extract: JSON.stringify(document.extract), + }) + ); + return {} as Promise>; + } } export const getFormList = (storage: Storage) => { diff --git a/packages/forms/src/context/test/index.ts b/packages/forms/src/context/test/index.ts index 6e4cc835..8122c05d 100644 --- a/packages/forms/src/context/test/index.ts +++ b/packages/forms/src/context/test/index.ts @@ -1,4 +1,5 @@ import { BrowserFormRepository } from '../browser/form-repo.js'; +import { parsePdf } from '../../documents/pdf/index.js'; import { defaultFormConfig } from '../../patterns/index.js'; import { type FormService, createFormService } from '../../services/index.js'; @@ -14,6 +15,7 @@ export const createTestBrowserFormService = ( repository, config: defaultFormConfig, isUserLoggedIn: () => true, + parsePdf, }); if (testData) { Object.entries(testData).forEach(([id, blueprint]) => { diff --git a/packages/forms/src/patterns/package-download/submit.test.ts b/packages/forms/src/patterns/package-download/submit.test.ts index 53d57a9e..f304030e 100644 --- a/packages/forms/src/patterns/package-download/submit.test.ts +++ b/packages/forms/src/patterns/package-download/submit.test.ts @@ -83,6 +83,7 @@ const createTestForm = async (): Promise => { }, outputs: [ { + id: 'test-id', path: 'test.pdf', data: new Uint8Array(pdfBytes), fields: { diff --git a/packages/forms/src/repository/get-form.test.ts b/packages/forms/src/repository/get-form.test.ts index 415f809c..82acc0bb 100644 --- a/packages/forms/src/repository/get-form.test.ts +++ b/packages/forms/src/repository/get-form.test.ts @@ -12,7 +12,7 @@ describeDatabase('getForm', () => { .insertInto('forms') .values({ id: '45c66187-64e2-4d75-a45a-e80f1d035bc5', - data: '{"summary":{"title":"Title","description":"Description"},"root":"root","patterns":{"root":{"type":"sequence","id":"root","data":{"patterns":[]}}},"outputs":[{"data":"AQID","path":"test.pdf","fields":{},"formFields":{}}]}', + data: '{"summary":{"title":"Title","description":"Description"},"root":"root","patterns":{"root":{"type":"sequence","id":"root","data":{"patterns":[]}}},"outputs":[{"id":"test-id","data":"AQID","path":"test.pdf","fields":{},"formFields":{}}]}', }) .execute(); @@ -39,6 +39,7 @@ const TEST_FORM: Blueprint = { patterns: { root: { type: 'sequence', id: 'root', data: { patterns: [] } } }, outputs: [ { + id: 'test-id', data: new Uint8Array([1, 2, 3]), path: 'test.pdf', fields: {}, diff --git a/packages/forms/src/repository/save-form.test.ts b/packages/forms/src/repository/save-form.test.ts index 8befac51..4465bac9 100644 --- a/packages/forms/src/repository/save-form.test.ts +++ b/packages/forms/src/repository/save-form.test.ts @@ -23,6 +23,7 @@ const TEST_FORM: Blueprint = { }, outputs: [ { + id: 'test-id', data: new Uint8Array([1, 2, 3]), path: 'test.pdf', fields: {}, @@ -55,7 +56,7 @@ describeDatabase('saveForm', () => { expect(result[0].id).toEqual(addResult.data.id); expect(result[0].data).toEqual( - '{"summary":{"title":"Updated title","description":"Updated description"},"root":"root","patterns":{"root":{"type":"sequence","id":"root","data":{"patterns":[]}}},"outputs":[{"data":"AQID","path":"test.pdf","fields":{},"formFields":{}}]}' + '{"summary":{"title":"Updated title","description":"Updated description"},"root":"root","patterns":{"root":{"type":"sequence","id":"root","data":{"patterns":[]}}},"outputs":[{"id":"test-id","data":"AQID","path":"test.pdf","fields":{},"formFields":{}}]}' ); }); }); diff --git a/packages/forms/src/services/initialize-form.ts b/packages/forms/src/services/initialize-form.ts index 2128f11b..c8374954 100644 --- a/packages/forms/src/services/initialize-form.ts +++ b/packages/forms/src/services/initialize-form.ts @@ -1,7 +1,8 @@ import { type Result, failure, success } from '@atj/common'; -import { type FormSummary, BlueprintBuilder } from '../index.js'; +import { BlueprintBuilder } from '../builder/index.js'; import { type FormServiceContext } from '../context/index.js'; +import type { FormSummary } from '../types.js'; type InitializeFormError = { status: number; diff --git a/packages/forms/src/testing.ts b/packages/forms/src/testing.ts index 1fbb4f8b..bcb32a6c 100644 --- a/packages/forms/src/testing.ts +++ b/packages/forms/src/testing.ts @@ -17,7 +17,6 @@ export const createTestFormServiceContext = async ( const db: DatabaseContext = await createInMemoryDatabaseContext(); const repository = createFormsRepository(db); return { - db, repository, config: defaultFormConfig, isUserLoggedIn: opts?.isUserLoggedIn || (() => true), diff --git a/packages/server/src/config/services.ts b/packages/server/src/config/services.ts index a378a3b4..98c2c250 100644 --- a/packages/server/src/config/services.ts +++ b/packages/server/src/config/services.ts @@ -3,6 +3,7 @@ import { createFormService, createFormsRepository, defaultFormConfig, + parsePdf, } from '@atj/forms'; import { type ServerOptions } from './options.js'; @@ -14,5 +15,6 @@ export const createServerFormService = ( repository: createFormsRepository(options.db), config: defaultFormConfig, isUserLoggedIn: ctx.isUserLoggedIn, + parsePdf, }); }; diff --git a/packages/server/src/lib/api-client.ts b/packages/server/src/lib/api-client.ts index 374f79a1..2def3113 100644 --- a/packages/server/src/lib/api-client.ts +++ b/packages/server/src/lib/api-client.ts @@ -5,6 +5,7 @@ import { type FormSessionId, type Blueprint, type FormService, + type FormSummary, } from '@atj/forms'; import { type FormServiceContext } from '@atj/forms/context'; @@ -102,4 +103,16 @@ export class FormServiceClient implements FormService { getContext() { return {} as unknown as FormServiceContext; } + + initializeForm(_: { + summary?: FormSummary; + document?: { fileName: string; data: Uint8Array }; + }): Promise< + Result< + { timestamp: string; id: string }, + { status: number; message: string } + > + > { + throw new Error('Not implemented'); + } }