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

fix: a few type errors #589

Merged
merged 19 commits into from
Oct 31, 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
13 changes: 11 additions & 2 deletions src/middleware/common.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,17 @@ export const fetchDatasetInfo = fetchOne({
* @returns {boolean}
*/
export const isResourceAccessible = (req) => req.resourceStatus.status === '200'
export const isResourceIdValid = (req) => req.resourceStatus.resource.trim() !== ''
export const isResourceNotAccessible = (req) => !isResourceAccessible(req)
export const isResourceIdInParams = ({ params }) => !('resourceId' in params)
export const isResourceDataPresent = (req) => 'resource' in req
rosado marked this conversation as resolved.
Show resolved Hide resolved

export const and = (...args) => {
return (req) => args.every(arg => arg(req))
}
export const or = (...args) => {
return (req) => args.some(arg => arg(req))
}

/**
* Middleware. Updates req with `resource`.
Expand Down Expand Up @@ -72,7 +81,7 @@ export const fetchOrgInfo = fetchOne({

/**
* Middleware. Validates query params according to schema.
* Short circuits with 400 error if validation fails
* Short circuits with 400 error if validation fails. Potentially updates req with `parsedParams`
*
* `this` needs: `{ schema }`
*
Expand All @@ -82,7 +91,7 @@ export const fetchOrgInfo = fetchOne({
*/
export function validateQueryParamsFn (req, res, next) {
try {
v.parse(this.schema || v.any(), req.params)
req.parsedParams = v.parse(this.schema || v.any(), req.params)
next()
} catch (error) {
res.status(400).render('errorPages/400', {})
Expand Down
35 changes: 25 additions & 10 deletions src/middleware/datasetOverview.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { fetchDatasetInfo, fetchLatestResource, fetchLpaDatasetIssues, fetchOrgI
import { fetchOne, fetchIf, fetchMany, renderTemplate, FetchOptions, onlyIf } from './middleware.builders.js'
import { fetchResourceStatus, prepareDatasetTaskListErrorTemplateParams } from './datasetTaskList.middleware.js'
import performanceDbApi from '../services/performanceDbApi.js'
import logger from '../utils/logger.js'
import { types } from '../utils/logging.js'

const fetchColumnSummary = fetchMany({
query: ({ params }) => `
Expand Down Expand Up @@ -36,11 +38,24 @@ const fetchSpecification = fetchOne({
result: 'specification'
})

/**
* Middleware. Updates req with `datasetSpecification`
*
* @param req
* @param res
* @param next
*/
export const pullOutDatasetSpecification = (req, res, next) => {
const { specification } = req
const collectionSpecifications = JSON.parse(specification.json)
const datasetSpecification = collectionSpecifications.find((spec) => spec.dataset === req.dataset.dataset)
req.specification = datasetSpecification
let collectionSpecifications
try {
collectionSpecifications = JSON.parse(specification.json)
} catch (e) {
// we can proceed but we probably should notify the user the displayed data may not be complete
logger.info('failed to parse specification JSON', { type: types.DataValidation, collection: req.dataset.collection })
collectionSpecifications = []
}
req.datasetSpecification = collectionSpecifications.find((spec) => spec.dataset === req.dataset.dataset)
next()
}
rosado marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -105,21 +120,21 @@ const fetchEntityCount = fetchOne({
})

export const prepareDatasetOverviewTemplateParams = (req, res, next) => {
const { orgInfo, specification, columnSummary, entityCount, sources, dataset, issues } = req
const { orgInfo, datasetSpecification, columnSummary, entityCount, sources, dataset, issues } = req

const mappingFields = columnSummary[0]?.mapping_field?.split(';') ?? []
const nonMappingFields = columnSummary[0]?.non_mapping_field?.split(';') ?? []
const allFields = [...mappingFields, ...nonMappingFields]

const numberOfFieldsSupplied = specification.fields.map(field => field.field).reduce((acc, current) => {
return allFields.includes(current) ? acc + 1 : acc
const specFields = datasetSpecification ? datasetSpecification.fields : []
const numberOfFieldsSupplied = specFields.reduce((acc, field) => {
return allFields.includes(field.field) ? acc + 1 : acc
}, 0)

const numberOfFieldsMatched = specification.fields.map(field => field.field).reduce((acc, current) => {
return mappingFields.includes(current) ? acc + 1 : acc
const numberOfFieldsMatched = specFields.reduce((acc, field) => {
return mappingFields.includes(field.field) ? acc + 1 : acc
rosado marked this conversation as resolved.
Show resolved Hide resolved
}, 0)

const numberOfExpectedFields = specification.fields.length
const numberOfExpectedFields = specFields.length

// I'm pretty sure every endpoint has a separate documentation-url, but this isn't currently represented in the performance db. need to double check this and update if so
const endpoints = sources.sort((a, b) => {
Expand Down
19 changes: 16 additions & 3 deletions src/middleware/datasetTaskList.middleware.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import { fetchDatasetInfo, isResourceAccessible, isResourceNotAccessible, fetchLatestResource, fetchEntityCount, logPageError, fetchLpaDatasetIssues, validateQueryParams, getDatasetTaskListError } from './common.middleware.js'
import {
fetchDatasetInfo,
isResourceAccessible,
isResourceNotAccessible,
fetchLatestResource,
fetchEntityCount,
logPageError,
fetchLpaDatasetIssues,
validateQueryParams,
getDatasetTaskListError,
isResourceIdValid, and
} from './common.middleware.js'
import { fetchOne, fetchIf, onlyIf, renderTemplate } from './middleware.builders.js'
import performanceDbApi from '../services/performanceDbApi.js'
import { statusToTagClass } from '../filters/filters.js'
Expand Down Expand Up @@ -46,7 +57,6 @@ export const prepareDatasetTaskListTemplateParams = (req, res, next) => {
const { issues, entityCount: entityCountRow, params, dataset, orgInfo: organisation } = req
const { entity_count: entityCount } = entityCountRow ?? { entity_count: 0 }
const { lpa, dataset: datasetId } = params
console.assert(req.resourceStatus.resource === req.resource.resource, 'mismatch between resourceStatus and resource data')
console.assert(typeof entityCount === 'number', 'entityCount should be a number')

const taskList = issues.map((issue) => {
Expand Down Expand Up @@ -113,14 +123,17 @@ const validateParams = validateQueryParams({
})
})

/* eslint-disable-next-line no-return-assign */
const zeroEntityCount = (req) => req.entityCount = { entity_count: 0 }
rosado marked this conversation as resolved.
Show resolved Hide resolved

export default [
validateParams,
fetchResourceStatus,
fetchOrgInfoWithStatGeo,
fetchDatasetInfo,
fetchIf(isResourceAccessible, fetchLatestResource),
fetchIf(isResourceAccessible, fetchLpaDatasetIssues),
fetchIf(isResourceAccessible, fetchEntityCount),
fetchIf(and(isResourceAccessible, isResourceIdValid), fetchEntityCount, zeroEntityCount),
onlyIf(isResourceAccessible, prepareDatasetTaskListTemplateParams),
onlyIf(isResourceAccessible, getDatasetTaskList),
onlyIf(isResourceNotAccessible, prepareDatasetTaskListErrorTemplateParams),
Expand Down
Loading