Skip to content

Commit

Permalink
pagination options and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeGoodall committed Oct 11, 2024
1 parent 82044cb commit 1efefb4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 36 deletions.
10 changes: 9 additions & 1 deletion src/middleware/common.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,19 @@ export const getEntryNumbersWithIssues = (req, res, next) => {
}

export const fetchEntitiesFromOrganisationAndEntryNumbers = fetchMany({
query: ({ req, params }) => performanceDbApi.fetchEntityNumbersFromEntryNumbers({ entryNumbers: req.entryNumbersWithIssues, organisationEntity: req.orgInfo.entity }),
query: ({ req, params }) => performanceDbApi.fetchEntitiesFromEntryNumbers({ entryNumbers: req.entryNumbersWithIssues, organisationEntity: req.orgInfo.entity, pagination: req.pagination }),
result: 'entities',
dataset: FetchOptions.fromParams
})

export const getPaginationOptions = (resultsCount) => (req, res, next) => {
const { pageNumber } = req.params

req.pagination = { offset: pageNumber * resultsCount, limit: resultsCount }

next()
}

export const extractJsonFieldFromEntities = (req, res, next) => {
const { entities } = req

Expand Down
6 changes: 5 additions & 1 deletion src/middleware/issueTable.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
fetchSpecification,
formatErrorSummaryParams,
getEntryNumbersWithIssues,
getPaginationOptions,
isResourceIdNotInParams,
logPageError,
nestEntityFields,
Expand All @@ -24,7 +25,7 @@ import {
import { fetchIf, parallel, renderTemplate } from './middleware.builders.js'
import * as v from 'valibot'

const paginationPageLength = 50
const paginationPageLength = 20

export const IssueTableQueryParams = v.object({
lpa: v.string(),
Expand All @@ -42,6 +43,8 @@ const validateIssueTableQueryParams = validateQueryParams.bind({
export const setDefaultQueryParams = (req, res, next) => {
if (!req.params.pageNumber) {
req.params.pageNumber = 1
} else {
req.params.pageNumber = parseInt(req.params.pageNumber)
}
next()
}
Expand Down Expand Up @@ -166,6 +169,7 @@ export default [
fetchIf(isResourceIdNotInParams, fetchLatestResource, takeResourceIdFromParams),
fetchSpecification,
pullOutDatasetSpecification,
getPaginationOptions(paginationPageLength),
fetchIssues,
getEntryNumbersWithIssues,
fetchEntitiesFromOrganisationAndEntryNumbers,
Expand Down
37 changes: 3 additions & 34 deletions src/services/performanceDbApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,46 +450,15 @@ export default {
return result.formattedData[0].entity_count
},

entitiesAndIssuesQuery ({ resource, pagination, issueType, issueField }) {
return /* sql */ `
SELECT
e.*,
fr.entry_number,
'{' || GROUP_CONCAT(
'"' || i.field || '": "' || i.issue_type || '"',
',' || CHAR(10)
) || '}' AS issues
from
entity e
LEFT JOIN (
SELECT
DISTINCT fr.entry_number,
f.entity
FROM
fact_resource fr
INNER JOIN fact f ON fr.fact = f.fact
WHERE
fr.resource = '${resource}'
) fr ON fr.entity = e.entity
LEFT JOIN issue i ON i.entry_number = fr.entry_number
WHERE i.resource = '${resource}'
AND i.issue_type = '${issueType}'
AND i.field = '${issueField}'
GROUP BY
(e.entity)
LIMIT ${pagination.limit}
OFFSET ${pagination.offset}
`
},

fetchEntityNumbersFromEntryNumbers ({ entryNumbers, organisationEntity }) {
fetchEntitiesFromEntryNumbers ({ entryNumbers, organisationEntity, pagination }) {
return /* sql */ `
select DISTINCT f.entity, fr.entry_number, fr.resource, e.* from fact f
LEFT JOIN fact_resource fr ON f.fact = fr.fact
LEFT JOIN entity e ON f.entity = e.entity
WHERE e.organisation_entity = ${organisationEntity}
AND entry_number in (${entryNumbers.join(', ')})
LIMIT ${pagination.limit}
OFFSET ${pagination.offset}
`
}
}

0 comments on commit 1efefb4

Please sign in to comment.