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

feature: Generated report with scores or raw scores (M2-7932) #61

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
Binary file removed outputs/.DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions src/core/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface IActivity {
reportIncludedItemName: string
responseIsEditable: boolean
scoresAndReports: IActivityScoresAndReports
subscaleSetting: ActivitySubscalesSetting
showAllAtOnce: boolean
splashScreen: string
items: IActivityItem[]
Expand Down Expand Up @@ -130,6 +131,11 @@ export interface IActivityScoresAndReports {
reports: IActivityScoresAndReportsScores[] | IActivityScoresAndReportsSections[]
}

export enum ScoringType {
score = 'score',
raw_score = 'raw_score',
}

export interface IActivityScoresAndReportsScores {
type: 'score'
id: string
Expand All @@ -139,6 +145,8 @@ export interface IActivityScoresAndReportsScores {
itemsPrint: string[]
itemsScore: string[]
message: string | null
scoringType: ScoringType | null
subscaleName: string | null
}

export interface IActivityScoresAndReportsSections {
Expand Down Expand Up @@ -225,3 +233,26 @@ export interface SendPdfReportResponse {
pdf: string
email: Email
}

export const TScoreSeverity = ['Minimal', 'Mild', 'Moderate', 'Severe'] as const

export type TScoreSeverity = (typeof TScoreSeverity)[number]

export type LookupTableDataItem = {
score?: string
rawScore: string
optionalText: string
severity: TScoreSeverity | null
age?: string | number | null
sex?: string | null
id: string
}

export type Subscale = {
name: string
subscaleTableData?: LookupTableDataItem[] | null
}

export type ActivitySubscalesSetting = {
subscales: Subscale[]
}
54 changes: 54 additions & 0 deletions src/models/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,28 @@ import {
User,
Map,
ScoreForSummary,
ActivitySubscalesSetting,
ScoringType,
} from '../core/interfaces'
import { Calculator, convertMarkdownToHtml, getScoresSummary, isFloat, toFixed } from '../core/helpers'
import { replaceVariablesInMarkdown } from '../core/helpers/markdownVariableReplacer/'
import { ScoresCalculator } from '../core/helpers/ScoresCalculator'
import { ConditionalLogicService } from '../modules/report/helpers/conditionalLogic'

const INTERVAL_SYMBOL = '~'

const enum LookupTableItems {
Age_screen = 'age_screen',
Gender_screen = 'gender_screen',
}

const enum Sex {
M = 'M',
F = 'F',
}

const parseSex = (sex: string | null | undefined) => (sex === Sex.M ? '0' : '1')

export class ActivityEntity {
public json: IActivity
public schemaId: string
Expand All @@ -26,6 +42,7 @@ export class ActivityEntity {
public reportIncludeItem: string
public allowSummary: boolean
public reports: IActivityScoresAndReportsSections[] | IActivityScoresAndReportsScores[]
public subscaleSetting: ActivitySubscalesSetting

constructor(data: IActivity, items: IActivityItem[] = []) {
this.json = data
Expand All @@ -44,6 +61,7 @@ export class ActivityEntity {

this.allowSummary = data.scoresAndReports?.showScoreSummary || false
this.reports = data.scoresAndReports?.reports || []
this.subscaleSetting = data.subscaleSetting || null
}

getVisibleItems(): ItemEntity[] {
Expand Down Expand Up @@ -93,6 +111,42 @@ export class ActivityEntity {
break
}

const subscaleItem = this.subscaleSetting.subscales.find(({ name }) => name === report.subscaleName)

if (subscaleItem?.subscaleTableData && subscaleItem.subscaleTableData.length > 0) {
const calculatedScore = scores[report.id]
const genderItemIndex = this.items.findIndex((item) => item.name == LookupTableItems.Gender_screen)
const genderAnswer = responses[genderItemIndex]
const ageItemIndex = this.items.findIndex((item) => item.name == LookupTableItems.Age_screen)
const ageAnswer = responses[ageItemIndex]
const subscaleTableData = subscaleItem.subscaleTableData

const subscaleTableDataItem = subscaleTableData.find(({ sex, age, rawScore }) => {
let reportedAge: number | null = null
if (typeof ageAnswer === 'string') {
reportedAge = Number(ageAnswer)
} else if ('value' in ageAnswer && typeof ageAnswer.value === 'number') {
reportedAge = ageAnswer.value
}

const withAge = age === reportedAge
const withSex = parseSex(sex) === String(genderAnswer?.value)

if (!withSex || !withAge) return false

const hasInterval = rawScore.includes(INTERVAL_SYMBOL)
if (!hasInterval) return rawScore === String(calculatedScore)

const [minScore, maxScore] = rawScore.replace(/\s/g, '').split(INTERVAL_SYMBOL)

return Number(minScore) <= calculatedScore && calculatedScore <= Number(maxScore)
})

if (report.scoringType == ScoringType.score && subscaleTableDataItem) {
scores[report.id] = Number(subscaleTableDataItem.score) || calculatedScore
}
}

for (const conditional of report.conditionalLogic) {
const isReportVisible = this.testVisibility(conditional, scores)

Expand Down