Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
},
"dependencies": {
"@aws-sdk/client-sqs": "3.894.0",
"@defra/forms-engine-plugin": "3.0.4",
"@defra/forms-model": "3.0.552",
"@defra/forms-engine-plugin": "3.0.9",
"@defra/forms-model": "3.0.559",
"@defra/hapi-tracing": "^1.28.0",
"@elastic/ecs-pino-format": "^1.5.0",
"@hapi/hapi": "^21.4.2",
Expand Down
4 changes: 2 additions & 2 deletions src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ export const config = convict({
designerUrl: {
doc: 'URL to call Forms Designer',
format: String,
default: null,
default: '',
env: 'DESIGNER_URL'
},
managerUrl: {
doc: 'URL to call Forms Manager API',
format: String,
default: null,
default: '',
env: 'MANAGER_URL'
},
isSecureContextEnabled: {
Expand Down
21 changes: 18 additions & 3 deletions src/lib/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ const managerUrl = config.get('managerUrl')
* @returns {Promise<FormDefinition>}
*/
export async function getFormDefinition(formId, formStatus, versionNumber) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!managerUrl) {
// TS / eslint conflict
throw new Error('Missing MANAGER_URL')
}

Expand All @@ -33,5 +31,22 @@ export async function getFormDefinition(formId, formStatus, versionNumber) {
}

/**
* @import { FormDefinition } from '@defra/forms-model'
* Gets the form metadata from the Forms Manager API
* @param {string} formId
* @returns {Promise<FormMetadata>}
*/
export async function getFormMetadata(formId) {
if (!managerUrl) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this eslint-disable-next-line needed? Shouldn't config.get('managerUrl') be string | undefined?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied from previous method. Unfortunately the types from config.get() aren't correct (for all config items) so coerced the assignment. Let me know if you're happy with that or if you want me to do something more drastic with the config.get stuff

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for sorting that Jez 🎉

throw new Error('Missing MANAGER_URL')
}

const formUrl = new URL(`/forms/${formId}`, managerUrl)

const { body } = await getJson(formUrl)

return body
}

/**
* @import { FormDefinition, FormMetadata } from '@defra/forms-model'
*/
226 changes: 132 additions & 94 deletions src/lib/manager.test.js
Original file line number Diff line number Diff line change
@@ -1,111 +1,149 @@
import { FormStatus } from '@defra/forms-model'
import { buildDefinition } from '@defra/forms-model/stubs'
import { buildDefinition, buildMetaData } from '@defra/forms-model/stubs'

import { getJson } from '~/src/lib/fetch.js'
import { getFormDefinition } from '~/src/lib/manager.js'
import { getFormDefinition, getFormMetadata } from '~/src/lib/manager.js'
jest.mock('~/src/lib/fetch.js')
jest.mock('~/src/config/index.js', () => ({
config: {
get: jest.fn().mockReturnValueOnce('http://forms-manager')
}
}))

describe('getDefinition', () => {
it('should get the current definition if draft', async () => {
const expectedDefinition = buildDefinition()
const formId = '68a890909ab460290c289409'
jest
.mocked(getJson)
.mockResolvedValueOnce({ response: {}, body: expectedDefinition })
const definition = await getFormDefinition(
formId,
FormStatus.Draft,
undefined
)
expect(getJson).toHaveBeenCalledWith(
expect.objectContaining({
href: 'http://forms-manager/forms/68a890909ab460290c289409/definition/draft'
})
)
expect(definition).toEqual(expectedDefinition)
})
describe('Manager', () => {
describe('getDefinition', () => {
it('should get the current definition if draft', async () => {
const expectedDefinition = buildDefinition()
const formId = '68a890909ab460290c289409'
jest
.mocked(getJson)
.mockResolvedValueOnce({ response: {}, body: expectedDefinition })
const definition = await getFormDefinition(
formId,
FormStatus.Draft,
undefined
)
expect(getJson).toHaveBeenCalledWith(
expect.objectContaining({
href: 'http://forms-manager/forms/68a890909ab460290c289409/definition/draft'
})
)
expect(definition).toEqual(expectedDefinition)
})

it('should get the current definition if live', async () => {
const expectedDefinition = buildDefinition()
const formId = '68a890909ab460290c289409'
jest
.mocked(getJson)
.mockResolvedValueOnce({ response: {}, body: expectedDefinition })
const definition = await getFormDefinition(
formId,
FormStatus.Live,
undefined
)
expect(getJson).toHaveBeenCalledWith(
expect.objectContaining({
href: 'http://forms-manager/forms/68a890909ab460290c289409/definition/'
})
)
expect(definition).toEqual(expectedDefinition)
})
it('should get the current definition if live', async () => {
const expectedDefinition = buildDefinition()
const formId = '68a890909ab460290c289409'
jest
.mocked(getJson)
.mockResolvedValueOnce({ response: {}, body: expectedDefinition })
const definition = await getFormDefinition(
formId,
FormStatus.Live,
undefined
)
expect(getJson).toHaveBeenCalledWith(
expect.objectContaining({
href: 'http://forms-manager/forms/68a890909ab460290c289409/definition/'
})
)
expect(definition).toEqual(expectedDefinition)
})

it('should get a specific version if version number is provided', async () => {
const expectedDefinition = buildDefinition()
const formId = '68a890909ab460290c289409'
const versionNumber = 3
jest
.mocked(getJson)
.mockResolvedValueOnce({ response: {}, body: expectedDefinition })
const definition = await getFormDefinition(
formId,
FormStatus.Live,
versionNumber
)
expect(getJson).toHaveBeenCalledWith(
expect.objectContaining({
href: 'http://forms-manager/forms/68a890909ab460290c289409/versions/3/definition'
})
)
expect(definition).toEqual(expectedDefinition)
})
it('should get a specific version if version number is provided', async () => {
const expectedDefinition = buildDefinition()
const formId = '68a890909ab460290c289409'
const versionNumber = 3
jest
.mocked(getJson)
.mockResolvedValueOnce({ response: {}, body: expectedDefinition })
const definition = await getFormDefinition(
formId,
FormStatus.Live,
versionNumber
)
expect(getJson).toHaveBeenCalledWith(
expect.objectContaining({
href: 'http://forms-manager/forms/68a890909ab460290c289409/versions/3/definition'
})
)
expect(definition).toEqual(expectedDefinition)
})

it('should get a specific version with draft status if version number is provided', async () => {
const expectedDefinition = buildDefinition()
const formId = '68a890909ab460290c289409'
const versionNumber = 1
jest
.mocked(getJson)
.mockResolvedValueOnce({ response: {}, body: expectedDefinition })
const definition = await getFormDefinition(
formId,
FormStatus.Draft,
versionNumber
)
expect(getJson).toHaveBeenCalledWith(
expect.objectContaining({
href: 'http://forms-manager/forms/68a890909ab460290c289409/versions/1/definition'
})
)
expect(definition).toEqual(expectedDefinition)
})

it('should handle version number 0 correctly', async () => {
const expectedDefinition = buildDefinition()
const formId = '68a890909ab460290c289409'
const versionNumber = 0
jest
.mocked(getJson)
.mockResolvedValueOnce({ response: {}, body: expectedDefinition })
const definition = await getFormDefinition(
formId,
FormStatus.Live,
versionNumber
)
expect(getJson).toHaveBeenCalledWith(
expect.objectContaining({
href: 'http://forms-manager/forms/68a890909ab460290c289409/versions/0/definition'
})
)
expect(definition).toEqual(expectedDefinition)
})

it('should get a specific version with draft status if version number is provided', async () => {
const expectedDefinition = buildDefinition()
const formId = '68a890909ab460290c289409'
const versionNumber = 1
jest
.mocked(getJson)
.mockResolvedValueOnce({ response: {}, body: expectedDefinition })
const definition = await getFormDefinition(
formId,
FormStatus.Draft,
versionNumber
)
expect(getJson).toHaveBeenCalledWith(
expect.objectContaining({
href: 'http://forms-manager/forms/68a890909ab460290c289409/versions/1/definition'
})
)
expect(definition).toEqual(expectedDefinition)
it('should throw if no manager url set', async () => {
const expectedDefinition = buildDefinition()
const formId = '68a890909ab460290c289409'
jest
.mocked(getJson)
.mockResolvedValueOnce({ response: {}, body: expectedDefinition })
const definition = await getFormDefinition(
formId,
FormStatus.Draft,
undefined
)
expect(getJson).toHaveBeenCalledWith(
expect.objectContaining({
href: 'http://forms-manager/forms/68a890909ab460290c289409/definition/draft'
})
)
expect(definition).toEqual(expectedDefinition)
})
})

it('should handle version number 0 correctly', async () => {
const expectedDefinition = buildDefinition()
const formId = '68a890909ab460290c289409'
const versionNumber = 0
jest
.mocked(getJson)
.mockResolvedValueOnce({ response: {}, body: expectedDefinition })
const definition = await getFormDefinition(
formId,
FormStatus.Live,
versionNumber
)
expect(getJson).toHaveBeenCalledWith(
expect.objectContaining({
href: 'http://forms-manager/forms/68a890909ab460290c289409/versions/0/definition'
})
)
expect(definition).toEqual(expectedDefinition)
describe('getMetadata', () => {
it('should get the metadata', async () => {
const expectedMetadata = buildMetaData()
const formId = '68a890909ab460290c289409'
jest
.mocked(getJson)
.mockResolvedValueOnce({ response: {}, body: expectedMetadata })
const metadata = await getFormMetadata(formId)
expect(getJson).toHaveBeenCalledWith(
expect.objectContaining({
href: 'http://forms-manager/forms/68a890909ab460290c289409'
})
)
expect(metadata).toEqual(expectedMetadata)
})
})
})
4 changes: 2 additions & 2 deletions src/service/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { handleFormSubmissionEvents } from '~/src/service/events.js'
import { sendNotifyEmail } from '~/src/service/notify.js'
import { sendNotifyEmails } from '~/src/service/notify.js'

/**
* @param {Message[]} messages
* @returns {Promise<{saved: FormAdapterSubmissionMessage[]; failed: any[]}>}
*/
export async function handleEvent(messages) {
const service = {
handleFormSubmission: sendNotifyEmail
handleFormSubmission: sendNotifyEmails
}
return handleFormSubmissionEvents(messages, service)
}
Expand Down
Loading
Loading