Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

profiler.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. // remap tags
  73. if (profileEntry && profileEntry.tags.length) {
  74. profileEntry.tags = profileEntry?.tags.map(tag => tagLookup[tag.tag_id])
  75. }
  76. const complete = new CompleteProfile(profileEntry, type)
  77. return complete
  78. }
  79. /**
  80. * Get complete profiles and return in order
  81. * @param {Array} orderedProfileIds
  82. * @param {Array} profilesEntries
  83. * @param {String} type
  84. * @param {Object} tagLookup
  85. * @returns {Array}
  86. */
  87. const makeOrderedCompleteProfiles = (
  88. orderedProfileIds,
  89. profilesEntries,
  90. type,
  91. tagLookup,
  92. ) => {
  93. return orderedProfileIds.map(pid => {
  94. const found = profilesEntries.find(entry => entry.profile_id == pid)
  95. return _makeCompleteProfile(found, type, tagLookup)
  96. })
  97. }
  98. /**
  99. * Get complete profiles from dB rows
  100. * @param {Array} profilesEntries
  101. * @param {String} type
  102. * @param {Object} tagLookup
  103. * @returns {Array}
  104. */
  105. const makeCompleteFromProfileEntries = (profilesEntries, type, tagLookup) => {
  106. return profilesEntries.map(entry =>
  107. _makeCompleteProfile(entry, type, tagLookup),
  108. )
  109. }
  110. module.exports = {
  111. CompleteProfile,
  112. makeOrderedCompleteProfiles,
  113. makeCompleteFromProfileEntries,
  114. }