Skip to content
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

Feat/org finder/datasette query #206

Merged
merged 8 commits into from
Aug 6, 2024
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
4 changes: 2 additions & 2 deletions .github/workflows/featureDeploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ jobs:
strategy:
matrix:
environment: ${{ fromJSON(needs.detect-environments.outputs.environments) }}
if: ${{ matrix.environment != 'production' }}
if: ${{ inputs.environment != 'production' }}
uses: ./.github/workflows/deploy.yml
with:
environment: '${{ matrix.environment }}'
environment: '${{ inputs.environment }}'
secrets: inherit


4 changes: 3 additions & 1 deletion src/assets/js/list-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
/* eslint-disable no-var */
//= require govuk_publishing_components/vendor/polyfills/closest

const keyPauseTime = 20

window.GOVUK = window.GOVUK || {}
window.GOVUK.Modules = window.GOVUK.Modules || {};

Expand Down Expand Up @@ -34,7 +36,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
clearTimeout(this.filterTimeout)
this.filterTimeout = setTimeout(function () {
this.$module.filterList(searchTerm)
}.bind(this), 200)
}.bind(this), keyPauseTime)
}.bind(this))
}

Expand Down
104 changes: 28 additions & 76 deletions src/controllers/OrganisationsController.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datasette from '../services/datasette.js'
import performanceDbApi from '../services/performanceDbApi.js' // Assume you have an API service module
import logger from '../utils/logger.js'
import { dataSubjects } from '../utils/utils.js'
Expand Down Expand Up @@ -70,85 +71,36 @@ const organisationsController = {
}
},

/**
* Handles the GET /organisations request
*
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
async getOrganisations (req, res, next) {
const alphabetisedOrgs = {
A: [
{
name: 'Aberdeen'
},
{
name: 'Aylesbury'
},
{
name: 'Ashford'
}
],
B: [
{
name: 'Bath'
},
{
name: 'Birmingham'
},
{
name: 'Brighton'
}
],
C: [
{
name: 'Cambridge'
},
{
name: 'Cardiff'
},
{
name: 'Cheltenham'
},
{
name: 'Chester'
}
],
D: [
{
name: 'Derby'
},
{
name: 'Dundee'
}
],
E: [
{
name: 'Edinburgh'
},
{
name: 'Epsom'
}
],
G: [
{
name: 'Glasgow'
},
{
name: 'Gloucester'
}
],
H: [
{
name: 'Hull'
}
],
L: [
{
name: 'Leeds'
},
{
name: 'London'
}
]
}
try {
const sql = 'select name, organisation from organisation'
const result = await datasette.runQuery(sql)

const sortedResults = result.formattedData.sort((a, b) => {
return a.name.localeCompare(b.name)
})

res.render('organisations/find.html', { alphabetisedOrgs })
const alphabetisedOrgs = sortedResults.reduce((acc, current) => {
const firstLetter = current.name.charAt(0).toUpperCase()
acc[firstLetter] = acc[firstLetter] || []
acc[firstLetter].push(current)
return acc
}, {})

res.render('organisations/find.html', { alphabetisedOrgs })
} catch (err) {
logger.warn(err)
next(err)
}
}

}

export default organisationsController
1 change: 0 additions & 1 deletion src/views/organisations/find.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ <h1 class="govuk-heading-xl govuk-!-margin-bottom-5">{{ pageName }}</h1>

<div class="govuk-grid-row">
<div class="govuk-grid-column-full" id="search_results">

{% for letter, orgs in alphabetisedOrgs %}
<div class="govuk-grid-row js-filter-item" data-filter="block">
<div class="govuk-grid-column-one-third">
Expand Down
77 changes: 72 additions & 5 deletions test/unit/organisationsController.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { describe, it, vi, expect, beforeEach } from 'vitest'
import LpaOverviewController from '../../src/controllers/OrganisationsController.js'
import organisationsController from '../../src/controllers/OrganisationsController.js'
import performanceDbApi from '../../src/services/performanceDbApi.js'
import datasette from '../../src/services/datasette.js'

vi.mock('../../src/services/performanceDbApi.js')
vi.mock('../../src/utils/utils.js', () => {
return {
dataSubjects: {}
}
})
vi.mock('../../src/services/datasette.js')

describe('OrganisationsController.js', () => {
beforeEach(() => {
Expand All @@ -31,7 +33,7 @@ describe('OrganisationsController.js', () => {

performanceDbApi.getLpaOverview = vi.fn().mockResolvedValue(expectedResponse)

await LpaOverviewController.getOverview(req, res, next)
await organisationsController.getOverview(req, res, next)

expect(res.render).toHaveBeenCalledTimes(1)
expect(res.render).toHaveBeenCalledWith('organisations/overview.html', expect.objectContaining({
Expand All @@ -57,18 +59,83 @@ describe('OrganisationsController.js', () => {

vi.mocked(performanceDbApi.getLpaOverview).mockRejectedValue(error)

await LpaOverviewController.getOverview(req, res, next)
await organisationsController.getOverview(req, res, next)

expect(next).toHaveBeenCalledTimes(1)
expect(next).toHaveBeenCalledWith(error)
})
})

describe('find', () => {
it.todo('should render the find page', () => {
it('should call render with the find page', async () => {
const req = {}
const res = { render: vi.fn() }
const next = vi.fn()

vi.mocked(datasette.runQuery).mockResolvedValue({ formattedData: [] })

await organisationsController.getOrganisations(req, res, next)

expect(res.render).toHaveBeenCalledTimes(1)
expect(res.render).toHaveBeenCalledWith('organisations/find.html', expect.objectContaining({
alphabetisedOrgs: {}
}))
})

it('should correctly sort and restructure the data recieved from datasette, then pass it on to the template', async () => {
const req = {}
const res = { render: vi.fn() }
const next = vi.fn()

const datasetteResponse = [
{ name: 'Aardvark Healthcare', organisation: 'Aardvark Healthcare' },
{ name: 'Bath NHS Trust', organisation: 'Bath NHS Trust' },
{ name: 'Bristol Hospital', organisation: 'Bristol Hospital' },
{ name: 'Cardiff Health Board', organisation: 'Cardiff Health Board' },
{ name: 'Derbyshire Healthcare', organisation: 'Derbyshire Healthcare' },
{ name: 'East Sussex NHS Trust', organisation: 'East Sussex NHS Trust' }
]

vi.mocked(datasette.runQuery).mockResolvedValue({ formattedData: datasetteResponse })

await organisationsController.getOrganisations(req, res, next)

expect(res.render).toHaveBeenCalledTimes(1)
expect(res.render).toHaveBeenCalledWith('organisations/find.html', expect.objectContaining({
alphabetisedOrgs: {
A: [
{ name: 'Aardvark Healthcare', organisation: 'Aardvark Healthcare' }
],
B: [
{ name: 'Bath NHS Trust', organisation: 'Bath NHS Trust' },
{ name: 'Bristol Hospital', organisation: 'Bristol Hospital' }
],
C: [
{ name: 'Cardiff Health Board', organisation: 'Cardiff Health Board' }
],
D: [
{ name: 'Derbyshire Healthcare', organisation: 'Derbyshire Healthcare' }
],
E: [
{ name: 'East Sussex NHS Trust', organisation: 'East Sussex NHS Trust' }
]
}
}))
})

it.todo('should catch errors and pass them onto the next function')
it('should catch errors and pass them onto the next function', async () => {
const req = {}
const res = {}
const next = vi.fn()

const error = new Error('Test error')

vi.mocked(datasette.runQuery).mockRejectedValue(error)

await organisationsController.getOrganisations(req, res, next)

expect(next).toHaveBeenCalledTimes(1)
expect(next).toHaveBeenCalledWith(error)
})
})
})