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

schema summary export: show min/max count expressions #3726

Merged
merged 2 commits into from
Feb 7, 2025
Merged
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
43 changes: 31 additions & 12 deletions server/modules/survey/service/schemaSummary/schemaSummary.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Surveys } from '@openforis/arena-core'
import { Objects, Surveys } from '@openforis/arena-core'

import { SamplingNodeDefs } from '@common/analysis/samplingNodeDefs'

Expand Down Expand Up @@ -36,20 +36,39 @@ const getDefaultValueApplyIf = ({ nodeDef }) => {
: ''
}

const getValidationsSummary = ({ nodeDef }) => {
const validations = NodeDef.getValidations(nodeDef)
const expressions = NodeDefValidations.getExpressions(validations)
return expressions
.reduce((summaryTexts, expression) => {
const getExpressionsSummary = ({ expressions, includeSeverity = false }) =>
expressions
.reduce((acc, expression) => {
const { applyIf, expression: expr, severity = ValidationResult.severity.error } = expression

let text = `(${severity}) Expression: ${expr.trim()}`
const parts = []
if (includeSeverity) {
parts.push(`(${severity})`)
}
parts.push(`Expression: ${expr.trim()}`)
if (applyIf) {
text += ` - Apply if: ${applyIf.trim()}`
parts.push(`- Apply if: ${applyIf.trim()}`)
}
return [...summaryTexts, text]
acc.push(parts.join(' '))
return acc
}, [])
.join('\n')

const getValidationsSummary = ({ nodeDef }) => {
const validations = NodeDef.getValidations(nodeDef)
const expressions = NodeDefValidations.getExpressions(validations)
return getExpressionsSummary({ expressions, includeSeverity: true })
}

const getValidationCountSummary = ({ nodeDef, countType }) => {
const validations = NodeDef.getValidations(nodeDef)
const count = NodeDefValidations.getCountProp(countType)(validations)
if (Objects.isEmpty(count)) {
return ''
}
if (Array.isArray(count)) {
return getExpressionsSummary({ expressions: count })
}
return String(count)
}

const getValidationMessages = ({ nodeDef, lang }) => {
Expand Down Expand Up @@ -152,8 +171,8 @@ export const generateSchemaSummaryItems = async ({ surveyId, cycle }) => {
defaultValueEvaluateOnce: String(NodeDef.isDefaultValueEvaluatedOneTime(nodeDef)),
required: String(NodeDefValidations.isRequired(NodeDef.getValidations(nodeDef))),
unique: String(NodeDefValidations.isUnique(NodeDef.getValidations(nodeDef))),
minCount: String(NodeDefValidations.getMinCount(NodeDef.getValidations(nodeDef))),
maxCount: String(NodeDefValidations.getMaxCount(NodeDef.getValidations(nodeDef))),
minCount: getValidationCountSummary({ nodeDef, countType: NodeDefValidations.keys.min }),
maxCount: getValidationCountSummary({ nodeDef, countType: NodeDefValidations.keys.max }),
validations: getValidationsSummary({ nodeDef }),
// validation messages
...languages.reduce(
Expand Down
Loading