|
|
@@ -4,6 +4,7 @@ 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 { response } = require('@hapi/hapi/lib/validation')
|
|
7
|
8
|
|
|
8
|
9
|
module.exports = class ProfileService extends Schmervice.Service {
|
|
9
|
10
|
constructor(...args) {
|
|
|
@@ -50,6 +51,25 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
50
|
51
|
return [...new Set(profileIdsToGrab)]
|
|
51
|
52
|
}
|
|
52
|
53
|
|
|
|
54
|
+ /**
|
|
|
55
|
+ * Convert indexes to actual score values
|
|
|
56
|
+ * Using using the input and converting to index
|
|
|
57
|
+ * of the generated possible prescore array in config
|
|
|
58
|
+ */
|
|
|
59
|
+ _convertResponse(responseToSave) {
|
|
|
60
|
+ if (scoring._isScorableResponse(responseToSave.response_key_id)) {
|
|
|
61
|
+ // Convert -3 to 0, 0 to 3, 3 to 6
|
|
|
62
|
+ const offset = (config.scoreVals.length - 1) / 2
|
|
|
63
|
+ const scoreFromInput = parseInt(responseToSave.val) + offset
|
|
|
64
|
+ const scoreFromConfig = config.scoreVals.indexOf(scoreFromInput)
|
|
|
65
|
+ if (scoreFromConfig < 0) {
|
|
|
66
|
+ console.error('score not found in possible config responses')
|
|
|
67
|
+ }
|
|
|
68
|
+ responseToSave.val = scoreFromConfig.toString()
|
|
|
69
|
+ }
|
|
|
70
|
+ return responseToSave
|
|
|
71
|
+ }
|
|
|
72
|
+
|
|
53
|
73
|
async getProfile(profileId) {
|
|
54
|
74
|
const { Profile } = this.server.models()
|
|
55
|
75
|
await this._setTagLookup()
|
|
|
@@ -124,21 +144,7 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
124
|
144
|
user_id: userId,
|
|
125
|
145
|
})
|
|
126
|
146
|
for (const responseToSave of responses) {
|
|
127
|
|
- /**
|
|
128
|
|
- * Convert indexes to actual score values
|
|
129
|
|
- * Using using the input and converting to index
|
|
130
|
|
- * of the generated possible prescore array in config
|
|
131
|
|
- * DUPLICATE:See saveResponseForProfile() line 343
|
|
132
|
|
- */
|
|
133
|
|
- let convertedResponse = responseToSave
|
|
134
|
|
- if (scoring._isScorableResponse(responseToSave.response_key_id)) {
|
|
135
|
|
- // Convert -3 to 0, 0 to 3, 3 to 6
|
|
136
|
|
- const offset = (config.scoreVals.length - 1) / 2
|
|
137
|
|
- const indexFromInput = parseInt(responseToSave.val) + offset
|
|
138
|
|
- convertedResponse.val =
|
|
139
|
|
- config.scoreVals[indexFromInput].toString()
|
|
140
|
|
- }
|
|
141
|
|
-
|
|
|
147
|
+ const convertedResponse = this._convertResponse(responseToSave)
|
|
142
|
148
|
const responseInfo = {
|
|
143
|
149
|
profile_id: profile.id,
|
|
144
|
150
|
response_key_id: convertedResponse.response_key_id,
|
|
|
@@ -177,6 +183,14 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
177
|
183
|
})
|
|
178
|
184
|
}
|
|
179
|
185
|
|
|
|
186
|
+ async insertSingleResponseForProfile(responseToSave) {
|
|
|
187
|
+ const { Response } = this.server.models()
|
|
|
188
|
+ const convertedResponse = this._convertResponse(responseToSave)
|
|
|
189
|
+ const savedResponse = await Response.query().insert(convertedResponse)
|
|
|
190
|
+ delete savedResponse.id
|
|
|
191
|
+ return savedResponse
|
|
|
192
|
+ }
|
|
|
193
|
+
|
|
180
|
194
|
/** Add response
|
|
181
|
195
|
* @param {Object} response to save
|
|
182
|
196
|
* @returns {null} updated responses
|
|
|
@@ -203,24 +217,7 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
203
|
217
|
.delete()
|
|
204
|
218
|
.whereIn('response_key_id', alreadyAnswered)
|
|
205
|
219
|
}
|
|
206
|
|
-
|
|
207
|
|
- /**
|
|
208
|
|
- * Convert indexes to actual score values
|
|
209
|
|
- * Using using the input and converting to index
|
|
210
|
|
- * of the generated possible prescore array in config
|
|
211
|
|
- */
|
|
212
|
|
- let convertedResponse = responseToSave
|
|
213
|
|
- if (scoring._isScorableResponse(responseToSave.response_key_id)) {
|
|
214
|
|
- // Convert -3 to 0, 0 to 3, 3 to 6
|
|
215
|
|
- const offset = (config.scoreVals.length - 1) / 2
|
|
216
|
|
- const scoreFromInput = parseInt(responseToSave.val) + offset
|
|
217
|
|
- const scoreFromConfig = config.scoreVals.indexOf(scoreFromInput)
|
|
218
|
|
- if (scoreFromConfig < 0) {
|
|
219
|
|
- console.error('score not found in possible config responses')
|
|
220
|
|
- }
|
|
221
|
|
- convertedResponse.val = scoreFromConfig.toString()
|
|
222
|
|
- }
|
|
223
|
|
-
|
|
|
220
|
+ const convertedResponse = this._convertResponse(responseToSave)
|
|
224
|
221
|
await Response.query().insert(convertedResponse)
|
|
225
|
222
|
return allResponses
|
|
226
|
223
|
}
|