Repository navigation
Backfill of 'Register or renew temporary use of land or buildings to keep livestock' form - Runner #1361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Scullyon
wants to merge
2
commits into
main
Choose a base branch
from
feat/DF-1425-backfill
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+424
−4
Open
Backfill of 'Register or renew temporary use of land or buildings to keep livestock' form - Runner #1361
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { config } from '~/src/config/index.js' | ||
|
|
||
| describe('submission linking configuration', () => { | ||
| const original = process.env.SUBMISSION_LINKING_ALLOWED_FORMS | ||
|
|
||
| afterEach(() => { | ||
| if (original === undefined) { | ||
| delete process.env.SUBMISSION_LINKING_ALLOWED_FORMS | ||
| } else { | ||
| process.env.SUBMISSION_LINKING_ALLOWED_FORMS = original | ||
| } | ||
| jest.resetModules() | ||
| }) | ||
|
|
||
| /** | ||
| * Loads the config again, with the allowed forms set to the given value | ||
| */ | ||
| async function loadWith(value: string) { | ||
| process.env.SUBMISSION_LINKING_ALLOWED_FORMS = value | ||
| jest.resetModules() | ||
|
|
||
| const fresh = await import('~/src/config/index.js') | ||
|
|
||
| return fresh.config.get('submissionLinking.allowedForms') | ||
| } | ||
|
|
||
| it('allows no forms by default', () => { | ||
| expect(config.default('submissionLinking.allowedForms')).toEqual([]) | ||
| }) | ||
|
|
||
| it('reads an empty value as no forms', async () => { | ||
| await expect(loadWith(' ')).resolves.toEqual([]) | ||
| }) | ||
|
|
||
| it('reads a JSON array of forms, with or without a name', async () => { | ||
| const forms = [ | ||
| { formId: 'form-1', formName: 'Order a pizza' }, | ||
| { formId: 'form-2', formName: '' }, | ||
| { formId: 'form-3', formName: null }, | ||
| { formId: 'form-4' } | ||
| ] | ||
|
|
||
| await expect(loadWith(JSON.stringify(forms))).resolves.toEqual(forms) | ||
| }) | ||
|
|
||
| it.each([ | ||
| ['a value that is not JSON', 'form-1,form-2'], | ||
| ['a JSON value that is not an array', '{"formId":"form-1"}'] | ||
| ])('refuses %s', async (_name, value) => { | ||
| await expect(loadWith(value)).rejects.toThrow( | ||
| 'must be a JSON array of allowed forms' | ||
| ) | ||
| }) | ||
|
|
||
| it.each([ | ||
| ['a form with no ID', '[{"formName":"Order a pizza"}]'], | ||
| [ | ||
| 'a form with a name that is not a string', | ||
| '[{"formId":"form-1","formName":1}]' | ||
| ], | ||
| ['the same form twice', '[{"formId":"form-1"},{"formId":"form-1"}]'], | ||
| ['an entry that is not a form', '["form-1"]'] | ||
| ])('refuses %s', async (_name, value) => { | ||
| await expect(loadWith(value)).rejects.toThrow( | ||
| 'must be a list of allowed forms' | ||
| ) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,12 @@ import { | |
|
|
||
| const submissionUrl = config.get('submissionUrl') | ||
|
|
||
| /** | ||
| * How long the homepage waits for earlier submissions to be linked, so a slow | ||
| * submission API delays the page by no more than this | ||
| */ | ||
| const LINK_SUBMISSIONS_TIMEOUT_MS = 5000 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this give ample time for all a user's previous submissions to be linked? |
||
|
|
||
| /** | ||
| * Persists uploaded files so they are kept beyond the initial upload expiry. | ||
| * The retention period is set by the S3 lifecycle rules for the submission | ||
|
|
@@ -145,6 +151,40 @@ export async function getFormRecords(accessToken, formId, preview) { | |
| return /** @type {SavedForm[]} */ (payload) | ||
| } | ||
|
|
||
| /** | ||
| * Links the earlier submissions of a form to the account of the citizen. The | ||
| * submission API finds them by the email in the access token, and only for | ||
| * the forms it allows. | ||
| * @param {string} accessToken - the citizen's access token | ||
| * @param {string} formId - the form the submissions belong to | ||
| * @returns {Promise<LinkSubmissionsResult>} | ||
| */ | ||
| export async function linkSubmissionsToUser(accessToken, formId) { | ||
| const { res, error, payload } = await postJson( | ||
| `${submissionUrl}/form/submissions/link`, | ||
| { | ||
| payload: { formId }, | ||
| headers: { authorization: `Bearer ${accessToken}` }, | ||
| timeout: LINK_SUBMISSIONS_TIMEOUT_MS | ||
| } | ||
| ) | ||
|
|
||
| if (error) { | ||
| throw new Error( | ||
| `Could not link the submissions of form ${formId} - ${res.statusCode}`, | ||
| { cause: error } | ||
| ) | ||
| } | ||
|
|
||
| return /** @type {LinkSubmissionsResult} */ (payload) | ||
| } | ||
|
|
||
| /** | ||
| * The result of linking earlier submissions to the account of a citizen | ||
| * @typedef {object} LinkSubmissionsResult | ||
| * @property {number} linked - the number of submissions linked | ||
| */ | ||
|
|
||
| /** | ||
| * One saved form, as forms-submission-api describes it. | ||
| * @typedef {object} SavedForm | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bit chicken-and-egg. Do we read the forms first to determine if linking has already been done (and therefore don;t need a 'link' API call each time) or do we check if linked (using a 'link' API call) even though this will become redundant after the first time it happens for this form/user.
Is there a way to tell forms are linked purely on the 'getForms'-type call, because if there was, we could avoid the extra 'link' API call every time?