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.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. return new CompleteProfile(profileEntry, type)
  69. }
  70. const makeOrderedCompleteProfiles = (
  71. orderedProfileIds,
  72. profilesEntries,
  73. type,
  74. tagLookup,
  75. ) => {
  76. return orderedProfileIds.map(pid => {
  77. const foundEntry = profilesEntries.find(entry => {
  78. pid == entry.profile_id
  79. })
  80. return _makeCompleteProfile(foundEntry, type, tagLookup)
  81. })
  82. }
  83. const makeCompleteFromProfileEntries = (profilesEntries, type, tagLookup) =>
  84. profilesEntries.map(entry => _makeCompleteProfile(entry, type, tagLookup))
  85. module.exports = {
  86. CompleteProfile,
  87. makeOrderedCompleteProfiles,
  88. makeCompleteFromProfileEntries,
  89. }