diff --git a/services/application/src/actions/user/set-unverified-data.js b/services/application/src/actions/user/set-unverified-data.js index 7fb37dee..1ea59c12 100644 --- a/services/application/src/actions/user/set-unverified-data.js +++ b/services/application/src/actions/user/set-unverified-data.js @@ -3,6 +3,7 @@ const { createRequiredParamError } = require('@base-cms/micro').service; const { isObject, asArray } = require('@base-cms/utils'); const setConsentAnswer = require('./utils/set-regional-consent-answer'); const setCustomSelectAnswers = require('./utils/set-custom-select-answers'); +const setCustomTextAnswers = require('./utils/set-custom-text-answers'); const { AppUser } = require('../../mongodb/models'); @@ -20,7 +21,7 @@ module.exports = async ({ applicationId, email, payload } = {}) => { const { // customBooleanFieldAnswers, customSelectFieldAnswers, - // customTextFieldAnswers, + customTextFieldAnswers, regionalConsentAnswers, ...fields } = payload; @@ -33,9 +34,9 @@ module.exports = async ({ applicationId, email, payload } = {}) => { setCustomSelectAnswers({ user, answers: customSelectFieldAnswers }); } - // if (asArray(customTextFieldAnswers).length) { - // setCustomTextAnswers({ user, answers: customTextFieldAnswers }); - // } + if (asArray(customTextFieldAnswers).length) { + setCustomTextAnswers({ user, answers: customTextFieldAnswers }); + } // overwrite/set regional consent answers user.set('regionalConsentAnswers', []); diff --git a/services/application/src/actions/user/update-custom-text-answers.js b/services/application/src/actions/user/update-custom-text-answers.js index 79052f34..77f8f9f8 100644 --- a/services/application/src/actions/user/update-custom-text-answers.js +++ b/services/application/src/actions/user/update-custom-text-answers.js @@ -3,6 +3,8 @@ const { createRequiredParamError } = require('@base-cms/micro').service; const { handleError } = require('@identity-x/utils').mongoose; const { AppUser } = require('../../mongodb/models'); +const setCustomTextAnswers = require('./utils/set-custom-text-answers'); + const { isArray } = Array; @@ -21,26 +23,8 @@ module.exports = async ({ // 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; - }); + setCustomTextAnswers({ user, answers }); - user.set('customTextFieldAnswers', toSet); if (profileLastVerifiedAt) { user.set('profileLastVerifiedAt', profileLastVerifiedAt); user.set('forceProfileReVerification', false); diff --git a/services/application/src/actions/user/utils/set-custom-text-answers.js b/services/application/src/actions/user/utils/set-custom-text-answers.js new file mode 100644 index 00000000..f873ffc6 --- /dev/null +++ b/services/application/src/actions/user/utils/set-custom-text-answers.js @@ -0,0 +1,25 @@ +const { createRequiredParamError } = require('@base-cms/micro').service; + +module.exports = ({ user, answers }) => { + if (!answers) throw createRequiredParamError('answers'); + // 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); +}; diff --git a/services/graphql/src/graphql/definitions/app-user.js b/services/graphql/src/graphql/definitions/app-user.js index f45ba80e..bc9e642b 100644 --- a/services/graphql/src/graphql/definitions/app-user.js +++ b/services/graphql/src/graphql/definitions/app-user.js @@ -461,7 +461,7 @@ input SetAppUserUnverifiedDataMutationInput { # customBooleanFieldAnswers: [UpdateAppUserCustomBooleanAnswer!] = [] customSelectFieldAnswers: [UpdateAppUserCustomSelectAnswer!] = [] - # customTextFieldAnswers: [UpdateAppUserCustomTextAnswer!] = [] + customTextFieldAnswers: [UpdateAppUserCustomTextAnswer!] = [] regionalConsentAnswers: [SetAppUserRegionalConsentAnswerInput!] = [] } diff --git a/services/graphql/src/graphql/resolvers/app-user.js b/services/graphql/src/graphql/resolvers/app-user.js index 7994dd5d..2bb5c16d 100644 --- a/services/graphql/src/graphql/resolvers/app-user.js +++ b/services/graphql/src/graphql/resolvers/app-user.js @@ -614,7 +614,7 @@ module.exports = { regionalConsentAnswers, // customBooleanFieldAnswers, customSelectFieldAnswers, - // customTextFieldAnswers, + customTextFieldAnswers, } = input; const payload = { @@ -633,7 +633,7 @@ module.exports = { regionalConsentAnswers, // customBooleanFieldAnswers, customSelectFieldAnswers, - // customTextFieldAnswers, + customTextFieldAnswers, }; return applicationService.request('user.setUnverifiedData', {