You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

profiler.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. const config = require('../../../db/data-generator/config.json')
  2. /**
  3. * Unscored preferences used for filtering
  4. * Does NOT include blurb, media languages
  5. */
  6. const unscoredProfilePreferences = [
  7. 'zipcode',
  8. 'duration',
  9. 'presence',
  10. 'urgency',
  11. 'pronouns',
  12. 'distance',
  13. ]
  14. const otherProfileInfo = ['blurb', 'media', 'lang']
  15. /**
  16. * Class to hold our retrieved profile information
  17. * in a convenient wrapper
  18. * !: This needs to match the responseSchema in profiles.js
  19. */
  20. class CompleteProfile {
  21. constructor(profile, type) {
  22. this.user_id = profile.user_id // int user_id
  23. this.profile_id = profile.profile_id // int profile_id
  24. this.responses = profile.responses
  25. this.user_type = type
  26. this.blurb = ''
  27. this.profile_media = []
  28. this.profile_languages = []
  29. this.profile_prefs = this.getPrefsFromResponses(this.responses)
  30. otherProfileInfo.forEach(prefName => {
  31. if (prefName == 'blurb') {
  32. const blurbRes = this.responses.find(
  33. r => r.response_key_id === config.blurbKey,
  34. )
  35. this.profile_description = blurbRes ? blurbRes.val : ''
  36. } else if (['media', 'lang'].includes(prefName)) {
  37. const key =
  38. prefName == 'media'
  39. ? `profile_${prefName}`
  40. : [`profile_${prefName}uages`]
  41. const resForKey = this.responses.filter(
  42. r => r.response_key_id === config[`${prefName}Key`],
  43. )
  44. this[key] = resForKey.length ? resForKey.map(r => r.val) : []
  45. }
  46. })
  47. // TODO: These should be getters
  48. this.user_name = 'bleh'
  49. this.user_email = 'bleh@bleh.com'
  50. this.reveal = profile.tags.filter(t => t.tag_category == 'reveal')
  51. this.tags = profile.tags.filter(t => t.tag_category !== 'reveal')
  52. }
  53. /** Map pref name to dB key associated with preference */
  54. get byPrefName() {
  55. return unscoredProfilePreferences.reduce((byPref, prefName) => {
  56. byPref[prefName] = this.responses.find(
  57. r => config[`${prefName}Key`] == r.response_key_id,
  58. )?.val
  59. return byPref
  60. }, {})
  61. }
  62. getPrefsFromResponses(responses) {
  63. if (!responses.length) return
  64. const prefs = {}
  65. unscoredProfilePreferences.forEach(prefName => {
  66. prefs[prefName] = this.byPrefName[prefName]
  67. })
  68. return prefs
  69. }
  70. }
  71. const _makeCompleteProfile = (profileEntry, type, tagLookup) => {
  72. profileEntry.tags = profileEntry.tags.map(tag => tagLookup[tag.tag_id])
  73. const complete = new CompleteProfile(profileEntry, type)
  74. return complete
  75. }
  76. /**
  77. * Get complete profiles and return in order
  78. * @param {Array} orderedProfileIds
  79. * @param {Array} profilesEntries
  80. * @param {String} type
  81. * @param {Object} tagLookup
  82. * @returns {Array}
  83. */
  84. const makeOrderedCompleteProfiles = (
  85. orderedProfileIds,
  86. profilesEntries,
  87. type,
  88. tagLookup,
  89. ) => {
  90. return orderedProfileIds.map(pid => {
  91. const found = profilesEntries.find(entry => entry.profile_id == pid)
  92. return _makeCompleteProfile(found, type, tagLookup)
  93. })
  94. }
  95. /**
  96. * Get complete profiles from dB rows
  97. * @param {Array} profilesEntries
  98. * @param {String} type
  99. * @param {Object} tagLookup
  100. * @returns {Array}
  101. */
  102. const makeCompleteFromProfileEntries = (profilesEntries, type, tagLookup) => {
  103. try {
  104. return profilesEntries.map(entry =>
  105. _makeCompleteProfile(entry, type, tagLookup),
  106. )
  107. } catch (err) {
  108. throw new Error(err)
  109. }
  110. }
  111. module.exports = {
  112. CompleteProfile,
  113. makeOrderedCompleteProfiles,
  114. makeCompleteFromProfileEntries,
  115. }