-
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 commit '70f35d8b2040f09ef86b6204fac1a66798935031' into 681-dash…
…board-fetch-data-from-all-active-resources
- Loading branch information
Showing
14 changed files
with
142 additions
and
17 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
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
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,32 @@ | ||
import axios from 'axios' | ||
import config from '../../config/index.js' | ||
|
||
export const getBoundaryForLpa = async (boundaryId) => { | ||
try { | ||
const [datasetId, lpaId] = boundaryId.split(':') | ||
if (!datasetId || !lpaId) { | ||
return { | ||
error: 'Invalid boundary ID' | ||
} | ||
} | ||
|
||
const response = await axios.get(`${config.mainWebsiteUrl}/entity.json?dataset=${datasetId}&reference=${lpaId}`) | ||
const entity = response?.data?.entities?.[0] ?? null | ||
if (!entity || !entity?.['local-planning-authority']) { | ||
return { | ||
error: `Failed to get boundary data for ${boundaryId}. Please ensure you are sending the correct parameters.` | ||
} | ||
} | ||
|
||
const boundaryResponse = await axios.get(`${config.mainWebsiteUrl}/entity.geojson?reference=${entity['local-planning-authority']}`) | ||
return boundaryResponse.data | ||
} catch (error) { | ||
return { | ||
error: `Failed to get boundary data: ${ | ||
error.response?.status === 404 | ||
? 'LPA not found' | ||
: `Error ${error.response?.status}: ${error.response?.statusText || 'Service temporarily unavailable'}` | ||
}` | ||
} | ||
} | ||
} |
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
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
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,67 @@ | ||
import { describe, it, expect, vi } from 'vitest' | ||
import axios from 'axios' | ||
import { getBoundaryForLpa } from '../../../src/services/boundaryService.js' | ||
import config from '../../../config/index.js' | ||
|
||
vi.mock('axios') | ||
|
||
describe('getBoundaryForLpa', () => { | ||
const boundaryId = 'dataset1:lpa1' | ||
const invalidBoundaryId = 'invalidBoundaryId' | ||
const entityResponse = { | ||
data: { | ||
entities: [ | ||
{ 'local-planning-authority': 'lpa1' } | ||
] | ||
} | ||
} | ||
const boundaryResponse = { | ||
data: { type: 'FeatureCollection', features: [] } | ||
} | ||
|
||
it('should return boundary data for valid boundary ID', async () => { | ||
axios.get.mockResolvedValueOnce(entityResponse) | ||
axios.get.mockResolvedValueOnce(boundaryResponse) | ||
|
||
const result = await getBoundaryForLpa(boundaryId) | ||
|
||
expect(result).toEqual(boundaryResponse.data) | ||
expect(axios.get).toHaveBeenCalledWith(`${config.mainWebsiteUrl}/entity.json?dataset=dataset1&reference=lpa1`) | ||
expect(axios.get).toHaveBeenCalledWith(`${config.mainWebsiteUrl}/entity.geojson?reference=lpa1`) | ||
}) | ||
|
||
it('should return error for invalid boundary ID format', async () => { | ||
const result = await getBoundaryForLpa(invalidBoundaryId) | ||
|
||
expect(result).toEqual({ error: 'Invalid boundary ID' }) | ||
}) | ||
|
||
it('should return error if no entity found', async () => { | ||
axios.get.mockResolvedValueOnce({ data: { entities: [] } }) | ||
|
||
const result = await getBoundaryForLpa(boundaryId) | ||
|
||
expect(result).toEqual({ error: 'Failed to get boundary data for dataset1:lpa1. Please ensure you are sending the correct parameters.' }) | ||
expect(axios.get).toHaveBeenCalledWith(`${config.mainWebsiteUrl}/entity.json?dataset=dataset1&reference=lpa1`) | ||
}) | ||
|
||
it('should return error if no local planning authority found', async () => { | ||
axios.get.mockResolvedValueOnce({ data: { entities: [{}] } }) | ||
|
||
const result = await getBoundaryForLpa(boundaryId) | ||
|
||
expect(result).toEqual({ error: 'Failed to get boundary data for dataset1:lpa1. Please ensure you are sending the correct parameters.' }) | ||
expect(axios.get).toHaveBeenCalledWith(`${config.mainWebsiteUrl}/entity.json?dataset=dataset1&reference=lpa1`) | ||
}) | ||
|
||
it('should return error if failed to get boundary data', async () => { | ||
axios.get.mockResolvedValueOnce(entityResponse) | ||
axios.get.mockRejectedValueOnce(new Error('Network Error')) | ||
|
||
const result = await getBoundaryForLpa(boundaryId) | ||
|
||
expect(result).toEqual({ error: 'Failed to get boundary data: Error undefined: Service temporarily unavailable' }) | ||
expect(axios.get).toHaveBeenCalledWith(`${config.mainWebsiteUrl}/entity.json?dataset=dataset1&reference=lpa1`) | ||
expect(axios.get).toHaveBeenCalledWith(`${config.mainWebsiteUrl}/entity.geojson?reference=lpa1`) | ||
}) | ||
}) |
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