const config = require('../../../db/data-generator/config.json') const makeScoreLookup = (aspects, labels) => { const labelLookup = {} labels.forEach(label => (labelLookup[label.aspect_id] = label)) const scoreLookup = {} aspects.forEach(aspect => { const key = labelLookup[aspect.aspect_id] scoreLookup[`${key.a}:${key.b}`] = {} Object.keys(aspect).forEach(aspect_id => { if (!labelLookup[aspect_id]) return const comp = labelLookup[aspect_id] const score = aspect[aspect_id] scoreLookup[`${key.a}:${key.b}`][`${comp.a}:${comp.b}`] = score }) }) return scoreLookup } const _isScorableResponse = res_key_id => config.scoreKeys.includes(res_key_id) ? true : false const scoreResponses = (seeker, potentialMatch, prescoreLookup) => { if (seeker.responses.length != potentialMatch.responses.length) return { error: `complete responses for profile: ${seeker.profile_id} unqeual to profile: ${potentialMatch.profile_id} | ${seeker.responses.length}:${potentialMatch.responses.length}`, } const aRes = seeker.responses.filter(res => _isScorableResponse(res.response_key_id), ) const bRes = potentialMatch.responses.filter(res => _isScorableResponse(res.response_key_id), ) const composite = [] while (aRes.length + bRes.length > 0) { const mKey = resList => { let el = resList.shift() let pair = el.val el = resList.shift() return `${pair}:${el.val}` } composite.push(prescoreLookup[mKey(aRes)][mKey(bRes)]) } const scoreAvg = composite.reduce((a, b) => a + b) / composite.length return { total: Math.round(scoreAvg), aspects: composite, } } const scoreAll = (profileList, userProfile, prescoreLookup) => { return profileList.map(profile => { return { // Uncomment to return the whole profile // ...profile, profile_id: profile.profile_id, score: scoreResponses(userProfile, profile, prescoreLookup), distance: profile.distance, } }) } module.exports = { _isScorableResponse, scoreResponses, makeScoreLookup, scoreAll, }