Skip to content

Commit

Permalink
Fix test and typing errors; implement localStorage version of addDocu…
Browse files Browse the repository at this point in the history
…ment
  • Loading branch information
danielnaab committed Nov 1, 2024
1 parent 8072a03 commit 071c41c
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 5 deletions.
28 changes: 27 additions & 1 deletion packages/forms/src/context/browser/form-repo.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -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<Result<{ id: string }>>;
}
}

export const getFormList = (storage: Storage) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/forms/src/context/test/index.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -14,6 +15,7 @@ export const createTestBrowserFormService = (
repository,
config: defaultFormConfig,
isUserLoggedIn: () => true,
parsePdf,
});
if (testData) {
Object.entries(testData).forEach(([id, blueprint]) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const createTestForm = async (): Promise<Blueprint> => {
},
outputs: [
{
id: 'test-id',
path: 'test.pdf',
data: new Uint8Array(pdfBytes),
fields: {
Expand Down
3 changes: 2 additions & 1 deletion packages/forms/src/repository/get-form.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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: {},
Expand Down
3 changes: 2 additions & 1 deletion packages/forms/src/repository/save-form.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const TEST_FORM: Blueprint = {
},
outputs: [
{
id: 'test-id',
data: new Uint8Array([1, 2, 3]),
path: 'test.pdf',
fields: {},
Expand Down Expand Up @@ -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":{}}]}'
);
});
});
3 changes: 2 additions & 1 deletion packages/forms/src/services/initialize-form.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
1 change: 0 additions & 1 deletion packages/forms/src/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions packages/server/src/config/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
createFormService,
createFormsRepository,
defaultFormConfig,
parsePdf,
} from '@atj/forms';
import { type ServerOptions } from './options.js';

Expand All @@ -14,5 +15,6 @@ export const createServerFormService = (
repository: createFormsRepository(options.db),
config: defaultFormConfig,
isUserLoggedIn: ctx.isUserLoggedIn,
parsePdf,
});
};
13 changes: 13 additions & 0 deletions packages/server/src/lib/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
type FormSessionId,
type Blueprint,
type FormService,
type FormSummary,
} from '@atj/forms';
import { type FormServiceContext } from '@atj/forms/context';

Expand Down Expand Up @@ -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');
}
}

0 comments on commit 071c41c

Please sign in to comment.