const config = require('../../../db/data-generator/config.json') /** * Unscored preferences used for filtering * Does NOT include blurb, media languages */ const unscoredProfilePreferences = [ 'zipcode', 'duration', 'presence', 'urgency', 'pronouns', 'distance', ] const otherProfileInfo = ['blurb', 'media', 'lang'] /** * Class to hold our retrieved profile information * in a convenient wrapper * !: This needs to match the responseSchema in profiles.js */ class CompleteProfile { constructor(profile, type) { this.user_id = profile.user_id // int user_id this.profile_id = profile.profile_id // int profile_id this.responses = profile.responses this.user_type = type this.profile_prefs = this.getPrefsFromResponses(this.responses) otherProfileInfo.forEach(prefName => { if (prefName == 'blurb') { this.profile_description = this.responses.find( r => r.response_key_id === config.blurbKey, ).val } else if (['media', 'lang'].includes(prefName)) { const key = prefName == 'media' ? `profile_${prefName}` : [`profile_${prefName}uages`] this[key] = this.responses .filter(r => r.response_key_id === config[`${prefName}Key`]) .map(r => r.val) } }) // TODO: These should be getters this.user_name = 'bleh' this.user_email = 'bleh@bleh.com' this.reveal = profile.tags.filter(t => t.tag_category == 'reveal') this.tags = profile.tags.filter(t => t.tag_category !== 'reveal') } /** Map pref name to dB key associated with preference */ get byPrefName() { return unscoredProfilePreferences.reduce((byPref, prefName) => { byPref[prefName] = this.responses.find( r => config[`${prefName}Key`] == r.response_key_id, ).val return byPref }, {}) } getPrefsFromResponses(responses) { if (!responses.length) return const prefs = {} unscoredProfilePreferences.forEach(prefName => { prefs[prefName] = this.byPrefName[prefName] }) return prefs } } const _makeCompleteProfile = (profileEntry, type, tagLookup) => { profileEntry.tags = profileEntry.tags.map(tag => tagLookup[tag.tag_id]) return new CompleteProfile(profileEntry, type) } const makeOrderedCompleteProfiles = ( orderedProfileIds, profilesEntries, type, tagLookup, ) => { return orderedProfileIds.map(pid => { const foundEntry = profilesEntries.find(entry => { pid == entry.profile_id }) return _makeCompleteProfile(foundEntry, type, tagLookup) }) } const makeCompleteFromProfileEntries = (profilesEntries, type, tagLookup) => profilesEntries.map(entry => _makeCompleteProfile(entry, type, tagLookup)) module.exports = { CompleteProfile, makeOrderedCompleteProfiles, makeCompleteFromProfileEntries, }