Skip to content

Commit

Permalink
Call parseStringForm() in getForm db function
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnaab committed Nov 25, 2024
1 parent 7b8ae25 commit 8377230
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 31 deletions.
1 change: 0 additions & 1 deletion packages/forms/src/repository/add-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
6 changes: 3 additions & 3 deletions packages/forms/src/repository/delete-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { FormRepositoryContext } from '.';
export type DeleteForm = (
ctx: FormRepositoryContext,
formId: string
) => Promise<VoidResult>;
) => Promise<VoidResult<{ message: string; code: 'not-found' | 'unknown' }>>;

export const deleteForm: DeleteForm = async (ctx, formId) => {
const db = await ctx.db.getKysely();
Expand All @@ -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);
Expand All @@ -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 });
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/forms/src/repository/get-form-list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ describeDatabase('getFormList', () => {
context,
'45c66187-64e2-4d75-a45a-e80f1d035bc5'
);
expect(result).toBeNull();
expect(result).toEqual({ success: true, data: null });
});
});
4 changes: 2 additions & 2 deletions packages/forms/src/repository/get-form.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ 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<DbTestContext>('return null with non-existent form', async ({ db }) => {
const result = await getForm(
{ db: db.ctx, formConfig: defaultFormConfig },
'45c66187-64e2-4d75-a45a-e80f1d035bc5'
);
expect(result).toBeNull();
expect(result).toEqual({ success: true, data: null });
});
});

Expand Down
13 changes: 10 additions & 3 deletions packages/forms/src/repository/get-form.ts
Original file line number Diff line number Diff line change
@@ -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<Blueprint | null>;
) => Promise<Result<Blueprint | null>>;

export const getForm: GetForm = async (ctx, formId) => {
const db = await ctx.db.getKysely();
Expand All @@ -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);
};
10 changes: 8 additions & 2 deletions packages/forms/src/services/delete-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
15 changes: 11 additions & 4 deletions packages/forms/src/services/get-form-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
17 changes: 8 additions & 9 deletions packages/forms/src/services/get-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,20 @@ export type GetForm = (
) => Promise<Result<Blueprint, GetFormError>>;

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);
};
7 changes: 5 additions & 2 deletions packages/forms/src/services/save-form.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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));
});
});
15 changes: 11 additions & 4 deletions packages/forms/src/services/submit-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand All @@ -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);
}
Expand Down

0 comments on commit 8377230

Please sign in to comment.