Skip to content

Commit

Permalink
make multiple namespaces configurable and added to the form
Browse files Browse the repository at this point in the history
  • Loading branch information
will-byrne committed Nov 15, 2024
1 parent ba50c74 commit 27bfe34
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 33 deletions.
18 changes: 13 additions & 5 deletions integration-tests/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('<label for="manuscript-data">Input JSON:</label>');
});
Expand All @@ -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 <a href="${url}">${url}</a>`);
});

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'));
});
Expand All @@ -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.');
});
});
Expand Down
19 changes: 16 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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
Expand All @@ -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', {
Expand All @@ -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 <a href="${url}">${url}</a>`))
.catch((error) => {
// eslint-disable-next-line no-console
Expand Down
35 changes: 35 additions & 0 deletions src/form.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { config } from './config';

export const generateForm = () => (`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Import Manuscript</title>
</head>
<body>
<form action="/input" method="post">
<h2>Manuscript Data</h2>
<label for="manuscript-data">Input JSON:</label>
<textarea id="manuscript-data" style="width: 600px; height: 280px;" name="manuscript[data]" required>
{
"id": "[ID]",
"versions": []
}
</textarea>
<br/>
<label for="temporal_namespace">Select a Namespace:</label>
<select id="temporal_namespace" name="temporalNamespace" required>
<option value="" disabled selected hidden>-- Please choose an option --</option>
${config.temporalNamespace.split(',').map((ns) => `<option value="${ns}">${ns}</option>`).join('\n')}
</select>
<br><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
`);
25 changes: 0 additions & 25 deletions src/index.html

This file was deleted.

0 comments on commit 27bfe34

Please sign in to comment.