diff --git a/packages/forms/src/repository/add-form.ts b/packages/forms/src/repository/add-form.ts index 48a4b696..c272f2c7 100644 --- a/packages/forms/src/repository/add-form.ts +++ b/packages/forms/src/repository/add-form.ts @@ -10,7 +10,6 @@ export type AddForm = ( export const addForm: AddForm = async (ctx, form) => { const uuid = crypto.randomUUID(); - console.log('ctx', ctx); const db = await ctx.db.getKysely(); return db .insertInto('forms') diff --git a/packages/forms/src/repository/delete-form.ts b/packages/forms/src/repository/delete-form.ts index 34b4d7a0..022b5539 100644 --- a/packages/forms/src/repository/delete-form.ts +++ b/packages/forms/src/repository/delete-form.ts @@ -6,7 +6,7 @@ import type { FormRepositoryContext } from '.'; export type DeleteForm = ( ctx: FormRepositoryContext, formId: string -) => Promise; +) => Promise>; export const deleteForm: DeleteForm = async (ctx, formId) => { const db = await ctx.db.getKysely(); @@ -19,7 +19,7 @@ export const deleteForm: DeleteForm = async (ctx, formId) => { .executeTakeFirst(); if (!deleteResult) { - return failure('form not found'); + return failure({ message: 'form not found', code: 'not-found' as const }); } const form = JSON.parse(deleteResult.data); @@ -37,7 +37,7 @@ export const deleteForm: DeleteForm = async (ctx, formId) => { .execute() .then(_ => voidSuccess) .catch((error: Error) => { - return failure(error.message); + return failure({ message: error.message, code: 'unknown' as const }); }); }); diff --git a/packages/forms/src/repository/get-form-list.test.ts b/packages/forms/src/repository/get-form-list.test.ts index 04601c66..001704e3 100644 --- a/packages/forms/src/repository/get-form-list.test.ts +++ b/packages/forms/src/repository/get-form-list.test.ts @@ -44,6 +44,6 @@ describeDatabase('getFormList', () => { context, '45c66187-64e2-4d75-a45a-e80f1d035bc5' ); - expect(result).toBeNull(); + expect(result).toEqual({ success: true, data: null }); }); }); diff --git a/packages/forms/src/repository/get-form.test.ts b/packages/forms/src/repository/get-form.test.ts index 8a183deb..5d4441b1 100644 --- a/packages/forms/src/repository/get-form.test.ts +++ b/packages/forms/src/repository/get-form.test.ts @@ -20,7 +20,7 @@ describeDatabase('getForm', () => { { db: db.ctx, formConfig: defaultFormConfig }, '45c66187-64e2-4d75-a45a-e80f1d035bc5' ); - expect(result).toEqual(TEST_FORM); + expect(result).toEqual({ success: true, data: TEST_FORM }); }); it('return null with non-existent form', async ({ db }) => { @@ -28,7 +28,7 @@ describeDatabase('getForm', () => { { db: db.ctx, formConfig: defaultFormConfig }, '45c66187-64e2-4d75-a45a-e80f1d035bc5' ); - expect(result).toBeNull(); + expect(result).toEqual({ success: true, data: null }); }); }); diff --git a/packages/forms/src/repository/get-form.ts b/packages/forms/src/repository/get-form.ts index ad618c69..eeb11de2 100644 --- a/packages/forms/src/repository/get-form.ts +++ b/packages/forms/src/repository/get-form.ts @@ -1,10 +1,12 @@ +import { failure, success, type Result } from '@atj/common'; +import { parseFormString } from '../builder/parse-form.js'; import { type Blueprint } from '../index.js'; import type { FormRepositoryContext } from './index.js'; export type GetForm = ( ctx: FormRepositoryContext, formId: string -) => Promise; +) => Promise>; export const getForm: GetForm = async (ctx, formId) => { const db = await ctx.db.getKysely(); @@ -15,8 +17,13 @@ export const getForm: GetForm = async (ctx, formId) => { .executeTakeFirst(); if (selectResult === undefined) { - return null; + return success(null); } - return JSON.parse(selectResult.data); + const parseResult = parseFormString(ctx.formConfig, selectResult.data); + if (!parseResult.success) { + return failure(`Failed to parse form: ${parseResult.error}`); + } + + return success(parseResult.data); }; diff --git a/packages/forms/src/services/delete-form.ts b/packages/forms/src/services/delete-form.ts index a4aaa40b..f3eb1b60 100644 --- a/packages/forms/src/services/delete-form.ts +++ b/packages/forms/src/services/delete-form.ts @@ -19,8 +19,14 @@ export const deleteForm: DeleteForm = async (ctx, formId) => { message: 'You must be logged in to delete a form', }); } - const form = await ctx.repository.getForm(formId); - if (form === null) { + const formResult = await ctx.repository.getForm(formId); + if (!formResult.success) { + return failure({ + status: 500, + message: formResult.error, + }); + } + if (formResult.data === null) { return failure({ status: 404, message: `form '${formId} does not exist`, diff --git a/packages/forms/src/services/get-form-session.ts b/packages/forms/src/services/get-form-session.ts index eb3521af..13767d4d 100644 --- a/packages/forms/src/services/get-form-session.ts +++ b/packages/forms/src/services/get-form-session.ts @@ -24,15 +24,22 @@ export type GetFormSession = ( >; export const getFormSession: GetFormSession = async (ctx, opts) => { - const form = await ctx.repository.getForm(opts.formId); - if (form === null) { + const formResult = await ctx.repository.getForm(opts.formId); + if (!formResult.success) { + return failure(`Failed to retrieve form: ${formResult.error}`); + } + + if (formResult.data === null) { return failure(`form '${opts.formId} does not exist`); } // If this request corresponds to an non-existent session, return a new // session that is not yet persisted. if (opts.sessionId === undefined) { - const formSession = await createFormSession(form, opts.formRoute); + const formSession = await createFormSession( + formResult.data, + opts.formRoute + ); return success({ formId: opts.formId, data: formSession, @@ -44,7 +51,7 @@ export const getFormSession: GetFormSession = async (ctx, opts) => { console.error( `Error retrieving form session: ${formSession.error}. Returning new session.` ); - const newSession = await createFormSession(form, opts.formRoute); + const newSession = await createFormSession(formResult.data, opts.formRoute); return success({ formId: opts.formId, data: newSession, diff --git a/packages/forms/src/services/get-form.ts b/packages/forms/src/services/get-form.ts index 5b8a6e1c..d2c0f67a 100644 --- a/packages/forms/src/services/get-form.ts +++ b/packages/forms/src/services/get-form.ts @@ -15,21 +15,20 @@ export type GetForm = ( ) => Promise>; export const getForm: GetForm = async (ctx, formId) => { - const result = await ctx.repository.getForm(formId); - if (result === null) { + const formResult = await ctx.repository.getForm(formId); + if (!formResult.success) { return failure({ - status: 404, - message: 'Form not found', + status: 500, + message: formResult.error, }); } - const parseResult = parseForm(ctx.config, result); - if (!parseResult.success) { + if (formResult.data === null) { return failure({ - status: 500, - message: parseResult.error, + status: 404, + message: 'Form not found', }); } - return success(parseResult.data); + return success(formResult.data); }; diff --git a/packages/forms/src/services/save-form.test.ts b/packages/forms/src/services/save-form.test.ts index 94e3f873..3c180fef 100644 --- a/packages/forms/src/services/save-form.test.ts +++ b/packages/forms/src/services/save-form.test.ts @@ -4,6 +4,7 @@ import { createForm } from '../index.js'; import { createTestFormServiceContext } from '../testing.js'; import { saveForm } from './save-form.js'; +import { success } from '@atj/common'; const TEST_FORM = createForm({ title: 'Form Title', description: '' }); const TEST_FORM_2 = { @@ -42,9 +43,11 @@ describe('saveForm', () => { if (!result.success) { expect.fail('Failed to add form:', result.error); } - expect(result.data).toEqual({ timestamp: expect.any(Date) }); + expect(result.data).toEqual( + expect.objectContaining({ timestamp: expect.any(Date) }) + ); const savedForm = await ctx.repository.getForm(addResult.data.id); - expect(savedForm).toEqual(TEST_FORM_2); + expect(savedForm).toEqual(success(TEST_FORM_2)); }); }); diff --git a/packages/forms/src/services/submit-form.ts b/packages/forms/src/services/submit-form.ts index 1abaa2e9..533d1e6e 100644 --- a/packages/forms/src/services/submit-form.ts +++ b/packages/forms/src/services/submit-form.ts @@ -48,14 +48,18 @@ export const submitForm: SubmitForm = async ( formData, route ) => { - const form = await ctx.repository.getForm(formId); - if (form === null) { + const formResult = await ctx.repository.getForm(formId); + if (!formResult.success) { + return failure(formResult.error); + } + + if (formResult.data === null) { return failure('Form not found'); } const sessionResult = await getFormSessionOrCreate( ctx, - form, + formResult.data, route, sessionId ); @@ -75,7 +79,10 @@ export const submitForm: SubmitForm = async ( return failure(`Invalid action: ${actionString}`); } - const submitHandlerResult = registry.getHandlerForAction(form, actionString); + const submitHandlerResult = registry.getHandlerForAction( + formResult.data, + actionString + ); if (!submitHandlerResult.success) { return failure(submitHandlerResult.error); }