| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- 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.blurb = ''
- this.profile_media = []
- this.profile_languages = []
- this.profile_prefs = this.getPrefsFromResponses(this.responses)
-
- otherProfileInfo.forEach(prefName => {
- if (prefName == 'blurb') {
- const blurbRes = this.responses.find(
- r => r.response_key_id === config.blurbKey,
- )
- this.profile_description = blurbRes ? blurbRes.val : ''
- } else if (['media', 'lang'].includes(prefName)) {
- const key =
- prefName == 'media'
- ? `profile_${prefName}`
- : [`profile_${prefName}uages`]
- const resForKey = this.responses.filter(
- r => r.response_key_id === config[`${prefName}Key`],
- )
- this[key] = resForKey.length ? resForKey.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])
- const complete = new CompleteProfile(profileEntry, type)
- return complete
- }
-
- /**
- * Get complete profiles and return in order
- * @param {Array} orderedProfileIds
- * @param {Array} profilesEntries
- * @param {String} type
- * @param {Object} tagLookup
- * @returns {Array}
- */
- const makeOrderedCompleteProfiles = (
- orderedProfileIds,
- profilesEntries,
- type,
- tagLookup,
- ) => {
- return orderedProfileIds.map(pid => {
- const found = profilesEntries.find(entry => entry.profile_id == pid)
- return _makeCompleteProfile(found, type, tagLookup)
- })
- }
-
- /**
- * Get complete profiles from dB rows
- * @param {Array} profilesEntries
- * @param {String} type
- * @param {Object} tagLookup
- * @returns {Array}
- */
- const makeCompleteFromProfileEntries = (profilesEntries, type, tagLookup) => {
- try {
- return profilesEntries.map(entry =>
- _makeCompleteProfile(entry, type, tagLookup),
- )
- } catch (err) {
- throw new Error(err)
- }
- }
-
- module.exports = {
- CompleteProfile,
- makeOrderedCompleteProfiles,
- makeCompleteFromProfileEntries,
- }
|