-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add form_documents table and an initializeForm service, responsible f…
…or creating a form that's initialized with data from a PDF. initializeForm is not currently wired to the UI.
- Loading branch information
1 parent
10bd089
commit ece3e1a
Showing
18 changed files
with
356 additions
and
15 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
packages/database/migrations/20241031103354_form_documents_table.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* @param { import("knex").Knex } knex | ||
* @returns { Promise<void> } | ||
*/ | ||
export async function up(knex) { | ||
await knex.schema.createTable('form_documents', table => { | ||
table.uuid('id').primary(); | ||
table.string('type').notNullable(); | ||
table.string('file_name').notNullable(); | ||
table.binary('data').notNullable(); | ||
table.string('extract').notNullable(); | ||
}); | ||
} | ||
|
||
/** | ||
* @param { import("knex").Knex } knex | ||
* @returns { Promise<void> } | ||
*/ | ||
export async function down(knex) { | ||
await knex.schema.dropTableIfExists('form_documents'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
import { type FormConfig } from '../pattern.js'; | ||
import { type FormRepository } from '../repository/index.js'; | ||
import type { ParsePdf } from '../documents/index.js'; | ||
import type { FormConfig } from '../pattern.js'; | ||
import type { FormRepository } from '../repository/index.js'; | ||
|
||
export { createTestBrowserFormService } from './test/index.js'; | ||
export { BrowserFormRepository } from './browser/form-repo.js'; | ||
export { createTestBrowserFormService } from './test/index.js'; | ||
|
||
export type FormServiceContext = { | ||
repository: FormRepository; | ||
config: FormConfig; | ||
isUserLoggedIn: () => boolean; | ||
parsePdf: ParsePdf; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { beforeAll, expect, it, vi } from 'vitest'; | ||
|
||
import { type DbTestContext, describeDatabase } from '@atj/database/testing'; | ||
import { addDocument } from './add-document.js'; | ||
import type { ParsedPdf } from '../documents/pdf/parsing-api.js'; | ||
import type { DocumentFieldMap } from '../documents/types.js'; | ||
|
||
describeDatabase('add document', () => { | ||
const today = new Date(2000, 1, 1); | ||
|
||
beforeAll(async () => { | ||
vi.setSystemTime(today); | ||
}); | ||
|
||
it<DbTestContext>('works', async ({ db }) => { | ||
const result = await addDocument(db.ctx, { | ||
fileName: 'file.pdf', | ||
data: new Uint8Array([1, 2, 3]), | ||
extract: { | ||
parsedPdf: {} as ParsedPdf, | ||
fields: {} as DocumentFieldMap, | ||
}, | ||
}); | ||
if (result.success === false) { | ||
expect.fail(`addDocument failed: ${result.error}`); | ||
} | ||
expect(result.data.id).toBeTypeOf('string'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { type Result, failure, success } from '@atj/common'; | ||
import { type DatabaseContext } from '@atj/database'; | ||
|
||
import type { ParsedPdf } from '../documents/pdf/parsing-api'; | ||
import type { DocumentFieldMap } from '../documents/types'; | ||
|
||
export type AddDocument = ( | ||
ctx: DatabaseContext, | ||
document: { | ||
fileName: string; | ||
data: Uint8Array; | ||
extract: { | ||
parsedPdf: ParsedPdf; | ||
fields: DocumentFieldMap; | ||
}; | ||
} | ||
) => Promise<Result<{ id: string }>>; | ||
|
||
export const addDocument: AddDocument = async (ctx, document) => { | ||
const uuid = crypto.randomUUID(); | ||
const db = await ctx.getKysely(); | ||
|
||
return await db | ||
.insertInto('form_documents') | ||
.values({ | ||
id: uuid, | ||
type: 'pdf', | ||
file_name: document.fileName, | ||
data: Buffer.from(document.data), | ||
extract: JSON.stringify(document.extract), | ||
}) | ||
.execute() | ||
.then(() => | ||
success({ | ||
id: uuid, | ||
}) | ||
) | ||
.catch(err => failure(err.message)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.