diff --git a/integration-tests/api.test.ts b/integration-tests/api.test.ts index f1bd9b7..e05a92a 100644 --- a/integration-tests/api.test.ts +++ b/integration-tests/api.test.ts @@ -46,7 +46,7 @@ describe('import-controller api tests', () => { await request(app) .get('/input') .expect(200) - .expect('Content-Type', 'text/html; charset=UTF-8') + .expect('Content-Type', 'text/html; charset=utf-8') .expect((response) => { expect(response.text).toContain(''); }); @@ -60,18 +60,26 @@ describe('import-controller api tests', () => { firstExecutionRunId: 4321, }); - const url = 'http://localhost:8233/namespaces/default/workflows/1234/4321'; + const url = 'http://localhost:8233/namespaces/foo/workflows/1234/4321'; await request(app) .post('/input') - .send({ manuscript: { data: JSON.stringify(requiredManuscriptData) } }) + .send({ manuscript: { data: JSON.stringify(requiredManuscriptData) }, temporalNamespace: 'foo' }) .expect(200, `Import started ${url}`); }); + it('returns 400 if namespace is not provided', async () => { + await request(app) + .post('/input') + .send({ manuscript: { data: JSON.stringify({ foo: 'bar' }) }}) + .expect(400) + .expect((response) => expect(response.body.message).toStrictEqual('missing namespace')); + }); + it('returns 400 if the form will not validate', async () => { await request(app) .post('/input') - .send({ manuscript: { data: JSON.stringify({ foo: 'bar' }) } }) + .send({ manuscript: { data: JSON.stringify({ foo: 'bar' }) }, temporalNamespace: 'foo' }) .expect(400) .expect((response) => expect(response.body.message).toStrictEqual('validation failed')); }); @@ -81,7 +89,7 @@ describe('import-controller api tests', () => { await request(app) .post('/input') - .send({ manuscript: { data: JSON.stringify(requiredManuscriptData) } }) + .send({ manuscript: { data: JSON.stringify(requiredManuscriptData) }, temporalNamespace: 'foo' }) .expect(500, 'An error occurred while processing your request: Unknown error.'); }); }); diff --git a/src/app.ts b/src/app.ts index 4cfc1d2..29a9ad2 100644 --- a/src/app.ts +++ b/src/app.ts @@ -5,6 +5,7 @@ import { Client, Connection } from '@temporalio/client'; import { randomBytes } from 'node:crypto'; import { manuscriptDataSchema } from './form-validation'; import { config } from './config'; +import { generateForm } from './form'; const app: Express = express(); @@ -16,11 +17,23 @@ app.get('/', (_, res) => { }); app.get('/input', (_, res) => { - res.sendFile(join(__dirname, 'index.html')); + res.send(generateForm()); }); app.post('/input', async (req, res) => { const input = JSON.parse(req.body.manuscript.data); + const namespace = req.body.temporalNamespace; + if (!namespace || namespace.length === 0) { + res.status(400).send({ + result: false, + message: 'missing namespace', + }); + + // eslint-disable-next-line no-console + console.error('namespace was not provided'); + return; + } + // this is not destructured because for some reason that removes the type from the value property and marks it as an any const validationResult = manuscriptDataSchema.validate(input, { abortEarly: false, allowUnknown: true }); // type for value only exists if its inside this check @@ -34,7 +47,7 @@ app.post('/input', async (req, res) => { const client = new Client({ connection, - namespace: config.temporalNamespace, + namespace, }); // send to temporal await client.workflow.start('importManuscriptData', { @@ -49,7 +62,7 @@ app.post('/input', async (req, res) => { validationResult.value, ], }) - .then((result) => `${config.temporalUi}/namespaces/${config.temporalNamespace}/workflows/${result.workflowId}/${result.firstExecutionRunId}`) + .then((result) => `${config.temporalUi}/namespaces/${namespace}/workflows/${result.workflowId}/${result.firstExecutionRunId}`) .then((url) => res.status(200).send(`Import started ${url}`)) .catch((error) => { // eslint-disable-next-line no-console diff --git a/src/form.ts b/src/form.ts new file mode 100644 index 0000000..1a9dc63 --- /dev/null +++ b/src/form.ts @@ -0,0 +1,35 @@ +import { config } from './config'; + +export const generateForm = () => (` + + + + + + Import Manuscript + + + +
+

Manuscript Data

+ + +
+ + +

+ + +
+ + + +`); diff --git a/src/index.html b/src/index.html deleted file mode 100644 index ae87f19..0000000 --- a/src/index.html +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - Import Manuscript - - - -
-

Manuscript Data

- - -

- - -
- - -