@@ -41,7 +41,7 @@ const fetchDoc = async <T>(db: any, id: string): Promise<T | null> => {
4141 try {
4242 return await db . get ( id ) as T ;
4343 } catch ( error : any ) {
44- if ( error ?. statusCode === 404 || error ?. statusCode === 401 ) {
44+ if ( error ?. statusCode === 404 ) {
4545 return null ;
4646 }
4747 throw error ;
@@ -65,17 +65,28 @@ const isPublicSurvey = (survey: SurveyDoc | null, teamId: string): survey is Sur
6565 Array . isArray ( survey . questions ) &&
6666 survey . questions . length > 0 ;
6767
68+ const sanitizePublicQuestion = ( question : any ) => {
69+ const { correctChoice, marks, ...publicQuestion } = question || { } ;
70+ void correctChoice ;
71+ void marks ;
72+ return publicQuestion ;
73+ } ;
74+
6875const sanitizePublicSurvey = ( survey : SurveyDoc ) => ( {
6976 '_id' : survey . _id ,
7077 'name' : survey . name ,
7178 'description' : survey . description || '' ,
72- 'questions' : survey . questions ,
79+ 'questions' : survey . questions . map ( ( question ) => sanitizePublicQuestion ( question ) ) ,
7380 'type' : 'survey'
7481} ) ;
7582
7683const sanitizeSurveySnapshot = ( survey : SurveyDoc ) => ( {
77- ...sanitizePublicSurvey ( survey ) ,
78- '_rev' : survey . _rev
84+ '_id' : survey . _id ,
85+ '_rev' : survey . _rev ,
86+ 'name' : survey . name ,
87+ 'description' : survey . description || '' ,
88+ 'questions' : survey . questions ,
89+ 'type' : 'survey'
7990} ) ;
8091
8192const sanitizeTeam = ( team : TeamDoc ) => ( {
@@ -165,6 +176,13 @@ export const createPublicSurveySubmission = async (req: Request, res: Response)
165176 } ) ;
166177 }
167178
179+ if ( answers . length !== survey . questions . length ) {
180+ return res . status ( 400 ) . json ( {
181+ 'error' : 'Bad Request' ,
182+ 'message' : 'answers must contain one entry per survey question'
183+ } ) ;
184+ }
185+
168186 const submission = buildPublicSubmission ( survey , team , payload , configuration ) ;
169187 const response = await submissionsDB . insert ( submission as any ) ;
170188
0 commit comments