|
|
@@ -4,7 +4,6 @@ const config = require('../../../db/data-generator/config.json')
|
|
4
|
4
|
const profiler = require('./profiler')
|
|
5
|
5
|
const scoring = require('./scorer')
|
|
6
|
6
|
const zipcoder = require('./zipcoder')
|
|
7
|
|
-const tagger = require('./tagger')
|
|
8
|
7
|
|
|
9
|
8
|
module.exports = class ProfileService extends Schmervice.Service {
|
|
10
|
9
|
constructor(...args) {
|
|
|
@@ -24,15 +23,14 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
24
|
23
|
}
|
|
25
|
24
|
}
|
|
26
|
25
|
async _setTagLookup() {
|
|
27
|
|
- if (!Object.keys(this.tagLookup).length) {
|
|
28
|
|
- const { Tag } = this.server.models()
|
|
29
|
|
- const allTagDescriptions = await Tag.query()
|
|
30
|
|
- allTagDescriptions.forEach(desc => {
|
|
31
|
|
- if (desc.is_active) {
|
|
32
|
|
- this.tagLookup[desc.tag_id] = desc
|
|
33
|
|
- }
|
|
34
|
|
- })
|
|
35
|
|
- }
|
|
|
26
|
+ /** Grab tag descriptions if they do NOT exist: Needed once per app load */
|
|
|
27
|
+ if (Object.keys(this.tagLookup).length) return
|
|
|
28
|
+ const { Tag } = this.server.models()
|
|
|
29
|
+ const allTagDescriptions = await Tag.query()
|
|
|
30
|
+ allTagDescriptions.forEach(desc => {
|
|
|
31
|
+ if (!desc.is_active) return
|
|
|
32
|
+ this.tagLookup[desc.tag_id] = desc
|
|
|
33
|
+ })
|
|
36
|
34
|
}
|
|
37
|
35
|
/**
|
|
38
|
36
|
* Internal method to get list of profile_ids for this user
|
|
|
@@ -55,16 +53,17 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
55
|
53
|
async getProfile(profileId) {
|
|
56
|
54
|
const { Profile } = this.server.models()
|
|
57
|
55
|
await this._setTagLookup()
|
|
58
|
|
-
|
|
59
|
56
|
const matchingProfile = await Profile.query()
|
|
60
|
57
|
.where('profile_id', profileId)
|
|
61
|
58
|
.first()
|
|
62
|
59
|
.withGraphFetched('tags')
|
|
63
|
60
|
.withGraphFetched('responses')
|
|
64
|
61
|
.withGraphFetched('user')
|
|
65
|
|
- tagger.setProfileTags(matchingProfile, matchingProfile, this.tagLookup)
|
|
|
62
|
+ matchingProfile.tags = matchingProfile.tags.map(
|
|
|
63
|
+ tag => this.tagLookup[tag.tag_id],
|
|
|
64
|
+ )
|
|
66
|
65
|
const complete = new profiler.CompleteProfile(matchingProfile, true)
|
|
67
|
|
- return complete
|
|
|
66
|
+ return this.setDefaults(complete)
|
|
68
|
67
|
}
|
|
69
|
68
|
|
|
70
|
69
|
async getCompleteProfilesFor(userId, type) {
|
|
|
@@ -81,7 +80,7 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
81
|
80
|
// so without this, we get undefined user_name
|
|
82
|
81
|
.withGraphFetched('user')
|
|
83
|
82
|
|
|
84
|
|
- return profiler.makeCompleteProfilesFromProfile(
|
|
|
83
|
+ return profiler.makeCompleteFromProfileEntries(
|
|
85
|
84
|
profilesEntries,
|
|
86
|
85
|
type,
|
|
87
|
86
|
this.tagLookup,
|
|
|
@@ -103,7 +102,7 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
103
|
102
|
// taking the info from profilesEntries
|
|
104
|
103
|
// to repack into completeProfiles
|
|
105
|
104
|
// in same order as profileIdArray
|
|
106
|
|
- return profiler.makeCompleteProfiles(
|
|
|
105
|
+ return profiler.makeOrderedCompleteProfiles(
|
|
107
|
106
|
profileIdArray,
|
|
108
|
107
|
profilesEntries,
|
|
109
|
108
|
type,
|
|
|
@@ -226,6 +225,40 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
226
|
225
|
return allResponses
|
|
227
|
226
|
}
|
|
228
|
227
|
|
|
|
228
|
+ /**
|
|
|
229
|
+ * Sets default profile attributes
|
|
|
230
|
+ * @param {object} complete
|
|
|
231
|
+ * @returns {object} updated profile
|
|
|
232
|
+ */
|
|
|
233
|
+ setDefaults(complete) {
|
|
|
234
|
+ let defaultValues = {
|
|
|
235
|
+ user_email: 'hidden@email.com',
|
|
|
236
|
+ user_name: 'Hidden Name',
|
|
|
237
|
+ }
|
|
|
238
|
+
|
|
|
239
|
+ let defaultProfile = {
|
|
|
240
|
+ ...complete,
|
|
|
241
|
+ user_email: defaultValues.user_email,
|
|
|
242
|
+ user_name: defaultValues.user_name,
|
|
|
243
|
+ }
|
|
|
244
|
+ console.log('---')
|
|
|
245
|
+ console.log('defaultProfile: ', defaultProfile.user_email)
|
|
|
246
|
+ if (!complete.reveal.length) return defaultProfile // nothing to reveal
|
|
|
247
|
+
|
|
|
248
|
+ // swap out values of keys that are not found as complete.reveal.tag.tag_description values
|
|
|
249
|
+ for (let [attribute, defaultVal] of Object.entries(defaultValues)) {
|
|
|
250
|
+ if (
|
|
|
251
|
+ typeof complete.reveal.find(
|
|
|
252
|
+ tag => tag.tag_description == attribute,
|
|
|
253
|
+ ) == 'undefined'
|
|
|
254
|
+ )
|
|
|
255
|
+ complete[attribute] = defaultVal
|
|
|
256
|
+ }
|
|
|
257
|
+ console.log('complete: ', complete.user_email)
|
|
|
258
|
+
|
|
|
259
|
+ return complete
|
|
|
260
|
+ }
|
|
|
261
|
+
|
|
229
|
262
|
/**
|
|
230
|
263
|
* Delete a profile
|
|
231
|
264
|
* @param {number} userId
|
|
|
@@ -356,8 +389,8 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
356
|
389
|
await this._setTagLookup()
|
|
357
|
390
|
let associations = groupingId
|
|
358
|
391
|
? await TagAssociation.query()
|
|
359
|
|
- .where('grouping_id', groupingId)
|
|
360
|
|
- .andWhere('profile_id', profileId)
|
|
|
392
|
+ .where('grouping_id', groupingId)
|
|
|
393
|
+ .andWhere('profile_id', profileId)
|
|
361
|
394
|
: await TagAssociation.query().andWhere('profile_id', profileId)
|
|
362
|
395
|
return associations
|
|
363
|
396
|
.map(assoc => ({
|