fleet/website/api/controllers/save-questionnaire-progress.js
Eric a0d1172f89
Website Update /start signup flow. (#18027)
Closes: #17958

Changes:
- Updated the primary and secondary CTAs site-wide
- Updated the /start page to have a multi-stage form that users can fill
out to personalize their onboarding experience
- Updated the signup action to not set a `primaryBuyingSituation` on new
user records (This will now be set when users progress through the
/start form)
- Added two new attributes to the `User` model:
`currentGetStartedQuestionnarieStep` and
`getStartedQuestionnarieAnswers` that save user's progress in the /start
form.


Before this PR can be merged we will need to:
- [x] Update the Zapier webhook that runs when new users sign up to no
longer expect a `primaryBuyingSituation` value
- [ ] Update the User table in the website's database
- [ ] Migrate the existing user records
2024-04-04 11:13:53 -05:00

71 lines
2.7 KiB
JavaScript
Vendored

module.exports = {
friendlyName: 'Save questionnaire progress and continue',
description: 'Saves the user\'s current progress in the get started questionnaire',
inputs: {
currentStep: {
type: 'string',
description: 'The step of the get started questionnaire that is being saved.',
isIn: [
'start',
'what-are-you-using-fleet-for',
'have-you-ever-used-fleet',
'how-many-hosts',
'will-you-be-self-hosting',
'what-are-you-working-on-eo-security',
'is-it-any-good',
'what-did-you-think',
'deploy-fleet-in-your-environment',
'managed-cloud-for-growing-deployments',
'self-hosted-deploy',
]
},
formData: {
type: {},
description: 'The formdata that will be saved for this step of the get started questionnaire'
}
},
exits: {
},
fn: async function ({currentStep, formData}) {
// find this user's DB record.
let userRecord = await User.findOne({id: this.req.me.id});
if(!userRecord){
throw new Error(`Consistency violation: when trying to save a user's progress in the get started questionnaire, a User record with the ID ${this.req.me.id} could not be found.`);
}
let questionnaireProgress;
// If this user doesn't have a lastSubmittedGetStartedQuestionnaireStep or getStartedQuestionnaireAnswers, create an empty dictionary to store their answers.
if(!userRecord.lastSubmittedGetStartedQuestionnaireStep || _.isEmpty(userRecord.getStartedQuestionnaireAnswers)) {
questionnaireProgress = {};
} else {// other wise clone it from the user record.
questionnaireProgress = _.clone(userRecord.getStartedQuestionnaireAnswers);
}
// When the 'what-are-you-using-fleet-for' is completed, update this user's DB record and session to include their answer.
if(currentStep === 'what-are-you-using-fleet-for') {
let primaryBuyingSituation = formData.primaryBuyingSituation;
await User.updateOne({id: this.req.me.id}).set({primaryBuyingSituation});
// Set the primary buying situation in the user's session.
this.req.session.primaryBuyingSituation = primaryBuyingSituation;
}
// Set the user's answer to the current step.
questionnaireProgress[currentStep] = formData;
// Clone the questionnaireProgress to prevent any mutations from sending it through the updateOne Waterline method.
let getStartedProgress = _.clone(questionnaireProgress);
// Update the user's database model.
await User.updateOne({id: userRecord.id}).set({getStartedQuestionnaireAnswers: questionnaireProgress, lastSubmittedGetStartedQuestionnaireStep: currentStep});
// Return the JSON dictionary of form data submitted by this user.
return getStartedProgress;
}
};