31 lines
1.0 KiB
TypeScript
31 lines
1.0 KiB
TypeScript
type PerformanceSurveyProgressQuestion = {
|
|
id: string
|
|
}
|
|
|
|
export function calculatePerformanceSurveyResponseProgress(input: {
|
|
questions: PerformanceSurveyProgressQuestion[]
|
|
draftScores?: Record<string, unknown> | null
|
|
submittedScores?: Record<string, unknown> | null
|
|
submitted: boolean
|
|
}) {
|
|
const scoreSource = input.submitted
|
|
? input.submittedScores || {}
|
|
: input.draftScores || {}
|
|
const values = input.questions.map((question) => {
|
|
const rawValue = scoreSource[question.id]
|
|
if (rawValue === null || rawValue === undefined || rawValue === '') return null
|
|
const numericValue = Number(rawValue)
|
|
return Number.isFinite(numericValue) ? numericValue : null
|
|
})
|
|
const ratedValues = values.filter((value): value is number => value !== null)
|
|
const totalCount = input.questions.length
|
|
const ratedCount = ratedValues.length
|
|
|
|
return {
|
|
ratedCount,
|
|
totalCount,
|
|
totalScore: ratedValues.reduce((sum, value) => sum + value, 0),
|
|
percent: totalCount > 0 ? Math.min(100, (ratedCount / totalCount) * 100) : 0,
|
|
}
|
|
}
|