-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #112 from digital-land/feat/lpa-autoselect
Feat/lpa autoselect
- Loading branch information
Showing
8 changed files
with
194 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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,19 @@ | ||
import PageController from './pageController.js' | ||
import { fetchLocalAuthorities } from '../utils/fetchLocalAuthorities.js' | ||
|
||
class LpaDetailsController extends PageController { | ||
async locals (req, res, next) { | ||
const localAuthoritiesNames = await fetchLocalAuthorities() | ||
|
||
const listItems = localAuthoritiesNames.map(name => ({ | ||
text: name, | ||
value: name | ||
})) | ||
|
||
req.form.options.localAuthorities = listItems | ||
|
||
super.locals(req, res, next) | ||
} | ||
} | ||
|
||
export default LpaDetailsController |
This file contains 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 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 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,42 @@ | ||
import axios from 'axios' | ||
|
||
/** | ||
* Fetches a list of local authority names from a specified dataset. | ||
* | ||
* This function queries a dataset for local authorities, extracting a distinct list of names. | ||
* It performs an HTTP GET request to retrieve the data, then processes the response to return | ||
* only the names of the local authorities. | ||
* | ||
* @returns {Promise<string[]>} A promise that resolves to an array of local authority names. | ||
* @throws {Error} Throws an error if the HTTP request fails or data processing encounters an issue. | ||
*/ | ||
export const fetchLocalAuthorities = async () => { | ||
const sql = `select | ||
distinct provision.organisation, | ||
organisation.name, | ||
organisation.dataset | ||
from | ||
provision, | ||
organisation | ||
where | ||
provision.organisation = organisation.organisation | ||
order by | ||
provision.organisation` | ||
|
||
const url = `https://datasette.planning.data.gov.uk/digital-land.json?sql=${encodeURIComponent(sql)}` | ||
try { | ||
const response = await axios.get(url) | ||
const names = response.data.rows.map(row => { | ||
if (row[1] === null) { | ||
console.log('Null value found in response:', row) | ||
return null | ||
} else { | ||
return row[1] | ||
} | ||
}).filter(name => name !== null) // Filter out null values | ||
return names // Return the fetched data | ||
} catch (error) { | ||
console.error('Error fetching local authorities data:', error) | ||
throw error // Rethrow the error to be handled by the caller | ||
} | ||
} |
This file contains 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 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 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,41 @@ | ||
import axios from 'axios' | ||
import { vi, it, describe, expect } from 'vitest' | ||
import { fetchLocalAuthorities } from '../../src/utils/fetchLocalAuthorities' | ||
|
||
// Mock axios.get to return a fake response | ||
vi.mock('axios') | ||
axios.get.mockResolvedValue({ | ||
data: { | ||
rows: [ | ||
[1, 'Local Authority 1'], | ||
[2, 'Local Authority 2'], | ||
[3, 'Local Authority 3'] | ||
] | ||
} | ||
}) | ||
|
||
describe('fetchLocalAuthorities', () => { | ||
it('should fetch local authority names', async () => { | ||
const result = await fetchLocalAuthorities() | ||
expect(result).toEqual(['Local Authority 1', 'Local Authority 2', 'Local Authority 3']) | ||
}) | ||
|
||
it('should throw an error if the HTTP request fails', async () => { | ||
axios.get.mockRejectedValue(new Error('Failed to fetch data')) | ||
await expect(fetchLocalAuthorities()).rejects.toThrow('Failed to fetch data') | ||
}) | ||
|
||
it('should throw an error if data processing encounters an issue', async () => { | ||
axios.get.mockResolvedValue({ | ||
data: { | ||
rows: [ | ||
[1, 'Local Authority 1'], | ||
[2, null], // Simulate null value in the response | ||
[3, 'Local Authority 3'] | ||
] | ||
} | ||
}) | ||
const result = await fetchLocalAuthorities() | ||
expect(result).toEqual(['Local Authority 1', 'Local Authority 3']) | ||
}) | ||
}) |
This file contains 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,66 @@ | ||
/* eslint-disable no-import-assign */ | ||
/* eslint-disable new-cap */ | ||
|
||
import PageController from '../../src/controllers/pageController.js' | ||
import { vi, it, describe, expect, beforeEach, afterEach } from 'vitest' | ||
|
||
vi.mock('../../src/utils/fetchLocalAuthorities.js') | ||
|
||
describe('lpaDetailsController', async () => { | ||
let fetchLocalAuthorities | ||
let controller | ||
|
||
beforeEach(async () => { | ||
fetchLocalAuthorities = await import('../../src/utils/fetchLocalAuthorities') | ||
const LpaDetailsController = await import('../../src/controllers/lpaDetailsController.js') | ||
controller = new LpaDetailsController.default({ | ||
route: '/lpa-details' | ||
}) | ||
}) | ||
|
||
afterEach(() => { | ||
vi.restoreAllMocks() | ||
}) | ||
|
||
describe('locals', () => { | ||
it('should set localAuthorities options in the form', async () => { | ||
const req = { | ||
form: { | ||
options: {} | ||
} | ||
} | ||
const res = {} | ||
const next = vi.fn() | ||
|
||
const localAuthoritiesNames = ['Authority 1', 'Authority 2'] | ||
|
||
fetchLocalAuthorities.fetchLocalAuthorities = vi.fn().mockResolvedValue(localAuthoritiesNames) | ||
|
||
await controller.locals(req, res, next) | ||
|
||
expect(fetchLocalAuthorities.fetchLocalAuthorities).toHaveBeenCalled() | ||
expect(req.form.options.localAuthorities).toEqual([ | ||
{ text: 'Authority 1', value: 'Authority 1' }, | ||
{ text: 'Authority 2', value: 'Authority 2' } | ||
]) | ||
expect(next).toHaveBeenCalled() | ||
}) | ||
|
||
it('should call super.locals', async () => { | ||
const req = { | ||
form: { | ||
options: {} | ||
} | ||
} | ||
const res = {} | ||
const next = vi.fn() | ||
|
||
fetchLocalAuthorities.fetchLocalAuthorities = vi.fn().mockResolvedValue([]) | ||
const superLocalsSpy = vi.spyOn(PageController.prototype, 'locals') | ||
|
||
await controller.locals(req, res, next) | ||
|
||
expect(superLocalsSpy).toHaveBeenCalledWith(req, res, next) | ||
}) | ||
}) | ||
}) |