-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from solocommand/custom-text-field
Custom text field support
- Loading branch information
Showing
26 changed files
with
673 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
services/application/src/actions/field/user-text-answers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const { Sort } = require('@identity-x/pagination'); | ||
const TextField = require('../../mongodb/models/field/text'); | ||
|
||
const { isArray } = Array; | ||
|
||
module.exports = async ({ | ||
applicationId, | ||
fieldIds, | ||
customTextFieldAnswers, | ||
onlyAnswered, | ||
onlyActive, | ||
sort, | ||
} = {}) => { | ||
const $sort = new Sort(sort); | ||
const fieldQuery = { | ||
applicationId, | ||
...(isArray(fieldIds) && fieldIds.length && { _id: { $in: fieldIds } }), | ||
...(onlyActive && { active: { $ne: false } }), | ||
}; | ||
const fields = await TextField.find(fieldQuery, {}, { sort: $sort.value }); | ||
// return nothing when no custom booleans are found. | ||
if (!fields.length) return []; | ||
|
||
// ensure answers are an array | ||
const customFieldAnswers = !isArray(customTextFieldAnswers) | ||
|| !customTextFieldAnswers.length | ||
? [] | ||
: customTextFieldAnswers; | ||
|
||
|
||
// return nothing when no answers are found and only answered questions have been requested. | ||
if (onlyAnswered && !customFieldAnswers.length) return []; | ||
|
||
const mapped = fields.map((field) => { | ||
const fieldAnswer = customFieldAnswers.find(answer => `${answer._id}` === `${field._id}`); | ||
const value = fieldAnswer ? fieldAnswer.value : null; | ||
return { | ||
id: field._id, | ||
field, | ||
hasAnswered: Boolean(fieldAnswer), | ||
value, | ||
}; | ||
}); | ||
return onlyAnswered ? mapped.filter(field => field.hasAnswered) : mapped; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
services/application/src/actions/user/update-custom-text-answers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const { createError } = require('micro'); | ||
const { createRequiredParamError } = require('@base-cms/micro').service; | ||
const { handleError } = require('@identity-x/utils').mongoose; | ||
|
||
const { AppUser } = require('../../mongodb/models'); | ||
|
||
const { isArray } = Array; | ||
|
||
module.exports = async ({ | ||
id, | ||
applicationId, | ||
answers, | ||
profileLastVerifiedAt, | ||
} = {}) => { | ||
if (!id) throw createRequiredParamError('id'); | ||
if (!applicationId) throw createRequiredParamError('applicationId'); | ||
|
||
const user = await AppUser.findByIdForApp(id, applicationId); | ||
if (!user) throw createError(404, `No user was found for '${id}'`); | ||
|
||
// do not update user answers when passed answers are not an array | ||
if (!isArray(answers)) return user; | ||
|
||
// get all current answers as object { id, value } | ||
const userObj = user.customTextFieldAnswers.reduce( | ||
(obj, item) => ({ ...obj, [item._id]: item.value }), {}, | ||
); | ||
|
||
// get new answers as object { id, value } | ||
const newAnswers = answers.reduce( | ||
(obj, item) => ({ ...obj, [item.fieldId]: item.value }), {}, | ||
); | ||
|
||
// merge new and old ansers to account for old non active answers | ||
const mergedAnswers = { ...userObj, ...newAnswers }; | ||
|
||
// convert merged answers into valid array of { _id, value } answers | ||
const toSet = Object.keys(mergedAnswers).map((key) => { | ||
const obj = { _id: key, value: mergedAnswers[key] }; | ||
return obj; | ||
}); | ||
|
||
user.set('customTextFieldAnswers', toSet); | ||
if (profileLastVerifiedAt) { | ||
user.set('profileLastVerifiedAt', profileLastVerifiedAt); | ||
user.set('forceProfileReVerification', false); | ||
} | ||
try { | ||
await user.save(); | ||
return user; | ||
} catch (e) { | ||
throw handleError(createError, e); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
const Field = require('./index'); | ||
const schema = require('../../schema/field/text'); | ||
|
||
module.exports = Field.discriminator('field-text', schema, 'text'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const { Schema } = require('mongoose'); | ||
|
||
const schema = new Schema({}); | ||
|
||
module.exports = schema; |
Oops, something went wrong.