您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

profiler.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.profile_prefs = this.getPrefsFromResponses(this.responses)
  27. otherProfileInfo.forEach(prefName => {
  28. if (prefName == 'blurb') {
  29. this.profile_description = this.responses.find(
  30. r => r.response_key_id === config.blurbKey,
  31. ).val
  32. } else if (['media', 'lang'].includes(prefName)) {
  33. const key =
  34. prefName == 'media'
  35. ? `profile_${prefName}`
  36. : [`profile_${prefName}uages`]
  37. this[key] = this.responses
  38. .filter(r => r.response_key_id === config[`${prefName}Key`])
  39. .map(r => r.val)
  40. }
  41. })
  42. // TODO: These should be getters
  43. this.user_name = 'bleh'
  44. this.user_email = 'bleh@bleh.com'
  45. this.reveal = profile.tags.filter(t => t.tag_category == 'reveal')
  46. this.tags = profile.tags.filter(t => t.tag_category !== 'reveal')
  47. }
  48. /** Map pref name to dB key associated with preference */
  49. get byPrefName() {
  50. return unscoredProfilePreferences.reduce((byPref, prefName) => {
  51. byPref[prefName] = this.responses.find(
  52. r => config[`${prefName}Key`] == r.response_key_id,
  53. ).val
  54. return byPref
  55. }, {})
  56. }
  57. getPrefsFromResponses(responses) {
  58. if (!responses.length) return
  59. const prefs = {}
  60. unscoredProfilePreferences.forEach(prefName => {
  61. prefs[prefName] = this.byPrefName[prefName]
  62. })
  63. return prefs
  64. }
  65. }
  66. const _makeCompleteProfile = (profileEntry, type, tagLookup) => {
  67. profileEntry.tags = profileEntry.tags.map(tag => tagLookup[tag.tag_id])
  68. const complete = new CompleteProfile(profileEntry, type)
  69. return complete
  70. }
  71. /**
  72. * Get complete profiles and return in order
  73. * @param {Array} orderedProfileIds
  74. * @param {Array} profilesEntries
  75. * @param {String} type
  76. * @param {Object} tagLookup
  77. * @returns {Array}
  78. */
  79. const makeOrderedCompleteProfiles = (
  80. orderedProfileIds,
  81. profilesEntries,
  82. type,
  83. tagLookup,
  84. ) => {
  85. return orderedProfileIds.map(pid => {
  86. const found = profilesEntries.find(entry => entry.profile_id == pid)
  87. return _makeCompleteProfile(found, type, tagLookup)
  88. })
  89. }
  90. /**
  91. * Get complete profiles from dB rows
  92. * @param {Array} profilesEntries
  93. * @param {String} type
  94. * @param {Object} tagLookup
  95. * @returns {Array}
  96. */
  97. const makeCompleteFromProfileEntries = (profilesEntries, type, tagLookup) => {
  98. return profilesEntries.map(entry =>
  99. _makeCompleteProfile(entry, type, tagLookup),
  100. )
  101. }
  102. module.exports = {
  103. CompleteProfile,
  104. makeOrderedCompleteProfiles,
  105. makeCompleteFromProfileEntries,
  106. }