diff --git a/worklenz-backend/src/middlewares/validators/survey-submission-validator.ts b/worklenz-backend/src/middlewares/validators/survey-submission-validator.ts index a9f75bdb..bbeefcee 100644 --- a/worklenz-backend/src/middlewares/validators/survey-submission-validator.ts +++ b/worklenz-backend/src/middlewares/validators/survey-submission-validator.ts @@ -27,10 +27,7 @@ export default function surveySubmissionValidator(req: IWorkLenzRequest, res: IW return res.status(200).send(new ServerResponse(false, null, `Answer ${i + 1}: Question ID is required and must be a string`)); } - // At least one of answer_text or answer_json should be provided - if (!answer.answer_text && !answer.answer_json) { - return res.status(200).send(new ServerResponse(false, null, `Answer ${i + 1}: Either answer_text or answer_json is required`)); - } + // answer_text and answer_json are both optional - users can submit empty answers // Validate answer_text if provided if (answer.answer_text && typeof answer.answer_text !== 'string') { diff --git a/worklenz-frontend/src/components/survey/SurveyPromptModal.tsx b/worklenz-frontend/src/components/survey/SurveyPromptModal.tsx index 63453a85..7f368261 100644 --- a/worklenz-frontend/src/components/survey/SurveyPromptModal.tsx +++ b/worklenz-frontend/src/components/survey/SurveyPromptModal.tsx @@ -96,31 +96,47 @@ export const SurveyPromptModal: React.FC = ({ forceShow return acc; }, {} as Record); - // Prepare submission data with actual question IDs + // Prepare submission data with actual question IDs - only include answered questions + const answers: any[] = []; + + if (surveyData.organization_type && questionMap['organization_type']) { + answers.push({ + question_id: questionMap['organization_type'], + answer_text: surveyData.organization_type + }); + } + + if (surveyData.user_role && questionMap['user_role']) { + answers.push({ + question_id: questionMap['user_role'], + answer_text: surveyData.user_role + }); + } + + if (surveyData.main_use_cases && surveyData.main_use_cases.length > 0 && questionMap['main_use_cases']) { + answers.push({ + question_id: questionMap['main_use_cases'], + answer_json: surveyData.main_use_cases + }); + } + + if (surveyData.previous_tools && questionMap['previous_tools']) { + answers.push({ + question_id: questionMap['previous_tools'], + answer_text: surveyData.previous_tools + }); + } + + if (surveyData.how_heard_about && questionMap['how_heard_about']) { + answers.push({ + question_id: questionMap['how_heard_about'], + answer_text: surveyData.how_heard_about + }); + } + const submissionData: ISurveySubmissionRequest = { survey_id: surveyInfo.id, - answers: [ - { - question_id: questionMap['organization_type'], - answer_text: surveyData.organization_type || '' - }, - { - question_id: questionMap['user_role'], - answer_text: surveyData.user_role || '' - }, - { - question_id: questionMap['main_use_cases'], - answer_json: surveyData.main_use_cases || [] - }, - { - question_id: questionMap['previous_tools'], - answer_text: surveyData.previous_tools || '' - }, - { - question_id: questionMap['how_heard_about'], - answer_text: surveyData.how_heard_about || '' - } - ].filter(answer => answer.question_id) // Filter out any missing question IDs + answers }; const response = await surveyApiService.submitSurveyResponse(submissionData);