Skip to content

Commit

Permalink
Remove pdf bytes from blueprint object
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnaab committed Nov 7, 2024
1 parent df56bcb commit 846e0c2
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 83 deletions.
15 changes: 2 additions & 13 deletions packages/forms/src/context/browser/form-repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class BrowserFormRepository implements FormRepository {

async saveForm(formId: string, form: Blueprint): Promise<VoidResult> {
try {
this.storage.setItem(formKey(formId), stringifyForm(form));
this.storage.setItem(formKey(formId), JSON.stringify(form));
} catch {
return failure(`error saving '${formId}' to storage`);
}
Expand Down Expand Up @@ -179,7 +179,7 @@ export const getFormList = (storage: Storage) => {

export const saveForm = (storage: Storage, formId: string, form: Blueprint) => {
try {
storage.setItem(formKey(formId), stringifyForm(form));
storage.setItem(formKey(formId), JSON.stringify(form));
} catch {
return {
success: false as const,
Expand All @@ -191,17 +191,6 @@ export const saveForm = (storage: Storage, formId: string, form: Blueprint) => {
};
};

const stringifyForm = (form: Blueprint) => {
return JSON.stringify({
...form,
outputs: form.outputs.map(output => ({
...output,
// TODO: we probably want to do this somewhere in the documents module
data: uint8ArrayToBase64(output.data),
})),
});
};

const parseStringForm = (formString: string): Blueprint => {
const form = JSON.parse(formString) as Blueprint;
return {
Expand Down
3 changes: 0 additions & 3 deletions packages/forms/src/documents/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export const addParsedPdfToForm = async (
form = addPatternMap(form, document.extract.patterns, document.extract.root);
const updatedForm = addFormOutput(form, {
id: document.id,
data: new Uint8Array(), // TODO: remove this no-longer-used field
path: document.label,
fields: document.extract.outputs,
formFields: Object.fromEntries(
Expand Down Expand Up @@ -71,7 +70,6 @@ export const addDocument = async (
form = addPatternMap(form, parsedPdf.patterns, parsedPdf.root);
const updatedForm = addFormOutput(form, {
id: 'document-1', // TODO: generate a unique ID
data: fileDetails.data,
path: fileDetails.name,
fields: parsedPdf.outputs,
formFields: Object.fromEntries(
Expand All @@ -89,7 +87,6 @@ export const addDocument = async (
const formWithFields = addDocumentFieldsToForm(form, fields);
const updatedForm = addFormOutput(formWithFields, {
id: 'document-1', // TODO: generate a unique ID
data: fileDetails.data,
path: fileDetails.name,
fields,
// TODO: for now, reuse the field IDs from the PDF. we need to generate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ const createTestForm = async (): Promise<Blueprint> => {
{
id: 'test-id',
path: 'test.pdf',
data: new Uint8Array(pdfBytes),
fields: {
[input1.id]: {
type: 'TextField',
Expand Down
37 changes: 19 additions & 18 deletions packages/forms/src/patterns/package-download/submit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,24 @@ export const downloadPackageHandler: SubmitHandler<
return failure('Form is not complete');
}

const outputsResult: Result<FormOutput[]> = await Promise.all(
opts.session.form.outputs.map(async output => {
const doc = await context.getDocument(output.id);
if (!doc.success) {
throw new Error(doc.error);
}
return {
id: output.id,
data: doc.data.data,
path: doc.data.path,
fields: output.fields,
formFields: output.formFields,
} satisfies FormOutput;
})
)
.then(values => success(values))
.catch(error => failure(error));
const outputsResult: Result<(FormOutput & { data: Uint8Array })[]> =
await Promise.all(
opts.session.form.outputs.map(async output => {
const doc = await context.getDocument(output.id);
if (!doc.success) {
throw new Error(doc.error);
}
return {
id: output.id,
path: doc.data.path,
fields: output.fields,
formFields: output.formFields,
data: doc.data.data,
};
})
)
.then(values => success(values))
.catch(error => failure(error));
if (!outputsResult.success) {
return failure(outputsResult.error);
}
Expand All @@ -50,7 +51,7 @@ export const downloadPackageHandler: SubmitHandler<
};

const generateDocumentPackage = async (
outputs: FormOutput[],
outputs: (FormOutput & { data: Uint8Array })[],
formData: Record<string, string>
) => {
const errors = new Array<string>();
Expand Down
3 changes: 1 addition & 2 deletions packages/forms/src/repository/add-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { type Result, failure, success } from '@atj/common';
import { type DatabaseContext } from '@atj/database';

import { type Blueprint } from '../index.js';
import { stringifyForm } from './serialize.js';

export type AddForm = (
ctx: DatabaseContext,
Expand All @@ -16,7 +15,7 @@ export const addForm: AddForm = async (ctx, form) => {
.insertInto('forms')
.values({
id: uuid,
data: stringifyForm(form),
data: JSON.stringify(form),
})
.execute()
.then(() =>
Expand Down
3 changes: 1 addition & 2 deletions 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":[{"id":"test-id","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","path":"test.pdf","fields":{},"formFields":{}}]}',
})
.execute();

Expand Down Expand Up @@ -40,7 +40,6 @@ const TEST_FORM: Blueprint = {
outputs: [
{
id: 'test-id',
data: new Uint8Array([1, 2, 3]),
path: 'test.pdf',
fields: {},
formFields: {},
Expand Down
19 changes: 1 addition & 18 deletions packages/forms/src/repository/get-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,5 @@ export const getForm: GetForm = async (ctx, formId) => {
};

const parseStringForm = (formString: string): Blueprint => {
const form = JSON.parse(formString) as Blueprint;
return {
...form,
outputs: form.outputs.map((output: any) => ({
...output,
data: base64ToUint8Array((output as any).data),
})),
};
};

const base64ToUint8Array = (base64: string): Uint8Array => {
const binaryString = atob(base64);
const len = binaryString.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes;
return JSON.parse(formString);
};
3 changes: 1 addition & 2 deletions packages/forms/src/repository/save-form.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const TEST_FORM: Blueprint = {
outputs: [
{
id: 'test-id',
data: new Uint8Array([1, 2, 3]),
path: 'test.pdf',
fields: {},
formFields: {},
Expand Down Expand Up @@ -56,7 +55,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":[{"id":"test-id","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","path":"test.pdf","fields":{},"formFields":{}}]}'
);
});
});
3 changes: 1 addition & 2 deletions packages/forms/src/repository/save-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { type VoidResult, failure, success } from '@atj/common';
import { type DatabaseContext } from '@atj/database';

import { type Blueprint } from '../index.js';
import { stringifyForm } from './serialize.js';

export type SaveForm = (
ctx: DatabaseContext,
Expand All @@ -16,7 +15,7 @@ export const saveForm: SaveForm = async (ctx, id, blueprint) => {
return await db
.updateTable('forms')
.set({
data: stringifyForm(blueprint),
data: JSON.stringify(blueprint),
})
.where('id', '=', id)
.execute()
Expand Down
21 changes: 0 additions & 21 deletions packages/forms/src/repository/serialize.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/forms/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export type FormSummary = {

export type FormOutput = {
id: string;
data: Uint8Array;
path: string;
fields: DocumentFieldMap;
formFields: Record<string, string>;
Expand Down

0 comments on commit 846e0c2

Please sign in to comment.