Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

profiler.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. const config = require('../../../db/data-generator/config.json')
  2. /**
  3. * Class to hold our retrieved profile information
  4. * in a convenient wrapper
  5. * !: This needs to match the responseSchema in profiles.js
  6. */
  7. class CompleteProfile {
  8. constructor(profile, includeResponses = false, type) {
  9. this.user_id = profile.user_id // int user_id
  10. this.profile_id = profile.profile_id // int profile_id
  11. this.user_name = profile.user.user_name // string user_name
  12. this.user_email = profile.user.user_email
  13. this.responses = []
  14. this.user_type = type
  15. this.tags = profile.tags.filter(t => t.tag_category != 'reveal')
  16. // TODO: generalize this for multiple images, and languages
  17. this.profile_description = ''
  18. this.profile_media = []
  19. this.profile_languages = []
  20. this.profile_prefs = {}
  21. // TODO: Use reveal tags to add or remove information from profile!
  22. // TODO: ---
  23. // TODO: Use reveal tags to rebuild profile based on group/membership
  24. // TODO: and include for certain profiles
  25. this.reveal = profile.tags.filter(t => t.tag_category == 'reveal')
  26. // Hides values until revealed
  27. const defaultValues = {
  28. user_name: 'hidden_name',
  29. user_email: 'hidden@email.com',
  30. }
  31. // BUG: If user reveals EITHER name or email, BOTH get revealed together...
  32. if (!this.reveal.length) {
  33. for (let [attribute, defaultVal] of Object.entries(defaultValues)) {
  34. const typeOfReveal = typeof this.reveal.find(tag => tag.tag_description === attribute)
  35. if (typeOfReveal == 'undefined') {
  36. this[attribute] = defaultVal
  37. }
  38. }
  39. }
  40. // TODO: filter these correctly
  41. if (profile?.responses?.length && includeResponses) {
  42. // [] of all "profile" responses
  43. this.responses = profile.responses
  44. // image, language, duration, presence, blurb, urgency, role, pronouns, distance
  45. const prefs = [
  46. 'zipcode',
  47. 'duration',
  48. 'presence',
  49. 'urgency',
  50. 'pronouns',
  51. 'distance',
  52. ]
  53. const prefsKeys = config.prefKeys
  54. prefs.forEach((pref, i) => {
  55. this.profile_prefs[pref] = this.responses.find(
  56. r => r.response_key_id === prefsKeys[i],
  57. )
  58. })
  59. this.profile_description = this.responses.find(
  60. r => r.response_key_id === config.blurbKey,
  61. ).val
  62. this.profile_media = this.responses
  63. .filter(r => r.response_key_id === config.mediaKey)
  64. .map(r => r.val)
  65. this.profile_languages = this.responses
  66. .filter(r => r.response_key_id === config.langKey)
  67. .map(r => r.val)
  68. }
  69. }
  70. }
  71. const _makeCompleteProfile = (
  72. profileEntry,
  73. type,
  74. tagLookup,
  75. includeResponses,
  76. ) => {
  77. profileEntry.tags = profileEntry.tags.map(tag => tagLookup[tag.tag_id])
  78. return new CompleteProfile(profileEntry, includeResponses, type)
  79. }
  80. const makeOrderedCompleteProfiles = (
  81. orderedProfileIds,
  82. profilesEntries,
  83. type,
  84. includeResponses,
  85. tagLookup,
  86. ) => {
  87. const completeProfiles = []
  88. orderedProfileIds.forEach(pid => {
  89. profilesEntries.forEach(entry => {
  90. if (entry.profile_id != pid) return
  91. completeProfiles.push(
  92. _makeCompleteProfile(entry, type, tagLookup, includeResponses),
  93. )
  94. })
  95. })
  96. return completeProfiles
  97. }
  98. const makeCompleteFromProfileEntries = (profilesEntries, type, tagLookup) =>
  99. profilesEntries.map(entry => _makeCompleteProfile(entry, type, tagLookup))
  100. module.exports = {
  101. CompleteProfile,
  102. makeOrderedCompleteProfiles,
  103. makeCompleteFromProfileEntries,
  104. }